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

Side by Side Diff: safari/contentBlocking.js

Issue 29340571: Issue 3687 - Add experimental support for Safari content blockers (Closed)
Patch Set: Improve comment about strange API behaviour Created May 17, 2016, 3:53 p.m.
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
View unified diff | Download patch
« no previous file with comments | « popup.js ('k') | safari/ext/background.js » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
1 /*
2 * This file is part of Adblock Plus <https://adblockplus.org/>,
3 * Copyright (C) 2006-2016 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 /** @module contentBlocking */
19
20 "use strict";
21
22 let {Prefs} = require("prefs");
23 let {ContentBlockerList} = require("abp2blocklist");
24 let {FilterStorage} = require("filterStorage");
25 let {FilterNotifier} = require("filterNotifier");
26 let {port} = require("messaging");
27
28 let contentBlockingSupported = "setContentBlocker" in safari.extension;
29 let legacyAPISupported = new Promise(resolve =>
30 {
31 function onLegacyAPISupported(msg, sender)
32 {
33 port.off("safari.legacyAPISupported", onLegacyAPISupported);
34 resolve(msg.legacyAPISupported);
35 }
36 port.on("safari.legacyAPISupported", onLegacyAPISupported);
37 });
38 let contentBlockingActive = false;
39 let afterContentBlockingFinished = null;
40 let contentBlockListDirty = true;
41 let lastSetContentBlockerResult;
42
43 function clearBlockCounters()
44 {
45 ext.pages.query({}, pages =>
46 {
47 for (let page of pages)
48 page.browserAction.setBadge();
49 });
50 }
51
52 function setContentBlocker(callback)
53 {
54 // setContentBlocker returns null if given the same blocklist as last time,
55 // even when there was an error. (It's also wasteful to re-generate the
56 // blocklist when nothing has changed!)
57 if (!contentBlockListDirty)
58 {
59 callback(lastSetContentBlockerResult);
60 return;
61 }
62
63 let contentBlockerList = new ContentBlockerList();
64 for (let subscription of FilterStorage.subscriptions)
65 if (!subscription.disabled)
66 for (let filter of subscription.filters)
67 contentBlockerList.addFilter(filter);
68
69 contentBlockListDirty = false;
70 safari.extension.setContentBlocker(
71 // setContentBlocker seems to not work in Safari 9 when a callback is passed
72 // unless the rules are converted to JSON first. (An error is thrown:
73 // "Extension compilation failed: Failed to parse the JSON String.")
74 JSON.stringify(contentBlockerList.generateRules()),
75 function (result)
76 {
77 // Safari 9 performs the callback twice under some conditions, first with
78 // an empty string and then with an Error!
79 if (result == "")
80 return;
81
82 lastSetContentBlockerResult = result;
83 callback(result);
84 }
85 );
86 }
87
88 function updateContentBlocker(isStartup, legacyAPISupported)
89 {
90 afterContentBlockingFinished = new Promise(resolve =>
91 {
92 setContentBlocker(result =>
93 {
94 if (result instanceof Error)
95 {
96 let suppressErrorMessage = false;
97
98 // If the content blocking API fails the first time it's used the
99 // legacy blocking API (if available) won't have been disabled.
100 if (!contentBlockingActive && legacyAPISupported)
101 {
102 Prefs.safariContentBlocker = false;
103 // If content blocking failed on startup and we're switching back to
104 // the legacy API anyway we don't need to show an error message.
105 if (isStartup)
106 suppressErrorMessage = true;
107 }
108
109 if (!suppressErrorMessage)
110 alert(result.message);
111 }
112 else if (!contentBlockingActive)
113 {
114 contentBlockingActive = true;
115 clearBlockCounters();
116 }
117
118 resolve(contentBlockingActive);
119 afterContentBlockingFinished = null;
120 });
121 });
122 }
123
124 if (contentBlockingSupported)
125 {
126 Promise.all([Prefs.untilLoaded,
127 FilterNotifier.once("load"),
128 legacyAPISupported]).then(resolvedValues =>
129 {
130 let legacyAPISupported = resolvedValues[2];
131 if (!legacyAPISupported)
132 Prefs.safariContentBlocker = true;
133
134 if (Prefs.safariContentBlocker)
135 updateContentBlocker(true, legacyAPISupported);
136
137 Prefs.on("safariContentBlocker", () =>
138 {
139 if (!contentBlockingActive && Prefs.safariContentBlocker)
140 updateContentBlocker(false, legacyAPISupported);
141 });
142
143 FilterNotifier.on("filter.behaviorChanged", () =>
144 {
145 contentBlockListDirty = true;
146 if (contentBlockingActive)
147 updateContentBlocker(false, legacyAPISupported);
148 });
149 });
150 }
151
152 port.on("safari.contentBlockingActive", (msg, sender) =>
153 {
154 if (!contentBlockingActive && afterContentBlockingFinished)
155 return afterContentBlockingFinished;
156 return contentBlockingActive;
157 });
OLDNEW
« no previous file with comments | « popup.js ('k') | safari/ext/background.js » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld