Don't show supervised user as "already on this device" while they're being imported.
[chromium-blink-merge.git] / remoting / webapp / crd / js / desktop_remoting.js
blob965dc939c9af5d49e5d5a2cd0a42bb54d80379a9
1 // Copyright 2014 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 /**
6 * @fileoverview
7 * This class implements the functionality that is specific to desktop
8 * remoting ("Chromoting" or CRD).
9 */
11 'use strict';
13 /** @suppress {duplicate} */
14 var remoting = remoting || {};
16 /**
17 * @constructor
18 * @implements {remoting.ApplicationInterface}
19 * @extends {remoting.Application}
21 remoting.DesktopRemoting = function() {
22 base.inherits(this, remoting.Application);
24 /** @protected {remoting.DesktopRemoting.Mode} */
25 this.connectionMode_ = remoting.DesktopRemoting.Mode.ME2ME;
27 /** @private {remoting.Activity} */
28 this.activity_ = null;
31 /**
32 * The current desktop remoting mode (IT2Me or Me2Me).
34 * @enum {number}
36 remoting.DesktopRemoting.Mode = {
37 IT2ME: 0,
38 ME2ME: 1
41 /**
42 * Get the connection mode (Me2Me or IT2Me).
44 * @return {remoting.DesktopRemoting.Mode}
46 remoting.DesktopRemoting.prototype.getConnectionMode = function() {
47 return this.connectionMode_;
50 /**
51 * @return {string} Application product name to be used in UI.
52 * @override {remoting.ApplicationInterface}
54 remoting.DesktopRemoting.prototype.getApplicationName = function() {
55 return chrome.i18n.getMessage(/*i18n-content*/'PRODUCT_NAME');
58 /**
59 * @param {!remoting.Error} error The failure reason.
60 * @override {remoting.ApplicationInterface}
62 remoting.DesktopRemoting.prototype.signInFailed_ = function(error) {
63 remoting.showErrorMessage(error);
66 /**
67 * @override {remoting.ApplicationInterface}
69 remoting.DesktopRemoting.prototype.initApplication_ = function() {
70 remoting.initElementEventHandlers();
72 if (base.isAppsV2()) {
73 remoting.windowFrame = new remoting.WindowFrame(
74 document.getElementById('title-bar'), this.disconnect_.bind(this));
75 remoting.optionsMenu = remoting.windowFrame.createOptionsMenu();
77 var START_FULLSCREEN = 'start-fullscreen';
78 remoting.fullscreen = new remoting.FullscreenAppsV2();
79 remoting.fullscreen.addListener(function(isFullscreen) {
80 chrome.storage.local.set({START_FULLSCREEN: isFullscreen});
81 });
82 // TODO(jamiewalch): This should be handled by the background page when the
83 // window is created, but due to crbug.com/51587 it needs to be done here.
84 // Remove this hack once that bug is fixed.
85 chrome.storage.local.get(
86 START_FULLSCREEN,
87 /** @param {Object} values */
88 function(values) {
89 if (values[START_FULLSCREEN]) {
90 remoting.fullscreen.activate(true);
95 } else {
96 remoting.fullscreen = new remoting.FullscreenAppsV1();
97 remoting.toolbar = new remoting.Toolbar(
98 document.getElementById('session-toolbar'),
99 this.disconnect_.bind(this));
100 remoting.optionsMenu = remoting.toolbar.createOptionsMenu();
102 window.addEventListener('beforeunload',
103 this.promptClose_.bind(this), false);
104 window.addEventListener('unload', this.disconnect_.bind(this), false);
107 remoting.initHostlist_(this.connectMe2Me_.bind(this));
108 document.getElementById('access-mode-button').addEventListener(
109 'click', this.connectIt2Me_.bind(this), false);
111 var homeFeedback = new remoting.MenuButton(
112 document.getElementById('help-feedback-main'));
113 var toolbarFeedback = new remoting.MenuButton(
114 document.getElementById('help-feedback-toolbar'));
115 remoting.manageHelpAndFeedback(
116 document.getElementById('title-bar'));
117 remoting.manageHelpAndFeedback(
118 document.getElementById('help-feedback-toolbar'));
119 remoting.manageHelpAndFeedback(
120 document.getElementById('help-feedback-main'));
122 remoting.showOrHideIT2MeUi();
123 remoting.showOrHideMe2MeUi();
125 // For Apps v1, check the tab type to warn the user if they are not getting
126 // the best keyboard experience.
127 if (!base.isAppsV2() && !remoting.platformIsMac()) {
128 /** @param {boolean} isWindowed */
129 var onIsWindowed = function(isWindowed) {
130 if (!isWindowed) {
131 document.getElementById('startup-mode-box-me2me').hidden = false;
132 document.getElementById('startup-mode-box-it2me').hidden = false;
135 this.isWindowed_(onIsWindowed);
138 remoting.ClientPlugin.factory.preloadPlugin();
142 * @param {string} token An OAuth access token.
143 * @override {remoting.ApplicationInterface}
145 remoting.DesktopRemoting.prototype.startApplication_ = function(token) {
146 remoting.identity.getEmail().then(
147 function(/** string */ email) {
148 document.getElementById('current-email').innerText = email;
149 document.getElementById('get-started-it2me').disabled = false;
150 document.getElementById('get-started-me2me').disabled = false;
154 /** @override {remoting.ApplicationInterface} */
155 remoting.DesktopRemoting.prototype.exitApplication_ = function() {
156 this.closeMainWindow_();
160 * Determine whether or not the app is running in a window.
161 * @param {function(boolean):void} callback Callback to receive whether or not
162 * the current tab is running in windowed mode.
163 * @private
165 remoting.DesktopRemoting.prototype.isWindowed_ = function(callback) {
166 var windowCallback = function(/** ChromeWindow */ win) {
167 callback(win.type == 'popup');
169 /** @param {Tab=} tab */
170 var tabCallback = function(tab) {
171 if (tab.pinned) {
172 callback(false);
173 } else {
174 chrome.windows.get(tab.windowId, null, windowCallback);
177 if (chrome.tabs) {
178 chrome.tabs.getCurrent(tabCallback);
179 } else {
180 console.error('chome.tabs is not available.');
185 * If an IT2Me client or host is active then prompt the user before closing.
186 * If a Me2Me client is active then don't bother, since closing the window is
187 * the more intuitive way to end a Me2Me session, and re-connecting is easy.
188 * @private
190 remoting.DesktopRemoting.prototype.promptClose_ = function() {
191 if (this.getConnectionMode() === remoting.DesktopRemoting.Mode.IT2ME) {
192 switch (remoting.currentMode) {
193 case remoting.AppMode.CLIENT_CONNECTING:
194 case remoting.AppMode.HOST_WAITING_FOR_CODE:
195 case remoting.AppMode.HOST_WAITING_FOR_CONNECTION:
196 case remoting.AppMode.HOST_SHARED:
197 case remoting.AppMode.IN_SESSION:
198 return chrome.i18n.getMessage(/*i18n-content*/'CLOSE_PROMPT');
199 default:
200 return null;
205 /** @returns {remoting.DesktopConnectedView} */
206 remoting.DesktopRemoting.prototype.getConnectedViewForTesting = function() {
207 var activity = /** @type {remoting.Me2MeActivity} */ (this.activity_);
208 return activity.getDesktopActivity().getConnectedView();
211 remoting.DesktopRemoting.prototype.getActivity = function() {
212 return this.activity_;
215 remoting.DesktopRemoting.prototype.disconnect_ = function() {
216 if (this.activity_) {
217 this.activity_.stop();
222 * Entry-point for Me2Me connections.
224 * @param {string} hostId The unique id of the host.
225 * @return {void} Nothing.
226 * @private
228 remoting.DesktopRemoting.prototype.connectMe2Me_ = function(hostId) {
229 var host = remoting.hostList.getHostForId(hostId);
230 base.dispose(this.activity_);
231 this.activity_ = new remoting.Me2MeActivity(host, remoting.hostList);
232 this.activity_.start();
233 this.connectionMode_ = remoting.DesktopRemoting.Mode.ME2ME;
237 * Entry-point for It2Me connections.
239 * @private
241 remoting.DesktopRemoting.prototype.connectIt2Me_ = function() {
242 base.dispose(this.activity_);
243 this.activity_ = new remoting.It2MeActivity();
244 this.activity_.start();
245 this.connectionMode_ = remoting.DesktopRemoting.Mode.IT2ME;