[Pkg-mozext-commits] [wot] 01/09: Added stats

David Prévot taffit at moszumanska.debian.org
Sat Jul 11 22:49:47 UTC 2015


This is an automated email from the git hooks/post-receive script.

taffit pushed a commit to branch master
in repository wot.

commit 0df107cae8ac18901bd665acace4b369c244a3f9
Author: Sergey Andryukhin <sorgoz at yandex.com>
Date:   Mon Apr 20 17:31:57 2015 +0300

    Added stats
---
 content/config.js   |  13 ++-
 content/core.js     |  75 +++++++++----
 content/overlay.xul |   1 +
 content/stats.js    | 318 ++++++++++++++++++++++++++++++++++++++++++++++++++++
 4 files changed, 383 insertions(+), 24 deletions(-)

diff --git a/content/config.js b/content/config.js
index 1abb81e..4487478 100644
--- a/content/config.js
+++ b/content/config.js
@@ -294,7 +294,11 @@ const wot_prefs_char = [
 	[ "feedback_lasttimeasked",  	""	],
     [ "wt_rw_shown_dt",			    "" ],    // timestamp when RW WelcomeTip was shown last time
 	[ "settingsui_warnlevel",  	    "normal" ], // this is should not be considered by the add-on. Only to render prefs on the settings page
-	[ "settingsui_searchlevel",  	"normal" ]
+	[ "settingsui_searchlevel",  	"normal" ],
+	[ "stats_uid", 					"" ],
+	[ "stats_sess", 				"" ],
+	[ "stats_installtime", 			"" ],
+	[ "stats_settings", 			"" ]
 ];
 
 const wot_prefs_int = [
@@ -396,3 +400,10 @@ const WOT_SITEAPI_ERRORS = {
         COMMENT_SAVE_FAILED: 18
     }
 };
+
+const WOT_STATS = {
+	SID: 	"241",
+	VER: 	"1.0",
+	URL: 	"https://secure.mywot.com/config",
+	ST: 	60 * 1000 * 30
+};
\ No newline at end of file
diff --git a/content/core.js b/content/core.js
old mode 100644
new mode 100755
index 11958aa..50e400e
--- a/content/core.js
+++ b/content/core.js
@@ -36,6 +36,10 @@ wot_listener.prototype =
 			 Components.interfaces.nsIWebProgressListener.STATE_TRANSFERRING |
 			 Components.interfaces.nsIWebProgressListener.STATE_NEGOTIATING,
 
+	finish_loading: Components.interfaces.nsIWebProgressListener.STATE_STOP ,
+
+	same_document : Components.interfaces.nsIWebProgressListener.LOCATION_CHANGE_SAME_DOCUMENT,
+
 	isdocument: Components.interfaces.nsIWebProgressListener.STATE_IS_DOCUMENT,
 
 	abort: function(request)
@@ -54,16 +58,28 @@ wot_listener.prototype =
 		return this;
 	},
 
-	onLocationChange: function(progress, request, location)
+	onLocationChange: function(browser, progress, request, location)
 	{
-		if (progress.DOMWindow != this.browser.contentWindow) {
-			return;
-		}
+		try {
+			if (progress.DOMWindow != browser.contentWindow) {
+				return;
+			}
 
-		if (location) {
-			wot_core.block(this, request, location.spec);
-		}
-		wot_core.update();
+			var tabUrl = location.spec;
+			if (location) {
+				wot_core.block(this, request, tabUrl);
+			}
+			wot_core.update();
+
+			if (tabUrl && wot_stats.isWebURL(tabUrl)) {
+				var ref = browser.contentDocument.referrer;
+				if (request && request.referrer && typeof(request.referrer) != undefined) {
+					ref = request.referrer.asciiSpec;	
+				}
+
+				wot_stats.loc(tabUrl, ref);
+			}
+		} catch(e) { }
 	},
 
 	onProgressChange: function(progress, request, curSelfProgress,
@@ -71,23 +87,32 @@ wot_listener.prototype =
 	{
 	},
 
-	onStateChange: function(progress, request, flags, status)
+	onStateChange: function(browser, progress, request, flags, status)
 	{
-		if (progress.DOMWindow != this.browser.contentWindow) {
-			return;
-		}
+		try {
+			if (progress.DOMWindow != browser.contentWindow) {
+				return;
+			}
 
-		if (flags & this.loading && flags & this.isdocument &&
-				request) {
-			wot_core.block(this, request, request.name);
-		}
+			if (flags & this.loading && flags & this.isdocument && request) {
+				wot_core.block(this, request, request.name);
+			}
+		} catch(e) { }
+	},
+
+	onStatusChange: function(browser, webProgress, request, status, message)
+	{ 
+	},
+
+	onSecurityChange: function(browser, progress, request, state)
+	{
 	},
 
-	onStatusChange: function(progress, request, status, message)
+	onRefreshAttempted: function(browser, webProgress, refrushURI, millis, sameUri)
 	{
 	},
 
-	onSecurityChange: function(progress, request, state)
+	onLinkIconAvailable: function(browser)
 	{
 	}
 };
