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

Delta Between Two Patch Sets: lib/filterStorage.js

Issue 29426559: Issue 5137 - [emscripten] Added basic filter storage implementation (Closed) Base URL: https://hg.adblockplus.org/adblockpluscore
Left Patch Set: Fixed some inconsistencies Created May 3, 2017, 8:25 a.m.
Right Patch Set: Fixed bogus assert Created Aug. 31, 2017, 12:44 p.m.
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
Left: Side by side diff | Download
Right: Side by side diff | Download
« no previous file with change/comment | « compiled/subscription/UserDefinedSubscription.cpp ('k') | lib/subscriptionClasses.js » ('j') | no next file with change/comment »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
LEFTRIGHT
1 /* 1 /*
2 * This file is part of Adblock Plus <https://adblockplus.org/>, 2 * This file is part of Adblock Plus <https://adblockplus.org/>,
3 * Copyright (C) 2006-2017 eyeo GmbH 3 * Copyright (C) 2006-present eyeo GmbH
4 * 4 *
5 * Adblock Plus is free software: you can redistribute it and/or modify 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 6 * it under the terms of the GNU General Public License version 3 as
7 * published by the Free Software Foundation. 7 * published by the Free Software Foundation.
8 * 8 *
9 * Adblock Plus is distributed in the hope that it will be useful, 9 * Adblock Plus is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of 10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details. 12 * GNU General Public License for more details.
13 * 13 *
14 * You should have received a copy of the GNU General Public License 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/>. 15 * along with Adblock Plus. If not, see <http://www.gnu.org/licenses/>.
16 */ 16 */
17 17
18 "use strict"; 18 "use strict";
19 19
20 const {FilterStorage} = require("compiled"); 20 const {FilterStorage} = require("compiled");
21 const {Subscription, SpecialSubscription} = require("subscriptionClasses"); 21 const {Subscription, SpecialSubscription} = require("subscriptionClasses");
22 const {ActiveFilter} = require("filterClasses");
23 const {Prefs} = require("prefs");
24 22
25 // Backwards compatibility 23 // Backwards compatibility
26 FilterStorage.getGroupForFilter = FilterStorage.getSubscriptionForFilter; 24 FilterStorage.getGroupForFilter = FilterStorage.getSubscriptionForFilter;
27 25
28 /** 26 /**
29 * This property allows iterating over the list of subscriptions. It will delete 27 * This property allows iterating over the list of subscriptions. It will delete
30 * references automatically at the end of the current loop iteration. If you 28 * references automatically at the end of the current loop iteration. If you
31 * need persistent references or element access by position you should use 29 * need persistent references or element access by position you should use
32 * FilterStorage.subscriptionAt() instead. 30 * FilterStorage.subscriptionAt() instead.
33 * @type {Iterable} 31 * @type {Iterable}
34 */ 32 */
35 FilterStorage.subscriptions = { 33 FilterStorage.subscriptions = {
36 [Symbol.iterator]: function*() 34 *[Symbol.iterator]()
37 { 35 {
38 for (let i = 0, l = FilterStorage.subscriptionCount; i < l; i++) 36 for (let i = 0, l = FilterStorage.subscriptionCount; i < l; i++)
39 { 37 {
40 let subscription = FilterStorage.subscriptionAt(i); 38 let subscription = FilterStorage.subscriptionAt(i);
41 try 39 try
42 { 40 {
43 yield subscription; 41 yield subscription;
44 } 42 }
45 finally 43 finally
46 { 44 {
47 subscription.delete(); 45 subscription.delete();
48 } 46 }
49 } 47 }
50 } 48 }
51 }; 49 };
52 50
53 /** 51 /**
54 * Adds a user-defined filter to the most suitable subscription in the list, 52 * Adds a user-defined filter to the most suitable subscription in the list,
55 * creates one if none found. 53 * creates one if none found.
56 * @param {Filter} filter 54 * @param {Filter} filter
55 * @returns {boolean}
56 * false if the filter was already in the list and no adding was performed
57 */ 57 */
58 FilterStorage.addFilter = function(filter) 58 FilterStorage.addFilter = function(filter)
59 { 59 {
60 for (let subscription of this.subscriptions) 60 for (let subscription of this.subscriptions)
61 if (!subscription.disabled && subscription.indexOfFilter(filter) >= 0) 61 if (!subscription.disabled && subscription.indexOfFilter(filter) >= 0)
62 return; 62 return false;
63 63
64 let subscription = this.getSubscriptionForFilter(filter); 64 let subscription = this.getSubscriptionForFilter(filter);
65 if (!subscription) 65 try
66 { 66 {
67 subscription = Subscription.fromURL(null); 67 if (!subscription)
68 subscription.makeDefaultFor(filter); 68 {
69 this.addSubscription(subscription); 69 subscription = Subscription.fromURL(null);
70 subscription.makeDefaultFor(filter);
71 this.addSubscription(subscription);
72 }
73 subscription.insertFilterAt(filter, subscription.filterCount);
70 } 74 }
71 subscription.insertFilterAt(filter, subscription.filterCount); 75 finally
72 subscription.delete(); 76 {
sergei 2017/05/08 10:54:39 strictly speaking it should be in finally block, b
Wladimir Palant 2017/05/08 12:55:41 You are correct. I tend to be too comfortable for
77 if (subscription)
78 subscription.delete();
79 }
73 return true; 80 return true;
74 }; 81 };
75 82
76 /** 83 /**
77 * Removes a user-defined filter from the list 84 * Removes a user-defined filter from the list
78 * @param {Filter} filter 85 * @param {Filter} filter
79 */ 86 */
80 FilterStorage.removeFilter = function(filter) 87 FilterStorage.removeFilter = function(filter)
81 { 88 {
82 for (let subscription of this.subscriptions) 89 for (let subscription of this.subscriptions)
83 { 90 {
84 if (subscription instanceof SpecialSubscription) 91 if (subscription instanceof SpecialSubscription)
85 { 92 {
86 while (true) 93 while (true)
sergei 2017/05/08 10:54:39 Is this loop to remove duplicates of the filter in
Wladimir Palant 2017/05/08 12:55:41 No, a subscription can contain the same filter mul
87 { 94 {
88 let index = subscription.indexOfFilter(filter); 95 let index = subscription.indexOfFilter(filter);
89 if (index >= 0) 96 if (index >= 0)
90 subscription.removeFilterAt(index); 97 subscription.removeFilterAt(index);
91 else 98 else
92 break; 99 break;
93 } 100 }
94 } 101 }
95 } 102 }
96 }; 103 };
97 104
98 exports.FilterStorage = FilterStorage; 105 exports.FilterStorage = FilterStorage;
LEFTRIGHT

Powered by Google App Engine
This is Rietveld