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

Unified Diff: lib/typedItCollector.js

Issue 8397151: Use a better format for typed in data and record domain corrections; only send data once per hour (Closed)
Patch Set: Created Sept. 28, 2012, 8:10 a.m.
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « no previous file | lib/typoFixer.js » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: lib/typedItCollector.js
===================================================================
--- a/lib/typedItCollector.js
+++ b/lib/typedItCollector.js
@@ -2,19 +2,22 @@
* License, v. 2.0. If a copy of the MPL was not distributed with this file,
* You can obtain one at http://mozilla.org/MPL/2.0/. */
Cu.import("resource://gre/modules/Services.jsm");
let {Prefs} = require("prefs");
let {WindowObserver} = require("windowObserver");
+let DOMAIN_TYPED = 1;
+let DOMAIN_TYPO = 2;
+let DOMAIN_CORRECTION = 3;
+let DOMAIN_FALSE_POSITIVE = 4;
+
let domains = null;
-let userCorrections = null;
-let falsePositives = null;
let timer = null;
// Initialize and make sure to react to pref changes
if (Prefs.domainOptIn)
startCollection();
Prefs.addListener(function(name)
{
if (name != "domainOptIn")
@@ -59,34 +62,46 @@ function onBrowserInitialized(window)
else if (!Prefs.domainOptInAsk && !Prefs.domainOptIn)
window.openDialog("chrome://url-fixer/content/typedItOptIn.xul", "typedItOptIn", "chrome,dialog,centerscreen,titlebar");
}
exports.processTypedDomain = processTypedDomain;
function processTypedDomain(domain)
{
if (domains && !privateBrowsingEnabled())
- domains.push(domain);
+ domains[domain] = DOMAIN_TYPED;
+}
+
+exports.processDomainCorrection = processDomainCorrection;
+function processDomainCorrection(domainFrom, domainTo)
+{
+ if (domains && !privateBrowsingEnabled())
+ {
+ domains[domainFrom] = DOMAIN_TYPO;
+ domains[domainTo] = DOMAIN_CORRECTION;
+ }
}
exports.processFalsePositive = processFalsePositive;
function processFalsePositive(domainFrom, domainTo)
{
- if (falsePositives && !privateBrowsingEnabled())
+ if (domains && !privateBrowsingEnabled())
{
- falsePositives.push([domainFrom, domainTo]);
+ domains[domainFrom] = DOMAIN_FALSE_POSITIVE;
+ domains[domainTo] = DOMAIN_TYPED;
}
}
exports.processUserCorrection = processUserCorrection;
function processUserCorrection(domainFrom, domainTo)
{
- if (userCorrections && !privateBrowsingEnabled())
+ if (domains && !privateBrowsingEnabled())
{
- userCorrections.push([domainFrom, domainTo]);
+ domains[domainFrom] = DOMAIN_TYPO;
+ domains[domainTo] = DOMAIN_CORRECTION;
}
}
exports.openDisclosurePage = openDisclosurePage;
function openDisclosurePage()
{
let window = Services.wm.getMostRecentWindow("navigator:browser");
if (!window)
@@ -96,39 +111,35 @@ function openDisclosurePage()
if ("Browser" in window && typeof window.Browser.addTab != 'undefined')
window.Browser.addTab(url, true);
else if ("gBrowser" in window)
window.gBrowser.loadOneTab(url, {inBackground: false});
}
function startCollection()
{
- if (domains || falsePositives || userCorrections)
+ if (domains)
return;
onShutdown.add(stopCollection);
- domains = [];
- falsePositives = [];
- userCorrections = [];
+ domains = {};
- // Send data every 15 minutes
+ // Send data every 60 minutes
timer = Cc["@mozilla.org/timer;1"].createInstance(Ci.nsITimer);
- timer.initWithCallback(sendAnonymousData, 1000 * 60 * 15, Ci.nsITimer.TYPE_REPEATING_SLACK);
+ timer.initWithCallback(sendAnonymousData, 1000 * 60 * 60, Ci.nsITimer.TYPE_REPEATING_SLACK);
}
function stopCollection()
{
- if (!domains || !falsePositives || !userCorrections)
+ if (!domains)
return;
onShutdown.remove(stopCollection);
domains = null;
- falsePositives = null;
- userCorrections = null;
try
{
timer.cancel();
}
catch (e)
{
Cu.reportError(e);
@@ -141,53 +152,35 @@ function privateBrowsingEnabled()
if (!("service" in privateBrowsingEnabled))
privateBrowsingEnabled.service = Cc["@mozilla.org/privatebrowsing;1"].getService(Ci.nsIPrivateBrowsingService);
return privateBrowsingEnabled.service.privateBrowsingEnabled;
}
function sendAnonymousData()
{
- if (!Prefs.domainOptIn || (domains.length == 0 && falsePositives.length == 0 && userCorrections.length == 0) || privateBrowsingEnabled())
+ if (!Prefs.domainOptIn || privateBrowsingEnabled())
return;
-
- let postData = {};
+
+ let postData = JSON.stringify(domains);
+ if (postData == JSON.stringify({}))
+ return;
+
let savedDomains = domains;
- let savedFalsePositives = falsePositives;
- let savedUserCorrections = userCorrections;
+ domains = {};
- if(domains.length > 0)
- {
- postData.domains = domains;
- domains = [];
- }
- if(falsePositives.length > 0)
- {
- postData.falsePositives = falsePositives;
- falsePositives = [];
- }
- if(userCorrections.length > 0)
- {
- postData.userCorrections = userCorrections;
- userCorrections = [];
- }
-
let request = Cc["@mozilla.org/xmlextras/xmlhttprequest;1"].createInstance(Ci.nsIXMLHttpRequest);
request.open("POST", "http://typed.it/submitData");
- request.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
+ request.setRequestHeader("Content-Type", "application/json");
request.addEventListener("load", function(event)
{
if (event.target.status != 200)
- {
domains = domains.concat(savedDomains);
- falsePositives = falsePositives.concat(savedFalsePositives);
- userCorrections = userCorrections.concat(savedUserCorrections);
- }
}, false);
- request.send("data=" + JSON.stringify(postData));
+ request.send(postData);
}
function initWebUI(event)
{
if (Prefs.domainOptIn)
return;
let container = event.target;
« no previous file with comments | « no previous file | lib/typoFixer.js » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld