LEFT | RIGHT |
1 /* This Source Code Form is subject to the terms of the Mozilla Public | 1 /* This Source Code Form is subject to the terms of the Mozilla Public |
2 * License, v. 2.0. If a copy of the MPL was not distributed with this file, | 2 * License, v. 2.0. If a copy of the MPL was not distributed with this file, |
3 * You can obtain one at http://mozilla.org/MPL/2.0/. */ | 3 * You can obtain one at http://mozilla.org/MPL/2.0/. */ |
4 | 4 |
5 Cu.import("resource://gre/modules/Services.jsm"); | 5 Cu.import("resource://gre/modules/Services.jsm"); |
6 | 6 |
7 let {Prefs} = require("prefs"); | 7 let {Prefs} = require("prefs"); |
8 let {WindowObserver} = require("windowObserver"); | 8 let {WindowObserver} = require("windowObserver"); |
9 | 9 |
| 10 let DOMAIN_TYPED = 1; |
| 11 let DOMAIN_TYPO = 2; |
| 12 let DOMAIN_CORRECTION = 3; |
| 13 let DOMAIN_FALSE_POSITIVE = 4; |
| 14 |
10 let domains = null; | 15 let domains = null; |
11 let userCorrections = null; | |
12 let falsePositives = null; | |
13 let timer = null; | 16 let timer = null; |
14 | 17 |
15 // Initialize and make sure to react to pref changes | 18 // Initialize and make sure to react to pref changes |
16 if (Prefs.domainOptIn) | 19 if (Prefs.domainOptIn) |
17 startCollection(); | 20 startCollection(); |
18 Prefs.addListener(function(name) | 21 Prefs.addListener(function(name) |
19 { | 22 { |
20 if (name != "domainOptIn") | 23 if (name != "domainOptIn") |
21 return; | 24 return; |
22 if (Prefs.domainOptIn) | 25 if (Prefs.domainOptIn) |
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
57 if (Prefs.counter < 5) | 60 if (Prefs.counter < 5) |
58 Prefs.counter++; | 61 Prefs.counter++; |
59 else if (!Prefs.domainOptInAsk && !Prefs.domainOptIn) | 62 else if (!Prefs.domainOptInAsk && !Prefs.domainOptIn) |
60 window.openDialog("chrome://url-fixer/content/typedItOptIn.xul", "typedItOpt
In", "chrome,dialog,centerscreen,titlebar"); | 63 window.openDialog("chrome://url-fixer/content/typedItOptIn.xul", "typedItOpt
In", "chrome,dialog,centerscreen,titlebar"); |
61 } | 64 } |
62 | 65 |
63 exports.processTypedDomain = processTypedDomain; | 66 exports.processTypedDomain = processTypedDomain; |
64 function processTypedDomain(domain) | 67 function processTypedDomain(domain) |
65 { | 68 { |
66 if (domains && !privateBrowsingEnabled()) | 69 if (domains && !privateBrowsingEnabled()) |
67 domains.push(domain); | 70 domains[domain] = DOMAIN_TYPED; |
| 71 } |
| 72 |
| 73 exports.processDomainCorrection = processDomainCorrection; |
| 74 function processDomainCorrection(domainFrom, domainTo) |
| 75 { |
| 76 if (domains && !privateBrowsingEnabled()) |
| 77 { |
| 78 domains[domainFrom] = DOMAIN_TYPO; |
| 79 domains[domainTo] = DOMAIN_CORRECTION; |
| 80 } |
68 } | 81 } |
69 | 82 |
70 exports.processFalsePositive = processFalsePositive; | 83 exports.processFalsePositive = processFalsePositive; |
71 function processFalsePositive(domainFrom, domainTo) | 84 function processFalsePositive(domainFrom, domainTo) |
72 { | 85 { |
73 if (falsePositives && !privateBrowsingEnabled()) | 86 if (domains && !privateBrowsingEnabled()) |
74 { | 87 { |
75 falsePositives.push([domainFrom, domainTo]); | 88 domains[domainFrom] = DOMAIN_FALSE_POSITIVE; |
| 89 domains[domainTo] = DOMAIN_TYPED; |
76 } | 90 } |
77 } | 91 } |
78 | 92 |
79 exports.processUserCorrection = processUserCorrection; | 93 exports.processUserCorrection = processUserCorrection; |
80 function processUserCorrection(domainFrom, domainTo) | 94 function processUserCorrection(domainFrom, domainTo) |
81 { | 95 { |
82 if (userCorrections && !privateBrowsingEnabled()) | 96 if (domains && !privateBrowsingEnabled()) |
83 { | 97 { |
84 userCorrections.push([domainFrom, domainTo]); | 98 domains[domainFrom] = DOMAIN_TYPO; |
| 99 domains[domainTo] = DOMAIN_CORRECTION; |
85 } | 100 } |
86 } | 101 } |
87 | 102 |
88 exports.openDisclosurePage = openDisclosurePage; | 103 exports.openDisclosurePage = openDisclosurePage; |
89 function openDisclosurePage() | 104 function openDisclosurePage() |
90 { | 105 { |
91 let window = Services.wm.getMostRecentWindow("navigator:browser"); | 106 let window = Services.wm.getMostRecentWindow("navigator:browser"); |
92 if (!window) | 107 if (!window) |
93 return; | 108 return; |
94 | 109 |
95 let url = "http://urlfixer.org/data/"; | 110 let url = "http://urlfixer.org/data/"; |
96 if ("Browser" in window && typeof window.Browser.addTab != 'undefined') | 111 if ("Browser" in window && typeof window.Browser.addTab != 'undefined') |
97 window.Browser.addTab(url, true); | 112 window.Browser.addTab(url, true); |
98 else if ("gBrowser" in window) | 113 else if ("gBrowser" in window) |
99 window.gBrowser.loadOneTab(url, {inBackground: false}); | 114 window.gBrowser.loadOneTab(url, {inBackground: false}); |
100 } | 115 } |
101 | 116 |
102 function startCollection() | 117 function startCollection() |
103 { | 118 { |
104 if (domains || falsePositives || userCorrections) | 119 if (domains) |
105 return; | 120 return; |
106 | 121 |
107 onShutdown.add(stopCollection); | 122 onShutdown.add(stopCollection); |
108 | 123 |
109 domains = []; | 124 domains = {}; |
110 falsePositives = []; | 125 |
111 userCorrections = []; | 126 // Send data every 60 minutes |
112 | |
113 // Send data every 15 minutes | |
114 timer = Cc["@mozilla.org/timer;1"].createInstance(Ci.nsITimer); | 127 timer = Cc["@mozilla.org/timer;1"].createInstance(Ci.nsITimer); |
115 timer.initWithCallback(sendAnonymousData, 1000 * 60 * 15, Ci.nsITimer.TYPE_REP
EATING_SLACK); | 128 timer.initWithCallback(sendAnonymousData, 1000 * 60 * 60, Ci.nsITimer.TYPE_REP
EATING_SLACK); |
116 } | 129 } |
117 | 130 |
118 function stopCollection() | 131 function stopCollection() |
119 { | 132 { |
120 if (!domains || !falsePositives || !userCorrections) | 133 if (!domains) |
121 return; | 134 return; |
122 | 135 |
123 onShutdown.remove(stopCollection); | 136 onShutdown.remove(stopCollection); |
124 domains = null; | 137 domains = null; |
125 falsePositives = null; | |
126 userCorrections = null; | |
127 | 138 |
128 try | 139 try |
129 { | 140 { |
130 timer.cancel(); | 141 timer.cancel(); |
131 } | 142 } |
132 catch (e) | 143 catch (e) |
133 { | 144 { |
134 Cu.reportError(e); | 145 Cu.reportError(e); |
135 } | 146 } |
136 timer = null; | 147 timer = null; |
137 } | 148 } |
138 | 149 |
139 function privateBrowsingEnabled() | 150 function privateBrowsingEnabled() |
140 { | 151 { |
141 if (!("service" in privateBrowsingEnabled)) | 152 if (!("service" in privateBrowsingEnabled)) |
142 privateBrowsingEnabled.service = Cc["@mozilla.org/privatebrowsing;1"].getSer
vice(Ci.nsIPrivateBrowsingService); | 153 privateBrowsingEnabled.service = Cc["@mozilla.org/privatebrowsing;1"].getSer
vice(Ci.nsIPrivateBrowsingService); |
143 | 154 |
144 return privateBrowsingEnabled.service.privateBrowsingEnabled; | 155 return privateBrowsingEnabled.service.privateBrowsingEnabled; |
145 } | 156 } |
146 | 157 |
147 function sendAnonymousData() | 158 function sendAnonymousData() |
148 { | 159 { |
149 if (!Prefs.domainOptIn || (domains.length == 0 && falsePositives.length == 0 &
& userCorrections.length == 0) || privateBrowsingEnabled()) | 160 if (!Prefs.domainOptIn || privateBrowsingEnabled()) |
150 return; | 161 return; |
151 | 162 |
152 let postData = {}; | 163 let postData = JSON.stringify(domains); |
| 164 if (postData == JSON.stringify({})) |
| 165 return; |
| 166 |
153 let savedDomains = domains; | 167 let savedDomains = domains; |
154 let savedFalsePositives = falsePositives; | 168 domains = {}; |
155 let savedUserCorrections = userCorrections; | 169 |
156 | |
157 if(domains.length > 0) | |
158 { | |
159 postData.domains = domains; | |
160 domains = []; | |
161 } | |
162 if(falsePositives.length > 0) | |
163 { | |
164 postData.falsePositives = falsePositives; | |
165 falsePositives = []; | |
166 } | |
167 if(userCorrections.length > 0) | |
168 { | |
169 postData.userCorrections = userCorrections; | |
170 userCorrections = []; | |
171 } | |
172 | |
173 let request = Cc["@mozilla.org/xmlextras/xmlhttprequest;1"].createInstance(Ci.
nsIXMLHttpRequest); | 170 let request = Cc["@mozilla.org/xmlextras/xmlhttprequest;1"].createInstance(Ci.
nsIXMLHttpRequest); |
174 request.open("POST", "http://typed.it/submitData"); | 171 request.open("POST", "http://typed.it/submitData"); |
175 request.setRequestHeader("Content-Type", "application/x-www-form-urlencoded"); | 172 request.setRequestHeader("Content-Type", "application/json"); |
176 request.addEventListener("load", function(event) | 173 request.addEventListener("load", function(event) |
177 { | 174 { |
178 if (event.target.status != 200) | 175 if (event.target.status != 200) |
179 { | |
180 domains = domains.concat(savedDomains); | 176 domains = domains.concat(savedDomains); |
181 falsePositives = falsePositives.concat(savedFalsePositives); | |
182 userCorrections = userCorrections.concat(savedUserCorrections); | |
183 } | |
184 }, false); | 177 }, false); |
185 request.send("data=" + JSON.stringify(postData)); | 178 request.send(postData); |
186 } | 179 } |
187 | 180 |
188 function initWebUI(event) | 181 function initWebUI(event) |
189 { | 182 { |
190 if (Prefs.domainOptIn) | 183 if (Prefs.domainOptIn) |
191 return; | 184 return; |
192 | 185 |
193 let container = event.target; | 186 let container = event.target; |
194 let source = container.ownerDocument.defaultView.location.hostname; | 187 let source = container.ownerDocument.defaultView.location.hostname; |
195 if (!/(^|\.)urlfixer\.org$/.test(source)) | 188 if (!/(^|\.)urlfixer\.org$/.test(source)) |
(...skipping 12 matching lines...) Expand all Loading... |
208 | 201 |
209 Prefs.domainOptInAsk = true; | 202 Prefs.domainOptInAsk = true; |
210 Prefs.domainOptIn = true; | 203 Prefs.domainOptIn = true; |
211 button.style.display = "none"; | 204 button.style.display = "none"; |
212 message.style.display = ""; | 205 message.style.display = ""; |
213 }, false); | 206 }, false); |
214 | 207 |
215 message.style.display = "none"; | 208 message.style.display = "none"; |
216 container.style.display = ""; | 209 container.style.display = ""; |
217 } | 210 } |
LEFT | RIGHT |