Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(267)

Side by Side Diff: chrome/renderer/resources/extension_process_bindings.js

Issue 164458: Land http://codereview.chromium.org/159067: (Closed)
Patch Set: undo docs Created 11 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright (c) 2009 The chrome Authors. All rights reserved. 1 // Copyright (c) 2009 The chrome Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 // ----------------------------------------------------------------------------- 5 // -----------------------------------------------------------------------------
6 // NOTE: If you change this file you need to touch renderer_resources.grd to 6 // NOTE: If you change this file you need to touch renderer_resources.grd to
7 // have your change take effect. 7 // have your change take effect.
8 // ----------------------------------------------------------------------------- 8 // -----------------------------------------------------------------------------
9 9
10 // This script contains privileged chrome extension related javascript APIs. 10 // This script contains privileged chrome extension related javascript APIs.
11 // It is loaded by pages whose URL has the chrome-extension protocol. 11 // It is loaded by pages whose URL has the chrome-extension protocol.
12 12
13 var chrome = chrome || {}; 13 var chrome = chrome || {};
14 (function() { 14 (function() {
15 native function GetExtensionAPIDefinition(); 15 native function GetExtensionAPIDefinition();
16 native function StartRequest(); 16 native function StartRequest();
17 native function GetCurrentPageActions(extensionId); 17 native function GetCurrentPageActions(extensionId);
18 native function GetViews(); 18 native function GetExtensionViews();
19 native function GetChromeHidden(); 19 native function GetChromeHidden();
20 native function GetNextRequestId(); 20 native function GetNextRequestId();
21 native function OpenChannelToTab(); 21 native function OpenChannelToTab();
22 22
23 if (!chrome) 23 if (!chrome)
24 chrome = {}; 24 chrome = {};
25 25
26 var chromeHidden = GetChromeHidden(); 26 var chromeHidden = GetChromeHidden();
27 27
28 // Validate arguments. 28 // Validate arguments.
(...skipping 86 matching lines...) Expand 10 before | Expand all | Expand 10 after
115 var sargs = JSON.stringify(request.args); 115 var sargs = JSON.stringify(request.args);
116 var requestId = GetNextRequestId(); 116 var requestId = GetNextRequestId();
117 var hasCallback = false; 117 var hasCallback = false;
118 if (request.callback) { 118 if (request.callback) {
119 hasCallback = true; 119 hasCallback = true;
120 callbacks[requestId] = request.callback; 120 callbacks[requestId] = request.callback;
121 } 121 }
122 return StartRequest(functionName, sargs, requestId, hasCallback); 122 return StartRequest(functionName, sargs, requestId, hasCallback);
123 } 123 }
124 124
125 // Read api definitions and setup api functions in the chrome namespace.
126 // TODO(rafaelw): Consider defining a json schema for an api definition
127 // and validating either here, in a unit_test or both.
128 // TODO(rafaelw): Handle synchronous functions.
129 // TOOD(rafaelw): Consider providing some convenient override points
130 // for api functions that wish to insert themselves into the call.
131 var apiDefinitions = JSON.parse(GetExtensionAPIDefinition());
132
133 // |apiFunctions| is a hash of name -> object that stores the
134 // name & definition of the apiFunction. Custom handling of api functions
135 // is implemented by adding a "handleRequest" function to the object.
136 var apiFunctions = {};
137
138 // Using forEach for convenience, and to bind |module|s & |apiDefs|s via 125 // Using forEach for convenience, and to bind |module|s & |apiDefs|s via
139 // closures. 126 // closures.
140 function forEach(a, f) { 127 function forEach(a, f) {
141 for (var i = 0; i < a.length; i++) { 128 for (var i = 0; i < a.length; i++) {
142 f(a[i], i); 129 f(a[i], i);
143 } 130 }
144 } 131 }
145 132
146 function bind(obj, func) { 133 function bind(obj, func) {
147 return function() { 134 return function() {
148 return func.apply(obj, arguments); 135 return func.apply(obj, arguments);
149 }; 136 };
150 } 137 }
151 138
152 forEach(apiDefinitions, function(apiDef) {
153 chrome[apiDef.namespace] = chrome[apiDef.namespace] || {};
154 var module = chrome[apiDef.namespace];
155
156 // Setup Functions.
157 if (apiDef.functions) {
158 forEach(apiDef.functions, function(functionDef) {
159 // Module functions may have been defined earlier by hand. Don't clobber
160 // them.
161 if (module[functionDef.name])
162 return;
163
164 var apiFunction = {};
165 apiFunction.definition = functionDef;
166 apiFunction.name = apiDef.namespace + "." + functionDef.name;;
167 apiFunctions[apiFunction.name] = apiFunction;
168
169 module[functionDef.name] = bind(apiFunction, function() {
170 validate(arguments, this.definition.parameters);
171
172 if (this.handleRequest)
173 return this.handleRequest.apply(this, arguments);
174 else
175 return sendRequest(this.name, arguments,
176 this.definition.parameters);
177 });
178 });
179 }
180
181 // Setup Events
182 if (apiDef.events) {
183 forEach(apiDef.events, function(eventDef) {
184 // Module events may have been defined earlier by hand. Don't clobber
185 // them.
186 if (module[eventDef.name])
187 return;
188
189 var eventName = apiDef.namespace + "." + eventDef.name;
190 module[eventDef.name] = new chrome.Event(eventName);
191 });
192 }
193 });
194
195 // --- Setup additional api's not currently handled in common/extensions/api 139 // --- Setup additional api's not currently handled in common/extensions/api
196 140
197 // Page action events send (pageActionId, {tabId, tabUrl}). 141 // Page action events send (pageActionId, {tabId, tabUrl}).
198 function setupPageActionEvents(extensionId) { 142 function setupPageActionEvents(extensionId) {
199 var pageActions = GetCurrentPageActions(extensionId); 143 var pageActions = GetCurrentPageActions(extensionId);
200 var eventName = ""; 144 var eventName = "";
201 for (var i = 0; i < pageActions.length; ++i) { 145 for (var i = 0; i < pageActions.length; ++i) {
202 eventName = extensionId + "/" + pageActions[i]; 146 eventName = extensionId + "/" + pageActions[i];
203 // Setup events for each extension_id/page_action_id string we find. 147 // Setup events for each extension_id/page_action_id string we find.
204 chrome.pageActions[pageActions[i]] = new chrome.Event(eventName); 148 chrome.pageActions[pageActions[i]] = new chrome.Event(eventName);
205 } 149 }
206 } 150 }
207 151
208 // Tabs connect()
209 apiFunctions["tabs.connect"].handleRequest = function(tabId, opt_name) {
210 var portId = OpenChannelToTab(tabId, chrome.extension.id_, opt_name || "");
211 return chromeHidden.Port.createPort(portId, opt_name);
212 }
213
214 // chrome.self / chrome.extension.
215 chrome.self = chrome.self || {};
216
217 chromeHidden.onLoad.addListener(function (extensionId) { 152 chromeHidden.onLoad.addListener(function (extensionId) {
218 chrome.extension = new chrome.Extension(extensionId); 153 chrome.extension = new chrome.Extension(extensionId);
219 // TODO(mpcomplete): self.onConnect is deprecated. Remove it at 1.0. 154
155 // TODO(mpcomplete): chrome.self is deprecated. Remove it at 1.0.
220 // http://code.google.com/p/chromium/issues/detail?id=16356 156 // http://code.google.com/p/chromium/issues/detail?id=16356
221 chrome.self.onConnect = chrome.extension.onConnect; 157 chrome.self = chrome.extension;
158
159 // |apiFunctions| is a hash of name -> object that stores the
160 // name & definition of the apiFunction. Custom handling of api functions
161 // is implemented by adding a "handleRequest" function to the object.
162 var apiFunctions = {};
163
164 // Read api definitions and setup api functions in the chrome namespace.
165 // TODO(rafaelw): Consider defining a json schema for an api definition
166 // and validating either here, in a unit_test or both.
167 // TODO(rafaelw): Handle synchronous functions.
168 // TOOD(rafaelw): Consider providing some convenient override points
169 // for api functions that wish to insert themselves into the call.
170 var apiDefinitions = JSON.parse(GetExtensionAPIDefinition());
171
172 forEach(apiDefinitions, function(apiDef) {
173 chrome[apiDef.namespace] = chrome[apiDef.namespace] || {};
174 var module = chrome[apiDef.namespace];
175
176 // Setup Functions.
177 if (apiDef.functions) {
178 forEach(apiDef.functions, function(functionDef) {
179 // Module functions may have been defined earlier by hand. Don't
180 // clobber them.
181 if (module[functionDef.name])
182 return;
183
184 var apiFunction = {};
185 apiFunction.definition = functionDef;
186 apiFunction.name = apiDef.namespace + "." + functionDef.name;;
187 apiFunctions[apiFunction.name] = apiFunction;
188
189 module[functionDef.name] = bind(apiFunction, function() {
190 validate(arguments, this.definition.parameters);
191
192 if (this.handleRequest)
193 return this.handleRequest.apply(this, arguments);
194 else
195 return sendRequest(this.name, arguments,
196 this.definition.parameters);
197 });
198 });
199 }
200
201 // Setup Events
202 if (apiDef.events) {
203 forEach(apiDef.events, function(eventDef) {
204 // Module events may have been defined earlier by hand. Don't clobber
205 // them.
206 if (module[eventDef.name])
207 return;
208
209 var eventName = apiDef.namespace + "." + eventDef.name;
210 module[eventDef.name] = new chrome.Event(eventName);
211 });
212 }
213 });
214
215 apiFunctions["tabs.connect"].handleRequest = function(tabId, opt_name) {
216 var portId = OpenChannelToTab(
217 tabId, chrome.extension.id_, opt_name || "");
218 return chromeHidden.Port.createPort(portId, opt_name);
219 }
220
221 apiFunctions["extension.getViews"].handleRequest = function() {
222 return GetExtensionViews(-1, "ALL");
223 }
224
225 apiFunctions["extension.getBackgroundPage"].handleRequest = function() {
226 return GetExtensionViews(-1, "BACKGROUND")[0] || null;
227 }
228
229 apiFunctions["extension.getToolstrips"].handleRequest =
230 function(windowId) {
231 if (typeof(windowId) == "undefined")
232 windowId = -1;
233 return GetExtensionViews(windowId, "TOOLSTRIP");
234 }
235
236 apiFunctions["extension.getTabContentses"].handleRequest =
237 function(windowId) {
238 if (typeof(windowId) == "undefined")
239 windowId = -1;
240 return GetExtensionViews(windowId, "TAB");
241 }
222 242
223 setupPageActionEvents(extensionId); 243 setupPageActionEvents(extensionId);
224 }); 244 });
225 245 })();
226 // Self getViews();
227 apiFunctions["self.getViews"].handleRequest = function() {
228 return GetViews();
229 }
230 })();
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698