mirror of
https://github.com/fergalmoran/chrometophone.git
synced 2025-12-22 09:41:51 +00:00
- Fix race condition bug in getting access token.
- Cosmetic improvements to flow. - Make sure browser channel starts and stops properly. - s/tabs/2-spaces.
This commit is contained in:
@@ -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});
|
||||
}
|
||||
});
|
||||
|
||||
@@ -17,8 +17,9 @@
|
||||
<html>
|
||||
<head>
|
||||
<title>Google Chrome to Phone Extension</title>
|
||||
|
||||
<style type="text/css">
|
||||
body,td {
|
||||
body, td {
|
||||
min-width: 320px;
|
||||
overflow-x: hidden;
|
||||
font-family: verdana;
|
||||
@@ -34,35 +35,21 @@ body,td {
|
||||
|
||||
<script type="text/javascript">
|
||||
function loadHandler() {
|
||||
// We may be called directly, as options, or as result of a
|
||||
// redirect from OAuth1 flow
|
||||
var params = ChromeExOAuth.getQueryStringParams();
|
||||
if (params['chromeexoauthcallback'] == 'true') {
|
||||
// End of the oauth flow, convert access token with refresh one
|
||||
oauth.initOAuthFlow(oauthGotTokenCallback);
|
||||
document.getElementById('signed_in_div').innerHTML = '<p><b><font color="#00A000">' +
|
||||
chrome.i18n.getMessage('signed_in_message') + '</font></b></p>';
|
||||
}
|
||||
if (oauth.hasToken()) {
|
||||
activateSignOutLink();
|
||||
} else {
|
||||
if (params['fromPopup'] == '1') {
|
||||
// Popup clicked 'login'
|
||||
oauth.initOAuthFlow(oauthGotTokenCallback);
|
||||
return;
|
||||
}
|
||||
activateSignInLink("oauth.initOAuthFlow(oauthGotTokenCallback)");
|
||||
}
|
||||
if (oauth.hasToken()) {
|
||||
document.getElementById('sign_in_out_div').innerHTML =
|
||||
'<a href="help.html" onclick="chrome.extension.getBackgroundPage().closeBrowserChannel(); oauth.clearTokens()">' + chrome.i18n.getMessage('sign_out_message');
|
||||
} else {
|
||||
document.getElementById('sign_in_out_div').innerHTML =
|
||||
'<a href="oauth_interstitial.html">' + chrome.i18n.getMessage('sign_in_message');;
|
||||
}
|
||||
}
|
||||
</script>
|
||||
</head>
|
||||
|
||||
<body onload="loadHandler()">
|
||||
<!-- Signin or signout link ( same as in popup.html ) -->
|
||||
<div id="signed_in_div"></div>
|
||||
<div id="sign_in_out_div"></div>
|
||||
<b><div id="msg"></div></b><a id="signout" href="#" style="color: gray;"></a></p>
|
||||
<h1><img src="icon_128.png" width="64" height="64" valign="bottom"> Google Chrome to Phone Extension</h1>
|
||||
<br>
|
||||
|
||||
<h2 style="padding-left: 10px"><script>document.write(chrome.i18n.getMessage('about_title_message'));</script></h2>
|
||||
<p style="padding-left: 10px"><script>document.write(chrome.i18n.getMessage('about_message'));</script></p>
|
||||
|
||||
47
extension/oauth_interstitial.html
Normal file
47
extension/oauth_interstitial.html
Normal file
@@ -0,0 +1,47 @@
|
||||
<html>
|
||||
<head>
|
||||
|
||||
<title>OAuth Redirect Page</title>
|
||||
|
||||
<style type="text/css">
|
||||
body {
|
||||
min-width: 320px;
|
||||
overflow-x: hidden;
|
||||
font-family: verdana;
|
||||
font-size: 12px;
|
||||
color: black;
|
||||
background-color: white;
|
||||
}
|
||||
</style>
|
||||
|
||||
<script src="http://chrometophone.appspot.com/_ah/channel/jsapi"></script>
|
||||
<script src="chrome_ex_oauthsimple.js"></script>
|
||||
<script src="chrome_ex_oauth.js"></script>
|
||||
<script src="send_logic.js"></script>
|
||||
|
||||
<script type="text/javascript">
|
||||
function loadHandler() {
|
||||
// We may be called directly, as options, or as result of a
|
||||
// redirect from OAuth1 flow
|
||||
var params = ChromeExOAuth.getQueryStringParams();
|
||||
if (params['chromeexoauthcallback'] == 'true') {
|
||||
// End of the oauth request flow, get access token
|
||||
oauth.initOAuthFlow(function(token, secret) {
|
||||
chrome.extension.getBackgroundPage().initializeBrowserChannel();
|
||||
window.location = 'help.html';
|
||||
});
|
||||
} else {
|
||||
oauth.initOAuthFlow(function(token, secret) {
|
||||
chrome.extension.getBackgroundPage().initializeBrowserChannel();
|
||||
});
|
||||
}
|
||||
}
|
||||
</script>
|
||||
|
||||
</head>
|
||||
|
||||
<body onload="loadHandler();">
|
||||
Redirecting...
|
||||
</body>
|
||||
|
||||
</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 = '<a href="#" onclick="' + onclick + '">' +
|
||||
chrome.i18n.getMessage('sign_in_message') + '</a>';
|
||||
document.getElementById('msg').innerHTML =
|
||||
chrome.i18n.getMessage('sign_in_required_message', link);
|
||||
setSignOutVisibility(false);
|
||||
}
|
||||
|
||||
</script>
|
||||
|
||||
|
||||
@@ -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 <id>';
|
||||
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 <id>';
|
||||
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 = '<a href="#" onclick="' + onclick + '">' +
|
||||
chrome.i18n.getMessage('sign_in_message') + '</a>';
|
||||
document.getElementById('msg').innerHTML =
|
||||
chrome.i18n.getMessage('sign_in_required_message', link);
|
||||
setSignOutVisibility(false);
|
||||
|
||||
function closeBrowserChannel() {
|
||||
socketCloseRequested = true;
|
||||
socket.close();
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user