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

Side by Side Diff: messageResponder.js

Issue 4731979438227456: Issue 1663 - Emulate background page and implement proper message responder (Closed)
Patch Set: Using PageMap for listeners Created Dec. 19, 2014, 3:41 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 | « firstRun.js ('k') | no next file » | 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 <http://adblockplus.org/>,
3 * Copyright (C) 2006-2014 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 (function(global)
19 {
20 var subscriptionKeys = ["disabled", "homepage", "lastSuccess", "title", "url", "downloadStatus"];
21 function convertSubscription(subscription)
22 {
23 var result = {};
24 for (var i = 0; i < subscriptionKeys.length; i++)
25 result[subscriptionKeys[i]] = subscription[subscriptionKeys[i]]
26 return result;
27 }
28
29 var changeListeners = null;
30 var messageTypes = {
31 "app": "app.listen",
32 "filter": "filters.listen",
33 "subscription": "subscriptions.listen"
34 };
35
36 function onFilterChange(action)
37 {
38 var parts = action.split(".", 2);
39 var type;
40 if (parts.length == 1)
41 {
42 type = "app";
43 action = parts[0];
44 }
45 else
46 {
47 type = parts[0];
48 action = parts[1];
49 }
50
51 if (!messageTypes.hasOwnProperty(type))
52 return;
53
54 var args = Array.prototype.slice.call(arguments, 1).map(function(arg)
55 {
56 if (arg instanceof Subscription)
57 return convertSubscription(arg);
58 else
59 return arg;
60 });
61
62 var pages = changeListeners.keys();
63 for (var i = 0; i < pages.length; i++)
64 {
65 var filters = changeListeners.get(pages[i]);
66 if (filters[type] && filters[type].indexOf(action) >= 0)
67 {
68 pages[i].sendMessage({
69 type: messageTypes[type],
70 action: action,
71 args: args
72 });
73 }
74 }
75 };
76
77 ext.onMessage.addListener(function(message, sender, callback)
78 {
79 switch (message.type)
80 {
81 case "app.get":
82 if (message.what == "issues")
83 {
84 var info = require("info");
85 callback({
86 seenDataCorruption: "seenDataCorruption" in global ? global.seenData Corruption : false,
87 filterlistsReinitialized: "filterlistsReinitialized" in global ? glo bal.filterlistsReinitialized : false,
88 legacySafariVersion: (info.platform == "safari" && (
89 parseInt(info.platformVersion, 10) < 6 || // beforeload breaks w ebsites in Safari 5
Thomas Greiner 2014/12/19 16:58:23 Since this is only running in the context of the b
Wladimir Palant 2014/12/19 17:45:38 Done.
90 info.platformVersion == "6.1" || // extensions are brok en in 6.1 and 7.0
91 info.platformVersion == "7.0"))
92 });
93 }
94 else if (message.what == "doclink")
95 callback(Utils.getDocLink(message.link));
96 else
97 callback(null);
98 break;
99 case "app.open":
100 if (message.what == "options")
101 {
102 if (typeof UI != "undefined")
103 UI.openFiltersDialog();
104 else
105 global.openOptions();
106 }
107 break;
108 case "subscriptions.get":
109 var subscriptions = FilterStorage.subscriptions.filter(function(s)
110 {
111 if (message.ignoreDisabled && s.disabled)
112 return false;
113 if (s instanceof DownloadableSubscription && message.downloadable)
114 return true;
115 if (s instanceof SpecialSubscription && message.special)
116 return true;
117 return false;
118 });
119 callback(subscriptions.map(convertSubscription));
120 break;
121 case "filters.blocked":
122 var filter = defaultMatcher.matchesAny(message.url, message.requestType, message.docDomain, message.thirdParty);
123 callback(filter instanceof BlockingFilter);
124 break;
125 case "subscriptions.toggle":
126 var subscription = Subscription.fromURL(message.url);
127 if (subscription.url in FilterStorage.knownSubscriptions && !subscriptio n.disabled)
128 FilterStorage.removeSubscription(subscription);
129 else
130 {
131 subscription.disabled = false;
132 subscription.title = message.title;
133 subscription.homepage = message.homepage;
134 FilterStorage.addSubscription(subscription);
135 if (!subscription.lastDownload)
136 Synchronizer.execute(subscription);
137 }
138 break;
139 case "subscriptions.listen":
140 if (!changeListeners)
141 {
142 changeListeners = new global.ext.PageMap();
143 FilterNotifier.addListener(onFilterChange);
144 }
145
146 var filters = changeListeners.get(sender.page);
147 if (!filters)
148 {
149 filters = Object.create(null);
150 changeListeners.set(sender.page, filters);
151 }
152
153 if (message.filter)
154 filters.subscription = message.filter;
155 else
156 delete filters.subscription;
157 break;
158 }
159 });
160 })(this);
OLDNEW
« no previous file with comments | « firstRun.js ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld