OLD | NEW |
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 extensionNatives = requireNative('extension'); | 7 var extensionNatives = requireNative('extension'); |
8 var GetExtensionViews = extensionNatives.GetExtensionViews; | 8 var GetExtensionViews = extensionNatives.GetExtensionViews; |
9 var runtimeNatives = requireNative('runtime'); | 9 var runtimeNatives = requireNative('runtime'); |
10 var OpenChannelToExtension = runtimeNatives.OpenChannelToExtension; | 10 var OpenChannelToExtension = runtimeNatives.OpenChannelToExtension; |
(...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
63 }); | 63 }); |
64 | 64 |
65 apiFunctions.setHandleRequest('getURL', function(path) { | 65 apiFunctions.setHandleRequest('getURL', function(path) { |
66 path = String(path); | 66 path = String(path); |
67 if (!path.length || path[0] != '/') | 67 if (!path.length || path[0] != '/') |
68 path = '/' + path; | 68 path = '/' + path; |
69 return 'chrome-extension://' + extensionId + path; | 69 return 'chrome-extension://' + extensionId + path; |
70 }); | 70 }); |
71 | 71 |
72 // Alias several messaging deprecated APIs to their runtime counterparts. | 72 // Alias several messaging deprecated APIs to their runtime counterparts. |
73 chrome.extension.connect = chrome.runtime.connect; | 73 var mayNeedAlias = [ |
74 chrome.extension.sendMessage = chrome.runtime.sendMessage; | 74 // Types |
75 chrome.extension.onConnect = chrome.runtime.onConnect; | 75 'Port', |
76 chrome.extension.onMessage = chrome.runtime.onMessage; | 76 // Functions |
77 if (contextType == 'BLESSED_EXTENSION') { | 77 'connect', 'sendMessage', 'connectNative', 'sendNativeMessage', |
78 chrome.extension.connectNative = chrome.runtime.connectNative; | 78 // Events |
79 chrome.extension.sendNativeMessage = chrome.runtime.sendNativeMessage; | 79 'onConnect', 'onConnectExternal', 'onMessage', 'onMessageExternal' |
80 chrome.extension.onConnectExternal = chrome.runtime.onConnectExternal; | 80 ]; |
81 chrome.extension.onMessageExternal = chrome.runtime.onMessageExternal; | 81 mayNeedAlias.forEach(function(alias) { |
82 } | 82 try { |
| 83 // Deliberately accessing runtime[alias] rather than testing its |
| 84 // existence, since accessing may throw an exception if this context |
| 85 // doesn't have access. |
| 86 if (chrome.runtime[alias]) |
| 87 chrome.extension[alias] = chrome.runtime[alias]; |
| 88 } catch(e) {} |
| 89 }); |
83 | 90 |
84 apiFunctions.setUpdateArgumentsPreValidate('sendRequest', | 91 apiFunctions.setUpdateArgumentsPreValidate('sendRequest', |
85 sendMessageUpdateArguments.bind(null, 'sendRequest')); | 92 sendMessageUpdateArguments.bind(null, 'sendRequest')); |
86 | 93 |
87 apiFunctions.setHandleRequest('sendRequest', | 94 apiFunctions.setHandleRequest('sendRequest', |
88 function(targetId, request, responseCallback) { | 95 function(targetId, request, responseCallback) { |
89 if (sendRequestIsDisabled) | 96 if (sendRequestIsDisabled) |
90 throw new Error(sendRequestIsDisabled); | 97 throw new Error(sendRequestIsDisabled); |
91 var port = chrome.runtime.connect(targetId || extensionId, | 98 var port = chrome.runtime.connect(targetId || extensionId, |
92 {name: chromeHidden.kRequestChannel}); | 99 {name: chromeHidden.kRequestChannel}); |
93 chromeHidden.Port.sendMessageImpl(port, request, responseCallback); | 100 chromeHidden.Port.sendMessageImpl(port, request, responseCallback); |
94 }); | 101 }); |
95 | 102 |
96 if (sendRequestIsDisabled) { | 103 if (sendRequestIsDisabled) { |
97 chrome.extension.onRequest.addListener = function() { | 104 chrome.extension.onRequest.addListener = function() { |
98 throw new Error(sendRequestIsDisabled); | 105 throw new Error(sendRequestIsDisabled); |
99 }; | 106 }; |
100 if (contextType == 'BLESSED_EXTENSION') { | 107 if (contextType == 'BLESSED_EXTENSION') { |
101 chrome.extension.onRequestExternal.addListener = function() { | 108 chrome.extension.onRequestExternal.addListener = function() { |
102 throw new Error(sendRequestIsDisabled); | 109 throw new Error(sendRequestIsDisabled); |
103 }; | 110 }; |
104 } | 111 } |
105 } | 112 } |
106 | 113 |
107 }); | 114 }); |
OLD | NEW |