Roll src/third_party/WebKit 3529d49:06e8485 (svn 202554:202555)
[chromium-blink-merge.git] / remoting / webapp / crd / js / crd_main.js
blobcdfaecffd87640fc3294a3e05d04ad384852f181
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 'use strict';
7 /** @suppress {duplicate} */
8 var remoting = remoting || {};
10 /**
11  * Initialize the host list.
12  *
13  * @param {function(string)} handleConnect Function to call to connect to the
14  *     host with |hostId|.
15  */
16 remoting.initHostlist_ = function(handleConnect) {
17   remoting.hostController = new remoting.HostController();
18   remoting.hostList = new remoting.HostList(
19       document.getElementById('host-list'),
20       document.getElementById('host-list-empty'),
21       document.getElementById('host-list-error-message'),
22       document.getElementById('host-list-refresh-failed-button'),
23       document.getElementById('host-list-loading-indicator'),
24       remoting.showErrorMessage,
25       handleConnect);
27   isHostModeSupported_().then(function(/** boolean */ supported) {
28     if (supported) {
29       var noShare = document.getElementById('unsupported-platform-message');
30       noShare.parentNode.removeChild(noShare);
31     } else {
32       var button = document.getElementById('share-button');
33       button.disabled = true;
34     }
35   });
37   var onLoad = function() {
38     // Parse URL parameters.
39     var urlParams = base.getUrlParameters();
40     if ('mode' in urlParams) {
41       if (urlParams['mode'] === 'me2me') {
42         var hostId = urlParams['hostId'];
43         handleConnect(hostId);
44         return;
45       }
46     }
47     // No valid URL parameters, start up normally.
48     remoting.initHomeScreenUi();
49   }
50   remoting.hostList.load(onLoad);
53 /**
54  * Returns whether Host mode is supported on this platform for It2Me.
55  *
56  * @return {Promise} Resolves to true if Host mode is supported.
57  */
58 function isHostModeSupported_() {
59   if (remoting.HostInstaller.canInstall()) {
60     return Promise.resolve(true);
61   }
62   return remoting.HostInstaller.isInstalled();
65 /**
66  * initHomeScreenUi is called if the app is not starting up in session mode,
67  * and also if the user cancels pin entry or the connection in session mode.
68  */
69 remoting.initHomeScreenUi = function() {
70   remoting.setMode(remoting.AppMode.HOME);
71   var dialog = document.getElementById('paired-clients-list');
72   var message = document.getElementById('paired-client-manager-message');
73   var deleteAll = document.getElementById('delete-all-paired-clients');
74   var close = document.getElementById('close-paired-client-manager-dialog');
75   var working = document.getElementById('paired-client-manager-dialog-working');
76   var error = document.getElementById('paired-client-manager-dialog-error');
77   var noPairedClients = document.getElementById('no-paired-clients');
78   remoting.pairedClientManager =
79       new remoting.PairedClientManager(remoting.hostController, dialog, message,
80                                        deleteAll, close, noPairedClients,
81                                        working, error);
82   // Display the cached host list, then asynchronously update and re-display it.
83   remoting.updateLocalHostState();
84   remoting.hostList.refresh(remoting.updateLocalHostState);
85   remoting.butterBar = new remoting.ButterBar();
88 /**
89  * Fetches local host state and updates the DOM accordingly.
90  */
91 remoting.updateLocalHostState = function() {
92   /**
93    * @param {remoting.HostController.State} state Host state.
94    */
95   var onHostState = function(state) {
96     if (state == remoting.HostController.State.STARTED) {
97       remoting.hostController.getLocalHostId(onHostId.bind(null, state));
98     } else {
99       onHostId(state, null);
100     }
101   };
103   /**
104    * @param {remoting.HostController.State} state Host state.
105    * @param {string?} hostId Host id.
106    */
107   var onHostId = function(state, hostId) {
108     remoting.hostList.setLocalHostStateAndId(state, hostId);
109     remoting.hostList.display();
110   };
112   /**
113    * @param {boolean} response True if the feature is present.
114    */
115   var onHasFeatureResponse = function(response) {
116     /**
117      * @param {!remoting.Error} error
118      */
119     var onError = function(error) {
120       console.error('Failed to get pairing status: ' + error.toString());
121       remoting.pairedClientManager.setPairedClients([]);
122     };
124     if (response) {
125       remoting.hostController.getPairedClients(
126           remoting.pairedClientManager.setPairedClients.bind(
127               remoting.pairedClientManager),
128           onError);
129     } else {
130       console.log('Pairing registry not supported by host.');
131       remoting.pairedClientManager.setPairedClients([]);
132     }
133   };
135   remoting.hostController.hasFeature(
136       remoting.HostController.Feature.PAIRING_REGISTRY).
137       then(onHasFeatureResponse);
138   remoting.hostController.getLocalHostState(onHostState);
142  * Entry point for test code. In order to make test and production
143  * code as similar as possible, the same entry point is used for
144  * production code, but since production code should never have
145  * 'source' set to 'test', it continue with initialization
146  * immediately. As a fail-safe, the mechanism by which initialization
147  * completes when under test is controlled by a simple UI, making it
148  * possible to use the app even if the previous assumption fails to
149  * hold in some corner cases.
150  */
151 remoting.startDesktopRemotingForTesting = function() {
152   var urlParams = base.getUrlParameters();
153   if (urlParams['source'] === 'test') {
154     document.getElementById('browser-test-continue-init').addEventListener(
155         'click', remoting.startDesktopRemoting, false);
156     document.getElementById('browser-test-deferred-init').hidden = false;
157   } else {
158     remoting.startDesktopRemoting();
159   }
163  * @param {!remoting.Error} error The failure reason.
164  */
165 remoting.showErrorMessage = function(error) {
166   l10n.localizeElementFromTag(
167       document.getElementById('token-refresh-error-message'),
168       error.getTag());
169   var auth_failed = (error.hasTag(remoting.Error.Tag.AUTHENTICATION_FAILED));
170   if (auth_failed && base.isAppsV2()) {
171     remoting.handleAuthFailureAndRelaunch();
172   } else {
173     document.getElementById('token-refresh-auth-failed').hidden = !auth_failed;
174     document.getElementById('token-refresh-other-error').hidden = auth_failed;
175     remoting.setMode(remoting.AppMode.TOKEN_REFRESH_FAILED);
176   }
180 remoting.startDesktopRemoting = function() {
181   remoting.app = new remoting.DesktopRemoting();
182   remoting.app.start();
185 window.addEventListener('load', remoting.startDesktopRemotingForTesting, false);