Fix tool-bar auto-hide behaviour.
[chromium-blink-merge.git] / remoting / webapp / me2mom / remoting.js
blob33de90042581d7be8a67e127ee1272d65f8ea9e8
1 // Copyright (c) 2011 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 'use strict';
7 /** @suppress {duplicate} */
8 var remoting = remoting || {};
10 /** @type {remoting.HostSession} */ remoting.hostSession = null;
12 /**
13  * @enum {string} All error messages from messages.json
14  */
15 remoting.Error = {
16   NO_RESPONSE: /*i18n-content*/'ERROR_NO_RESPONSE',
17   INVALID_ACCESS_CODE: /*i18n-content*/'ERROR_INVALID_ACCESS_CODE',
18   MISSING_PLUGIN: /*i18n-content*/'ERROR_MISSING_PLUGIN',
19   AUTHENTICATION_FAILED: /*i18n-content*/'ERROR_AUTHENTICATION_FAILED',
20   HOST_IS_OFFLINE: /*i18n-content*/'ERROR_HOST_IS_OFFLINE',
21   INCOMPATIBLE_PROTOCOL: /*i18n-content*/'ERROR_INCOMPATIBLE_PROTOCOL',
22   BAD_PLUGIN_VERSION: /*i18n-content*/'ERROR_BAD_PLUGIN_VERSION',
23   GENERIC: /*i18n-content*/'ERROR_GENERIC'
26 (function() {
28 /**
29  * Entry point for app initialization.
30  */
31 remoting.init = function() {
32   l10n.localize();
33   var button = document.getElementById('toggle-scaling');
34   button.title = chrome.i18n.getMessage(/*i18n-content*/'TOOLTIP_SCALING');
35   // Create global objects.
36   remoting.oauth2 = new remoting.OAuth2();
37   remoting.debug = new remoting.DebugLog(
38       document.getElementById('debug-messages'),
39       document.getElementById('statistics'));
40   remoting.hostList = new remoting.HostList(
41       document.getElementById('host-list'),
42       document.getElementById('host-list-error'));
43   remoting.toolbar = new remoting.Toolbar(
44       document.getElementById('session-toolbar'));
46   refreshEmail_();
47   var email = remoting.oauth2.getCachedEmail();
48   if (email) {
49     document.getElementById('current-email').innerText = email;
50   }
52   window.addEventListener('blur', pluginLostFocus_, false);
54   // Parse URL parameters.
55   var urlParams = getUrlParameters();
56   if ('mode' in urlParams) {
57     if (urlParams['mode'] == 'me2me') {
58       var hostJid = urlParams['hostJid'];
59       var hostPublicKey = urlParams['hostPublicKey'];
60       var hostName = urlParams['hostName'];
61       remoting.connectHost(hostJid, hostPublicKey, hostName);
62       return;
63     }
64   }
66   // No valid URL parameters, start up normally.
67   remoting.setMode(getAppStartupMode_());
68   if (isHostModeSupported_()) {
69     var noShare = document.getElementById('chrome-os-no-share');
70     noShare.parentNode.removeChild(noShare);
71   } else {
72     var button = document.getElementById('share-button');
73     button.disabled = true;
74   }
77 remoting.cancelPendingOperation = function() {
78   document.getElementById('cancel-button').disabled = true;
79   switch (remoting.getMajorMode()) {
80     case remoting.AppMode.HOST:
81       remoting.cancelShare();
82       break;
83     case remoting.AppMode.CLIENT:
84       remoting.cancelConnect();
85       break;
86   }
89 /**
90  * If the client is connected, or the host is shared, prompt before closing.
91  *
92  * @return {?string} The prompt string if a connection is active.
93  */
94 remoting.promptClose = function() {
95   switch (remoting.currentMode) {
96     case remoting.AppMode.CLIENT_CONNECTING:
97     case remoting.AppMode.HOST_WAITING_FOR_CODE:
98     case remoting.AppMode.HOST_WAITING_FOR_CONNECTION:
99     case remoting.AppMode.HOST_SHARED:
100     case remoting.AppMode.IN_SESSION:
101       var result = chrome.i18n.getMessage(/*i18n-content*/'CLOSE_PROMPT');
102       return result;
103     default:
104       return null;
105   }
109  * Sign the user out of Chromoting by clearing the OAuth refresh token.
110  */
111 remoting.clearOAuth2 = function() {
112   remoting.oauth2.clear();
113   window.localStorage.removeItem(KEY_EMAIL_);
114   remoting.setMode(remoting.AppMode.UNAUTHENTICATED);
118  * Callback function called when the browser window loses focus. In this case,
119  * release all keys to prevent them becoming 'stuck down' on the host.
120  */
121 function pluginLostFocus_() {
122   if (remoting.clientSession && remoting.clientSession.plugin) {
123     remoting.clientSession.plugin.releaseAllKeys();
124   }
128  * If the user is authenticated, but there is no email address cached, get one.
129  */
130 function refreshEmail_() {
131   if (!getEmail_() && remoting.oauth2.isAuthenticated()) {
132     remoting.oauth2.getEmail(setEmail_);
133   }
136 /** The key under which the email address is stored. */
137 var KEY_EMAIL_ = 'remoting-email';
140  * Save the user's email address in local storage.
142  * @param {?string} email The email address to place in local storage.
143  * @return {void} Nothing.
144  */
145 function setEmail_(email) {
146   if (email) {
147     document.getElementById('current-email').innerText = email;
148   } else {
149     // TODO(ajwong): Have a better way of showing an error.
150     document.getElementById('current-email').innerText = '???';
151   }
155  * Read the user's email address from local storage.
157  * @return {?string} The email address associated with the auth credentials.
158  */
159 function getEmail_() {
160   var result = window.localStorage.getItem(KEY_EMAIL_);
161   return typeof result == 'string' ? result : null;
165  * Gets the major-mode that this application should start up in.
167  * @return {remoting.AppMode} The mode to start in.
168  */
169 function getAppStartupMode_() {
170   return remoting.oauth2.isAuthenticated() ? remoting.AppMode.HOME :
171       remoting.AppMode.UNAUTHENTICATED;
175  * Returns whether Host mode is supported on this platform.
177  * @return {boolean} True if Host mode is supported.
178  */
179 function isHostModeSupported_() {
180   // Currently, sharing on Chromebooks is not supported.
181   return !navigator.userAgent.match(/\bCrOS\b/);
183 }());