1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
|
// Copyright 2014 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
// Custom binding for the runtime API.
var binding = require('binding').Binding.create('runtime');
var messaging = require('messaging');
var runtimeNatives = requireNative('runtime');
var process = requireNative('process');
var forEach = require('utils').forEach;
var backgroundPage = window;
var backgroundRequire = require;
var contextType = process.GetContextType();
if (contextType == 'BLESSED_EXTENSION' ||
contextType == 'UNBLESSED_EXTENSION') {
var manifest = runtimeNatives.GetManifest();
if (manifest.app && manifest.app.background) {
// Get the background page if one exists. Otherwise, default to the current
// window.
backgroundPage = runtimeNatives.GetExtensionViews(-1, 'BACKGROUND')[0];
if (backgroundPage) {
var GetModuleSystem = requireNative('v8_context').GetModuleSystem;
backgroundRequire = GetModuleSystem(backgroundPage).require;
} else {
backgroundPage = window;
}
}
}
// For packaged apps, all windows use the bindFileEntryCallback from the
// background page so their FileEntry objects have the background page's context
// as their own. This allows them to be used from other windows (including the
// background page) after the original window is closed.
if (window == backgroundPage) {
var lastError = require('lastError');
var fileSystemNatives = requireNative('file_system_natives');
var GetIsolatedFileSystem = fileSystemNatives.GetIsolatedFileSystem;
var bindDirectoryEntryCallback = function(functionName, apiFunctions) {
apiFunctions.setCustomCallback(functionName,
function(name, request, callback, response) {
if (callback) {
if (!response) {
callback();
return;
}
var fileSystemId = response.fileSystemId;
var baseName = response.baseName;
var fs = GetIsolatedFileSystem(fileSystemId);
try {
fs.root.getDirectory(baseName, {}, callback, function(fileError) {
lastError.run('runtime.' + functionName,
'Error getting Entry, code: ' + fileError.code,
request.stack,
callback);
});
} catch (e) {
lastError.run('runtime.' + functionName,
'Error: ' + e.stack,
request.stack,
callback);
}
}
});
};
} else {
// Force the runtime API to be loaded in the background page. Using
// backgroundPageModuleSystem.require('runtime') is insufficient as
// requireNative is only allowed while lazily loading an API.
backgroundPage.chrome.runtime;
var bindDirectoryEntryCallback = backgroundRequire(
'runtime').bindDirectoryEntryCallback;
}
binding.registerCustomHook(function(binding, id, contextType) {
var apiFunctions = binding.apiFunctions;
var runtime = binding.compiledApi;
//
// Unprivileged APIs.
//
if (id != '')
runtime.id = id;
apiFunctions.setHandleRequest('getManifest', function() {
return runtimeNatives.GetManifest();
});
apiFunctions.setHandleRequest('getURL', function(path) {
path = String(path);
if (!path.length || path[0] != '/')
path = '/' + path;
return 'chrome-extension://' + id + path;
});
var sendMessageUpdateArguments = messaging.sendMessageUpdateArguments;
apiFunctions.setUpdateArgumentsPreValidate('sendMessage',
$Function.bind(sendMessageUpdateArguments, null, 'sendMessage',
true /* hasOptionsArgument */));
apiFunctions.setUpdateArgumentsPreValidate('sendNativeMessage',
$Function.bind(sendMessageUpdateArguments, null, 'sendNativeMessage',
false /* hasOptionsArgument */));
apiFunctions.setHandleRequest('sendMessage',
function(targetId, message, options, responseCallback) {
var connectOptions = {name: messaging.kMessageChannel};
forEach(options, function(k, v) {
connectOptions[k] = v;
});
var port = runtime.connect(targetId || runtime.id, connectOptions);
messaging.sendMessageImpl(port, message, responseCallback);
});
apiFunctions.setHandleRequest('sendNativeMessage',
function(targetId, message, responseCallback) {
var port = runtime.connectNative(targetId);
messaging.sendMessageImpl(port, message, responseCallback);
});
apiFunctions.setUpdateArgumentsPreValidate('connect', function() {
// Align missing (optional) function arguments with the arguments that
// schema validation is expecting, e.g.
// runtime.connect() -> runtime.connect(null, null)
// runtime.connect({}) -> runtime.connect(null, {})
var nextArg = 0;
// targetId (first argument) is optional.
var targetId = null;
if (typeof(arguments[nextArg]) == 'string')
targetId = arguments[nextArg++];
// connectInfo (second argument) is optional.
var connectInfo = null;
if (typeof(arguments[nextArg]) == 'object')
connectInfo = arguments[nextArg++];
if (nextArg != arguments.length)
throw new Error('Invalid arguments to connect.');
return [targetId, connectInfo];
});
apiFunctions.setUpdateArgumentsPreValidate('connectNative',
function(appName) {
if (typeof(appName) !== 'string') {
throw new Error('Invalid arguments to connectNative.');
}
return [appName];
});
apiFunctions.setHandleRequest('connect', function(targetId, connectInfo) {
if (!targetId) {
// runtime.id is only defined inside extensions. If we're in a webpage,
// the best we can do at this point is to fail.
if (!runtime.id) {
throw new Error('chrome.runtime.connect() called from a webpage must ' +
'specify an Extension ID (string) for its first ' +
'argument');
}
targetId = runtime.id;
}
var name = '';
if (connectInfo && connectInfo.name)
name = connectInfo.name;
var includeTlsChannelId =
!!(connectInfo && connectInfo.includeTlsChannelId);
var portId = runtimeNatives.OpenChannelToExtension(targetId, name,
includeTlsChannelId);
if (portId >= 0)
return messaging.createPort(portId, name);
});
//
// Privileged APIs.
//
if (contextType != 'BLESSED_EXTENSION')
return;
apiFunctions.setHandleRequest('connectNative',
function(nativeAppName) {
var portId = runtimeNatives.OpenChannelToNativeApp(runtime.id,
nativeAppName);
if (portId >= 0)
return messaging.createPort(portId, '');
throw new Error('Error connecting to native app: ' + nativeAppName);
});
apiFunctions.setCustomCallback('getBackgroundPage',
function(name, request, callback, response) {
if (callback) {
var bg = runtimeNatives.GetExtensionViews(-1, 'BACKGROUND')[0] || null;
callback(bg);
}
});
bindDirectoryEntryCallback('getPackageDirectoryEntry', apiFunctions);
});
exports.$set('bindDirectoryEntryCallback', bindDirectoryEntryCallback);
exports.$set('binding', binding.generate());
|