summaryrefslogtreecommitdiffstats
path: root/remoting/client/extension/background.js
blob: f41740794a2ac731486d65ef35e8d9b8a57b32c2 (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
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
// 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.

/**
 * If there is already a tab with the given URL, switch focus to it. Otherwise,
 * create a new tab and open the URL.
 *
 * @param url The URL to open.
 */
function focusOrCreateTab(url) {
  chrome.windows.getAll({"populate":true}, function(windows) {
      var existing_tab = null;
      for (var i in windows) {
        var tabs = windows[i].tabs;
        for (var j in tabs) {
          var tab = tabs[j];
          if (tab.url == url) {
            existing_tab = tab;
            break;
          }
        }
      }
      if (existing_tab) {
        chrome.tabs.update(existing_tab.id, {"selected": true});
      } else {
        chrome.tabs.create({"url": url, "selected": true});
      }
    });
}

/**
 * In the current tab, navigate to the specified URL.
 *
 * @param url The URL to navigate to.
 */
function navigate(url, callback) {
  chrome.tabs.getSelected(null, function(tab) {
      chrome.tabs.update(tab.id, {url: url}, callback);
  });
}

// Open the Chromoting HostList tab when
chrome.browserAction.onClicked.addListener(function(tab) {
    var hostlist_url = chrome.extension.getURL("hostlist.html");
    focusOrCreateTab(hostlist_url);
  });


function openChromotingTab(hostName, hostJid) {
  var username = getCookie('username');
  var xmppAuth = getCookie('xmpp_auth');
  var newTabUrl = chrome.extension.getURL("chromoting_tab.html");
  var request = {
    username: getCookie('username'),
    xmppAuth: getCookie('xmpp_auth'),
    hostName: hostName,
    hostJid: hostJid,
  };

  console.log("Attempt to connect with" +
              " username='" + request.username + "'" +
              " hostName='" + request.hostName + "'" +
              " hostJid='" + request.hostJid + "'" +
              " auth_token='" + request.xmppAuth + "'");

  var sendRequestFunc = function (tab) {
    console.log("We're trying now to send to " + tab.id);
    chrome.tabs.sendRequest(
        tab.id, request, function() {
          console.log('Tab finished connect.');
        });
  };

  // This function will run when after the url for the tab is updated. If
  // the tab is not yet loaded it will wait for another 500ms to inspect
  // again.
  var checkStatusFunc = function (tab) {
    if (tab.status == "complete") {
      sendRequestFunc(tab);
      return;
    }

    // Wait for 500ms and then get the tab and check its status.
    setTimeout(function() {
        chrome.tabs.get(tab.id, checkStatusFunc);
      }, 500);
  }

  navigate(newTabUrl, checkStatusFunc);
}