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 binding for the runtime API. | 5 // Custom binding for the runtime API. |
6 | 6 |
7 var binding = require('binding').Binding.create('runtime'); | 7 var binding = require('binding').Binding.create('runtime'); |
8 | 8 |
| 9 var extensionNatives = requireNative('extension'); |
| 10 var miscBindings = require('miscellaneous_bindings'); |
9 var runtimeNatives = requireNative('runtime'); | 11 var runtimeNatives = requireNative('runtime'); |
10 var extensionNatives = requireNative('extension'); | 12 var unloadEvent = require('unload_event'); |
11 var GetExtensionViews = extensionNatives.GetExtensionViews; | |
12 var OpenChannelToExtension = runtimeNatives.OpenChannelToExtension; | |
13 var OpenChannelToNativeApp = runtimeNatives.OpenChannelToNativeApp; | |
14 var chromeHidden = requireNative('chrome_hidden').GetChromeHidden(); | |
15 var sendMessageUpdateArguments = | |
16 require('miscellaneous_bindings').sendMessageUpdateArguments; | |
17 | 13 |
18 binding.registerCustomHook(function(binding, id, contextType) { | 14 binding.registerCustomHook(function(binding, id, contextType) { |
19 var apiFunctions = binding.apiFunctions; | 15 var apiFunctions = binding.apiFunctions; |
20 var runtime = binding.compiledApi; | 16 var runtime = binding.compiledApi; |
21 | 17 |
22 // | 18 // |
23 // Unprivileged APIs. | 19 // Unprivileged APIs. |
24 // | 20 // |
25 | 21 |
26 runtime.id = id; | 22 runtime.id = id; |
27 | 23 |
28 apiFunctions.setHandleRequest('getManifest', function() { | 24 apiFunctions.setHandleRequest('getManifest', function() { |
29 return runtimeNatives.GetManifest(); | 25 return runtimeNatives.GetManifest(); |
30 }); | 26 }); |
31 | 27 |
32 apiFunctions.setHandleRequest('getURL', function(path) { | 28 apiFunctions.setHandleRequest('getURL', function(path) { |
33 path = String(path); | 29 path = String(path); |
34 if (!path.length || path[0] != '/') | 30 if (!path.length || path[0] != '/') |
35 path = '/' + path; | 31 path = '/' + path; |
36 return 'chrome-extension://' + id + path; | 32 return 'chrome-extension://' + id + path; |
37 }); | 33 }); |
38 | 34 |
| 35 var sendMessageUpdateArguments = miscBindings.sendMessageUpdateArguments; |
39 apiFunctions.setUpdateArgumentsPreValidate('sendMessage', | 36 apiFunctions.setUpdateArgumentsPreValidate('sendMessage', |
40 sendMessageUpdateArguments.bind(null, 'sendMessage')); | 37 sendMessageUpdateArguments.bind(null, 'sendMessage')); |
41 apiFunctions.setUpdateArgumentsPreValidate('sendNativeMessage', | 38 apiFunctions.setUpdateArgumentsPreValidate('sendNativeMessage', |
42 sendMessageUpdateArguments.bind(null, 'sendNativeMessage')); | 39 sendMessageUpdateArguments.bind(null, 'sendNativeMessage')); |
43 | 40 |
44 apiFunctions.setHandleRequest('sendMessage', | 41 apiFunctions.setHandleRequest('sendMessage', |
45 function(targetId, message, responseCallback) { | 42 function(targetId, message, responseCallback) { |
46 var port = runtime.connect(targetId || runtime.id, | 43 var port = runtime.connect(targetId || runtime.id, |
47 {name: chromeHidden.kMessageChannel}); | 44 {name: miscBindings.kMessageChannel}); |
48 chromeHidden.Port.sendMessageImpl(port, message, responseCallback); | 45 miscBindings.sendMessageImpl(port, message, responseCallback); |
49 }); | 46 }); |
50 | 47 |
51 apiFunctions.setHandleRequest('sendNativeMessage', | 48 apiFunctions.setHandleRequest('sendNativeMessage', |
52 function(targetId, message, responseCallback) { | 49 function(targetId, message, responseCallback) { |
53 var port = runtime.connectNative(targetId); | 50 var port = runtime.connectNative(targetId); |
54 chromeHidden.Port.sendMessageImpl(port, message, responseCallback); | 51 miscBindings.sendMessageImpl(port, message, responseCallback); |
55 }); | 52 }); |
56 | 53 |
57 apiFunctions.setUpdateArgumentsPreValidate('connect', function() { | 54 apiFunctions.setUpdateArgumentsPreValidate('connect', function() { |
58 // Align missing (optional) function arguments with the arguments that | 55 // Align missing (optional) function arguments with the arguments that |
59 // schema validation is expecting, e.g. | 56 // schema validation is expecting, e.g. |
60 // runtime.connect() -> runtime.connect(null, null) | 57 // runtime.connect() -> runtime.connect(null, null) |
61 // runtime.connect({}) -> runtime.connect(null, {}) | 58 // runtime.connect({}) -> runtime.connect(null, {}) |
62 var nextArg = 0; | 59 var nextArg = 0; |
63 | 60 |
64 // targetId (first argument) is optional. | 61 // targetId (first argument) is optional. |
(...skipping 21 matching lines...) Expand all Loading... |
86 | 83 |
87 apiFunctions.setHandleRequest('connect', function(targetId, connectInfo) { | 84 apiFunctions.setHandleRequest('connect', function(targetId, connectInfo) { |
88 if (!targetId) | 85 if (!targetId) |
89 targetId = runtime.id; | 86 targetId = runtime.id; |
90 var name = ''; | 87 var name = ''; |
91 if (connectInfo && connectInfo.name) | 88 if (connectInfo && connectInfo.name) |
92 name = connectInfo.name; | 89 name = connectInfo.name; |
93 | 90 |
94 // Don't let orphaned content scripts communicate with their extension. | 91 // Don't let orphaned content scripts communicate with their extension. |
95 // http://crbug.com/168263 | 92 // http://crbug.com/168263 |
96 if (!chromeHidden.wasUnloaded) { | 93 if (!unloadEvent.wasDispatched) { |
97 var portId = OpenChannelToExtension(runtime.id, targetId, name); | 94 var portId = runtimeNatives.OpenChannelToExtension(runtime.id, |
| 95 targetId, |
| 96 name); |
98 if (portId >= 0) | 97 if (portId >= 0) |
99 return chromeHidden.Port.createPort(portId, name); | 98 return miscBindings.createPort(portId, name); |
100 } | 99 } |
101 throw new Error('Error connecting to extension ' + targetId); | 100 throw new Error('Error connecting to extension ' + targetId); |
102 }); | 101 }); |
103 | 102 |
104 // | 103 // |
105 // Privileged APIs. | 104 // Privileged APIs. |
106 // | 105 // |
107 if (contextType != 'BLESSED_EXTENSION') | 106 if (contextType != 'BLESSED_EXTENSION') |
108 return; | 107 return; |
109 | 108 |
110 apiFunctions.setHandleRequest('connectNative', | 109 apiFunctions.setHandleRequest('connectNative', |
111 function(nativeAppName) { | 110 function(nativeAppName) { |
112 if (!chromeHidden.wasUnloaded) { | 111 if (!unloadEvent.wasDispatched) { |
113 var portId = OpenChannelToNativeApp(runtime.id, nativeAppName); | 112 var portId = runtimeNatives.OpenChannelToNativeApp(runtime.id, |
| 113 nativeAppName); |
114 if (portId >= 0) | 114 if (portId >= 0) |
115 return chromeHidden.Port.createPort(portId, ''); | 115 return miscBindings.createPort(portId, ''); |
116 } | 116 } |
117 throw new Error('Error connecting to native app: ' + nativeAppName); | 117 throw new Error('Error connecting to native app: ' + nativeAppName); |
118 }); | 118 }); |
119 | 119 |
120 apiFunctions.setCustomCallback('getBackgroundPage', | 120 apiFunctions.setCustomCallback('getBackgroundPage', |
121 function(name, request, response) { | 121 function(name, request, response) { |
122 if (request.callback) { | 122 if (request.callback) { |
123 var bg = GetExtensionViews(-1, 'BACKGROUND')[0] || null; | 123 var bg = extensionNatives.GetExtensionViews(-1, 'BACKGROUND')[0] || null; |
124 request.callback(bg); | 124 request.callback(bg); |
125 } | 125 } |
126 request.callback = null; | 126 request.callback = null; |
127 }); | 127 }); |
128 | 128 |
129 }); | 129 }); |
130 | 130 |
131 exports.binding = binding.generate(); | 131 exports.binding = binding.generate(); |
OLD | NEW |