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

Unified Diff: messageResponder.js

Issue 4731979438227456: Issue 1663 - Emulate background page and implement proper message responder (Closed)
Patch Set: Rebased and addressed comments Created Dec. 18, 2014, 9:52 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 | « firstRun.js ('k') | no next file » | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: messageResponder.js
===================================================================
new file mode 100644
--- /dev/null
+++ b/messageResponder.js
@@ -0,0 +1,159 @@
+/*
+ * This file is part of Adblock Plus <http://adblockplus.org/>,
+ * Copyright (C) 2006-2014 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/>.
+ */
+
+(function(global)
+{
+ var subscriptionKeys = ["disabled", "homepage", "lastSuccess", "title", "url", "downloadStatus"];
+ function convertSubscription(subscription)
+ {
+ var result = {};
+ for (var i = 0; i < subscriptionKeys.length; i++)
+ result[subscriptionKeys[i]] = subscription[subscriptionKeys[i]]
+ return result;
+ }
+
+ var changeListeners = null;
+ ext.onPageRemoved.addListener(function(comparator)
+ {
+ if (!changeListeners)
+ return;
+
+ for (var i = 0; i < changeListeners.length; i++)
+ if (comparator(changeListeners[i].page))
+ changeListeners.splice(i--, 1);
+
+ if (!changeListeners.length)
+ {
+ changeListeners = null;
+ FilterNotifier.removeListener(onFilterChange);
+ }
+ });
+
+ function onFilterChange(action)
+ {
+ var parts = action.split(".", 2);
+ var type;
+ if (parts.length == 1)
+ {
+ type = "app";
+ action = parts[0];
+ }
+ else
+ {
+ type = parts[0];
+ action = parts[1];
+ }
+
+ var args = Array.prototype.slice.call(arguments, 1).map(function(arg)
+ {
+ if (arg instanceof Subscription)
+ return convertSubscription(arg);
+ else
+ return arg;
+ });
+
+ for (var i = 0; i < changeListeners.length; i++)
+ {
+ if (changeListeners[i].type == type && changeListeners[i].filter.indexOf(action) >= 0)
+ {
+ changeListeners[i].page.sendMessage({
+ type: changeListeners[i].messageType,
+ action: action,
+ args: args
+ });
+ }
+ }
+ };
+
+ ext.onMessage.addListener(function(message, sender, callback)
+ {
+ switch (message.type)
+ {
+ case "app.get":
+ if (message.what == "issues")
+ {
+ var info = require("info");
+ callback({
+ seenDataCorruption: "seenDataCorruption" in global ? global.seenDataCorruption : false,
+ filterlistsReinitialized: "filterlistsReinitialized" in global ? global.filterlistsReinitialized : false,
+ legacySafariVersion: (info.platform == "safari" && (
+ parseInt(info.platformVersion, 10) < 6 || // beforeload breaks websites in Safari 5
+ info.platformVersion == "6.1" || // extensions are broken in 6.1 and 7.0
+ info.platformVersion == "7.0"))
+ });
+ }
+ else if (message.what == "doclink")
+ callback(Utils.getDocLink(message.link));
+ else
+ callback(null);
+ break;
+ case "app.open":
+ if (message.what == "options")
+ {
+ if (typeof UI != "undefined")
+ UI.openFiltersDialog();
+ else
+ global.openOptions();
+ }
+ break;
+ case "subscriptions.get":
+ var subscriptions = FilterStorage.subscriptions.filter(function(s)
+ {
+ if (message.ignoreDisabled && s.disabled)
+ return false;
+ if (s instanceof DownloadableSubscription && message.downloadable)
+ return true;
+ if (s instanceof SpecialSubscription && message.special)
+ return true;
+ return false;
+ });
+ callback(subscriptions.map(convertSubscription));
+ break;
+ case "filters.blocked":
+ var filter = defaultMatcher.matchesAny(message.url, message.requestType, message.docDomain, message.thirdParty);
+ callback(filter instanceof BlockingFilter);
+ break;
+ case "subscriptions.toggle":
+ var subscription = Subscription.fromURL(message.url);
+ if (subscription.url in FilterStorage.knownSubscriptions && !subscription.disabled)
+ FilterStorage.removeSubscription(subscription);
+ else
+ {
+ subscription.disabled = false;
+ subscription.title = message.title;
+ subscription.homepage = message.homepage;
+ FilterStorage.addSubscription(subscription);
+ if (!subscription.lastDownload)
+ Synchronizer.execute(subscription);
+ }
+ break;
+ case "subscriptions.listen":
+ if (!changeListeners)
+ {
+ changeListeners = [];
+ FilterNotifier.addListener(onFilterChange);
+ }
+ changeListeners.push({
+ type: "subscription",
+ filter: message.filter,
+ messageType: message.type,
+ page: sender.page
+ });
+ break;
+ }
+ });
+})(this);
« no previous file with comments | « firstRun.js ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld