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

Side by Side Diff: ext/common.js

Issue 5056848617013248: Issue 1706 - Move first-run page to adblockplusui repository (Closed)
Patch Set: Export variables differently in common.js Created Jan. 7, 2015, 6:58 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 | « ext/background.js ('k') | ext/content.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 <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 const Ci = Components.interfaces;
21
22 if (!global.ext)
23 global.ext = {};
24
25 var holder = {
26 get Page()
27 {
28 delete this.Page;
29 this.Page = (typeof require == "function" ?
30 require("ext_background").Page :
31 function() {});
32 return this.Page;
33 }
34 };
35
36 var getSender = global.ext._getSender = function(origin)
37 {
38 if (origin instanceof Ci.nsIDOMXULElement)
39 return origin.messageManager;
40 else if (origin instanceof Ci.nsIMessageSender)
41 return origin;
42 else
43 return null;
44 }
45
46 var MessageProxy = global.ext._MessageProxy = function(messageManager, message Target)
47 {
48 this._messageManager = messageManager;
49 this._messageTarget = messageTarget;
50 this._callbacks = new Map();
51 this._responseCallbackCounter = 0;
52
53 this._handleRequest = this._handleRequest.bind(this);
54 this._handleResponse = this._handleResponse.bind(this);
55 this._messageManager.addMessageListener("AdblockPlus:Message", this._handleR equest);
56 this._messageManager.addMessageListener("AdblockPlus:Response", this._handle Response);
57 }
58 MessageProxy.prototype = {
59 _disconnect: function()
60 {
61 this._messageManager.removeMessageListener("AdblockPlus:Message", this._ha ndleRequest);
62 this._messageManager.removeMessageListener("AdblockPlus:Response", this._h andleResponse);
63 },
64
65 _sendResponse: function(sender, callbackId, response)
66 {
67 this._responseSent = true;
68
69 if (sender instanceof Ci.nsIMessageSender)
70 {
71 sender.sendAsyncMessage("AdblockPlus:Response", {
72 callbackId: callbackId,
73 responseSent: typeof response != "undefined",
74 payload: response
75 });
76 }
77 },
78
79 _handleRequest: function(message)
80 {
81 var sender = getSender(message.target);
82 var request = message.data;
83 var sendResponse;
84 if (sender && "callbackId" in request)
85 sendResponse = this._sendResponse.bind(this, sender, request.callbackId) ;
86 else
87 sendResponse = function() {};
88
89 this._responseSent = false;
90 var result = this._messageTarget._dispatch(request.payload, {
91 page: new holder.Page(sender)
92 }, sendResponse);
93 if (!result && !this._responseSent)
94 sendResponse(undefined);
95 },
96
97 _handleResponse: function(message)
98 {
99 var response = message.data;
100 var callback = this._callbacks.get(response.callbackId);
101 if (callback)
102 {
103 this._callbacks.delete(response.callbackId);
104 if (response.responseSent)
105 callback(response.payload);
106 }
107 },
108
109 sendMessage: function(message, responseCallback)
110 {
111 if (!(this._messageManager instanceof Ci.nsIMessageSender))
112 throw new Error("Not implemented");
113
114 var request = {
115 payload: message
116 };
117 if (responseCallback)
118 {
119 request.callbackId = ++this._responseCallbackCounter;
120 this._callbacks.set(request.callbackId, responseCallback);
121 }
122
123 this._messageManager.sendAsyncMessage("AdblockPlus:Message", request);
124 }
125 };
126
127 var EventTarget = global.ext._EventTarget = function()
128 {
129 this._listeners = [];
130 };
131 EventTarget.prototype = {
132 addListener: function(listener)
133 {
134 if (this._listeners.indexOf(listener) == -1)
135 this._listeners.push(listener);
136 },
137 removeListener: function(listener)
138 {
139 var idx = this._listeners.indexOf(listener);
140 if (idx != -1)
141 this._listeners.splice(idx, 1);
142 },
143 _dispatch: function()
144 {
145 var result = null;
146
147 for (var i = 0; i < this._listeners.length; i++)
148 result = this._listeners[i].apply(null, arguments);
149
150 return result;
151 }
152 };
153
154 if (typeof exports == "object")
155 exports = global.ext;
156 })(this);
OLDNEW
« no previous file with comments | « ext/background.js ('k') | ext/content.js » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld