summaryrefslogtreecommitdiffstats
path: root/remoting/client/extension/client.js
blob: e01de07ab6e82183fcf6eaaf199f8f82113cb788 (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
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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
// 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.

function init_params() {
  var hash;
  var hashes = window.location.href.slice(
      window.location.href.indexOf('?') + 1).split('&');

  // Prepopulate via cookies first.
  document.getElementById('xmpp_auth').value = get_cookie('xmpp_auth');
  document.getElementById('chromoting_auth').value = get_cookie('chromoting_auth');
  document.getElementById('username').value = get_cookie('username');

  for(var i = 0; i < hashes.length; i++)
  {
    hash = hashes[i].split('=');
    if (hash[0] == 'xmpp_auth') {
      document.getElementById('xmpp_auth').value = hash[1];
      set_cookie('xmpp_auth', hash[1]);

    } else if (hash[0] == "chromoting_auth") {
      document.getElementById('chromoting_auth').value = hash[1];
      set_cookie('chromoting_auth', hash[1]);

    } else if (hash[0] == 'username') {
      document.getElementById('username').value = hash[1];
      set_cookie('username', hash[1]);

    } else if (hash[0] == 'password') {
      document.getElementById('password').value = hash[1];

    } else if (hash[0] == 'host_jid') {
      document.getElementById('host_jid').value = hash[1];

    }
  }
}

function find_hosts(form) {
  // If either cookie is missing, login first.
  if (get_cookie('chromoting_auth') == null || get_cookie('xmpp_auth') == null) {
    do_login(form.username.value, form.username.password, do_list_hosts);
  } else {
    do_list_hosts();
  }
}

function login(form) {
  do_login(form.username.value, form.password.value);
}

function extract_auth_token(message) {
  var lines = message.split('\n');
  for (var i = 0; i < lines.length; i++) {
    if (lines[i].match('^Auth=.*')) {
      return lines[i].split('=')[1];
    }
  }

  console.log('Could not parse auth token in : "' + message + '"');
  return 'bad_token';
}

function do_gaia_login(username, password, service, done) {
  var xhr = new XMLHttpRequest();
  xhr.open('POST', 'https://www.google.com/accounts/ClientLogin', true);
  xhr.onreadystatechange = function() {
    if (xhr.readyState != 4) {
      return;
    }
    if (xhr.status = 200) {
      done(extract_auth_token(xhr.responseText));
    } else {
      console.log('Bad status on auth: ' + xhr.statusText);
    }
  };

  xhr.setRequestHeader('Content-type', 'application/x-www-form-urlencoded');
  xhr.send('accountType=HOSTED_OR_GOOGLE&Email=' + username + '&Passwd=' + password + '&service=' + service + '&source=chromoclient');
}

function do_login(username, password, done) {
  var count = 2;
  var barrier = function() {
    count--;
    if (done && count == 0) {
      done();
    }
  }
  set_cookie('username', username, 100);
  do_gaia_login(username, password, 'chromoting',
                function(token1) {
                  set_cookie('chromoting_auth', token1, 100);
                  document.getElementById('chromoting_auth').value = token1;
                  barrier();
                });
  do_gaia_login(username, password, 'chromiumsync',
                function(token) {
                  set_cookie('xmpp_auth', token, 100);
                  document.getElementById('xmpp_auth').value = token;
                  barrier();
                });
}

function do_list_hosts() {
  var xhr = new XMLHttpRequest();
  var token = get_cookie('chromoting_auth');

  // Unhide host list.
  var hostlist_div = document.getElementById('hostlist_div');
  hostlist_div.style.display = "block";

  xhr.onreadystatechange = function() {
    if (xhr.readyState == 1) {
      hostlist_div.appendChild(document.createTextNode('Finding..'));
      hostlist_div.appendChild(document.createElement('br'));
    }
    if (xhr.readyState != 4) {
      return;
    }
    if (xhr.status == 200) {
      parsed_response = JSON.parse(xhr.responseText);
      hostlist_div.appendChild(document.createTextNode('--Found Hosts--'));
      hostlist_div.appendChild(document.createElement('br'));
      append_host_links(parsed_response.data.items);
    } else {
      console.log('bad status on host list query: "' + xhr.status + ' ' + xhr.statusText);
      hostlist_div.appendChild(document.createTextNode('!! Failed !!.  :\'('));
    }
  };

  xhr.open('GET', 'http://www-googleapis-test.sandbox.google.com/chromoting/v1/@me/hosts');
  xhr.setRequestHeader('Content-Type', 'text/plain;charset=UTF-8');
  xhr.setRequestHeader('Authorization', 'GoogleLogin auth=' + token);
  xhr.send(null);
}

function append_host_links(hostlist) {
// A host link entry should look like:
// - Host: <a onclick="open_chromoting_tab(host_jid); return false;">NAME (JID)</a> <br />
  var host;
  var host_link;
  var hostlist_div = document.getElementById('hostlist_div');

  // Add the hosts.
  for(var i = 0; i < hostlist.length; ++i) {
    hostlist_div.appendChild(document.createTextNode('-*- Host: '));
    host = hostlist[i];
    host_link = document.createElement('a');
    // TODO(ajwong): Reenable once we figure out how to control a new tab.
    host_link.setAttribute('onclick', 'open_chromoting_tab(\'' + host.jabberId + '\'); return false;');
    host_link.setAttribute('href', 'javascript:void(0)');
    host_link.appendChild(document.createTextNode(host.hostName + ' (' + host.hostId  + ', ' + host.jabberId + ')'));
    hostlist_div.appendChild(host_link);
    hostlist_div.appendChild(document.createElement('br'));
  }
}

// Cookie reading code taken from quirksmode with modification for escaping.
//   http://www.quirksmode.org/js/cookies.html
function set_cookie(name,value,days) {
  if (days) {
    var date = new Date();
    date.setTime(date.getTime()+(days*24*60*60*1000));
    var expires = "; expires="+date.toGMTString();
  }
  else var expires = "";
  document.cookie = name+"="+escape(value)+expires+"; path=/";
}

function get_cookie(name) {
  var nameEQ = name + "=";
  var ca = document.cookie.split(';');
  for(var i=0;i < ca.length;i++) {
    var c = ca[i];
    while (c.charAt(0)==' ') c = c.substring(1,c.length);
    if (c.indexOf(nameEQ) == 0) return unescape(c.substring(nameEQ.length,c.length));
  }
  return null;
}

function set_auth_cookies(form) {
  var now = new Date();
  now.setTime(now.getTime() + 1000 * 60 * 60 * 24 * 365)

  create_cookie('xmpp_auth', form.xmpp_auth.value, 100);
  create_cookie('chromoting_auth', form.chromoting_auth.value, 100);
}

function connect(form) {
  open_chromoting_tab(form.host_jid.value);
}

function debug_output(message) {
  var debug_div = document.getElementById('debug_div');
  debug_div.appendChild(document.createTextNode(message));
  debug_div.appendChild(document.createElement('br'));
}

function open_chromoting_tab(host_jid) {
  var username = get_cookie('username');
  var xmpp_auth = get_cookie('xmpp_auth');
  var new_tab_url = chrome.extension.getURL("chromoting_tab.html");
  var request = {
    username: get_cookie('username'),
    xmpp_auth: get_cookie('xmpp_auth'),
    host_jid: host_jid,
  };
  var tab_args = {
    url: new_tab_url,
  };

  console.log("Attempt to connect with " +
              "username='" + request.username + "'" +
              " host_jid='" + request.host_jid + "'" +
              " auth_token='" + request.xmpp_auth + "'");

  chrome.tabs.create(tab_args, function(tab) {
    console.log("We're trying now to send to " + tab.id);
    // TODO(ajwong): This request does not always seem to make it through.
    // I think there's a race condition sending to the view. Figure out how
    // to correctly synchronize this call.
    chrome.tabs.sendRequest(
      tab.id, request,
      function() {console.log('Tab finished conenct.')});
  });
}