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
|
var chromium;
(function() {
native function GetNextCallbackId();
native function GetTabsForWindow();
native function GetTab();
native function CreateTab();
native function UpdateTab();
native function RemoveTab();
if (!chromium)
chromium = {};
// Validate arguments.
function validate(inst, schemas) {
if (inst.length > schemas.length)
throw new Error("Too many arguments.");
for (var i = 0; i < schemas.length; i++) {
if (inst[i]) {
var validator = new chromium.JSONSchemaValidator();
validator.validate(inst[i], schemas[i]);
if (validator.errors.length == 0)
continue;
var message = "Invalid value for argument " + i + ". ";
for (var i = 0, err; err = validator.errors[i]; i++) {
if (err.path) {
message += "Property '" + err.path + "': ";
}
message += err.message;
message = message.substring(0, message.length - 1);
message += ", ";
}
message = message.substring(0, message.length - 2);
message += ".";
throw new Error(message);
} else if (!schemas[i].optional) {
throw new Error("Argument " + i + " is required.");
}
}
}
// callback handling
var callbacks = [];
chromium._dispatchCallback = function(callbackId, str) {
try {
if (str) {
callbacks[callbackId](goog.json.parse(str));
} else {
callbacks[callbackId]();
}
} finally {
delete callbacks[callbackId];
}
};
// Send an API request and optionally register a callback.
function sendRequest(request, args, callback) {
var sargs = goog.json.serialize(args);
var callbackId = -1;
if (callback) {
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) {
validate(arguments, arguments.callee.params);
sendRequest(GetTabsForWindow, null, callback);
};
chromium.tabs.getTabsForWindow.params = [
chromium.types.optFun
];
chromium.tabs.getTab = function(tabId, callback) {
validate(arguments, arguments.callee.params);
sendRequest(GetTab, tabId, callback);
};
chromium.tabs.getTab.params = [
chromium.types.pInt,
chromium.types.optFun
];
chromium.tabs.createTab = function(tab, callback) {
validate(arguments, arguments.callee.params);
sendRequest(CreateTab, tab, callback);
};
chromium.tabs.createTab.params = [
{
type: "object",
properties: {
windowId: chromium.types.optPInt,
url: chromium.types.optStr,
selected: chromium.types.optBool
},
additionalProperties: false
},
chromium.types.optFun
];
chromium.tabs.updateTab = function(tab) {
validate(arguments, arguments.callee.params);
sendRequest(UpdateTab, tab);
};
chromium.tabs.updateTab.params = [
{
type: "object",
properties: {
id: chromium.types.pInt,
windowId: chromium.types.optPInt,
url: chromium.types.optStr,
selected: chromium.types.optBool
},
additionalProperties: false
}
];
chromium.tabs.removeTab = function(tabId) {
validate(arguments, arguments.callee.params);
sendRequest(RemoveTab, tabId);
};
chromium.tabs.removeTab.params = [
chromium.types.pInt
];
})();
|