Index: lib/filterHits.js |
=================================================================== |
new file mode 100644 |
--- /dev/null |
+++ b/lib/filterHits.js |
@@ -0,0 +1,232 @@ |
+/* |
+ * This file is part of Adblock Plus <https://adblockplus.org/>, |
+ * Copyright (C) 2006-2015 Eyeo GmbH |
+ * |
+ * Adblock Plus is free software: you can redistribute it and/or modify |
+ * it under the terms of the GNU General Public License version 3 as |
+ * published by the Free Software Foundation. |
+ * |
+ * Adblock Plus is distributed in the hope that it will be useful, |
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of |
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
+ * GNU General Public License for more details. |
+ * |
+ * You should have received a copy of the GNU General Public License |
+ * along with Adblock Plus. If not, see <http://www.gnu.org/licenses/>. |
+ */ |
+ |
+let {Services} = Cu.import("resource://gre/modules/Services.jsm", null); |
+let {FileUtils} = Cu.import("resource://gre/modules/FileUtils.jsm", null); |
+ |
+let {Utils} = require("utils"); |
+let {MILLIS_IN_DAY} = require("downloader"); |
+ |
+/** |
+ * This class reads filter hits statistics from SQLite database, |
+ * manages them in memory and writes them back. |
+ * @class |
+ */ |
+let FilterHits = exports.FilterHits = |
+{ |
+ filters: {}, |
+ |
+ _serviceURL: "", |
Thomas Greiner
2015/02/23 18:43:05
Where is the value for this supposed to be coming
|
+ _lastPush: 0, |
+ _pushInterval: MILLIS_IN_DAY * 7, |
+ _loading: false, |
+ _saving: false, |
+ _sending: false, |
saroyanm
2015/02/28 15:24:30
Done.
|
+ |
+ /** |
+ * Increases the filter hit count |
+ * @param {Filter} filter |
+ * @param {Window} window Window that the match originated in (required to get host) |
+ */ |
+ increaseFilterHits: function(filter, wnd) |
+ { |
+ if (!filter.text || (filter.subscriptions[0] && filter.subscriptions[0].url.indexOf("~user~") == 0)) |
Thomas Greiner
2015/02/23 18:43:05
This means that if the first subscription a filter
|
+ return; |
+ |
+ if (!(filter.text in this.filters)) |
+ this.filters[filter.text] = {}; |
+ |
+ var statFilter = this.filters[filter.text]; |
Thomas Greiner
2015/02/23 18:43:05
Any reason why you're using `var` here?
|
+ let filterType = filter.thirdParty ? "thirdParty" : "firstParty"; |
+ |
+ if (!(filterType in statFilter)) |
+ statFilter[filterType] = {}; |
+ |
+ if (!("subscriptions" in statFilter)) |
+ statFilter.subscriptions = []; |
+ |
+ if (filter.subscriptions) |
+ { |
+ for (let i = 0; i < filter.subscriptions.length; i++) |
+ { |
+ if (statFilter.subscriptions.indexOf(filter.subscriptions[i]._title) == -1) |
+ statFilter.subscriptions.push(filter.subscriptions[i]._title); |
+ } |
+ } |
+ |
+ let wndLocation = Utils.getOriginWindow(wnd).location.href; |
+ let host = Utils.unwrapURL(wndLocation).host; |
+ |
+ if (!(host in statFilter[filterType])) |
+ statFilter[filterType][host] = {hits: 1, latest: filter.lastHit}; |
+ else |
+ { |
+ statFilter[filterType][host].hits++; |
+ statFilter[filterType][host].latest = filter.lastHit; |
+ } |
+ }, |
+ |
+ resetFilterHits: function() |
+ { |
+ this.filters = {}; |
+ this.saveFilterHitsToDatabase(); |
+ }, |
+ |
+ sendFilterHitsToServer: function() |
+ { |
+ let prepareData = function() |
saroyanm
2015/02/28 15:24:30
Done.
|
+ { |
+ let {addonName, addonVersion, application, applicationVersion, platform, platformVersion} = require("info"); |
+ return { |
+ version: 1, |
+ timeSincePush: this._lastPush, |
+ addonName: addonName, |
+ addonVersion: addonVersion, |
+ application: application, |
+ applicationVersion: applicationVersion, |
+ platform: platform, |
+ platformVersion: platformVersion, |
+ filters: this.filters |
+ } |
+ }.bind(this); |
+ |
+ let request = new XMLHttpRequest(); |
+ request.open("POST", this._serviceURL); |
+ request.setRequestHeader("Content-Type", "application/json"); |
+ request.addEventListener("load", function(event) |
+ { |
+ let request = event.target; |
saroyanm
2015/02/28 15:24:30
Done.
|
+ FilterHits._sending = false; |
+ if (request.status == 200) |
+ { |
+ FilterHits._lastPush = new Date().getTime(); |
+ FilterHits.resetFilterHits(); |
+ } |
+ else |
+ Cu.reportError("could not send filter hit statistics to AdBlock Plus server"); |
Thomas Greiner
2015/02/23 18:43:05
Detail: In the other error messages you start with
saroyanm
2015/02/28 15:24:30
Done.
saroyanm
2015/02/28 15:24:30
Done.
|
+ }, false); |
+ this._sending = true; |
+ request.send(JSON.stringify(prepareData())); |
+ }, |
+ |
+ getStorageFile: function() |
+ { |
+ return FileUtils.getFile("ProfD", ["adblockplus.sqlite"]); |
+ }, |
+ |
+ checkCreateTable: function(connection) |
+ { |
+ if (!connection.tableExists("filterhits")) |
+ connection.executeSimpleSQL("CREATE TABLE filterhits (id INTEGER PRIMARY KEY, filters TEXT, date INTEGER)"); |
+ }, |
+ |
+ /** |
+ * Load Filter hits from database |
+ */ |
+ loadFilterHitsFromDatabase: function() |
+ { |
+ let storageFile = this.getStorageFile(); |
+ if (!storageFile) |
+ return; |
+ |
+ let connection = Services.storage.openDatabase(storageFile); |
+ this.checkCreateTable(connection); |
+ |
+ let statement = connection.createStatement("SELECT * FROM filterhits"); |
+ if (!this._loading) |
+ { |
+ this._loading = true; |
+ statement.executeAsync( |
+ { |
+ handleResult: function(aResultSet) |
+ { |
+ let row = aResultSet.getNextRow(); |
+ if (row) |
+ { |
+ let filters = row.getResultByName("filters"); |
+ let lastDate = row.getResultByName("date"); |
+ FilterHits.filters = JSON.parse(filters); |
+ FilterHits._lastPush = lastDate; |
+ } |
+ FilterHits._loading = false; |
Thomas Greiner
2015/02/23 18:43:05
This seems redundant since `_loading` should alrea
|
+ }, |
+ |
+ handleError: function(aError) |
+ { |
+ Cu.reportError(aError.message); |
Thomas Greiner
2015/02/23 18:43:05
Unless `handleCompletion` is also called when ther
saroyanm
2015/02/28 15:24:30
Right, updated to always set `_loading` variable t
|
+ }, |
+ |
+ handleCompletion: function(aReason) |
+ { |
+ if (aReason != Components.interfaces.mozIStorageStatementCallback.REASON_FINISHED) |
+ { |
+ Cu.reportError("Loading of filter hits canceled or aborted."); |
+ FilterHits._loading = false; |
+ } |
+ } |
+ }); |
+ } |
+ |
+ connection.asyncClose(); |
+ }, |
+ |
+ /** |
+ * Save Filter hits to database |
+ */ |
+ saveFilterHitsToDatabase: function() |
+ { |
+ if (!this._lastPush) |
+ this._lastPush = new Date().getTime(); |
Thomas Greiner
2015/02/23 18:43:05
You're using `new Date().getTime()` twice so best
saroyanm
2015/02/28 15:24:30
Done.
|
+ |
+ if (!this._sending && new Date().getTime() - this._lastPush > this._pushInterval) |
+ { |
+ this.sendFilterHitsToServer(); |
+ return; |
+ } |
+ |
+ let storageFile = this.getStorageFile(); |
+ if (!storageFile) |
+ return; |
+ |
+ let connection = Services.storage.openDatabase(storageFile); |
+ this.checkCreateTable(connection); |
+ |
+ let statement = connection.createStatement("INSERT OR REPLACE INTO filterhits (id, filters, date) VALUES(0, :filters, :date)"); |
+ statement.params.filters = JSON.stringify(this.filters); |
+ statement.params.date = this._lastPush; |
+ if (!this._saving) |
+ { |
+ this._saving = true; |
+ statement.executeAsync( |
+ { |
+ handleError: function(aError) |
+ { |
+ Cu.reportError(aError.message); |
saroyanm
2015/02/28 15:24:30
Handle completion should also be triggered during
|
+ }, |
+ |
+ handleCompletion: function(aReason) |
+ { |
+ if (aReason != Components.interfaces.mozIStorageStatementCallback.REASON_FINISHED) |
+ Cu.reportError("Writing of filter hits canceled or aborted."); |
+ FilterHits._saving = false; |
+ } |
+ }); |
+ } |
+ |
+ connection.asyncClose(); |
+ } |
+}; |