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

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: android compilation Created 7 years, 9 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 binding for the extension API.
6
7 var binding = require('binding').Binding.create('extension');
6 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();
19
20 chrome.extension = chrome.extension || {};
21
22 var manifestVersion = requireNative('process').GetManifestVersion(); 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 23
30 // This should match chrome.windows.WINDOW_ID_NONE. 24 // This should match chrome.windows.WINDOW_ID_NONE.
31 // 25 //
32 // We can't use chrome.windows.WINDOW_ID_NONE directly because the 26 // 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; 27 // chrome.windows API won't exist unless this extension has permission for it;
34 // which may not be the case. 28 // which may not be the case.
35 var WINDOW_ID_NONE = -1; 29 var WINDOW_ID_NONE = -1;
36 30
37 chromeHidden.registerCustomHook('extension', 31 binding.registerCustomHook(function(bindingsAPI, extensionId) {
38 function(bindingsAPI, extensionId) { 32 var extension = bindingsAPI.compiledApi;
33 if (manifestVersion < 2) {
34 chrome.self = extension;
35 extension.inIncognitoTab = inIncognitoContext;
36 }
37 extension.inIncognitoContext = inIncognitoContext;
38
39 var apiFunctions = bindingsAPI.apiFunctions; 39 var apiFunctions = bindingsAPI.apiFunctions;
40 40
41 apiFunctions.setHandleRequest('getViews', function(properties) { 41 apiFunctions.setHandleRequest('getViews', function(properties) {
42 var windowId = WINDOW_ID_NONE; 42 var windowId = WINDOW_ID_NONE;
43 var type = 'ALL'; 43 var type = 'ALL';
44 if (properties) { 44 if (properties) {
45 if (properties.type != null) { 45 if (properties.type != null) {
46 type = properties.type; 46 type = properties.type;
47 } 47 }
48 if (properties.windowId != null) { 48 if (properties.windowId != null) {
(...skipping 27 matching lines...) Expand all
76 // Functions 76 // Functions
77 'connect', 'sendMessage', 'connectNative', 'sendNativeMessage', 77 'connect', 'sendMessage', 'connectNative', 'sendNativeMessage',
78 // Events 78 // Events
79 'onConnect', 'onConnectExternal', 'onMessage', 'onMessageExternal' 79 'onConnect', 'onConnectExternal', 'onMessage', 'onMessageExternal'
80 ]; 80 ];
81 mayNeedAlias.forEach(function(alias) { 81 mayNeedAlias.forEach(function(alias) {
82 // Checking existence isn't enough since some functions are disabled via 82 // Checking existence isn't enough since some functions are disabled via
83 // getters that throw exceptions. Assume that any getter is such a function. 83 // getters that throw exceptions. Assume that any getter is such a function.
84 if (chrome.runtime.hasOwnProperty(alias) && 84 if (chrome.runtime.hasOwnProperty(alias) &&
85 chrome.runtime.__lookupGetter__(alias) === undefined) { 85 chrome.runtime.__lookupGetter__(alias) === undefined) {
86 chrome.extension[alias] = chrome.runtime[alias]; 86 extension[alias] = chrome.runtime[alias];
87 } 87 }
88 }); 88 });
89 89
90 apiFunctions.setUpdateArgumentsPreValidate('sendRequest', 90 apiFunctions.setUpdateArgumentsPreValidate('sendRequest',
91 sendMessageUpdateArguments.bind(null, 'sendRequest')); 91 sendMessageUpdateArguments.bind(null, 'sendRequest'));
92 92
93 apiFunctions.setHandleRequest('sendRequest', 93 apiFunctions.setHandleRequest('sendRequest',
94 function(targetId, request, responseCallback) { 94 function(targetId, request, responseCallback) {
95 if (sendRequestIsDisabled) 95 if (sendRequestIsDisabled)
96 throw new Error(sendRequestIsDisabled); 96 throw new Error(sendRequestIsDisabled);
97 var port = chrome.runtime.connect(targetId || extensionId, 97 var port = chrome.runtime.connect(targetId || extensionId,
98 {name: chromeHidden.kRequestChannel}); 98 {name: chromeHidden.kRequestChannel});
99 chromeHidden.Port.sendMessageImpl(port, request, responseCallback); 99 chromeHidden.Port.sendMessageImpl(port, request, responseCallback);
100 }); 100 });
101 101
102 if (sendRequestIsDisabled) { 102 if (sendRequestIsDisabled) {
103 chrome.extension.onRequest.addListener = function() { 103 extension.onRequest.addListener = function() {
104 throw new Error(sendRequestIsDisabled); 104 throw new Error(sendRequestIsDisabled);
105 }; 105 };
106 if (contextType == 'BLESSED_EXTENSION') { 106 if (contextType == 'BLESSED_EXTENSION') {
107 chrome.extension.onRequestExternal.addListener = function() { 107 extension.onRequestExternal.addListener = function() {
108 throw new Error(sendRequestIsDisabled); 108 throw new Error(sendRequestIsDisabled);
109 }; 109 };
110 } 110 }
111 } 111 }
112 });
112 113
113 }); 114 exports.binding = binding.generate();
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698