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

Unified Diff: safari/contentBlocking.js

Issue 29340571: Issue 3687 - Add experimental support for Safari content blockers (Closed)
Patch Set: Added note about bug report Created May 17, 2016, 4:19 p.m.
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « popup.js ('k') | safari/ext/background.js » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: safari/contentBlocking.js
diff --git a/safari/contentBlocking.js b/safari/contentBlocking.js
new file mode 100644
index 0000000000000000000000000000000000000000..5d99a75e03040b66ea1875f2986688427b31cd3b
--- /dev/null
+++ b/safari/contentBlocking.js
@@ -0,0 +1,158 @@
+/*
+ * This file is part of Adblock Plus <https://adblockplus.org/>,
+ * Copyright (C) 2006-2016 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/>.
+ */
+
+/** @module contentBlocking */
+
+"use strict";
+
+let {Prefs} = require("prefs");
+let {ContentBlockerList} = require("abp2blocklist");
+let {FilterStorage} = require("filterStorage");
+let {FilterNotifier} = require("filterNotifier");
+let {port} = require("messaging");
+
+let contentBlockingSupported = "setContentBlocker" in safari.extension;
+let legacyAPISupported = new Promise(resolve =>
+{
+ function onLegacyAPISupported(msg, sender)
+ {
+ port.off("safari.legacyAPISupported", onLegacyAPISupported);
+ resolve(msg.legacyAPISupported);
+ }
+ port.on("safari.legacyAPISupported", onLegacyAPISupported);
+});
+let contentBlockingActive = false;
+let afterContentBlockingFinished = null;
+let contentBlockListDirty = true;
+let lastSetContentBlockerResult;
+
+function clearBlockCounters()
+{
+ ext.pages.query({}, pages =>
+ {
+ for (let page of pages)
+ page.browserAction.setBadge();
+ });
+}
+
+function setContentBlocker(callback)
+{
+ // setContentBlocker returns null if given the same blocklist as last time,
+ // even when there was an error. (It's also wasteful to re-generate the
+ // blocklist when nothing has changed!)
+ if (!contentBlockListDirty)
+ {
+ callback(lastSetContentBlockerResult);
+ return;
+ }
+
+ let contentBlockerList = new ContentBlockerList();
+ for (let subscription of FilterStorage.subscriptions)
+ if (!subscription.disabled)
+ for (let filter of subscription.filters)
+ contentBlockerList.addFilter(filter);
+
+ contentBlockListDirty = false;
+ safari.extension.setContentBlocker(
+ // setContentBlocker seems to not work in Safari 9 when a callback is passed
+ // unless the rules are converted to JSON first. (An error is thrown:
+ // "Extension compilation failed: Failed to parse the JSON String.")
+ // Bug #26322821 filed on bugreport.apple.com
+ JSON.stringify(contentBlockerList.generateRules()),
+ function (result)
Sebastian Noack 2016/05/17 18:35:25 Nit: We usually don't put a space before the argum
Sebastian Noack 2016/05/17 18:35:25 Nit: I think this variable should be called "error
kzar 2016/05/17 19:20:22 Done.
kzar 2016/05/17 19:20:22 Done.
+ {
+ // Safari 9 performs the callback twice under some conditions, first with
kzar 2016/05/17 17:13:06 As far as I can tell the behaviour of the callback
Sebastian Noack 2016/05/17 18:35:25 Well, if future versions of Safari won't have that
kzar 2016/05/17 19:20:22 Acknowledged.
+ // an empty string and then with an Error!
+ if (result == "")
+ return;
+
+ lastSetContentBlockerResult = result;
+ callback(result);
+ }
+ );
+}
+
+function updateContentBlocker(isStartup, legacyAPISupported)
+{
+ afterContentBlockingFinished = new Promise(resolve =>
+ {
+ setContentBlocker(result =>
+ {
+ if (result instanceof Error)
+ {
+ let suppressErrorMessage = false;
+
+ // If the content blocking API fails the first time it's used the
+ // legacy blocking API (if available) won't have been disabled.
+ if (!contentBlockingActive && legacyAPISupported)
+ {
+ Prefs.safariContentBlocker = false;
+ // If content blocking failed on startup and we're switching back to
+ // the legacy API anyway we don't need to show an error message.
+ if (isStartup)
+ suppressErrorMessage = true;
+ }
+
+ if (!suppressErrorMessage)
+ alert(result.message);
+ }
+ else if (!contentBlockingActive)
+ {
+ contentBlockingActive = true;
+ clearBlockCounters();
+ }
+
+ resolve(contentBlockingActive);
+ afterContentBlockingFinished = null;
+ });
+ });
+}
+
+if (contentBlockingSupported)
+{
+ Promise.all([Prefs.untilLoaded,
+ FilterNotifier.once("load"),
+ legacyAPISupported]).then(resolvedValues =>
+ {
+ let legacyAPISupported = resolvedValues[2];
+ if (!legacyAPISupported)
+ Prefs.safariContentBlocker = true;
+
+ if (Prefs.safariContentBlocker)
+ updateContentBlocker(true, legacyAPISupported);
+
+ Prefs.on("safariContentBlocker", () =>
+ {
+ if (!contentBlockingActive && Prefs.safariContentBlocker)
+ updateContentBlocker(false, legacyAPISupported);
+ });
+
+ FilterNotifier.on("filter.behaviorChanged", () =>
+ {
+ contentBlockListDirty = true;
+ if (contentBlockingActive)
+ updateContentBlocker(false, legacyAPISupported);
+ });
+ });
+}
+
+port.on("safari.contentBlockingActive", (msg, sender) =>
+{
+ if (!contentBlockingActive && afterContentBlockingFinished)
+ return afterContentBlockingFinished;
+ return contentBlockingActive;
+});
« 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