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
|
// Copyright (c) 2012 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 bindings for the extension API.
var extensionNatives = requireNative('extension');
var GetExtensionViews = extensionNatives.GetExtensionViews;
var OpenChannelToExtension = extensionNatives.OpenChannelToExtension;
var chromeHidden = requireNative('chrome_hidden').GetChromeHidden();
var inIncognitoContext = requireNative('process').InIncognitoContext();
chrome.extension = chrome.extension || {};
var manifestVersion = requireNative('process').GetManifestVersion();
if (manifestVersion < 2) {
chrome.self = chrome.extension;
chrome.extension.inIncognitoTab = inIncognitoContext;
}
chrome.extension.inIncognitoContext = inIncognitoContext;
// This should match chrome.windows.WINDOW_ID_NONE.
//
// We can't use chrome.windows.WINDOW_ID_NONE directly because the
// chrome.windows API won't exist unless this extension has permission for it;
// which may not be the case.
var WINDOW_ID_NONE = -1;
chromeHidden.registerCustomHook('extension',
function(bindingsAPI, extensionId) {
var apiFunctions = bindingsAPI.apiFunctions;
apiFunctions.setHandleRequest('getViews', function(properties) {
var windowId = WINDOW_ID_NONE;
var type = 'ALL';
if (properties) {
if (properties.type != null) {
type = properties.type;
}
if (properties.windowId != null) {
windowId = properties.windowId;
}
}
return GetExtensionViews(windowId, type) || null;
});
apiFunctions.setHandleRequest('getBackgroundPage', function() {
return GetExtensionViews(-1, 'BACKGROUND')[0] || null;
});
apiFunctions.setHandleRequest('getExtensionTabs', function(windowId) {
if (windowId == null)
windowId = WINDOW_ID_NONE;
return GetExtensionViews(windowId, 'TAB');
});
apiFunctions.setHandleRequest('getURL', function(path) {
path = String(path);
if (!path.length || path[0] != '/')
path = '/' + path;
return 'chrome-extension://' + extensionId + path;
});
function sendMessageUpdateArguments(functionName) {
// Align missing (optional) function arguments with the arguments that
// schema validation is expecting, e.g.
// extension.sendRequest(req) -> extension.sendRequest(null, req)
// extension.sendRequest(req, cb) -> extension.sendRequest(null, req, cb)
var args = Array.prototype.splice.call(arguments, 1); // skip functionName
var lastArg = args.length - 1;
// responseCallback (last argument) is optional.
var responseCallback = null;
if (typeof(args[lastArg]) == 'function')
responseCallback = args[lastArg--];
// request (second argument) is required.
var request = args[lastArg--];
// targetId (first argument, extensionId in the manfiest) is optional.
var targetId = null;
if (lastArg >= 0)
targetId = args[lastArg--];
if (lastArg != -1)
throw new Error('Invalid arguments to ' + functionName + '.');
return [targetId, request, responseCallback];
}
apiFunctions.setUpdateArgumentsPreValidate('sendRequest',
sendMessageUpdateArguments.bind(null, 'sendRequest'));
apiFunctions.setUpdateArgumentsPreValidate('sendMessage',
sendMessageUpdateArguments.bind(null, 'sendMessage'));
apiFunctions.setHandleRequest('sendRequest',
function(targetId, request, responseCallback) {
var port = chrome.extension.connect(targetId || extensionId,
{name: chromeHidden.kRequestChannel});
chromeHidden.Port.sendMessageImpl(port, request, responseCallback);
});
apiFunctions.setHandleRequest('sendMessage',
function(targetId, message, responseCallback) {
var port = chrome.extension.connect(targetId || extensionId,
{name: chromeHidden.kMessageChannel});
chromeHidden.Port.sendMessageImpl(port, message, responseCallback);
});
apiFunctions.setUpdateArgumentsPreValidate('connect', function() {
// Align missing (optional) function arguments with the arguments that
// schema validation is expecting, e.g.
// extension.connect() -> extension.connect(null, null)
// extension.connect({}) -> extension.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.setHandleRequest('connect', function(targetId, connectInfo) {
if (!targetId)
targetId = extensionId;
var name = '';
if (connectInfo && connectInfo.name)
name = connectInfo.name;
var portId = OpenChannelToExtension(extensionId, targetId, name);
if (portId >= 0)
return chromeHidden.Port.createPort(portId, name);
throw new Error('Error connecting to extension ' + targetId);
});
});
|