OLD | NEW |
| (Empty) |
1 /* | |
2 * This file is part of the URL Fixer, | |
3 * Copyright (C) 2006-2016 Eyeo GmbH | |
4 * | |
5 * URL Fixer is free software: you can redistribute it and/or modify | |
6 * it under the terms of the GNU General Public License version 3 as | |
7 * published by the Free Software Foundation. | |
8 * | |
9 * URL Fixer is distributed in the hope that it will be useful, | |
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
12 * GNU General Public License for more details. | |
13 * | |
14 * You should have received a copy of the GNU General Public License | |
15 * along with URL Fixer. If not, see <http://www.gnu.org/licenses/>. | |
16 */ | |
17 | |
18 Cu.import("resource://gre/modules/Services.jsm"); | |
19 | |
20 let {Prefs} = require("prefs"); | |
21 let {WindowObserver} = require("windowObserver"); | |
22 | |
23 let DOMAIN_TYPED = 1; | |
24 let DOMAIN_TYPO = 2; | |
25 let DOMAIN_CORRECTION = 3; | |
26 let DOMAIN_FALSE_POSITIVE = 4; | |
27 | |
28 let domains = null; | |
29 let timer = null; | |
30 | |
31 // Initialize and make sure to react to pref changes | |
32 if (Prefs.domainOptIn) | |
33 startCollection(); | |
34 Prefs.addListener(function(name) | |
35 { | |
36 if (name != "domainOptIn") | |
37 return; | |
38 if (Prefs.domainOptIn) | |
39 { | |
40 Prefs.domainOptInAsk = true; | |
41 startCollection(); | |
42 } | |
43 else | |
44 stopCollection(); | |
45 }); | |
46 | |
47 // Make sure to intercept TypedItCollectorInit event from web pages | |
48 new WindowObserver({ | |
49 applyToWindow: function(window) | |
50 { | |
51 if (window.document.documentElement.getAttribute("windowtype") != "navigator
:browser") | |
52 return; | |
53 | |
54 window.document.addEventListener("TypedItCollectorInit", initWebUI, false, t
rue); | |
55 }, | |
56 | |
57 removeFromWindow: function(window) | |
58 { | |
59 if (window.document.documentElement.getAttribute("windowtype") != "navigator
:browser") | |
60 return; | |
61 | |
62 window.document.removeEventListener("TypedItCollectorInit", initWebUI, false
, true); | |
63 } | |
64 }); | |
65 | |
66 exports.onBrowserInitialized = onBrowserInitialized; | |
67 function onBrowserInitialized(window) | |
68 { | |
69 // Don't do anything on Fennec. | |
70 if ("Browser" in window || "BrowserApp" in window) | |
71 return; | |
72 | |
73 if (Prefs.counter < 5) | |
74 Prefs.counter++; | |
75 else if (!Prefs.domainOptInAsk && !Prefs.domainOptIn) | |
76 window.openDialog("chrome://url-fixer/content/typedItOptIn.xul", "typedItOpt
In", "chrome,dialog,centerscreen,titlebar"); | |
77 } | |
78 | |
79 exports.processTypedDomain = processTypedDomain; | |
80 function processTypedDomain(domain) | |
81 { | |
82 if (domains && !privateBrowsingEnabled()) | |
83 domains[domain] = DOMAIN_TYPED; | |
84 } | |
85 | |
86 exports.processDomainCorrection = processDomainCorrection; | |
87 function processDomainCorrection(domainFrom, domainTo) | |
88 { | |
89 if (domains && !privateBrowsingEnabled()) | |
90 { | |
91 domains[domainFrom] = DOMAIN_TYPO; | |
92 domains[domainTo] = DOMAIN_CORRECTION; | |
93 } | |
94 } | |
95 | |
96 exports.processFalsePositive = processFalsePositive; | |
97 function processFalsePositive(domainFrom, domainTo) | |
98 { | |
99 if (domains && !privateBrowsingEnabled()) | |
100 { | |
101 domains[domainFrom] = DOMAIN_FALSE_POSITIVE; | |
102 domains[domainTo] = DOMAIN_TYPED; | |
103 } | |
104 } | |
105 | |
106 exports.processUserCorrection = processUserCorrection; | |
107 function processUserCorrection(domainFrom, domainTo) | |
108 { | |
109 if (domains && !privateBrowsingEnabled()) | |
110 { | |
111 domains[domainFrom] = DOMAIN_TYPO; | |
112 domains[domainTo] = DOMAIN_CORRECTION; | |
113 } | |
114 } | |
115 | |
116 exports.openDisclosurePage = openDisclosurePage; | |
117 function openDisclosurePage() | |
118 { | |
119 let window = Services.wm.getMostRecentWindow("navigator:browser"); | |
120 if (!window) | |
121 return; | |
122 | |
123 let url = "http://urlfixer.org/data/"; | |
124 if ("Browser" in window && typeof window.Browser.addTab != 'undefined') | |
125 window.Browser.addTab(url, true); | |
126 else if ("gBrowser" in window) | |
127 window.gBrowser.loadOneTab(url, {inBackground: false}); | |
128 } | |
129 | |
130 function startCollection() | |
131 { | |
132 if (domains) | |
133 return; | |
134 | |
135 onShutdown.add(stopCollection); | |
136 | |
137 domains = {}; | |
138 | |
139 // Send data every 60 minutes | |
140 timer = Cc["@mozilla.org/timer;1"].createInstance(Ci.nsITimer); | |
141 timer.initWithCallback(sendAnonymousData, 1000 * 60 * 60, Ci.nsITimer.TYPE_REP
EATING_SLACK); | |
142 } | |
143 | |
144 function stopCollection() | |
145 { | |
146 if (!domains) | |
147 return; | |
148 | |
149 onShutdown.remove(stopCollection); | |
150 domains = null; | |
151 | |
152 try | |
153 { | |
154 timer.cancel(); | |
155 } | |
156 catch (e) | |
157 { | |
158 Cu.reportError(e); | |
159 } | |
160 timer = null; | |
161 } | |
162 | |
163 function privateBrowsingEnabled() | |
164 { | |
165 if (!("service" in privateBrowsingEnabled)) | |
166 privateBrowsingEnabled.service = Cc["@mozilla.org/privatebrowsing;1"].getSer
vice(Ci.nsIPrivateBrowsingService); | |
167 | |
168 return privateBrowsingEnabled.service.privateBrowsingEnabled; | |
169 } | |
170 | |
171 function sendAnonymousData() | |
172 { | |
173 if (!Prefs.domainOptIn || privateBrowsingEnabled()) | |
174 return; | |
175 | |
176 let postData = JSON.stringify(domains); | |
177 if (postData == JSON.stringify({})) | |
178 return; | |
179 | |
180 let savedDomains = domains; | |
181 domains = {}; | |
182 | |
183 let request = Cc["@mozilla.org/xmlextras/xmlhttprequest;1"].createInstance(Ci.
nsIXMLHttpRequest); | |
184 request.open("POST", "http://urlfixer.org/submitData"); | |
185 request.setRequestHeader("Content-Type", "application/json"); | |
186 request.addEventListener("load", function(event) | |
187 { | |
188 if (event.target.status != 200) | |
189 for (var i in savedDomains) | |
190 domains[i] = savedDomains[i]; | |
191 }, false); | |
192 request.send(postData); | |
193 } | |
194 | |
195 function initWebUI(event) | |
196 { | |
197 if (Prefs.domainOptIn) | |
198 return; | |
199 | |
200 let container = event.target; | |
201 let source = container.ownerDocument.defaultView.location.hostname; | |
202 if (!/(^|\.)urlfixer\.org$/.test(source)) | |
203 return; | |
204 | |
205 let button = container.getElementsByClassName("allowButton")[0]; | |
206 let message = container.getElementsByClassName("confirmationMessage")[0]; | |
207 | |
208 if (!button || !message) | |
209 return; | |
210 | |
211 button.addEventListener("click", function(event) | |
212 { | |
213 if (!event.isTrusted) | |
214 return; | |
215 | |
216 Prefs.domainOptInAsk = true; | |
217 Prefs.domainOptIn = true; | |
218 button.style.display = "none"; | |
219 message.style.display = ""; | |
220 }, false); | |
221 | |
222 message.style.display = "none"; | |
223 container.style.display = ""; | |
224 } | |
OLD | NEW |