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

Delta Between Two Patch Sets: adblockplus/Api.jsm

Issue 5365916275572736: Issue 2351 - Add a custom menu item for whitelisting the current site (Closed)
Left Patch Set: Created April 25, 2015, 9:45 p.m.
Right Patch Set: Address comments, rename location, change item label Created May 6, 2015, 4:41 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 | « no previous file | mobile/android/base/BrowserApp.java » ('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-2015 Eyeo GmbH 3 * Copyright (C) 2006-2015 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 var EXPORTED_SYMBOLS = ["AdblockPlusApi"]; 18 var EXPORTED_SYMBOLS = ["AdblockPlusApi"];
19 19
20 const Cc = Components.classes; 20 const Cc = Components.classes;
21 const Ci = Components.interfaces; 21 const Ci = Components.interfaces;
22 const Cr = Components.results; 22 const Cr = Components.results;
23 const Cu = Components.utils; 23 const Cu = Components.utils;
24 24
25 Cu.import("resource://gre/modules/Services.jsm"); 25 Cu.import("resource://gre/modules/Services.jsm");
26 Cu.import("resource://gre/modules/Messaging.jsm"); 26 Cu.import("resource://gre/modules/Messaging.jsm");
27 27
28 let XMLHttpRequest = Components.Constructor(
29 "@mozilla.org/xmlextras/xmlhttprequest;1", "nsIXMLHttpRequest");
30
28 function require(module) 31 function require(module)
29 { 32 {
30 let result = {}; 33 let result = {};
31 result.wrappedJSObject = result; 34 result.wrappedJSObject = result;
32 Services.obs.notifyObservers(result, "adblockplus-require", module); 35 Services.obs.notifyObservers(result, "adblockplus-require", module);
33 return result.exports; 36 return result.exports;
34 } 37 }
35 38
36 let {Filter} = require("filterClasses"); 39 let {Filter} = require("filterClasses");
37 let {FilterStorage} = require("filterStorage"); 40 let {FilterStorage} = require("filterStorage");
38 let {defaultMatcher} = require("matcher"); 41 let {defaultMatcher} = require("matcher");
39 let {Prefs} = require("prefs"); 42 let {Prefs} = require("prefs");
43 let {Subscription, SpecialSubscription, RegularSubscription, DownloadableSubscri ption, ExternalSubscription} = require("subscriptionClasses");
44 let {Synchronizer} = require("synchronizer");
40 let {UI} = require("ui"); 45 let {UI} = require("ui");
46
47 function getWhitelistingFilter(url)
48 {
49 let uriObject = Services.io.newURI(url, null, null);
50 try
51 {
52 return defaultMatcher.whitelist.matchesAny(
53 uriObject.spec, "DOCUMENT", uriObject.host, false, null);
54 }
55 catch (e)
56 {
57 return null;
58 }
59 }
41 60
42 var AdblockPlusApi = 61 var AdblockPlusApi =
43 { 62 {
44 get filtersLoaded() 63 get filtersLoaded()
45 { 64 {
46 return !FilterStorage._loading; 65 return !FilterStorage._loading;
47 }, 66 },
48 get acceptableAdsEnabled() 67 get acceptableAdsEnabled()
49 { 68 {
50 return FilterStorage.subscriptions.some( 69 return FilterStorage.subscriptions.some(
51 (subscription) => subscription.url == Prefs.subscriptions_exceptionsurl); 70 (subscription) => subscription.url == Prefs.subscriptions_exceptionsurl);
52 }, 71 },
53 set acceptableAdsEnabled(acceptableAdsEnabled) 72 set acceptableAdsEnabled(acceptableAdsEnabled)
54 { 73 {
55 if (acceptableAdsEnabled != AdblockPlusApi.acceptableAdsEnabled) 74 if (acceptableAdsEnabled != AdblockPlusApi.acceptableAdsEnabled)
56 UI.toggleAcceptableAds(); 75 UI.toggleAcceptableAds();
57 }, 76 },
58 initCommunication: function() 77 get subscriptionsXml()
59 { 78 {
60 RequestService.addListener((function(data) 79 let request = new XMLHttpRequest();
61 { 80 // Synchronous requests are deprecated, but using an asynchronous request
62 if (!data) 81 // here (for a static resource that doesn't need to be downloaded anyway)
63 return {"success": false, "error": "malformed request"}; 82 // would require a huge code change on the Java side, so we stick to
64 83 // synchronous requests
65 if (data["action"] == "getFiltersLoaded") 84 request.open("GET",
66 return {"success": true, "value": this.filtersLoaded}; 85 "chrome://adblockplus/content/ui/subscriptions.xml",
67 86 false);
68 if (!this.filtersLoaded) 87 request.send();
69 return {"success": false, "error": "filters not loaded"}; 88 return request.responseText;
70 89 },
71 switch (data["action"]) 90 isSubscriptionListed: function(url)
72 { 91 {
73 case "getAcceptableAdsEnabled": 92 return url in FilterStorage.knownSubscriptions;
74 return {"success": true, "value": this.acceptableAdsEnabled}; 93 },
75 case "setAcceptableAdsEnabled": 94 addSubscription: function(url, title)
76 if ("enable" in data) 95 {
77 { 96 let subscription = Subscription.fromURL(url);
78 this.acceptableAdsEnabled = !!data["enable"]; 97 subscription.title = title;
79 return {"success" : true}; 98 FilterStorage.addSubscription(subscription);
80 } 99 if (!subscription.lastDownload)
81 return {"success": false, "error": "malformed request"}; 100 {
82 } 101 Synchronizer.execute(subscription);
83 return {"success": false, "error": "malformed request"}; 102 }
84 }).bind(this), "AdblockPlus:Api"); 103 },
85 }, 104 removeSubscription: function(url)
86 isLocal: function(location) 105 {
87 { 106 FilterStorage.removeSubscription(
88 return !location.schemeIs("http") && !location.schemeIs("https"); 107 FilterStorage.knownSubscriptions[url]);
89 }, 108 },
90 isPageWhitelisted: function(location) 109 isLocal: function(url)
91 { 110 {
92 return defaultMatcher.whitelist.matchesAny( 111 let uriObject = Services.io.newURI(url, null, null);
93 location.spec, "DOCUMENT", location.host, false, null); 112 return !uriObject.schemeIs("http") && !uriObject.schemeIs("https");
94 }, 113 },
95 whitelistSite: function(location, whitelisted) 114 isPageWhitelisted: function(url)
96 { 115 {
116 return AdblockPlusApi.isLocal(url) || !!getWhitelistingFilter(url);
117 },
118 whitelistSite: function(url, whitelisted)
119 {
120 let uriObject = Services.io.newURI(url, null, null);
97 if (whitelisted) 121 if (whitelisted)
98 { 122 {
99 var host = location.host.replace(/^www\./, ""); 123 var host = uriObject.host.replace(/^www\./, "");
100 var filter = Filter.fromText("@@||" + host + "^$document"); 124 var filter = Filter.fromText("@@||" + host + "^$document");
101 if (filter.subscriptions.length && filter.disabled) 125 if (filter.subscriptions.length && filter.disabled)
102 filter.disabled = false; 126 filter.disabled = false;
103 else 127 else
104 { 128 {
105 filter.disabled = false; 129 filter.disabled = false;
106 FilterStorage.addFilter(filter); 130 FilterStorage.addFilter(filter);
107 } 131 }
108 } 132 }
109 else 133 else
110 { 134 {
111 // Remove any exception rules applying to this URL 135 // Remove any exception rules applying to this URL
112 var filter = AdblockPlusApi.isPageWhitelisted(location); 136 var filter = getWhitelistingFilter(url);
113 while (filter) 137 while (filter)
114 { 138 {
115 FilterStorage.removeFilter(filter); 139 FilterStorage.removeFilter(filter);
116 if (filter.subscriptions.length) 140 if (filter.subscriptions.length)
117 filter.disabled = true; 141 filter.disabled = true;
118 filter = AdblockPlusApi.isPageWhitelisted(location); 142 filter = getWhitelistingFilter(url);
119 } 143 }
120 } 144 }
145 },
146 initCommunication: function()
147 {
148 RequestService.addListener((function(data)
149 {
150 if (!data)
151 return {"success": false, "error": "malformed request"};
152
153 if (data["action"] == "getFiltersLoaded")
154 return {"success": true, "value": this.filtersLoaded};
155
156 if (!this.filtersLoaded)
157 return {"success": false, "error": "filters not loaded"};
158
159 switch (data["action"])
160 {
161 case "getAcceptableAdsEnabled":
162 return {"success": true, "value": this.acceptableAdsEnabled};
163 case "setAcceptableAdsEnabled":
164 if ("enable" in data)
165 {
166 this.acceptableAdsEnabled = !!data["enable"];
167 return {"success": true};
168 }
169 break;
170 case "getSubscriptionsXml":
171 return {"success": true, "value": this.subscriptionsXml};
172 case "isSubscriptionListed":
173 if ("url" in data)
174 {
175 return {"success": true,
176 "value": this.isSubscriptionListed(data["url"])};
177 }
178 break;
179 case "addSubscription":
180 if ("url" in data && "title" in data)
181 {
182 this.addSubscription(data["url"], data["title"]);
183 return {"success": true};
184 }
185 break;
186 case "removeSubscription":
187 if ("url" in data)
188 {
189 this.removeSubscription(data["url"]);
190 return {"success": true};
191 }
192 break;
193 case "isLocal":
194 if ("url" in data)
195 return {"success": true,
196 "value": this.isLocal(data["url"])};
197 break;
198 case "isPageWhitelisted":
199 if ("url" in data)
200 return {"success": true,
201 "value": this.isPageWhitelisted(data["url"])};
202 break;
203 case "whitelistSite":
204 if ("url" in data && "whitelisted" in data)
205 {
206 this.whitelistSite(data["url"], data["whitelisted"]);
207 return {"success": true};
208 }
209 break;
210 }
211 return {"success": false, "error": "malformed request"};
212 }).bind(this), "AdblockPlus:Api");
121 } 213 }
122 }; 214 };
123
LEFTRIGHT

Powered by Google App Engine
This is Rietveld