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

Unified Diff: lib/typedItCollector.js

Issue 8382011: Applied changes from emailed code review (Closed)
Patch Set: Created Sept. 28, 2012, 11:09 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
« lib/survey.js ('K') | « lib/survey.js ('k') | 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
@@ -7,9 +7,12 @@
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
@@ -64,24 +67,36 @@
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;
}
}
@@ -101,29 +116,25 @@
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
{
@@ -146,61 +157,25 @@
function sendAnonymousData()
{
- if (!Prefs.domainOptIn || (domains.length == 0 && falsePositives.length == 0 && userCorrections.length == 0) || privateBrowsingEnabled())
+ if (!Prefs.domainOptIn || privateBrowsingEnabled())
return;
- let args = [];
+ let postData = JSON.stringify(domains);
+ if (postData == JSON.stringify({}))
+ return;
+
let savedDomains = domains;
- let savedFalsePositives = falsePositives;
- let savedUserCorrections = userCorrections;
-
- if(domains.length > 0)
- {
- args.push(domains.map(function(d) "domains[]=" + encodeURIComponent(d)).join("&"));
- domains = [];
- }
- if(falsePositives.length > 0)
- {
- args.push(
- falsePositives.map(
- function(fp)
- {
- return "falsePositives[]=" + encodeURIComponent(
- encodeURIComponent(fp[0]) + ((fp[1]) ? "&" + encodeURIComponent(fp[1]) : "")
- );
- }
- ).join("&")
- );
- falsePositives = [];
- }
- if(userCorrections.length > 0)
- {
- args.push(
- userCorrections.map(
- function(uc)
- {
- return "userCorrections[]=" + encodeURIComponent(
- encodeURIComponent(uc[0]) + ((uc[1]) ? "&" + encodeURIComponent(uc[1]) : "")
- );
- }
- ).join("&")
- );
- userCorrections = [];
- }
+ domains = {};
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(args.join("&"));
+ request.send(postData);
}
function initWebUI(event)
« lib/survey.js ('K') | « lib/survey.js ('k') | lib/typoFixer.js » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld