diff options
author | sergeyu@chromium.org <sergeyu@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2010-08-18 18:41:49 +0000 |
---|---|---|
committer | sergeyu@chromium.org <sergeyu@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2010-08-18 18:41:49 +0000 |
commit | e339c5ac1a1625e101ea5724ef441608e45a71c7 (patch) | |
tree | be55d149ec8ddf0d794dbf8b8fd512855a08abd6 /remoting | |
parent | 0755672a1e8bad7e9efd4b932836ab51a25cca18 (diff) | |
download | chromium_src-e339c5ac1a1625e101ea5724ef441608e45a71c7.zip chromium_src-e339c5ac1a1625e101ea5724ef441608e45a71c7.tar.gz chromium_src-e339c5ac1a1625e101ea5724ef441608e45a71c7.tar.bz2 |
Minor fixes for the client extension.
Added background page that passes messages to new tabs. CamelCase for function names.
BUG=51194
TEST=extension still works.
Review URL: http://codereview.chromium.org/3149017
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@56567 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'remoting')
-rw-r--r-- | remoting/client/extension/background.html | 13 | ||||
-rw-r--r-- | remoting/client/extension/background.js | 29 | ||||
-rw-r--r-- | remoting/client/extension/base.js | 29 | ||||
-rw-r--r-- | remoting/client/extension/client.js | 155 | ||||
-rw-r--r-- | remoting/client/extension/manifest.json | 1 | ||||
-rw-r--r-- | remoting/client/extension/popup.html | 7 |
6 files changed, 127 insertions, 107 deletions
diff --git a/remoting/client/extension/background.html b/remoting/client/extension/background.html new file mode 100644 index 0000000..3e17fad --- /dev/null +++ b/remoting/client/extension/background.html @@ -0,0 +1,13 @@ +<!-- +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. +--> + +<html> + <head> + <script type="text/javascript" src="base.js"></script> + <script type="text/javascript" src="background.js"></script> + </head> + <body/> +</html> diff --git a/remoting/client/extension/background.js b/remoting/client/extension/background.js new file mode 100644 index 0000000..9fdf017 --- /dev/null +++ b/remoting/client/extension/background.js @@ -0,0 +1,29 @@ +// 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 openChromotingTab(host_jid) { + var username = getCookie('username'); + var xmpp_auth = getCookie('xmpp_auth'); + var new_tab_url = chrome.extension.getURL("chromoting_tab.html"); + var request = { + username: getCookie('username'), + xmpp_auth: getCookie('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); + chrome.tabs.sendRequest( + tab.id, request, function() { + console.log('Tab finished connect.'); + }); + }); +} diff --git a/remoting/client/extension/base.js b/remoting/client/extension/base.js new file mode 100644 index 0000000..319df94 --- /dev/null +++ b/remoting/client/extension/base.js @@ -0,0 +1,29 @@ +// 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. + +// Cookie reading code taken from quirksmode with modification for escaping. +// http://www.quirksmode.org/js/cookies.html +function setCookie(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 getCookie(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; +} diff --git a/remoting/client/extension/client.js b/remoting/client/extension/client.js index e067bd7..cdedfe3f 100644 --- a/remoting/client/extension/client.js +++ b/remoting/client/extension/client.js @@ -2,55 +2,55 @@ // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. -function init_params() { +function initParams() { 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'); + document.getElementById('xmpp_auth').value = getCookie('xmpp_auth'); + document.getElementById('chromoting_auth').value = getCookie('chromoting_auth'); + document.getElementById('username').value = getCookie('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]); + setCookie('xmpp_auth', hash[1]); } else if (hash[0] == "chromoting_auth") { document.getElementById('chromoting_auth').value = hash[1]; - set_cookie('chromoting_auth', hash[1]); + setCookie('chromoting_auth', hash[1]); } else if (hash[0] == 'username') { document.getElementById('username').value = hash[1]; - set_cookie('username', hash[1]); + setCookie('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) { +function findHosts(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); + if (getCookie('chromoting_auth') == null || + getCookie('xmpp_auth') == null) { + doLogin(form.username.value, form.username.password, doListHosts); } else { - do_list_hosts(); + doListHosts(); } } function login(form) { - do_login(form.username.value, form.password.value); + doLogin(form.username.value, form.password.value); } -function extract_auth_token(message) { +function extractAuthToken(message) { var lines = message.split('\n'); for (var i = 0; i < lines.length; i++) { if (lines[i].match('^Auth=.*')) { @@ -62,7 +62,7 @@ function extract_auth_token(message) { return 'bad_token'; } -function do_gaia_login(username, password, service, done) { +function doGaiaLogin(username, password, service, done) { var xhr = new XMLHttpRequest(); xhr.open('POST', 'https://www.google.com/accounts/ClientLogin', true); xhr.onreadystatechange = function() { @@ -70,7 +70,7 @@ function do_gaia_login(username, password, service, done) { return; } if (xhr.status = 200) { - done(extract_auth_token(xhr.responseText)); + done(extractAuthToken(xhr.responseText)); } else { console.log('Bad status on auth: ' + xhr.statusText); } @@ -80,7 +80,7 @@ function do_gaia_login(username, password, service, done) { xhr.send('accountType=HOSTED_OR_GOOGLE&Email=' + username + '&Passwd=' + password + '&service=' + service + '&source=chromoclient'); } -function do_login(username, password, done) { +function doLogin(username, password, done) { var count = 2; var barrier = function() { count--; @@ -88,24 +88,24 @@ function do_login(username, password, done) { 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(); - }); + setCookie('username', username, 100); + doGaiaLogin(username, password, 'chromoting', + function(token1) { + setCookie('chromoting_auth', token1, 100); + document.getElementById('chromoting_auth').value = token1; + barrier(); + }); + doGaiaLogin(username, password, 'chromiumsync', + function(token) { + setCookie('xmpp_auth', token, 100); + document.getElementById('xmpp_auth').value = token; + barrier(); + }); } -function do_list_hosts() { +function doListHosts() { var xhr = new XMLHttpRequest(); - var token = get_cookie('chromoting_auth'); + var token = getCookie('chromoting_auth'); // Unhide host list. var hostlist_div = document.getElementById('hostlist_div'); @@ -123,7 +123,7 @@ function do_list_hosts() { 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); + appendHostLinks(parsed_response.data.items); } else { console.log('bad status on host list query: "' + xhr.status + ' ' + xhr.statusText); hostlist_div.appendChild(document.createTextNode('!! Failed !!. :\'(')); @@ -136,99 +136,46 @@ function do_list_hosts() { xhr.send(null); } -function append_host_links(hostlist) { +function appendHostLinks(hostlist) { // A host link entry should look like: -// - Host: <a onclick="open_chromoting_tab(host_jid); return false;">NAME (JID)</a> <br /> +// - Host: <a onclick="openChromotingTab(host_jid); return false;">NAME (JID)</a> <br /> var host; var host_link; var hostlist_div = document.getElementById('hostlist_div'); + // Cleanup the div + hostlist_div.innerHTML = ""; + // 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('onclick', 'openChromotingTab(\'' + host.jabberId + + '\'); return false;'); host_link.setAttribute('href', 'javascript:void(0)'); - host_link.appendChild(document.createTextNode(host.hostName + ' (' + host.hostId + ', ' + host.jabberId + ')')); + 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 connect(form) { + openChromotingTab(form.host_jid); } -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 openChromotingTab(host_jid) { + var background = chrome.extension.getBackgroundPage(); + background.openChromotingTab(host_jid); } -function set_auth_cookies(form) { +function setAuthCookies(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, - selected: false, - }; - - console.log("Attempt to connect with " + - "username='" + request.username + "'" + - " host_jid='" + request.host_jid + "'" + - " auth_token='" + request.xmpp_auth + "'"); - - // TODO(sergeyu): Currently we open a new tab, send a message and only after - // that select the new tab. This is neccessary because chrome closes the - // popup the moment the new tab is selected, and so we fail to send the - // message if the tab is selected when it is created. There is visible delay - // when opening a new tab, so it is necessary to pass the message somehow - // differently. Figure out how. - chrome.tabs.create(tab_args, 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.'); - chrome.tabs.update(tab.id, {selected: true}); - }); - }); + setCookie('xmpp_auth', form.xmpp_auth.value, 100); + setCookie('chromoting_auth', form.chromoting_auth.value, 100); } diff --git a/remoting/client/extension/manifest.json b/remoting/client/extension/manifest.json index 8492ffe..6dc9de5 100644 --- a/remoting/client/extension/manifest.json +++ b/remoting/client/extension/manifest.json @@ -6,6 +6,7 @@ "default_icon": "icon.png", "popup": "popup.html" }, + "background_page": "background.html", "permissions": [ "tabs", "http://www-googleapis-test.sandbox.google.com/", diff --git a/remoting/client/extension/popup.html b/remoting/client/extension/popup.html index 7b2bab9..a4162c5 100644 --- a/remoting/client/extension/popup.html +++ b/remoting/client/extension/popup.html @@ -6,10 +6,11 @@ found in the LICENSE file. <html> <head> + <script type="text/javascript" src="base.js"></script> <script type="text/javascript" src="client.js"></script> <title>Get hosts</title> </head> - <body onload="init_params();" style="width:250;"> + <body onload="initParams();" style="width:250;"> <div style="border: blue 1px dotted;"> <form name="hostqueryform" action="" method="GET"> @@ -25,7 +26,7 @@ found in the LICENSE file. <form name="connectparamsform" action="" method="GET"> chromoting: <input type="text" name="chromoting_auth" id="chromoting_auth" value="" /> <br /> xmpp: <input type="text" name="xmpp_auth" id="xmpp_auth" value="" /> - <input type="button" name="set_cookie_button" value="Set Auth Cookies" onclick="set_auth_cookies(this.form)" /> + <input type="button" name="set_cookie_button" value="Set Auth Cookies" onclick="setAuthCookies(this.form)" /> </form> </div> @@ -34,7 +35,7 @@ found in the LICENSE file. Conenct directly to host using auth cookies. Find Hosts doesn't require the host_jid to be filled out. <br /> host_jid: <input type="text" name="host_jid" id="host_jid" value="" /> <br /> <input type="button" name="connect_button" value="Connect" onclick="connect(this.form)" /> - <input type="button" name="find_host_button" value="Find Hosts" onclick="find_hosts(this.form)" /> + <input type="button" name="find_host_button" value="Find Hosts" onclick="findHosts(this.form)" /> </form> </div> |