OLD | NEW |
(Empty) | |
| 1 /* |
| 2 * This file is part of Adblock Plus <http://adblockplus.org/>, |
| 3 * Copyright (C) 2006-2014 Eyeo GmbH |
| 4 * |
| 5 * Adblock Plus is free software: you can redistribute it and/or modify |
| 6 * it under the terms of the GNU General Public License version 3 as |
| 7 * published by the Free Software Foundation. |
| 8 * |
| 9 * Adblock Plus is distributed in the hope that it will be useful, |
| 10 * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 * GNU General Public License for more details. |
| 13 * |
| 14 * You should have received a copy of the GNU General Public License |
| 15 * along with Adblock Plus. If not, see <http://www.gnu.org/licenses/>. |
| 16 */ |
| 17 |
| 18 let {IO} = require("io"); |
| 19 let {Utils} = require("utils"); |
| 20 let {Prefs} = require("prefs"); |
| 21 let {TimeLine} = require("timeline"); |
| 22 let {Filter} = require("filterClasses"); |
| 23 |
| 24 /** |
| 25 * This class reads filter hits statistics from disk, manages them in memory and
writes them back.W |
| 26 * @class |
| 27 */ |
| 28 let FilterHits = exports.FilterHits = |
| 29 { |
| 30 statistics: {}, |
| 31 |
| 32 _loading: false, |
| 33 _saving: false, |
| 34 _needsSave: false, |
| 35 |
| 36 /** |
| 37 * File that the filter hits statistics has been loaded from and should be sav
ed to |
| 38 * @type nsIFile |
| 39 */ |
| 40 get filterHitsFile() |
| 41 { |
| 42 let file = null; |
| 43 if (!file) |
| 44 { |
| 45 // Place the file in the data dir |
| 46 file = IO.resolveFilePath(Prefs.data_directory); |
| 47 if (file) |
| 48 file.append("filter-hits.ini"); |
| 49 } |
| 50 if (!file) |
| 51 { |
| 52 // Data directory pref misconfigured? Try the default value |
| 53 try |
| 54 { |
| 55 file = IO.resolveFilePath(Services.prefs.getDefaultBranch("extensions.ad
blockplus.").getCharPref("data_directory")); |
| 56 if (file) |
| 57 file.append("filter-hits.ini"); |
| 58 } catch(e) {} |
| 59 } |
| 60 |
| 61 if (!file) |
| 62 Cu.reportError("Adblock Plus: Failed to resolve filter-hits file"); |
| 63 |
| 64 this.__defineGetter__("filterHitsFile", function() file); |
| 65 return this.filterHitsFile; |
| 66 }, |
| 67 |
| 68 /** |
| 69 * Increases the filter hit count by one |
| 70 * @param {Filter} filter |
| 71 * @param {Window} window Window that the match originated in (required to ge
t host name) |
| 72 */ |
| 73 increaseFilterHits: function(filter, wnd) |
| 74 { |
| 75 if (!filter.text) |
| 76 return; |
| 77 |
| 78 if (filter.text in this.statistics) |
| 79 this.statistics[filter.text].statsHitCount++; |
| 80 else |
| 81 this.statistics[filter.text] = {statsHitCount:1, lastHit: 0, domainsCount:
{}, |
| 82 thirdParty: false, subscriptions: []}; |
| 83 |
| 84 let increaseHits = function(filterStats) |
| 85 { |
| 86 if (filter.lastHit) |
| 87 filterStats.lastHit = filter.lastHit; |
| 88 |
| 89 if (filter.subscriptions) |
| 90 { |
| 91 for (let i = 0; i < filter.subscriptions.length; i++) |
| 92 { |
| 93 if (filterStats.subscriptions.indexOf(filter.subscriptions[i]._title)
== -1) |
| 94 filterStats.subscriptions.push(filter.subscriptions[i]._title); |
| 95 } |
| 96 } |
| 97 |
| 98 if (filter.thirdParty) |
| 99 filterStats.thirdParty = true; |
| 100 |
| 101 // Get hostname from window object |
| 102 let wndLocation = Utils.getOriginWindow(wnd).location.href; |
| 103 let host = Utils.unwrapURL(wndLocation).host; |
| 104 |
| 105 if (host in filterStats.domainsCount) |
| 106 filterStats.domainsCount[host]++; |
| 107 else |
| 108 filterStats.domainsCount[host] = 1; |
| 109 }; |
| 110 |
| 111 increaseHits(this.statistics[filter.text]); |
| 112 }, |
| 113 |
| 114 /** |
| 115 * send filter hits statistics to ABP sever and reset filter hits count |
| 116 */ |
| 117 sendFilterHitsToServer: function() |
| 118 { |
| 119 //TODO implement ajax request to server sending this.statistics |
| 120 //clear statics on 200 response |
| 121 this.resetFilterHits(); |
| 122 this.saveFilterHitsToDisk(); |
| 123 }, |
| 124 |
| 125 /** |
| 126 * Loads Filter hit statistics from the disk |
| 127 */ |
| 128 loadFilterHitsFromDisk: function() |
| 129 { |
| 130 if (this._loading) |
| 131 return; |
| 132 |
| 133 let sourceFile = FilterHits.filterHitsFile; |
| 134 if (!sourceFile) |
| 135 return; |
| 136 |
| 137 TimeLine.enter("Entered FilterHits.loadFilterHitsFromDisk()"); |
| 138 this._loading = true; |
| 139 |
| 140 let readFile = function() |
| 141 { |
| 142 TimeLine.enter("FilterHits.loadFilterHitsFromDisk() -> readFile()"); |
| 143 let parser = new FilterHitsParser(); |
| 144 IO.readFromFile(sourceFile, true, parser, function(e) |
| 145 { |
| 146 TimeLine.enter("FilterHits.loadFilterHitsFromDisk() read callback"); |
| 147 if (e) |
| 148 Cu.reportError(e); |
| 149 |
| 150 doneReading(parser); |
| 151 }.bind(this), "FilterHitsRead"); |
| 152 |
| 153 TimeLine.leave("FilterHits.loadFilterHitsFromDisk() <- readFile()", "Filte
rHitsRead"); |
| 154 }.bind(this); |
| 155 |
| 156 var doneReading = function(parser) |
| 157 { |
| 158 FilterHits.statistics = parser.statistics; |
| 159 this._loading = false; |
| 160 TimeLine.leave("FilterHits.loadFilterHitsFromDisk() read callback done"); |
| 161 }.bind(this); |
| 162 |
| 163 IO.statFile(sourceFile, function(e, statData) |
| 164 { |
| 165 if (!e && statData.exists) |
| 166 readFile(sourceFile); |
| 167 }); |
| 168 TimeLine.leave("FilterHits.loadFilterHitsFromDisk() done"); |
| 169 }, |
| 170 |
| 171 /** |
| 172 * Stringify filter hits statistics object |
| 173 */ |
| 174 _generateFilterHitData: function() |
| 175 { |
| 176 yield JSON.stringify(FilterHits.statistics); |
| 177 }, |
| 178 |
| 179 /** |
| 180 * Save Filters hit statistics to the disk |
| 181 */ |
| 182 saveFilterHitsToDisk: function() |
| 183 { |
| 184 let targetFile = FilterHits.filterHitsFile; |
| 185 if (!targetFile) |
| 186 return; |
| 187 |
| 188 if (this._saving) |
| 189 { |
| 190 this._needsSave = true; |
| 191 return; |
| 192 } |
| 193 TimeLine.enter("Entered FilterHits.saveFilterHitsToDisk()"); |
| 194 // Make sure the file's parent directory exists |
| 195 try { |
| 196 targetFile.parent.create(Ci.nsIFile.DIRECTORY_TYPE, FileUtils.PERMS_DIRECT
ORY); |
| 197 } catch (e) {} |
| 198 |
| 199 let writeStats = function() |
| 200 { |
| 201 TimeLine.enter("FilterHits.saveFilterHitsToDisk() -> writeStats()"); |
| 202 IO.writeToFile(targetFile, true, this._generateFilterHitData(), function(e
) |
| 203 { |
| 204 TimeLine.enter("FilterHits.saveFilterHitsToDisk() write callback"); |
| 205 this._saving = false; |
| 206 |
| 207 if (e) |
| 208 Cu.reportError(e); |
| 209 |
| 210 if (this._needsSave) |
| 211 { |
| 212 this._needsSave = false; |
| 213 this.saveFilterHitsToDisk(); |
| 214 } |
| 215 TimeLine.leave("FilterHits.saveFilterHitsToDisk() write callback done"); |
| 216 }.bind(this), "FilterHitsWriteStats"); |
| 217 TimeLine.leave("FilterHits.saveFilterHitsToDisk() -> writeStats()", "Filte
rStorageWriteStats"); |
| 218 }.bind(this); |
| 219 |
| 220 this._saving = true; |
| 221 writeStats(); |
| 222 TimeLine.leave("FilterHits.saveFilterHitsToDisk() done"); |
| 223 } |
| 224 }; |
| 225 |
| 226 /** |
| 227 * IO.readFromFile() listener to parse filter-hits data. |
| 228 * @constructor |
| 229 */ |
| 230 function FilterHitsParser() |
| 231 { |
| 232 this.statistics = {__proto__: null}; |
| 233 } |
| 234 FilterHitsParser.prototype = |
| 235 { |
| 236 process: function(val) |
| 237 { |
| 238 if (val === null) |
| 239 return; |
| 240 this.statistics = JSON.parse(val); |
| 241 } |
| 242 }; |
OLD | NEW |