Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code

Side by Side Diff: lib/typedItCollector.js

Issue 8382011: Applied changes from emailed code review (Closed)
Patch Set: Created Sept. 28, 2012, 1:40 p.m.
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
View unified diff | Download patch
« no previous file with comments | « lib/survey.js ('k') | lib/typoFixer.js » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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
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 = [];
111 userCorrections = [];
112 125
113 // Send data every 15 minutes 126 // Send data every 60 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 args = []; 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;
156
157 if(domains.length > 0)
158 {
159 args.push(domains.map(function(d) "domains[]=" + encodeURIComponent(d)).join ("&"));
160 domains = [];
161 }
162 if(falsePositives.length > 0)
163 {
164 args.push(
165 falsePositives.map(
166 function(fp)
167 {
168 return "falsePositives[]=" + encodeURIComponent(
169 encodeURIComponent(fp[0]) + ((fp[1]) ? "&" + encodeURIComponent(fp[1 ]) : "")
170 );
171 }
172 ).join("&")
173 );
174 falsePositives = [];
175 }
176 if(userCorrections.length > 0)
177 {
178 args.push(
179 userCorrections.map(
180 function(uc)
181 {
182 return "userCorrections[]=" + encodeURIComponent(
183 encodeURIComponent(uc[0]) + ((uc[1]) ? "&" + encodeURIComponent(uc[1 ]) : "")
184 );
185 }
186 ).join("&")
187 );
188 userCorrections = [];
189 }
190 169
191 let request = Cc["@mozilla.org/xmlextras/xmlhttprequest;1"].createInstance(Ci. nsIXMLHttpRequest); 170 let request = Cc["@mozilla.org/xmlextras/xmlhttprequest;1"].createInstance(Ci. nsIXMLHttpRequest);
192 request.open("POST", "http://typed.it/submitData"); 171 request.open("POST", "http://typed.it/submitData");
193 request.setRequestHeader("Content-Type", "application/x-www-form-urlencoded"); 172 request.setRequestHeader("Content-Type", "application/json");
194 request.addEventListener("load", function(event) 173 request.addEventListener("load", function(event)
195 { 174 {
196 if (event.target.status != 200) 175 if (event.target.status != 200)
197 {
198 domains = domains.concat(savedDomains); 176 domains = domains.concat(savedDomains);
199 falsePositives = falsePositives.concat(savedFalsePositives);
200 userCorrections = userCorrections.concat(savedUserCorrections);
201 }
202 }, false); 177 }, false);
203 request.send(args.join("&")); 178 request.send(postData);
204 } 179 }
205 180
206 function initWebUI(event) 181 function initWebUI(event)
207 { 182 {
208 if (Prefs.domainOptIn) 183 if (Prefs.domainOptIn)
209 return; 184 return;
210 185
211 let container = event.target; 186 let container = event.target;
212 let source = container.ownerDocument.defaultView.location.hostname; 187 let source = container.ownerDocument.defaultView.location.hostname;
213 if (!/(^|\.)urlfixer\.org$/.test(source)) 188 if (!/(^|\.)urlfixer\.org$/.test(source))
(...skipping 12 matching lines...) Expand all
226 201
227 Prefs.domainOptInAsk = true; 202 Prefs.domainOptInAsk = true;
228 Prefs.domainOptIn = true; 203 Prefs.domainOptIn = true;
229 button.style.display = "none"; 204 button.style.display = "none";
230 message.style.display = ""; 205 message.style.display = "";
231 }, false); 206 }, false);
232 207
233 message.style.display = "none"; 208 message.style.display = "none";
234 container.style.display = ""; 209 container.style.display = "";
235 } 210 }
OLDNEW
« no previous file with comments | « lib/survey.js ('k') | lib/typoFixer.js » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld