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) {
|
if (status == STATUS_LOGIN_REQUIRED) {
|
||||||
// user will have to click the link again
|
// user will have to click the link again
|
||||||
// TODO: encode the parameters, re-do the post after login
|
// 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});
|
chrome.tabs.create({url: signInUrl});
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|||||||
@@ -17,8 +17,9 @@
|
|||||||
<html>
|
<html>
|
||||||
<head>
|
<head>
|
||||||
<title>Google Chrome to Phone Extension</title>
|
<title>Google Chrome to Phone Extension</title>
|
||||||
|
|
||||||
<style type="text/css">
|
<style type="text/css">
|
||||||
body,td {
|
body, td {
|
||||||
min-width: 320px;
|
min-width: 320px;
|
||||||
overflow-x: hidden;
|
overflow-x: hidden;
|
||||||
font-family: verdana;
|
font-family: verdana;
|
||||||
@@ -34,35 +35,21 @@ body,td {
|
|||||||
|
|
||||||
<script type="text/javascript">
|
<script type="text/javascript">
|
||||||
function loadHandler() {
|
function loadHandler() {
|
||||||
// We may be called directly, as options, or as result of a
|
if (oauth.hasToken()) {
|
||||||
// redirect from OAuth1 flow
|
document.getElementById('sign_in_out_div').innerHTML =
|
||||||
var params = ChromeExOAuth.getQueryStringParams();
|
'<a href="help.html" onclick="chrome.extension.getBackgroundPage().closeBrowserChannel(); oauth.clearTokens()">' + chrome.i18n.getMessage('sign_out_message');
|
||||||
if (params['chromeexoauthcallback'] == 'true') {
|
} else {
|
||||||
// End of the oauth flow, convert access token with refresh one
|
document.getElementById('sign_in_out_div').innerHTML =
|
||||||
oauth.initOAuthFlow(oauthGotTokenCallback);
|
'<a href="oauth_interstitial.html">' + chrome.i18n.getMessage('sign_in_message');;
|
||||||
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)");
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
</script>
|
</script>
|
||||||
</head>
|
</head>
|
||||||
|
|
||||||
<body onload="loadHandler()">
|
<body onload="loadHandler()">
|
||||||
<!-- Signin or signout link ( same as in popup.html ) -->
|
<div id="sign_in_out_div"></div>
|
||||||
<div id="signed_in_div"></div>
|
|
||||||
<b><div id="msg"></div></b><a id="signout" href="#" style="color: gray;"></a></p>
|
<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>
|
<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>
|
<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>
|
<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');
|
document.getElementById('help').innerHTML = chrome.i18n.getMessage('help_message');
|
||||||
|
|
||||||
if (oauth.hasToken()) {
|
if (oauth.hasToken()) {
|
||||||
document.getElementById('msg').innerHTML = chrome.i18n.getMessage('sending_message');
|
document.getElementById('msg').innerHTML = chrome.i18n.getMessage('sending_message');
|
||||||
document.getElementById('signout').innerHTML = chrome.i18n.getMessage('sign_out_message');
|
document.getElementById('signout').innerHTML = chrome.i18n.getMessage('sign_out_message');
|
||||||
|
|
||||||
chrome.tabs.getSelected(null, function(tab) {
|
chrome.tabs.getSelected(null, function(tab) {
|
||||||
if (tab.url.indexOf('http:') == 0 ||
|
if (tab.url.indexOf('http:') == 0 ||
|
||||||
tab.url.indexOf('https:') == 0) {
|
tab.url.indexOf('https:') == 0) {
|
||||||
chrome.tabs.executeScript(null, {file: "content_script.js"});
|
chrome.tabs.executeScript(null, {file: "content_script.js"});
|
||||||
} else {
|
} else {
|
||||||
document.getElementById('msg').innerHTML = chrome.i18n.getMessage('invalid_scheme_message');
|
document.getElementById('msg').innerHTML = chrome.i18n.getMessage('invalid_scheme_message');
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
} else {
|
} else {
|
||||||
// we need the options page to show signin
|
// we need the options page to show signin
|
||||||
activateSignInLink("chrome.tabs.create({url: 'help.html?fromPopup=1'})");
|
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');
|
document.getElementById('msg').innerHTML = chrome.i18n.getMessage('sent_message');
|
||||||
activateSignOutLink();
|
activateSignOutLink();
|
||||||
} else if (status == STATUS_LOGIN_REQUIRED) {
|
} 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) {
|
} else if (status == STATUS_DEVICE_NOT_REGISTERED) {
|
||||||
document.getElementById('msg').innerHTML = chrome.i18n.getMessage('device_not_registered_message');
|
document.getElementById('msg').innerHTML = chrome.i18n.getMessage('device_not_registered_message');
|
||||||
activateSignOutLink();
|
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>
|
</script>
|
||||||
|
|
||||||
|
|||||||
@@ -26,10 +26,10 @@ if (deviceRegistrationId == undefined || deviceRegistrationId == null) {
|
|||||||
// use javascript console
|
// use javascript console
|
||||||
var host = localStorage['c2dmHost'];
|
var host = localStorage['c2dmHost'];
|
||||||
if (host == undefined) {
|
if (host == undefined) {
|
||||||
// This won't work very well, there is a cert validation issue (cert
|
// 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
|
// is for *.appspot.com ), workaround is to open the URL in the browser
|
||||||
// and accept the cert warnings.
|
// and accept the cert warnings.
|
||||||
host = "9.chrometophone.appspot.com";
|
host = "9.chrometophone.appspot.com";
|
||||||
}
|
}
|
||||||
var baseUrl = 'https://' + host;
|
var baseUrl = 'https://' + host;
|
||||||
var sendUrl = baseUrl + '/send?ver=' + apiVersion;
|
var sendUrl = baseUrl + '/send?ver=' + apiVersion;
|
||||||
@@ -51,147 +51,119 @@ var oauth = ChromeExOAuth.initBackgroundPage({
|
|||||||
'app_name' : 'Chrome To Phone'
|
'app_name' : 'Chrome To Phone'
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
||||||
var channel;
|
var channel;
|
||||||
var socket;
|
var socket;
|
||||||
|
var socketCloseRequested;
|
||||||
|
|
||||||
function sendToPhone(title, url, msgType, selection, listener) {
|
function sendToPhone(title, url, msgType, selection, listener) {
|
||||||
if (oauth.hasToken()) {
|
if (oauth.hasToken()) {
|
||||||
// OAuth1 and url-encoded is a nightmare ( well, Oauth1 is a nightmare in all cases,
|
var params = {
|
||||||
// this is worse )
|
"title": title,
|
||||||
var params = {
|
"url": url,
|
||||||
"title": title,
|
"sel": selection,
|
||||||
"url": url,
|
"type": msgType,
|
||||||
"sel": selection,
|
"deviceType":"ac2dm",
|
||||||
"type": msgType,
|
"debug": "1",
|
||||||
"deviceType":"ac2dm",
|
"token": localStorage['deviceRegistrationId']
|
||||||
"debug": "1",
|
};
|
||||||
"token": localStorage['deviceRegistrationId']
|
|
||||||
};
|
// No longer passing device name - this may be customized
|
||||||
// no longer passing device name - this may be customized
|
var data = JSON.stringify(params);
|
||||||
var data = JSON.stringify(params);
|
oauth.sendSignedRequest(baseUrl + "/send", function(responseText, req) {
|
||||||
oauth.sendSignedRequest(baseUrl + "/send", function(responseText, req) {
|
if (req.status == 200) {
|
||||||
if (req.status == 200) {
|
var body = req.responseText;
|
||||||
var body = req.responseText;
|
if (body.indexOf('OK') == 0) {
|
||||||
if (body.indexOf('OK') == 0) {
|
listener(STATUS_SUCCESS, "");
|
||||||
listener(STATUS_SUCCESS, "");
|
} else if (body.indexOf('LOGIN_REQUIRED') == 0) {
|
||||||
} else if (body.indexOf('LOGIN_REQUIRED') == 0) {
|
listener(STATUS_LOGIN_REQUIRED, responseText);
|
||||||
listener(STATUS_LOGIN_REQUIRED, responseText);
|
} else if (body.indexOf('DEVICE_NOT_REGISTERED') == 0) {
|
||||||
} else if (body.indexOf('DEVICE_NOT_REGISTERED') == 0) {
|
listener(STATUS_DEVICE_NOT_REGISTERED, responseText);
|
||||||
listener(STATUS_DEVICE_NOT_REGISTERED, responseText);
|
}
|
||||||
}
|
} else {
|
||||||
} else {
|
listener(STATUS_GENERAL_ERROR, responseText);
|
||||||
listener(STATUS_GENERAL_ERROR, responseText);
|
}
|
||||||
}
|
}, {
|
||||||
}, {
|
'method': 'POST',
|
||||||
'method': 'POST',
|
'body': data,
|
||||||
'body': data,
|
'headers': {
|
||||||
'headers': {
|
'X-Same-Domain': 'true',
|
||||||
'X-Same-Domain': 'true',
|
'Content-Type': 'application/json'
|
||||||
'Content-Type': 'application/json'
|
}
|
||||||
}
|
});
|
||||||
});
|
return;
|
||||||
return;
|
|
||||||
} else {
|
} else {
|
||||||
listener(STATUS_LOGIN_REQUIRED, "Login required");
|
listener(STATUS_LOGIN_REQUIRED, "Login required");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
function initializeBrowserChannel() {
|
function initializeBrowserChannel(callback) {
|
||||||
if (!oauth.hasToken()) {
|
if (!oauth.hasToken()) {
|
||||||
console.log('registration required for initializeBrowserChannel');
|
console.log('Login required for initializeBrowserChannel');
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
console.log('Initializing browser channel');
|
console.log('Initializing browser channel');
|
||||||
|
socketCloseRequested = false;
|
||||||
var params = {
|
var params = {
|
||||||
"devregid": deviceRegistrationId,
|
"devregid": deviceRegistrationId,
|
||||||
"deviceId": deviceRegistrationId,
|
"deviceId": deviceRegistrationId,
|
||||||
"ver": apiVersion,
|
"ver": apiVersion,
|
||||||
"deviceType": "chrome",
|
"deviceType": "chrome",
|
||||||
"debug":"1",
|
"debug":"1",
|
||||||
"deviceName":"Chrome"
|
"deviceName":"Chrome"
|
||||||
};
|
};
|
||||||
var data = JSON.stringify(params);
|
var data = JSON.stringify(params);
|
||||||
|
|
||||||
oauth.sendSignedRequest(baseUrl + "/register", function(responseText, req) {
|
oauth.sendSignedRequest(baseUrl + "/register", function(responseText, req) {
|
||||||
if (req.status == 200) {
|
if (req.status == 200) {
|
||||||
var channelId = req.responseText.substring(3).trim(); // expect 'OK <id>';
|
var channelId = req.responseText.substring(3).trim(); // expect 'OK <id>';
|
||||||
channel = new goog.appengine.Channel(channelId);
|
channel = new goog.appengine.Channel(channelId);
|
||||||
console.log('Attempting to open ' + channelId);
|
console.log('Attempting to open ' + channelId);
|
||||||
socket = channel.open();
|
socket = channel.open();
|
||||||
socket.onopen = function() {
|
socket.onopen = function() {
|
||||||
console.log('Browser channel initialized');
|
console.log('Browser channel initialized');
|
||||||
}
|
}
|
||||||
socket.onclose = function() {
|
socket.onclose = function() {
|
||||||
console.log('Browser channel closed');
|
console.log('Browser channel closed');
|
||||||
setTimeout('initializeBrowserChannel()', 0);
|
if (!socketCloseRequested) {
|
||||||
}
|
setTimeout('initializeBrowserChannel()', 0);
|
||||||
socket.onerror = function(error) {
|
}
|
||||||
if (error.code == 401) { // token expiry
|
}
|
||||||
console.log('Browser channel token expired - reconnecting');
|
socket.onerror = function(error) {
|
||||||
} else {
|
if (error.code == 401) { // token expiry
|
||||||
console.log('Browser channel error');
|
console.log('Browser channel token expired - reconnecting');
|
||||||
// Automatically reconnects
|
} else {
|
||||||
}
|
console.log('Browser channel error');
|
||||||
}
|
// Automatically reconnects
|
||||||
socket.onmessage = function(evt) {
|
}
|
||||||
console.log("Onmessage " + evt.data);
|
}
|
||||||
var url = unescape(evt.data);
|
socket.onmessage = function(evt) {
|
||||||
var regex = /http[s]?:\/\//;
|
console.log("Onmessage " + evt.data);
|
||||||
if (regex.test(url)) {
|
var url = unescape(evt.data);
|
||||||
chrome.tabs.create({url: url})
|
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.status == 400) {
|
||||||
} else if (req.responseText.indexOf('NOT_ENABLED') == 0) {
|
if (req.responseText.indexOf('LOGIN_REQUIRED') == 0) {
|
||||||
console.log('Not initializing browser channel because feature not enabled for user');
|
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': {
|
'method': 'POST',
|
||||||
'X-Same-Domain': 'true',
|
'body': data,
|
||||||
'Content-Type': 'application/json'
|
'headers': {
|
||||||
}
|
'X-Same-Domain': 'true',
|
||||||
|
'Content-Type': 'application/json'
|
||||||
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
// Callback from oauth - we can now register the chrome channel
|
function closeBrowserChannel() {
|
||||||
function oauthGotTokenCallback(token, secret) {
|
socketCloseRequested = true;
|
||||||
initializeBrowserChannel();
|
socket.close();
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
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);
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user