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

Side by Side Diff: chrome/renderer/resources/extensions/extension_custom_bindings.js

Issue 11571014: Lazy load chrome.* APIs (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: comments/TODOs Created 7 years, 11 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 | Annotate | Revision Log
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2012 The Chromium 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 // Custom bindings for the extension API. 5 // Custom bindings for the extension API.
6 6
7 var bindings = new (require('schema_binding_generator').Bindings)('extension');
8
7 var extensionNatives = requireNative('extension'); 9 var extensionNatives = requireNative('extension');
8 var GetExtensionViews = extensionNatives.GetExtensionViews; 10 var GetExtensionViews = extensionNatives.GetExtensionViews;
9 var runtimeNatives = requireNative('runtime'); 11 var runtimeNatives = requireNative('runtime');
10 var OpenChannelToExtension = runtimeNatives.OpenChannelToExtension; 12 var OpenChannelToExtension = runtimeNatives.OpenChannelToExtension;
11 var OpenChannelToNativeApp = runtimeNatives.OpenChannelToNativeApp; 13 var OpenChannelToNativeApp = runtimeNatives.OpenChannelToNativeApp;
12 var chromeHidden = requireNative('chrome_hidden').GetChromeHidden(); 14 var chromeHidden = requireNative('chrome_hidden').GetChromeHidden();
15 var chrome = requireNative('chrome').GetChrome();
13 var sendMessageUpdateArguments = 16 var sendMessageUpdateArguments =
14 require('miscellaneous_bindings').sendMessageUpdateArguments; 17 require('miscellaneous_bindings').sendMessageUpdateArguments;
15 18
16 var inIncognitoContext = requireNative('process').InIncognitoContext(); 19 var inIncognitoContext = requireNative('process').InIncognitoContext();
17 var sendRequestIsDisabled = requireNative('process').IsSendRequestDisabled(); 20 var sendRequestIsDisabled = requireNative('process').IsSendRequestDisabled();
18 var contextType = requireNative('process').GetContextType(); 21 var contextType = requireNative('process').GetContextType();
22 var manifestVersion = requireNative('process').GetManifestVersion();
19 23
20 chrome.extension = chrome.extension || {}; 24 var DCHECK = requireNative('logging').DCHECK;
21
22 var manifestVersion = requireNative('process').GetManifestVersion();
23 if (manifestVersion < 2) {
24 chrome.self = chrome.extension;
25 chrome.extension.inIncognitoTab = inIncognitoContext;
26 }
27
28 chrome.extension.inIncognitoContext = inIncognitoContext;
29 25
30 // This should match chrome.windows.WINDOW_ID_NONE. 26 // This should match chrome.windows.WINDOW_ID_NONE.
31 // 27 //
32 // We can't use chrome.windows.WINDOW_ID_NONE directly because the 28 // We can't use chrome.windows.WINDOW_ID_NONE directly because the
33 // chrome.windows API won't exist unless this extension has permission for it; 29 // chrome.windows API won't exist unless this extension has permission for it;
34 // which may not be the case. 30 // which may not be the case.
35 var WINDOW_ID_NONE = -1; 31 var WINDOW_ID_NONE = -1;
36 32
37 chromeHidden.registerCustomHook('extension', 33 bindings.registerCustomHook(function(bindingsAPI, extensionId) {
38 function(bindingsAPI, extensionId) { 34 var extension = bindingsAPI.compiledApi;
35 if (manifestVersion < 2) {
36 chrome.self = extension;
37 extension.inIncognitoTab = inIncognitoContext;
38 }
39 extension.inIncognitoContext = inIncognitoContext;
40
39 var apiFunctions = bindingsAPI.apiFunctions; 41 var apiFunctions = bindingsAPI.apiFunctions;
40 42
41 apiFunctions.setHandleRequest('getViews', function(properties) { 43 apiFunctions.setHandleRequest('getViews', function(properties) {
42 var windowId = WINDOW_ID_NONE; 44 var windowId = WINDOW_ID_NONE;
43 var type = 'ALL'; 45 var type = 'ALL';
44 if (properties) { 46 if (properties) {
45 if (properties.type != null) { 47 if (properties.type != null) {
46 type = properties.type; 48 type = properties.type;
47 } 49 }
48 if (properties.windowId != null) { 50 if (properties.windowId != null) {
(...skipping 28 matching lines...) Expand all
77 'connect', 'sendMessage', 'connectNative', 'sendNativeMessage', 79 'connect', 'sendMessage', 'connectNative', 'sendNativeMessage',
78 // Events 80 // Events
79 'onConnect', 'onConnectExternal', 'onMessage', 'onMessageExternal' 81 'onConnect', 'onConnectExternal', 'onMessage', 'onMessageExternal'
80 ]; 82 ];
81 mayNeedAlias.forEach(function(alias) { 83 mayNeedAlias.forEach(function(alias) {
82 try { 84 try {
83 // Deliberately accessing runtime[alias] rather than testing its 85 // Deliberately accessing runtime[alias] rather than testing its
84 // existence, since accessing may throw an exception if this context 86 // existence, since accessing may throw an exception if this context
85 // doesn't have access. 87 // doesn't have access.
86 if (chrome.runtime[alias]) 88 if (chrome.runtime[alias])
87 chrome.extension[alias] = chrome.runtime[alias]; 89 extension[alias] = chrome.runtime[alias];
88 } catch(e) {} 90 } catch(e) {}
89 }); 91 });
90 92
91 apiFunctions.setUpdateArgumentsPreValidate('sendRequest', 93 apiFunctions.setUpdateArgumentsPreValidate('sendRequest',
92 sendMessageUpdateArguments.bind(null, 'sendRequest')); 94 sendMessageUpdateArguments.bind(null, 'sendRequest'));
93 95
94 apiFunctions.setHandleRequest('sendRequest', 96 apiFunctions.setHandleRequest('sendRequest',
95 function(targetId, request, responseCallback) { 97 function(targetId, request, responseCallback) {
96 if (sendRequestIsDisabled) 98 if (sendRequestIsDisabled)
97 throw new Error(sendRequestIsDisabled); 99 throw new Error(sendRequestIsDisabled);
98 var port = chrome.runtime.connect(targetId || extensionId, 100 var port = chrome.runtime.connect(targetId || extensionId,
99 {name: chromeHidden.kRequestChannel}); 101 {name: chromeHidden.kRequestChannel});
100 chromeHidden.Port.sendMessageImpl(port, request, responseCallback); 102 chromeHidden.Port.sendMessageImpl(port, request, responseCallback);
101 }); 103 });
102 104
103 if (sendRequestIsDisabled) { 105 if (sendRequestIsDisabled) {
104 chrome.extension.onRequest.addListener = function() { 106 extension.onRequest.addListener = function() {
105 throw new Error(sendRequestIsDisabled); 107 throw new Error(sendRequestIsDisabled);
106 }; 108 };
107 if (contextType == 'BLESSED_EXTENSION') { 109 if (contextType == 'BLESSED_EXTENSION') {
108 chrome.extension.onRequestExternal.addListener = function() { 110 extension.onRequestExternal.addListener = function() {
109 throw new Error(sendRequestIsDisabled); 111 throw new Error(sendRequestIsDisabled);
110 }; 112 };
111 } 113 }
112 } 114 }
115 });
113 116
114 }); 117 exports.bindings = bindings.generate();
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698