diff --git a/extension/background.html b/extension/background.html index a30aeac..cc20f2a 100644 --- a/extension/background.html +++ b/extension/background.html @@ -38,7 +38,7 @@ function onClickHandler(info, tab) { if (status == STATUS_LOGIN_REQUIRED) { // user will have to click the link again // TODO: encode the parameters, re-do the post after login - // or TODO: display the 'loigin required' message first, if regToken is null + // or TODO: display the 'login required' message first, if regToken is null chrome.tabs.create({url: signInUrl}); } }); diff --git a/extension/help.html b/extension/help.html index 568f59e..00b513b 100644 --- a/extension/help.html +++ b/extension/help.html @@ -17,8 +17,9 @@ Google Chrome to Phone Extension + + + + + + + + + + + + + Redirecting... + + + diff --git a/extension/popup.html b/extension/popup.html index b5b70c3..2ed29bd 100644 --- a/extension/popup.html +++ b/extension/popup.html @@ -38,20 +38,20 @@ function loadHandler() { document.getElementById('help').innerHTML = chrome.i18n.getMessage('help_message'); if (oauth.hasToken()) { - document.getElementById('msg').innerHTML = chrome.i18n.getMessage('sending_message'); - document.getElementById('signout').innerHTML = chrome.i18n.getMessage('sign_out_message'); + document.getElementById('msg').innerHTML = chrome.i18n.getMessage('sending_message'); + document.getElementById('signout').innerHTML = chrome.i18n.getMessage('sign_out_message'); - chrome.tabs.getSelected(null, function(tab) { - if (tab.url.indexOf('http:') == 0 || - tab.url.indexOf('https:') == 0) { - chrome.tabs.executeScript(null, {file: "content_script.js"}); - } else { - document.getElementById('msg').innerHTML = chrome.i18n.getMessage('invalid_scheme_message'); - } - }); + chrome.tabs.getSelected(null, function(tab) { + if (tab.url.indexOf('http:') == 0 || + tab.url.indexOf('https:') == 0) { + chrome.tabs.executeScript(null, {file: "content_script.js"}); + } else { + document.getElementById('msg').innerHTML = chrome.i18n.getMessage('invalid_scheme_message'); + } + }); } else { - // we need the options page to show signin - activateSignInLink("chrome.tabs.create({url: 'help.html?fromPopup=1'})"); + // we need the options page to show signin + activateSignInLink("chrome.tabs.create({url: 'oauth_interstitial.html'})"); } } @@ -60,7 +60,7 @@ function sendToPhoneListener(status, responseText) { document.getElementById('msg').innerHTML = chrome.i18n.getMessage('sent_message'); activateSignOutLink(); } else if (status == STATUS_LOGIN_REQUIRED) { - activateSignInLink("chrome.tabs.create({url: 'help.html?fromPopup=1'})"); // token revoked + activateSignInLink("chrome.tabs.create({url: 'help.html?fromPopup=1'})"); // token revoked } else if (status == STATUS_DEVICE_NOT_REGISTERED) { document.getElementById('msg').innerHTML = chrome.i18n.getMessage('device_not_registered_message'); activateSignOutLink(); @@ -80,6 +80,32 @@ chrome.extension.onConnect.addListener(function(port) { }); }); +function setSignOutVisibility(visible) { + var signOutLink = document.getElementById('signout'); + signOutLink.style.visibility = visible ? 'visible' : 'hidden'; + var sep = document.getElementById('sep'); + sep.style.visibility = visible ? 'visible' : 'hidden'; +} + +function activateSignOutLink() { + setSignOutVisibility(true); + var signOutLink = document.getElementById('signout'); + signOutLink.innerHTML = chrome.i18n.getMessage('sign_out_message'); + signOutLink.style.color = 'blue'; + signOutLink.onclick = function() { + chrome.extension.getBackgroundPage().closeBrowserChannel(); + oauth.clearTokens(); + window.close(); + } +} + +function activateSignInLink(onclick) { + var link = '' + + chrome.i18n.getMessage('sign_in_message') + ''; + document.getElementById('msg').innerHTML = + chrome.i18n.getMessage('sign_in_required_message', link); + setSignOutVisibility(false); +} diff --git a/extension/send_logic.js b/extension/send_logic.js index ee9b2b7..0523c37 100644 --- a/extension/send_logic.js +++ b/extension/send_logic.js @@ -26,10 +26,10 @@ if (deviceRegistrationId == undefined || deviceRegistrationId == null) { // use javascript console var host = localStorage['c2dmHost']; if (host == undefined) { - // This won't work very well, there is a cert validation issue (cert - // is for *.appspot.com ), workaround is to open the URL in the browser - // and accept the cert warnings. - host = "9.chrometophone.appspot.com"; + // This won't work very well, there is a cert validation issue (cert + // is for *.appspot.com ), workaround is to open the URL in the browser + // and accept the cert warnings. + host = "9.chrometophone.appspot.com"; } var baseUrl = 'https://' + host; var sendUrl = baseUrl + '/send?ver=' + apiVersion; @@ -51,147 +51,119 @@ var oauth = ChromeExOAuth.initBackgroundPage({ 'app_name' : 'Chrome To Phone' }); - var channel; var socket; +var socketCloseRequested; function sendToPhone(title, url, msgType, selection, listener) { - if (oauth.hasToken()) { - // OAuth1 and url-encoded is a nightmare ( well, Oauth1 is a nightmare in all cases, - // this is worse ) - var params = { - "title": title, - "url": url, - "sel": selection, - "type": msgType, - "deviceType":"ac2dm", - "debug": "1", - "token": localStorage['deviceRegistrationId'] - }; - // no longer passing device name - this may be customized - var data = JSON.stringify(params); - oauth.sendSignedRequest(baseUrl + "/send", function(responseText, req) { - if (req.status == 200) { - var body = req.responseText; - if (body.indexOf('OK') == 0) { - listener(STATUS_SUCCESS, ""); - } else if (body.indexOf('LOGIN_REQUIRED') == 0) { - listener(STATUS_LOGIN_REQUIRED, responseText); - } else if (body.indexOf('DEVICE_NOT_REGISTERED') == 0) { - listener(STATUS_DEVICE_NOT_REGISTERED, responseText); - } - } else { - listener(STATUS_GENERAL_ERROR, responseText); - } - }, { - 'method': 'POST', - 'body': data, - 'headers': { - 'X-Same-Domain': 'true', - 'Content-Type': 'application/json' - } - }); - return; + if (oauth.hasToken()) { + var params = { + "title": title, + "url": url, + "sel": selection, + "type": msgType, + "deviceType":"ac2dm", + "debug": "1", + "token": localStorage['deviceRegistrationId'] + }; + + // No longer passing device name - this may be customized + var data = JSON.stringify(params); + oauth.sendSignedRequest(baseUrl + "/send", function(responseText, req) { + if (req.status == 200) { + var body = req.responseText; + if (body.indexOf('OK') == 0) { + listener(STATUS_SUCCESS, ""); + } else if (body.indexOf('LOGIN_REQUIRED') == 0) { + listener(STATUS_LOGIN_REQUIRED, responseText); + } else if (body.indexOf('DEVICE_NOT_REGISTERED') == 0) { + listener(STATUS_DEVICE_NOT_REGISTERED, responseText); + } + } else { + listener(STATUS_GENERAL_ERROR, responseText); + } + }, { + 'method': 'POST', + 'body': data, + 'headers': { + 'X-Same-Domain': 'true', + 'Content-Type': 'application/json' + } + }); + return; } else { - listener(STATUS_LOGIN_REQUIRED, "Login required"); + listener(STATUS_LOGIN_REQUIRED, "Login required"); } } -function initializeBrowserChannel() { +function initializeBrowserChannel(callback) { if (!oauth.hasToken()) { - console.log('registration required for initializeBrowserChannel'); - return; + console.log('Login required for initializeBrowserChannel'); + return; } + console.log('Initializing browser channel'); + socketCloseRequested = false; var params = { - "devregid": deviceRegistrationId, - "deviceId": deviceRegistrationId, - "ver": apiVersion, - "deviceType": "chrome", - "debug":"1", - "deviceName":"Chrome" + "devregid": deviceRegistrationId, + "deviceId": deviceRegistrationId, + "ver": apiVersion, + "deviceType": "chrome", + "debug":"1", + "deviceName":"Chrome" }; var data = JSON.stringify(params); oauth.sendSignedRequest(baseUrl + "/register", function(responseText, req) { - if (req.status == 200) { - var channelId = req.responseText.substring(3).trim(); // expect 'OK '; - channel = new goog.appengine.Channel(channelId); - console.log('Attempting to open ' + channelId); - socket = channel.open(); - socket.onopen = function() { - console.log('Browser channel initialized'); - } - socket.onclose = function() { - console.log('Browser channel closed'); - setTimeout('initializeBrowserChannel()', 0); - } - socket.onerror = function(error) { - if (error.code == 401) { // token expiry - console.log('Browser channel token expired - reconnecting'); - } else { - console.log('Browser channel error'); - // Automatically reconnects - } - } - socket.onmessage = function(evt) { - console.log("Onmessage " + evt.data); - var url = unescape(evt.data); - var regex = /http[s]?:\/\//; - if (regex.test(url)) { - chrome.tabs.create({url: url}) - } - } - } else if (req.status == 400) { - if (req.responseText.indexOf('LOGIN_REQUIRED') == 0) { - console.log('Not initializing browser channel because user not logged in'); - } else if (req.responseText.indexOf('NOT_ENABLED') == 0) { - console.log('Not initializing browser channel because feature not enabled for user'); - } - } - }, { - 'method': 'POST', - 'body': data, - 'headers': { - 'X-Same-Domain': 'true', - 'Content-Type': 'application/json' - } + if (req.status == 200) { + var channelId = req.responseText.substring(3).trim(); // expect 'OK '; + channel = new goog.appengine.Channel(channelId); + console.log('Attempting to open ' + channelId); + socket = channel.open(); + socket.onopen = function() { + console.log('Browser channel initialized'); + } + socket.onclose = function() { + console.log('Browser channel closed'); + if (!socketCloseRequested) { + setTimeout('initializeBrowserChannel()', 0); + } + } + socket.onerror = function(error) { + if (error.code == 401) { // token expiry + console.log('Browser channel token expired - reconnecting'); + } else { + console.log('Browser channel error'); + // Automatically reconnects + } + } + socket.onmessage = function(evt) { + console.log("Onmessage " + evt.data); + var url = unescape(evt.data); + var regex = /http[s]?:\/\//; + if (regex.test(url)) { + chrome.tabs.create({url: url}) + } + } + } else if (req.status == 400) { + if (req.responseText.indexOf('LOGIN_REQUIRED') == 0) { + console.log('Not initializing browser channel because user not logged in'); + } else if (req.responseText.indexOf('NOT_ENABLED') == 0) { + console.log('Not initializing browser channel because feature not enabled for user'); + } + } + }, { + 'method': 'POST', + 'body': data, + 'headers': { + 'X-Same-Domain': 'true', + 'Content-Type': 'application/json' + } }); } -// Callback from oauth - we can now register the chrome channel -function oauthGotTokenCallback(token, secret) { - initializeBrowserChannel(); -} - - -function setSignOutVisibility(visible) { - var signOutLink = document.getElementById('signout'); - signOutLink.style.visibility = visible ? 'visible' : 'hidden'; - var sep = document.getElementById('sep'); - if (sep != null) { - sep.style.visibility = visible ? 'visible' : 'hidden'; - } -} - -function activateSignOutLink() { - setSignOutVisibility(true); - var signOutLink = document.getElementById('signout'); - signOutLink.innerHTML = chrome.i18n.getMessage('sign_out_message'); - signOutLink.style.color = 'blue'; - signOutLink.onclick = function() { - oauth.clearTokens(); - chrome.tabs.create({url: 'help.html'}); - window.close(); - } -} - -function activateSignInLink(onclick) { - var link = '' + - chrome.i18n.getMessage('sign_in_message') + ''; - document.getElementById('msg').innerHTML = - chrome.i18n.getMessage('sign_in_required_message', link); - setSignOutVisibility(false); - +function closeBrowserChannel() { + socketCloseRequested = true; + socket.close(); }