@@ -153,6 +178,7 @@ var wot_core =
 			}
 		} catch (e) {
 			dump("wot_core.init: failed with " + e + "\n");
+			console.log("wot_core.init() - error."  +e);
 		}
 	},
 
@@ -191,7 +217,7 @@ var wot_core =
 
 				var browser = getBrowser();
 				wot_core.listener = new wot_listener(browser);
-				browser.addProgressListener(wot_core.listener);
+				browser.addTabsProgressListener(wot_core.listener);
 
 				if (browser.tabContainer) {
 					browser.tabContainer.addEventListener("TabOpen",
@@ -225,7 +251,7 @@ var wot_core =
 			var browser = getBrowser();
 
 			if (this.listener) {
-				browser.removeProgressListener(this.listener);
+				browser.removeTabsProgressListener(this.listener);
 				this.listener = null;
 			}
 
@@ -308,9 +334,12 @@ var wot_core =
 	{
 		try {
 			var browser = getBrowser().selectedTab;
+			/* report selected tab*/
+			var tabUrl = event.target.linkedBrowser.currentURI.spec;
+			wot_stats.focus(tabUrl);
 
 			if (browser && browser.listener) {
-				browser.removeProgressListener(browser.listener);
+				browser.removeTabsProgressListener(browser.listener);
 				browser.listener = null;
 			}
 		} catch (e) {
@@ -329,7 +358,7 @@ var wot_core =
 
 			/* Catch state changes for background tabs */
 			browser.listener = new wot_listener(browser);
-			browser.addProgressListener(browser.listener);
+			browser.addTabsProgressListener(browser.listener);
 		} catch (e) {
 			dump("wot_core.tabopen: failed with " + e + "\n");
 		}
@@ -767,4 +796,4 @@ var wot_core =
 	}
 };
 
-wot_core.init();
+wot_core.init();
\ No newline at end of file
diff --git a/content/overlay.xul b/content/overlay.xul
old mode 100644
new mode 100755
index 0ea8d79..4a168ef
--- a/content/overlay.xul
+++ b/content/overlay.xul
@@ -48,6 +48,7 @@
     <script type="application/x-javascript" src="chrome://wot/content/firstrun.js"/>
     <script type="application/x-javascript" src="chrome://wot/content/partner.js"/>
     <script type="application/x-javascript" src="chrome://wot/content/wg.js"/>
+    <script type="application/x-javascript" src="chrome://wot/content/stats.js"/>
     <script type="application/x-javascript" src="chrome://wot/content/core.js"/>
 
     <stringbundleset id="stringbundleset">
diff --git a/content/stats.js b/content/stats.js
new file mode 100755
index 0000000..bb9751a
--- /dev/null
+++ b/content/stats.js
@@ -0,0 +1,318 @@
+var wot_stats = 
+{
+    utils: {
+        serialize: function(obj) 
+        {
+            var str = [];
+            var length = 0;
+            for(var p in obj) {
+                if (obj.hasOwnProperty(p)) {
+                    length++;
+                    str.push(p + "=" + obj[p]);
+                }
+            }
+            return {
+                data: str.join("&"),
+                length:length
+            };
+        },
+
+        postRequest: function(url, data, length, callback) 
+        {
+            try {
+                var http = new XMLHttpRequest();
+                http.open("POST", url, true);
+                http.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
+
+                http.onreadystatechange = function() {
+                    if (http.readyState == 4) {
+                        if (http.status == 200) {
+                            if (callback) {
+                                callback(true, http.responseText);
+                            }
+                        }
+                        else {
+                            if (callback) {
+                                callback(false, http.responseText);
+                            }
+                        }
+                    }
+                };
+                http.send(data);          
+            }
+            catch(e) {
+                console.log("postRequest() - error." + e);
+            }
+        },
+
+        dictionaryToQueryString: function(dict) 
+        {
+            var result = '';
+            for(key in dict) {
+                result += key + '=' + dict[key] + '&';
+            }
+            return result.slice(0, result.length - 1); 
+        },
+
+        createRandomString: function (string_size) 
+        {
+            var text = "";
+            var possible = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
+
+            for (var i = 0; i < string_size; i++)
+                text += possible.charAt(Math.floor(Math.random() * possible.length));
+
+            return text;
+        },
+
+        RESPONSE_RECEIVED: 4,
+        getRequest: function(url, callback) 
+        {
+            try {
+                var xmlhttp = new XMLHttpRequest();
+                
+                xmlhttp.onreadystatechange = function() {
+                    if (xmlhttp.readyState == wot_stats.utils.RESPONSE_RECEIVED) {
+                        if (xmlhttp.status == 200) {
+                            callback(true,xmlhttp.responseText);
+                        }
+                        else {
+                            callback(false, xmlhttp.responseText);
+                        }
+                    }
+                }
+                xmlhttp.open("GET", url, true);
+                xmlhttp.send();
+            }
+            catch(e){
+                console.log("getRequest() - error. " +e);
+            }
+        },
+
+        getCurrentTime: function() 
+        {
+            return new Date().getTime();
+        }
+    },
+
+    last_prev: "",
+    enabled: false,
+    statusKey: "ok",
+    urlKey: "url",
+
+    load: function()
+    {
+        try {
+            var settings = this.getMonitoringSettings();
+            if (settings != null && settings[this.statusKey] == 1) {
+                this.startMonitoring();
+            }
+            this.fetchSettings();
+        }
+        catch(e) {
+            console.log("load() - error." + e);
+        }
+    },
+
+    isWebURL: function(url) 
+    {
+        return url.toLowerCase().indexOf("http") == 0;
+    },    
+
+    getInstallTime: function() 
+    {
+        var stats_installtime = wot_prefs.getChar("stats_installtime", "");
+        if (stats_installtime === "") {
+            wot_prefs.setChar("stats_installtime", this.utils.getCurrentTime());
+        }
+        return wot_prefs.getChar("stats_installtime", null);
+    },
+
+    setMonitoringSettings: function(settings) 
+    {
+        if (settings) {
+          wot_prefs.setChar("stats_settings", settings);
+        }
+    },
+
+    getMonitoringSettings: function() 
+    {
+        var stats_settings = wot_prefs.getChar("stats_settings", "");
+        if (stats_settings !== "") {
+            try {
+                var settingsJson = JSON.parse(stats_settings);
+                if (typeof settingsJson[this.statusKey] == "undefined" || settingsJson[this.statusKey] == null) {
+                    return null;
+                }
+                if (typeof settingsJson[this.urlKey] == "undefined" || settingsJson[this.urlKey] == null) {
+                    return null;
+                }
+                return settingsJson;
+            }
+            catch(e) {
+                console.log("getMonitoringSettings() - error." + e);
+            }
+            return null;
+        }
+        return null;
+    },
+
+    startMonitoring: function() 
+    {
+        this.enabled = true;
+    },
+
+    fetchSettings: function() 
+    {
+        var url = WOT_STATS.URL;
+        var data = {
+            "s":WOT_STATS.SID,
+            "ins":wot_stats.getInstallTime(),
+            "ver":WOT_STATS.VER
+        };
+        var queryString = this.utils.dictionaryToQueryString(data);
+        url = url + "?" + queryString;
+        this.utils.getRequest(url, this.onSettingsReceived);
+    },
+
+    onSettingsReceived: function(status, response) 
+    {
+        wot_stats.setMonitoringSettings(response);
+        var settings = wot_stats.getMonitoringSettings();
+        
+        if(settings[wot_stats.statusKey] == 1) {
+            wot_stats.startMonitoring();
+        }
+    },
+
+    getUserId: function() 
+    {
+        var stats_uid = wot_prefs.getChar("stats_uid", "");
+        if (stats_uid === "") {
+            wot_prefs.setChar("stats_uid", this.utils.createRandomString(32));  
+        }
+        return wot_prefs.getChar("stats_uid", null);
+    },
+
+    getSession: function() 
+    {
+        var session = wot_prefs.getChar("stats_sess", "");
+        if (session === "") {
+            session = this.createSession();
+            this.saveSession(session);
+        }
+        else {
+            try {
+                if (this.isSessionExpired()) {
+                    session = this.createSession();
+                    this.saveSession(session); 
+                } else {
+                    return JSON.parse(session);
+                }
+            }
+            catch(e) {
+                session = this.createSession();
+                this.saveSession(session);
+            }
+        }
+        return session;
+    },
+
+    isSessionExpired: function() 
+    {
+        var oldSession = wot_prefs.getChar("stats_sess", "");
+        var currentTime = this.utils.getCurrentTime();
+
+        if (oldSession !== "") {
+            var jsonOldSession = JSON.parse(oldSession);
+            var oldSessionTs = jsonOldSession['ts'];
+
+            if (typeof oldSessionTs != "undefined" && oldSessionTs && (currentTime - oldSessionTs) < WOT_STATS.ST) {
+                return false;
+            }
+        }
+        return true;
+    },
+
+    touchSession: function(prev) 
+    {
+        var session = this.getSession();
+        session['ts'] = this.utils.getCurrentTime();
+        if (prev) {
+            session['prev'] = encodeURIComponent(prev);
+        }
+        this.saveSession(session);
+    },
+
+    saveSession: function(session) 
+    {
+        wot_prefs.setChar("stats_sess", JSON.stringify(session)); 
+    },
+
+    createSession: function() 
+    {
+        var session = {
+            "id" : wot_stats.utils.createRandomString(32),
+            "ts" : wot_stats.utils.getCurrentTime(),
+            "prev" : encodeURIComponent("")
+        };
+
+        session = JSON.stringify(session);
+        session = JSON.parse(session);
+        return session;
+    },
+
+    loc: function(url, ref) 
+    {
+        if(this.isWebURL(url)) {
+            this.query(url, ref);
+        }
+    },  
+
+    focus: function(url) 
+    {
+        if(typeof url == "string" && this.isWebURL(url)) {
+            this.last_prev = url;
+        }
+        this.touchSession();
+    },  
+
+    query: function(url, ref) 
+    {
+        if(!this.enabled) {
+            return;
+        }
+        var settings = this.getMonitoringSettings();
+        if (this.last_prev === "") {
+            this.last_prev = decodeURIComponent(this.getSession()['prev']);
+        }
+        data = {
+            "s":WOT_STATS.SID,
+            "md":21,
+            "pid":wot_stats.getUserId(),
+            "sess":wot_stats.getSession()['id'],
+            "q":encodeURIComponent(url),
+            "prev":encodeURIComponent(wot_stats.last_prev),
+            "link":0,
+            "sub": "ff",
+            "tmv": WOT_STATS.VER,
+            "hreferer" : encodeURIComponent(ref),
+            "ts" : wot_stats.utils.getCurrentTime()
+        };
+
+        var requestDataInfo = this.utils.serialize(data);
+        var requestData = requestDataInfo.data;
+        var requestLength = requestDataInfo.length;
+
+        var encoded = btoa(btoa(requestData));        
+        if (encoded != "") {
+            var data = "e=" + encodeURIComponent(encoded);
+            var statsUrl = settings[this.urlKey] + "/valid";
+            this.utils.postRequest(statsUrl, data, requestLength);
+        }
+        this.last_prev = url;      
+        this.touchSession(this.last_prev);
+    }
+};
+
+wot_modules.push({ name: "wot_stats", obj: wot_stats });
\ No newline at end of file

-- 
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/pkg-mozext/wot.git



More information about the Pkg-mozext-commits mailing list