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

Delta Between Two Patch Sets: messageResponder.js

Issue 4731979438227456: Issue 1663 - Emulate background page and implement proper message responder (Closed)
Left Patch Set: Rebased and addressed comments Created Dec. 18, 2014, 9:52 p.m.
Right Patch Set: Using Services.vc Created Dec. 19, 2014, 5:45 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 | « firstRun.js ('k') | no next file » | 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 <http://adblockplus.org/>, 2 * This file is part of Adblock Plus <http://adblockplus.org/>,
3 * Copyright (C) 2006-2014 Eyeo GmbH 3 * Copyright (C) 2006-2014 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 (function(global) 18 (function(global)
19 { 19 {
20 var subscriptionKeys = ["disabled", "homepage", "lastSuccess", "title", "url", "downloadStatus"]; 20 var subscriptionKeys = ["disabled", "homepage", "lastSuccess", "title", "url", "downloadStatus"];
21 function convertSubscription(subscription) 21 function convertSubscription(subscription)
22 { 22 {
23 var result = {}; 23 var result = {};
24 for (var i = 0; i < subscriptionKeys.length; i++) 24 for (var i = 0; i < subscriptionKeys.length; i++)
25 result[subscriptionKeys[i]] = subscription[subscriptionKeys[i]] 25 result[subscriptionKeys[i]] = subscription[subscriptionKeys[i]]
26 return result; 26 return result;
27 } 27 }
28 28
29 var changeListeners = null; 29 var changeListeners = null;
30 ext.onPageRemoved.addListener(function(comparator) 30 var messageTypes = {
31 { 31 "app": "app.listen",
32 if (!changeListeners) 32 "filter": "filters.listen",
33 return; 33 "subscription": "subscriptions.listen"
34 34 };
35 for (var i = 0; i < changeListeners.length; i++)
36 if (comparator(changeListeners[i].page))
37 changeListeners.splice(i--, 1);
38
39 if (!changeListeners.length)
40 {
41 changeListeners = null;
42 FilterNotifier.removeListener(onFilterChange);
43 }
44 });
45 35
46 function onFilterChange(action) 36 function onFilterChange(action)
47 { 37 {
48 var parts = action.split(".", 2); 38 var parts = action.split(".", 2);
49 var type; 39 var type;
50 if (parts.length == 1) 40 if (parts.length == 1)
51 { 41 {
52 type = "app"; 42 type = "app";
53 action = parts[0]; 43 action = parts[0];
54 } 44 }
55 else 45 else
56 { 46 {
57 type = parts[0]; 47 type = parts[0];
58 action = parts[1]; 48 action = parts[1];
59 } 49 }
60 50
51 if (!messageTypes.hasOwnProperty(type))
52 return;
53
61 var args = Array.prototype.slice.call(arguments, 1).map(function(arg) 54 var args = Array.prototype.slice.call(arguments, 1).map(function(arg)
62 { 55 {
63 if (arg instanceof Subscription) 56 if (arg instanceof Subscription)
64 return convertSubscription(arg); 57 return convertSubscription(arg);
65 else 58 else
66 return arg; 59 return arg;
67 }); 60 });
68 61
69 for (var i = 0; i < changeListeners.length; i++) 62 var pages = changeListeners.keys();
63 for (var i = 0; i < pages.length; i++)
70 { 64 {
71 if (changeListeners[i].type == type && changeListeners[i].filter.indexOf(a ction) >= 0) 65 var filters = changeListeners.get(pages[i]);
66 if (filters[type] && filters[type].indexOf(action) >= 0)
72 { 67 {
73 changeListeners[i].page.sendMessage({ 68 pages[i].sendMessage({
74 type: changeListeners[i].messageType, 69 type: messageTypes[type],
75 action: action, 70 action: action,
76 args: args 71 args: args
77 }); 72 });
78 } 73 }
79 } 74 }
80 }; 75 };
81 76
82 ext.onMessage.addListener(function(message, sender, callback) 77 ext.onMessage.addListener(function(message, sender, callback)
83 { 78 {
84 switch (message.type) 79 switch (message.type)
85 { 80 {
86 case "app.get": 81 case "app.get":
87 if (message.what == "issues") 82 if (message.what == "issues")
88 { 83 {
89 var info = require("info"); 84 var info = require("info");
90 callback({ 85 callback({
91 seenDataCorruption: "seenDataCorruption" in global ? global.seenData Corruption : false, 86 seenDataCorruption: "seenDataCorruption" in global ? global.seenData Corruption : false,
92 filterlistsReinitialized: "filterlistsReinitialized" in global ? glo bal.filterlistsReinitialized : false, 87 filterlistsReinitialized: "filterlistsReinitialized" in global ? glo bal.filterlistsReinitialized : false,
93 legacySafariVersion: (info.platform == "safari" && ( 88 legacySafariVersion: (info.platform == "safari" && (
94 parseInt(info.platformVersion, 10) < 6 || // beforeload breaks w ebsites in Safari 5 89 Services.vc.compare(info.platformVersion, "6.0") < 0 || // bef oreload breaks websites in Safari 5
95 info.platformVersion == "6.1" || // extensions are brok en in 6.1 and 7.0 90 Services.vc.compare(info.platformVersion, "6.1") == 0 || // ext ensions are broken in 6.1 and 7.0
96 info.platformVersion == "7.0")) 91 Services.vc.compare(info.platformVersion, "7.0") == 0))
97 }); 92 });
98 } 93 }
99 else if (message.what == "doclink") 94 else if (message.what == "doclink")
100 callback(Utils.getDocLink(message.link)); 95 callback(Utils.getDocLink(message.link));
101 else 96 else
102 callback(null); 97 callback(null);
103 break; 98 break;
104 case "app.open": 99 case "app.open":
105 if (message.what == "options") 100 if (message.what == "options")
106 { 101 {
107 if (typeof UI != "undefined") 102 if (typeof UI != "undefined")
108 UI.openFiltersDialog(); 103 UI.openFiltersDialog();
109 else 104 else
110 global.openOptions(); 105 global.openOptions();
Sebastian Noack 2014/12/22 09:56:17 window.openOptions() is deprecated. Please use ext
Wladimir Palant 2015/01/15 13:03:22 I filed #1813 and #1814 on that.
111 } 106 }
112 break; 107 break;
113 case "subscriptions.get": 108 case "subscriptions.get":
114 var subscriptions = FilterStorage.subscriptions.filter(function(s) 109 var subscriptions = FilterStorage.subscriptions.filter(function(s)
115 { 110 {
116 if (message.ignoreDisabled && s.disabled) 111 if (message.ignoreDisabled && s.disabled)
117 return false; 112 return false;
118 if (s instanceof DownloadableSubscription && message.downloadable) 113 if (s instanceof DownloadableSubscription && message.downloadable)
119 return true; 114 return true;
120 if (s instanceof SpecialSubscription && message.special) 115 if (s instanceof SpecialSubscription && message.special)
(...skipping 16 matching lines...) Expand all
137 subscription.title = message.title; 132 subscription.title = message.title;
138 subscription.homepage = message.homepage; 133 subscription.homepage = message.homepage;
139 FilterStorage.addSubscription(subscription); 134 FilterStorage.addSubscription(subscription);
140 if (!subscription.lastDownload) 135 if (!subscription.lastDownload)
141 Synchronizer.execute(subscription); 136 Synchronizer.execute(subscription);
142 } 137 }
143 break; 138 break;
144 case "subscriptions.listen": 139 case "subscriptions.listen":
145 if (!changeListeners) 140 if (!changeListeners)
146 { 141 {
147 changeListeners = []; 142 changeListeners = new global.ext.PageMap();
148 FilterNotifier.addListener(onFilterChange); 143 FilterNotifier.addListener(onFilterChange);
149 } 144 }
150 changeListeners.push({ 145
151 type: "subscription", 146 var filters = changeListeners.get(sender.page);
152 filter: message.filter, 147 if (!filters)
153 messageType: message.type, 148 {
154 page: sender.page 149 filters = Object.create(null);
155 }); 150 changeListeners.set(sender.page, filters);
151 }
152
153 if (message.filter)
154 filters.subscription = message.filter;
155 else
156 delete filters.subscription;
156 break; 157 break;
157 } 158 }
158 }); 159 });
159 })(this); 160 })(this);
LEFTRIGHT
« firstRun.js ('k') | no next file » | Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Toggle Comments ('s')

Powered by Google App Engine
This is Rietveld