Commit a59c430f authored by garykac's avatar garykac Committed by Commit bot

[Chromoting] Move app-specific code out of remoting.js

After this cl, the last remaining reference to remoting.AppMode in AppRemoting is in app_remoting.js. That will be addressed in a followup cl.

Move the following out of remoting.js and into app-specific locations:
* remoting.signOut (into crd_event_handlers.js)
* remoting.showErrorMessage (into desktop_remoting.js)
* isWindowed_ (into desktop_remoting.js)
* remoting.promptClose (into desktop_remoting.js)
* remoting.isMe2MeInstallable (into local_host_section.js)

Rename Delegate.signInFailed -> Delegate.handleAuthError
Add Application.onAuthError to call Delegate.handleAuthError

Change references to global remoting.showErrorMessage to remoting.app.onAuthError

BUG=

Review URL: https://codereview.chromium.org/1020743002

Cr-Commit-Position: refs/heads/master@{#321502}
parent f1df20df
...@@ -84,3 +84,18 @@ remoting.initElementEventHandlers = function() { ...@@ -84,3 +84,18 @@ remoting.initElementEventHandlers = function() {
registerEventListeners(host_actions); registerEventListeners(host_actions);
registerEventListeners(auth_actions); registerEventListeners(auth_actions);
} }
/**
* Sign the user out of Chromoting by clearing (and revoking, if possible) the
* OAuth refresh token.
*
* Also clear all local storage, to avoid leaking information.
*/
remoting.signOut = function() {
remoting.oauth2.removeCachedAuthToken().then(function(){
chrome.storage.local.clear();
remoting.setMode(remoting.AppMode.HOME);
window.location.reload();
});
};
...@@ -17,7 +17,8 @@ remoting.initHostlist_ = function() { ...@@ -17,7 +17,8 @@ remoting.initHostlist_ = function() {
document.getElementById('host-list-empty'), document.getElementById('host-list-empty'),
document.getElementById('host-list-error-message'), document.getElementById('host-list-error-message'),
document.getElementById('host-list-refresh-failed-button'), document.getElementById('host-list-refresh-failed-button'),
document.getElementById('host-list-loading-indicator')); document.getElementById('host-list-loading-indicator'),
remoting.showErrorMessage);
isHostModeSupported_().then( isHostModeSupported_().then(
/** @param {Boolean} supported */ /** @param {Boolean} supported */
...@@ -180,6 +181,23 @@ remoting.startDesktopRemotingForTesting = function() { ...@@ -180,6 +181,23 @@ remoting.startDesktopRemotingForTesting = function() {
} }
} }
/**
* @param {!remoting.Error} error The failure reason.
*/
remoting.showErrorMessage = function(error) {
l10n.localizeElementFromTag(
document.getElementById('token-refresh-error-message'),
error.getTag());
var auth_failed = (error.hasTag(remoting.Error.Tag.AUTHENTICATION_FAILED));
if (auth_failed && base.isAppsV2()) {
remoting.handleAuthFailureAndRelaunch();
} else {
document.getElementById('token-refresh-auth-failed').hidden = !auth_failed;
document.getElementById('token-refresh-other-error').hidden = auth_failed;
remoting.setMode(remoting.AppMode.TOKEN_REFRESH_FAILED);
}
};
remoting.startDesktopRemoting = function() { remoting.startDesktopRemoting = function() {
remoting.app = new remoting.Application(remoting.app_capabilities()); remoting.app = new remoting.Application(remoting.app_capabilities());
......
...@@ -81,7 +81,8 @@ remoting.DesktopRemoting.prototype.init = function() { ...@@ -81,7 +81,8 @@ remoting.DesktopRemoting.prototype.init = function() {
document.getElementById('session-toolbar')); document.getElementById('session-toolbar'));
remoting.optionsMenu = remoting.toolbar.createOptionsMenu(); remoting.optionsMenu = remoting.toolbar.createOptionsMenu();
window.addEventListener('beforeunload', remoting.promptClose, false); window.addEventListener('beforeunload',
this.promptClose_.bind(this), false);
window.addEventListener('unload', window.addEventListener('unload',
remoting.app.disconnect.bind(remoting.app), false); remoting.app.disconnect.bind(remoting.app), false);
} }
...@@ -114,7 +115,7 @@ remoting.DesktopRemoting.prototype.init = function() { ...@@ -114,7 +115,7 @@ remoting.DesktopRemoting.prototype.init = function() {
document.getElementById('startup-mode-box-it2me').hidden = false; document.getElementById('startup-mode-box-it2me').hidden = false;
} }
}; };
isWindowed_(onIsWindowed); this.isWindowed_(onIsWindowed);
} }
remoting.ClientPlugin.factory.preloadPlugin(); remoting.ClientPlugin.factory.preloadPlugin();
...@@ -324,6 +325,56 @@ remoting.DesktopRemoting.prototype.handleError = function(error) { ...@@ -324,6 +325,56 @@ remoting.DesktopRemoting.prototype.handleError = function(error) {
remoting.DesktopRemoting.prototype.handleExit = function() { remoting.DesktopRemoting.prototype.handleExit = function() {
}; };
/**
* Determine whether or not the app is running in a window.
* @param {function(boolean):void} callback Callback to receive whether or not
* the current tab is running in windowed mode.
* @private
*/
remoting.DesktopRemoting.prototype.isWindowed_ = function(callback) {
/** @param {chrome.Window} win The current window. */
var windowCallback = function(win) {
callback(win.type == 'popup');
};
/** @param {chrome.Tab} tab The current tab. */
var tabCallback = function(tab) {
if (tab.pinned) {
callback(false);
} else {
chrome.windows.get(tab.windowId, null, windowCallback);
}
};
if (chrome.tabs) {
chrome.tabs.getCurrent(tabCallback);
} else {
console.error('chome.tabs is not available.');
}
}
/**
* If an IT2Me client or host is active then prompt the user before closing.
* If a Me2Me client is active then don't bother, since closing the window is
* the more intuitive way to end a Me2Me session, and re-connecting is easy.
* @private
*/
remoting.DesktopRemoting.prototype.promptClose_ = function() {
var sessionConnector = remoting.app.getSessionConnector();
if (sessionConnector &&
sessionConnector.getConnectionMode() ==
remoting.DesktopConnectedView.Mode.IT2ME) {
switch (remoting.currentMode) {
case remoting.AppMode.CLIENT_CONNECTING:
case remoting.AppMode.HOST_WAITING_FOR_CODE:
case remoting.AppMode.HOST_WAITING_FOR_CONNECTION:
case remoting.AppMode.HOST_SHARED:
case remoting.AppMode.IN_SESSION:
return chrome.i18n.getMessage(/*i18n-content*/'CLOSE_PROMPT');
default:
return null;
}
}
};
/** @returns {remoting.DesktopConnectedView} */ /** @returns {remoting.DesktopConnectedView} */
remoting.DesktopRemoting.prototype.getConnectedViewForTesting = function() { remoting.DesktopRemoting.prototype.getConnectedViewForTesting = function() {
return this.connectedView_; return this.connectedView_;
...@@ -333,4 +384,4 @@ remoting.DesktopRemoting.prototype.getConnectedViewForTesting = function() { ...@@ -333,4 +384,4 @@ remoting.DesktopRemoting.prototype.getConnectedViewForTesting = function() {
* Global instance of remoting.DesktopRemoting used for testing. * Global instance of remoting.DesktopRemoting used for testing.
* @type {remoting.DesktopRemoting} * @type {remoting.DesktopRemoting}
*/ */
remoting.desktopDelegateForTesting = null; remoting.desktopDelegateForTesting = null;
\ No newline at end of file
...@@ -26,9 +26,11 @@ var remoting = remoting || {}; ...@@ -26,9 +26,11 @@ var remoting = remoting || {};
* @param {HTMLElement} loadingIndicator The HTML <span> to update while the * @param {HTMLElement} loadingIndicator The HTML <span> to update while the
* host list is being loaded. The first element of this span should be * host list is being loaded. The first element of this span should be
* the reload button. * the reload button.
* @param {function(!remoting.Error)} onError Function to call when an error
* occurs.
*/ */
remoting.HostList = function(table, noHosts, errorMsg, errorButton, remoting.HostList = function(table, noHosts, errorMsg, errorButton,
loadingIndicator) { loadingIndicator, onError) {
/** @private {Element} */ /** @private {Element} */
this.table_ = table; this.table_ = table;
/** /**
...@@ -43,6 +45,8 @@ remoting.HostList = function(table, noHosts, errorMsg, errorButton, ...@@ -43,6 +45,8 @@ remoting.HostList = function(table, noHosts, errorMsg, errorButton,
this.errorButton_ = errorButton; this.errorButton_ = errorButton;
/** @private {HTMLElement} */ /** @private {HTMLElement} */
this.loadingIndicator_ = loadingIndicator; this.loadingIndicator_ = loadingIndicator;
this.onError_ = onError;
/** @private {Array<remoting.HostTableEntry>} */ /** @private {Array<remoting.HostTableEntry>} */
this.hostTableEntries_ = []; this.hostTableEntries_ = [];
/** @private {Array<remoting.Host>} */ /** @private {Array<remoting.Host>} */
...@@ -53,7 +57,8 @@ remoting.HostList = function(table, noHosts, errorMsg, errorButton, ...@@ -53,7 +57,8 @@ remoting.HostList = function(table, noHosts, errorMsg, errorButton,
this.localHostSection_ = new remoting.LocalHostSection( this.localHostSection_ = new remoting.LocalHostSection(
/** @type {HTMLElement} */ (document.querySelector('.daemon-control')), /** @type {HTMLElement} */ (document.querySelector('.daemon-control')),
new remoting.LocalHostSection.Controller( new remoting.LocalHostSection.Controller(
this, new remoting.HostSetupDialog(remoting.hostController))); this,
new remoting.HostSetupDialog(remoting.hostController, onError)));
/** @private {number} */ /** @private {number} */
this.webappMajorVersion_ = parseInt(chrome.runtime.getManifest().version, 10); this.webappMajorVersion_ = parseInt(chrome.runtime.getManifest().version, 10);
...@@ -295,7 +300,7 @@ remoting.HostList.prototype.deleteHost_ = function(hostTableEntry) { ...@@ -295,7 +300,7 @@ remoting.HostList.prototype.deleteHost_ = function(hostTableEntry) {
this.hostTableEntries_.splice(index, 1); this.hostTableEntries_.splice(index, 1);
} }
remoting.hostListApi.remove(hostTableEntry.host.hostId, base.doNothing, remoting.hostListApi.remove(hostTableEntry.host.hostId, base.doNothing,
remoting.showErrorMessage); this.onError_);
}; };
/** /**
...@@ -316,7 +321,7 @@ remoting.HostList.prototype.renameHost = function(hostTableEntry) { ...@@ -316,7 +321,7 @@ remoting.HostList.prototype.renameHost = function(hostTableEntry) {
hostTableEntry.host.hostName, hostTableEntry.host.hostName,
hostTableEntry.host.publicKey, hostTableEntry.host.publicKey,
function() {}, function() {},
remoting.showErrorMessage); this.onError_);
}; };
/** /**
...@@ -343,7 +348,7 @@ remoting.HostList.prototype.unregisterHostById = function(hostId, opt_onDone) { ...@@ -343,7 +348,7 @@ remoting.HostList.prototype.unregisterHostById = function(hostId, opt_onDone) {
onDone(); onDone();
}); });
}; };
remoting.hostListApi.remove(hostId, onRemoved, remoting.showErrorMessage); remoting.hostListApi.remove(hostId, onRemoved, this.onError_);
}; };
/** /**
......
...@@ -93,11 +93,15 @@ remoting.HostSetupFlow.prototype.switchToErrorState = function(error) { ...@@ -93,11 +93,15 @@ remoting.HostSetupFlow.prototype.switchToErrorState = function(error) {
/** /**
* @param {remoting.HostController} hostController The HostController * @param {remoting.HostController} hostController The HostController
* responsible for the host daemon. * responsible for the host daemon.
* @param {function(!remoting.Error)} onError Function to call when an error
* occurs.
* @constructor * @constructor
*/ */
remoting.HostSetupDialog = function(hostController) { remoting.HostSetupDialog = function(hostController, onError) {
this.hostController_ = hostController; this.hostController_ = hostController;
this.onError_ = onError;
this.pinEntry_ = document.getElementById('daemon-pin-entry'); this.pinEntry_ = document.getElementById('daemon-pin-entry');
this.pinConfirm_ = document.getElementById('daemon-pin-confirm'); this.pinConfirm_ = document.getElementById('daemon-pin-confirm');
this.pinErrorDiv_ = document.getElementById('daemon-pin-error-div'); this.pinErrorDiv_ = document.getElementById('daemon-pin-error-div');
...@@ -165,7 +169,7 @@ remoting.HostSetupDialog.prototype.showForStart = function() { ...@@ -165,7 +169,7 @@ remoting.HostSetupDialog.prototype.showForStart = function() {
// case where the refresh token is invalid. // case where the refresh token is invalid.
remoting.identity.getToken().then( remoting.identity.getToken().then(
that.showForStartWithToken_.bind(that, state), that.showForStartWithToken_.bind(that, state),
remoting.Error.handler(remoting.showErrorMessage)); remoting.Error.handler(that.onError_));
}; };
this.hostController_.getLocalHostState(onState); this.hostController_.getLocalHostState(onState);
......
...@@ -96,7 +96,7 @@ remoting.LocalHostSection.prototype.canChangeState = function() { ...@@ -96,7 +96,7 @@ remoting.LocalHostSection.prototype.canChangeState = function() {
// Return false if the host is uninstallable. The NOT_INSTALLED check is // Return false if the host is uninstallable. The NOT_INSTALLED check is
// required to handle the special case for Ubuntu, as we report the host as // required to handle the special case for Ubuntu, as we report the host as
// uninstallable on Linux. // uninstallable on Linux.
if (!remoting.isMe2MeInstallable() && if (!this.isMe2MeInstallable_() &&
state === remoting.HostController.State.NOT_INSTALLED) { state === remoting.HostController.State.NOT_INSTALLED) {
return false; return false;
} }
...@@ -107,6 +107,24 @@ remoting.LocalHostSection.prototype.canChangeState = function() { ...@@ -107,6 +107,24 @@ remoting.LocalHostSection.prototype.canChangeState = function() {
return this.isEnabled_() || !this.hasError_; return this.isEnabled_() || !this.hasError_;
}; };
/**
* Returns true if the current platform is fully supported. It's only used when
* we detect that host native messaging components are not installed. In that
* case the result of this function determines if the webapp should show the
* controls that allow to install and enable Me2Me host.
*
* @return {boolean}
* @private
*/
remoting.LocalHostSection.prototype.isMe2MeInstallable_ = function() {
// The chromoting host is currently not installable on ChromeOS.
// For Linux, we have a install package for Ubuntu but not other distros.
// Since we cannot tell from javascript alone the Linux distro the client is
// on, we don't show the daemon-control UI for Linux unless the host is
// installed.
return remoting.platformIsWindows() || remoting.platformIsMac();
}
/** @private */ /** @private */
remoting.LocalHostSection.prototype.updateUI_ = function() { remoting.LocalHostSection.prototype.updateUI_ = function() {
this.hostTableEntry_.setHost(this.host_); this.hostTableEntry_.setHost(this.host_);
......
...@@ -50,23 +50,6 @@ remoting.initGlobalObjects = function() { ...@@ -50,23 +50,6 @@ remoting.initGlobalObjects = function() {
remoting.testEvents.defineEvents(base.values(remoting.testEvents.Names)); remoting.testEvents.defineEvents(base.values(remoting.testEvents.Names));
} }
/**
* Returns true if the current platform is fully supported. It's only used when
* we detect that host native messaging components are not installed. In that
* case the result of this function determines if the webapp should show the
* controls that allow to install and enable Me2Me host.
*
* @return {boolean}
*/
remoting.isMe2MeInstallable = function() {
// The chromoting host is currently not installable on ChromeOS.
// For Linux, we have a install package for Ubuntu but not other distros.
// Since we cannot tell from javascript alone the Linux distro the client is
// on, we don't show the daemon-control UI for Linux unless the host is
// installed.
return remoting.platformIsWindows() || remoting.platformIsMac();
}
/** /**
* @return {string} Information about the current extension. * @return {string} Information about the current extension.
*/ */
...@@ -81,43 +64,6 @@ remoting.getExtensionInfo = function() { ...@@ -81,43 +64,6 @@ remoting.getExtensionInfo = function() {
} }
}; };
/**
* If an IT2Me client or host is active then prompt the user before closing.
* If a Me2Me client is active then don't bother, since closing the window is
* the more intuitive way to end a Me2Me session, and re-connecting is easy.
*/
remoting.promptClose = function() {
var sessionConnector = remoting.app.getSessionConnector();
if (sessionConnector &&
sessionConnector.getConnectionMode() ===
remoting.DesktopConnectedView.Mode.IT2ME) {
switch (remoting.currentMode) {
case remoting.AppMode.CLIENT_CONNECTING:
case remoting.AppMode.HOST_WAITING_FOR_CODE:
case remoting.AppMode.HOST_WAITING_FOR_CONNECTION:
case remoting.AppMode.HOST_SHARED:
case remoting.AppMode.IN_SESSION:
return chrome.i18n.getMessage(/*i18n-content*/'CLOSE_PROMPT');
default:
return null;
}
}
};
/**
* Sign the user out of Chromoting by clearing (and revoking, if possible) the
* OAuth refresh token.
*
* Also clear all local storage, to avoid leaking information.
*/
remoting.signOut = function() {
remoting.oauth2.removeCachedAuthToken().then(function(){
chrome.storage.local.clear();
remoting.setMode(remoting.AppMode.HOME);
window.location.reload();
});
};
/** /**
* Callback function called when the browser window gets a paste operation. * Callback function called when the browser window gets a paste operation.
* *
...@@ -170,49 +116,3 @@ remoting.timestamp = function() { ...@@ -170,49 +116,3 @@ remoting.timestamp = function() {
pad(now.getSeconds(), 2) + '.' + pad(now.getMilliseconds(), 3); pad(now.getSeconds(), 2) + '.' + pad(now.getMilliseconds(), 3);
return '[' + timestamp + ']'; return '[' + timestamp + ']';
}; };
/**
* Show an error message, optionally including a short-cut for signing in to
* Chromoting again.
*
* @param {!remoting.Error} error
* @return {void} Nothing.
*/
remoting.showErrorMessage = function(error) {
l10n.localizeElementFromTag(
document.getElementById('token-refresh-error-message'),
error.getTag());
var auth_failed = (error.hasTag(remoting.Error.Tag.AUTHENTICATION_FAILED));
if (auth_failed && base.isAppsV2()) {
remoting.handleAuthFailureAndRelaunch();
} else {
document.getElementById('token-refresh-auth-failed').hidden = !auth_failed;
document.getElementById('token-refresh-other-error').hidden = auth_failed;
remoting.setMode(remoting.AppMode.TOKEN_REFRESH_FAILED);
}
};
/**
* Determine whether or not the app is running in a window.
* @param {function(boolean):void} callback Callback to receive whether or not
* the current tab is running in windowed mode.
*/
function isWindowed_(callback) {
/** @param {chrome.Window} win The current window. */
var windowCallback = function(win) {
callback(win.type == 'popup');
};
/** @param {chrome.Tab} tab The current tab. */
var tabCallback = function(tab) {
if (tab.pinned) {
callback(false);
} else {
chrome.windows.get(tab.windowId, null, windowCallback);
}
};
if (chrome.tabs) {
chrome.tabs.getCurrent(tabCallback);
} else {
console.error('chome.tabs is not available.');
}
}
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment