Index: lib/filterHits.js |
=================================================================== |
new file mode 100644 |
--- /dev/null |
+++ b/lib/filterHits.js |
@@ -0,0 +1,231 @@ |
+/* |
+ * This file is part of Adblock Plus <http://adblockplus.org/>, |
+ * Copyright (C) 2006-2014 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: "", |
+ _lastPush: 0, |
+ _pushInterval: MILLIS_IN_DAY * 7, |
+ _loading: false, |
+ _saving: false, |
+ _sending: false, |
+ |
+ /** |
+ * 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)) |
+ return; |
+ |
+ if (!(filter.text in this.filters)) |
+ this.filters[filter.text] = {}; |
+ |
+ let filterType = filter.thirdParty ? "thirdParty" : "firstParty"; |
+ |
+ if (!(filterType in this.filters[filter.text])) |
+ this.filters[filter.text][filterType] = {}; |
+ |
+ if (!("subscriptions" in this.filters[filter.text])) |
+ this.filters[filter.text].subscriptions = []; |
+ |
+ if (filter.subscriptions) |
+ { |
+ for (let i = 0; i < filter.subscriptions.length; i++) |
+ { |
+ if (this.filters[filter.text].subscriptions.indexOf(filter.subscriptions[i]._title) == -1) |
+ this.filters[filter.text].subscriptions.push(filter.subscriptions[i]._title); |
+ } |
+ } |
+ |
+ let wndLocation = Utils.getOriginWindow(wnd).location.href; |
+ let host = Utils.unwrapURL(wndLocation).host; |
+ |
+ if (!(host in this.filters[filter.text][filterType])) |
+ this.filters[filter.text][filterType][host] = {hits: 1, latest: filter.lastHit}; |
+ else |
+ { |
+ this.filters[filter.text][filterType][host].hits++; |
+ this.filters[filter.text][filterType][host].latest = filter.lastHit; |
+ } |
+ }, |
+ |
+ resetFilterHits: function() |
+ { |
+ this.filters = {}; |
+ this.saveFilterHitsToDatabase(); |
+ }, |
+ |
+ sendFilterHitsToServer: function() |
+ { |
+ let prepareData = function() |
+ { |
+ 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; |
+ 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"); |
+ }, 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; |
+ }, |
+ |
+ handleError: function(aError) |
+ { |
+ Cu.reportError(aError.message); |
+ }, |
+ |
+ 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(); |
+ |
+ 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); |
+ }, |
+ |
+ handleCompletion: function(aReason) |
+ { |
+ if (aReason != Components.interfaces.mozIStorageStatementCallback.REASON_FINISHED) |
+ Cu.reportError("Writing of filter hits canceled or aborted."); |
+ FilterHits._saving = false; |
+ } |
+ }); |
+ } |
+ |
+ connection.asyncClose(); |
+ } |
+}; |