summaryrefslogtreecommitdiffstats
path: root/chrome/renderer/resources/extension_process_bindings.js
blob: 3a18c9784c977a08090a19ef4fe689ddca7ae80e (plain)
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
var chromium;
(function() {
  if (!chromium)
    chromium = {};

  // 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) {
      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.getTab = function(tabId, callback) {
    native function GetTab();
    sendRequest(GetTab, tabId, callback);
  };
  chromium.tabs.createTab = function(tab, callback) {
    native function CreateTab();
    sendRequest(CreateTab, tab, callback);
  };
  chromium.tabs.updateTab = function(tab) {
    native function UpdateTab();
    sendRequest(UpdateTab, tab);
  };
  chromium.tabs.removeTab = function(tabId) {
    native function RemoveTab();
    sendRequest(RemoveTab, tabId);
  };
})();