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

Delta Between Two Patch Sets: lib/typoFixer.js

Issue 8382011: Applied changes from emailed code review (Closed)
Left Patch Set: Created Sept. 18, 2012, 2:06 p.m.
Right 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:
Left: Side by side diff | Download
Right: Side by side diff | Download
« no previous file with change/comment | « lib/typedItCollector.js ('k') | lib/updateRules.js » ('j') | no next file with change/comment »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
LEFTRIGHT
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 Cu.import("resource://gre/modules/XPCOMUtils.jsm"); 6 Cu.import("resource://gre/modules/XPCOMUtils.jsm");
7 7
8 let {Prefs} = require("prefs"); 8 let {Prefs} = require("prefs");
9 let {WindowObserver} = require("windowObserver"); 9 let {WindowObserver} = require("windowObserver");
10 let {getSchemeCorrection, isKnownScheme, getDomainCorrection, getDomainReferral} = require("rules"); 10 let {getSchemeCorrection, isKnownScheme, getDomainCorrection, getDomainReferral} = require("rules");
11 let {processTypedDomain} = require("typedItCollector"); 11 let {processTypedDomain, processDomainCorrection,
12 let {processUserCorrection} = require("typedItCollector"); 12 processUserCorrection, processFalsePositive} = require("typedItCollector");
13 let {processFalsePositive} = require("typedItCollector");
14 let appIntegration = require("appIntegration"); 13 let appIntegration = require("appIntegration");
15 14
16 // Attach our handlers to all browser windows 15 // Attach our handlers to all browser windows
17 new WindowObserver( 16 new WindowObserver(
18 { 17 {
19 applyToWindow: function(window) 18 applyToWindow: function(window)
20 { 19 {
21 if (!appIntegration.isKnownWindow(window)) 20 if (!appIntegration.isKnownWindow(window))
22 return; 21 return;
23 22
(...skipping 63 matching lines...) Expand 10 before | Expand all | Expand 10 after
87 let retryButton = document.getElementById("url-fixer-retry"); 86 let retryButton = document.getElementById("url-fixer-retry");
88 retryButton.addEventListener("click", function() 87 retryButton.addEventListener("click", function()
89 { 88 {
90 let newURL = textField.value.replace(/^\s+/, "").replace(/\s+$/, ""); 89 let newURL = textField.value.replace(/^\s+/, "").replace(/\s+$/, "");
91 if (!newURL.length) 90 if (!newURL.length)
92 return; 91 return;
93 92
94 let [prefix, newHost, suffix] = parseURL(newURL); 93 let [prefix, newHost, suffix] = parseURL(newURL);
95 let oldHost = document.defaultView.location.hostname.toLowerCase(); 94 let oldHost = document.defaultView.location.hostname.toLowerCase();
96 95
97 processTypedDomain(newHost);
98 processUserCorrection(oldHost, newHost); 96 processUserCorrection(oldHost, newHost);
99 97
100 if (newHost.indexOf("www.") == 0 && oldHost.indexOf("www.") == 0) 98 if (newHost.indexOf("www.") == 0 && oldHost.indexOf("www.") == 0)
101 { 99 {
102 // Ignore www. prefix if they both start with it 100 // Ignore www. prefix if they both start with it
103 newHost = newHost.substr(4); 101 newHost = newHost.substr(4);
104 oldHost = oldHost.substr(4); 102 oldHost = oldHost.substr(4);
105 } 103 }
106 if (oldHost && newHost != oldHost) 104 if (oldHost && newHost != oldHost)
107 { 105 {
(...skipping 28 matching lines...) Expand all
136 } 134 }
137 catch (e) 135 catch (e)
138 { 136 {
139 return (e.result == Cr.NS_ERROR_HOST_IS_IP_ADDRESS); 137 return (e.result == Cr.NS_ERROR_HOST_IS_IP_ADDRESS);
140 } 138 }
141 } 139 }
142 140
143 function correctURL(window, value) 141 function correctURL(window, value)
144 { 142 {
145 let hasCorrection = false; 143 let hasCorrection = false;
146 144
147 value = value.trim(); 145 value = value.trim();
148 if (value.length == 0) 146 if (value.length == 0)
149 return null; 147 return null;
150 148
151 // Replace backslashes 149 // Replace backslashes
152 value = value.replace(/\\/g, "/"); 150 value = value.replace(/\\/g, "/");
153 151
154 // Does the URL scheme need correcting? 152 // Does the URL scheme need correcting?
155 if (/^([^\/]+)(\/.*)/.test(value)) 153 if (/^([^\/]+)(\/.*)/.test(value))
156 { 154 {
(...skipping 15 matching lines...) Expand all
172 if ("getShortcutOrURI" in window && window.getShortcutOrURI(value) != value) 170 if ("getShortcutOrURI" in window && window.getShortcutOrURI(value) != value)
173 return null; 171 return null;
174 172
175 // Spaces before the first slash or period is probably a quick search 173 // Spaces before the first slash or period is probably a quick search
176 if (/^[^\/\.\s]+\s/.test(value)) 174 if (/^[^\/\.\s]+\s/.test(value))
177 return null; 175 return null;
178 176
179 // Check manually entered corrections 177 // Check manually entered corrections
180 if (Prefs.custom_replace.hasOwnProperty(value) && Prefs.custom_replace[value]) 178 if (Prefs.custom_replace.hasOwnProperty(value) && Prefs.custom_replace[value])
181 return Prefs.custom_replace[value]; 179 return Prefs.custom_replace[value];
182 180
183 let [prefix, domain, suffix] = parseURL(value); 181 let [prefix, domain, suffix] = parseURL(value);
184 if (!domain) 182 if (!domain)
185 return null; 183 return null;
186 184
187 let oldDomain = domain; 185 let oldDomain = domain;
188 if (!isIPAddress(domain)) 186 if (!isIPAddress(domain))
189 { 187 {
190 processTypedDomain(domain); 188 processTypedDomain(domain);
191 189
192 let newDomain = getDomainCorrection(domain); 190 let newDomain = getDomainCorrection(domain);
193 if (newDomain != domain) 191 if (newDomain != domain)
194 { 192 {
193 processDomainCorrection(domain, newDomain);
195 domain = newDomain; 194 domain = newDomain;
196 hasCorrection = true; 195 hasCorrection = true;
197 196
198 let referral = getDomainReferral(domain.replace(/^www\./, "")); 197 let referral = getDomainReferral(domain.replace(/^www\./, ""));
199 if (referral) 198 if (referral)
200 { 199 {
201 // We need to add a query string parameter when sending users to this do main 200 // We need to add a query string parameter when sending users to this do main
202 let anchorIndex = suffix.indexOf("#"); 201 let anchorIndex = suffix.indexOf("#");
203 let anchor = ""; 202 let anchor = "";
204 if (anchorIndex >= 0) 203 if (anchorIndex >= 0)
(...skipping 18 matching lines...) Expand all
223 222
224 suffix += anchor; 223 suffix += anchor;
225 } 224 }
226 } 225 }
227 } 226 }
228 227
229 if (!hasCorrection) 228 if (!hasCorrection)
230 return null; 229 return null;
231 230
232 // Show infobar to inform and ask about correction 231 // Show infobar to inform and ask about correction
233 let browser = appIntegration.getBrowser(window); 232 let [message, yes, no] = getInfobarTexts();
234 let infobar = browser.getNotificationBox();
235 let notif = infobar.getNotificationWithValue("url-fixer-infobar-askafter");
236
237 let [message, yes, no, cancel] = getInfobarTexts();
238 message = message.replace(/\?1\?/g, prefix+domain); 233 message = message.replace(/\?1\?/g, prefix+domain);
239 234 let buttons = [
240 if (notif) 235 {
241 { 236 label: yes,
242 notif.label = message; 237 accessKey: null,
243 } 238 callback: function()
244 else 239 {
245 { 240 // Yes: Do nothing
246 let buttons = [ 241 }
247 { 242 },
248 label: yes, 243 {
249 accessKey: null, 244 label: no,
250 callback: function() 245 accessKey: null,
251 { 246 callback: function()
252 // Yes: Do nothing 247 {
253 } 248 // No: Add to list of corrections (ignore)
254 }, 249 let {onWhitelistEntryAdded} = require("rules");
255 { 250 let entry = oldDomain.replace(/^www\./, "");
256 label: no, 251 Prefs.whitelist[entry] = true;
257 accessKey: null, 252 onWhitelistEntryAdded(entry);
258 callback: function() 253 Prefs.whitelist = JSON.parse(JSON.stringify(Prefs.whitelist));
259 { 254
260 // No: Add to list of corrections (ignore) 255 require("appIntegration").loadURI(window, value);
261 if (/^www\./.test(value)) 256 processFalsePositive(domain, oldDomain);
262 { 257 }
263 value = value.substr(4); 258 }
264 } 259 ];
Wladimir Palant 2012/09/21 14:40:25 Somebody reading this code now has to recognize th
265 Prefs.whitelist[value] = value; 260 // We need to have persistence being set to 1 due to redirect which happens af terwards
266 Prefs.whitelist = JSON.parse(JSON.stringify(Prefs.whitelist)); 261 require("appIntegration").openInfobar(window, "url-fixer-infobar-askafter", me ssage, buttons, 1);
Wladimir Palant 2012/09/21 14:40:25 I understand why you wanted the whitelist to be a
267
268 browser.loadURI(value);
269 processFalsePositive(oldDomain, domain);
270 }
271 }
272 ];
273 notif = infobar.appendNotification(
274 message,
275 "url-fixer-infobar-askafter",
276 require("info").addonRoot + "icon64.png",
277 infobar.PRIORITY_INFO_HIGH,
278 buttons
279 );
280 notif.persistence = 1;
281 }
282 262
283 require("survey").incrementCorrectionsCounter(); 263 require("survey").incrementCorrectionsCounter();
284
285 // Consider the correction a second typed domain
286 if (!isIPAddress(domain))
287 processTypedDomain(domain);
288 264
289 return prefix + domain + suffix; 265 return prefix + domain + suffix;
290 } 266 }
291 267
292 let stringBundle = null; 268 let stringBundle = null;
293 269
294 function getInfobarTexts() 270 function getInfobarTexts()
295 { 271 {
296 // Randomize URI to work around bug 719376 272 // Randomize URI to work around bug 719376
297 if (!stringBundle) 273 if (!stringBundle)
298 stringBundle = Services.strings.createBundle("chrome://url-fixer/locale/loca le.properties?" + Math.random()); 274 stringBundle = Services.strings.createBundle("chrome://url-fixer/locale/loca le.properties?" + Math.random());
299 let result = [ 275 let result = [
300 stringBundle.GetStringFromName("urlfixer.isItCorrect"), 276 stringBundle.GetStringFromName("urlfixer.isItCorrect"),
301 stringBundle.GetStringFromName("urlfixer.yes"), 277 stringBundle.GetStringFromName("urlfixer.yes"),
302 stringBundle.GetStringFromName("urlfixer.no"), 278 stringBundle.GetStringFromName("urlfixer.no")
303 stringBundle.GetStringFromName("urlfixer.cancel")
304 ]; 279 ];
305 280
306 getInfobarTexts = function() result; 281 getInfobarTexts = function() result;
307 return getInfobarTexts(); 282 return getInfobarTexts();
308 } 283 }
LEFTRIGHT

Powered by Google App Engine
This is Rietveld