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

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

Powered by Google App Engine
This is Rietveld 408576698