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) 2010 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.
/**
* @fileoverview this file provides the bootstrap interface between the
* Chrome event and extension bindings JavaScript files, and the CEEE
* native IE interface, as well as initialization hooks for the native
* interface.
*/
// Console is diverted to nativeContentScriptApi.Log method.
var console = console || {};
// Any function declared native in the Chrome extension bindings files
// is diverted to the ceee namespace to allow the IE JS engine
// to grok the code.
var ceee = ceee || {};
(function () {
// Keep a reference to the global environment in
// effect during boostrap script parsing.
var global = this;
var chromeHidden = {};
var nativeContentScriptApi = null;
// Supply a JSON implementation by leeching off closure.
global.JSON = goog.json;
global.JSON.stringify = JSON.serialize;
ceee.AttachEvent = function (eventName) {
nativeContentScriptApi.AttachEvent(eventName);
};
ceee.DetachEvent = function (eventName) {
nativeContentScriptApi.DetachEvent(eventName);
};
ceee.OpenChannelToExtension = function (sourceId, targetId, name) {
return nativeContentScriptApi.OpenChannelToExtension(sourceId,
targetId,
name);
};
ceee.CloseChannel = function (portId) {
return nativeContentScriptApi.CloseChannel(portId);
};
ceee.PortAddRef = function (portId) {
return nativeContentScriptApi.PortAddRef(portId);
};
ceee.PortRelease = function (portId) {
return nativeContentScriptApi.PortRelease(portId);
};
ceee.PostMessage = function (portId, msg) {
return nativeContentScriptApi.PostMessage(portId, msg);
};
ceee.GetChromeHidden = function () {
return chromeHidden;
};
// This function is invoked from the native CEEE implementation by name
// to pass in the native CEEE interface implementation at the start of
// script engine initialization. This allows us to provide logging
// and any other required or convenient services during the initialization
// of other boostrap scripts.
ceee.startInit_ = function (nativenativeContentScriptApi, extensionId) {
nativeContentScriptApi = nativenativeContentScriptApi;
};
// Last uninitialization callback.
ceee.onUnload_ = function () {
// Dispatch the onUnload event.
chromeHidden.dispatchOnUnload();
// Release the native API as very last act.
nativeContentScriptApi = null;
};
// This function is invoked from the native CEEE implementation by name
// to pass in the extension ID, and to allow any final initialization of
// the script environment before the content scripts themselves are loaded.
ceee.endInit_ = function (nativenativeContentScriptApi, extensionId) {
chrome.initExtension(extensionId);
// Provide the native implementation with the the the
// event notification dispatchers.
nativeContentScriptApi.onLoad = chromeHidden.dispatchOnLoad;
nativeContentScriptApi.onUnload = ceee.onUnload_;
// And the port notification dispatchers.
// function(portId, channelName, tab, extensionId)
nativeContentScriptApi.onPortConnect = chromeHidden.Port.dispatchOnConnect;
// function(portId)
nativeContentScriptApi.onPortDisconnect =
chromeHidden.Port.dispatchOnDisconnect;
// function(msg, portId)
nativeContentScriptApi.onPortMessage =
chromeHidden.Port.dispatchOnMessage;
// TODO(siggi@chromium.org): If there is a different global
// environment at this point (i.e. we have cloned the scripting
// engine for a new window) this is where we can restore goog,
// JSON and chrome.
// Delete the ceee namespace from globals.
delete ceee;
}
console.log = console.log || function (msg) {
if (nativeContentScriptApi)
nativeContentScriptApi.Log("info", msg);
};
console.error = console.error || function (msg) {
if (nativeContentScriptApi)
nativeContentScriptApi.Log("error", msg);
}
// Provide an indexOf member for arrays if it's not already there
// to satisfy the Chrome extension bindings expectations.
if (!Array.prototype.indexOf) {
Array.prototype.indexOf = function(elt /*, from*/) {
var len = this.length >>> 0;
var from = Number(arguments[1]) || 0;
from = (from < 0) ? Math.ceil(from) : Math.floor(from);
if (from < 0)
from += len;
for (; from < len; from++) {
if (from in this && this[from] === elt)
return from;
}
return -1;
}
};
})();
|