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() {
registerEventListeners(host_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() {
document.getElementById('host-list-empty'),
document.getElementById('host-list-error-message'),
document.getElementById('host-list-refresh-failed-button'),
document.getElementById('host-list-loading-indicator'));
document.getElementById('host-list-loading-indicator'),
remoting.showErrorMessage);
isHostModeSupported_().then(
/** @param {Boolean} supported */
......@@ -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.app = new remoting.Application(remoting.app_capabilities());
......
......@@ -81,7 +81,8 @@ remoting.DesktopRemoting.prototype.init = function() {
document.getElementById('session-toolbar'));
remoting.optionsMenu = remoting.toolbar.createOptionsMenu();
window.addEventListener('beforeunload', remoting.promptClose, false);
window.addEventListener('beforeunload',
this.promptClose_.bind(this), false);
window.addEventListener('unload',
remoting.app.disconnect.bind(remoting.app), false);
}
......@@ -114,7 +115,7 @@ remoting.DesktopRemoting.prototype.init = function() {
document.getElementById('startup-mode-box-it2me').hidden = false;
}
};
isWindowed_(onIsWindowed);
this.isWindowed_(onIsWindowed);
}
remoting.ClientPlugin.factory.preloadPlugin();
......@@ -324,6 +325,56 @@ remoting.DesktopRemoting.prototype.handleError = function(error) {
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} */
remoting.DesktopRemoting.prototype.getConnectedViewForTesting = function() {
return this.connectedView_;
......
......@@ -26,9 +26,11 @@ var remoting = remoting || {};
* @param {HTMLElement} loadingIndicator The HTML <span> to update while the
* host list is being loaded. The first element of this span should be
* the reload button.
* @param {function(!remoting.Error)} onError Function to call when an error
* occurs.
*/
remoting.HostList = function(table, noHosts, errorMsg, errorButton,
loadingIndicator) {
loadingIndicator, onError) {
/** @private {Element} */
this.table_ = table;
/**
......@@ -43,6 +45,8 @@ remoting.HostList = function(table, noHosts, errorMsg, errorButton,
this.errorButton_ = errorButton;
/** @private {HTMLElement} */
this.loadingIndicator_ = loadingIndicator;
this.onError_ = onError;
/** @private {Array<remoting.HostTableEntry>} */
this.hostTableEntries_ = [];
/** @private {Array<remoting.Host>} */
......@@ -53,7 +57,8 @@ remoting.HostList = function(table, noHosts, errorMsg, errorButton,
this.localHostSection_ = new remoting.LocalHostSection(
/** @type {HTMLElement} */ (document.querySelector('.daemon-control')),
new remoting.LocalHostSection.Controller(
this, new remoting.HostSetupDialog(remoting.hostController)));
this,
new remoting.HostSetupDialog(remoting.hostController, onError)));
/** @private {number} */
this.webappMajorVersion_ = parseInt(chrome.runtime.getManifest().version, 10);
......@@ -295,7 +300,7 @@ remoting.HostList.prototype.deleteHost_ = function(hostTableEntry) {
this.hostTableEntries_.splice(index, 1);
}
remoting.hostListApi.remove(hostTableEntry.host.hostId, base.doNothing,
remoting.showErrorMessage);
this.onError_);
};
/**
......@@ -316,7 +321,7 @@ remoting.HostList.prototype.renameHost = function(hostTableEntry) {
hostTableEntry.host.hostName,
hostTableEntry.host.publicKey,
function() {},
remoting.showErrorMessage);
this.onError_);
};
/**
......@@ -343,7 +348,7 @@ remoting.HostList.prototype.unregisterHostById = function(hostId, opt_onDone) {
onDone();
});
};
remoting.hostListApi.remove(hostId, onRemoved, remoting.showErrorMessage);
remoting.hostListApi.remove(hostId, onRemoved, this.onError_);
};
/**
......
......@@ -94,10 +94,14 @@ remoting.HostSetupFlow.prototype.switchToErrorState = function(error) {
/**
* @param {remoting.HostController} hostController The HostController
* responsible for the host daemon.
* @param {function(!remoting.Error)} onError Function to call when an error
* occurs.
* @constructor
*/
remoting.HostSetupDialog = function(hostController) {
remoting.HostSetupDialog = function(hostController, onError) {
this.hostController_ = hostController;
this.onError_ = onError;
this.pinEntry_ = document.getElementById('daemon-pin-entry');
this.pinConfirm_ = document.getElementById('daemon-pin-confirm');
this.pinErrorDiv_ = document.getElementById('daemon-pin-error-div');
......@@ -165,7 +169,7 @@ remoting.HostSetupDialog.prototype.showForStart = function() {
// case where the refresh token is invalid.
remoting.identity.getToken().then(
that.showForStartWithToken_.bind(that, state),
remoting.Error.handler(remoting.showErrorMessage));
remoting.Error.handler(that.onError_));
};
this.hostController_.getLocalHostState(onState);
......
......@@ -96,7 +96,7 @@ remoting.LocalHostSection.prototype.canChangeState = function() {
// 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
// uninstallable on Linux.
if (!remoting.isMe2MeInstallable() &&
if (!this.isMe2MeInstallable_() &&
state === remoting.HostController.State.NOT_INSTALLED) {
return false;
}
......@@ -107,6 +107,24 @@ remoting.LocalHostSection.prototype.canChangeState = function() {
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 */
remoting.LocalHostSection.prototype.updateUI_ = function() {
this.hostTableEntry_.setHost(this.host_);
......
......@@ -50,23 +50,6 @@ remoting.initGlobalObjects = function() {
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.
*/
......@@ -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.
*
......@@ -170,49 +116,3 @@ remoting.timestamp = function() {
pad(now.getSeconds(), 2) + '.' + pad(now.getMilliseconds(), 3);
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