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
|
var chromium;
(function() {
if (!chromium)
chromium = {};
// callback handling
var callbacks = [];
chromium._dispatchCallback = function(callbackId, str) {
// We shouldn't be receiving evil JSON unless the browser is owned, but just
// to be safe, we sanitize it. This regex mania was borrowed from json2,
// from json.org.
if (!/^[\],:{}\s]*$/.test(
str.replace(/\\(?:["\\\/bfnrt]|u[0-9a-fA-F]{4})/g, '@').
replace(/"[^"\\\n\r]*"|true|false|null|-?\d+(?:\.\d*)?(?:[eE][+\-]?\d+)?/g, ']').
replace(/(?:^|:|,)(?:\s*\[)+/g, '')))
throw new Error("Unexpected characters in incoming JSON response.");
// This is lame. V8 disallows direct access to eval() in extensions (see:
// v8::internal::Parser::ParseLeftHandSideExpression()). So we must use
// this supa-jank hack instead. We really need native JSON.
str = 'return ' + str;
callbacks[callbackId](new Function(str)());
delete callbacks[callbackId];
};
// Quick and dirty json serialization.
// TODO(aa): Did I mention we need native JSON?
function serialize(thing) {
switch (typeof thing) {
case 'string':
return '\"' + thing.replace('\\', '\\\\').replace('\"', '\\\"') + '\"';
case 'boolean':
case 'number':
return String(thing);
case 'object':
if (thing === null)
return String(thing)
var items = [];
if (thing.constructor == Array) {
for (var i = 0; i < thing.length; i++)
items.push(serialize(thing[i]));
return '[' + items.join(',') + ']';
} else {
for (var p in thing)
items.push(serialize(p) + ':' + serialize(thing[p]));
return '{' + items.join(',') + '}';
}
default:
return '';
}
}
// Send an API request and optionally register a callback.
function sendRequest(request, args, callback) {
var sargs = serialize(args);
var callbackId = -1;
if (callback) {
native function GetNextCallbackId();
callbackId = GetNextCallbackId();
callbacks[callbackId] = callback;
}
request(sargs, callbackId);
}
// Tabs
chromium.tabs = {};
// TODO(aa): This should eventually take an optional windowId param.
chromium.tabs.getTabsForWindow = function(callback) {
native function GetTabsForWindow();
sendRequest(GetTabsForWindow, null, callback);
};
chromium.tabs.createTab = function(tab, callback) {
native function CreateTab();
sendRequest(CreateTab, tab, callback);
};
})();
|