Rewrite AndroidSyncSettings to be significantly simpler.
[chromium-blink-merge.git] / remoting / webapp / crd / js / crd_main.js
blob1fdc7a246d192c60161271c016e0693f3cfdd8b0
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 remoting.initHostlist_ = function() {
14   remoting.hostList = new remoting.HostList(
15       document.getElementById('host-list'),
16       document.getElementById('host-list-empty'),
17       document.getElementById('host-list-error-message'),
18       document.getElementById('host-list-refresh-failed-button'),
19       document.getElementById('host-list-loading-indicator'));
21   isHostModeSupported_().then(
22       /** @param {Boolean} supported */
23       function(supported) {
24         if (supported) {
25           var noShare = document.getElementById('unsupported-platform-message');
26           noShare.parentNode.removeChild(noShare);
27         } else {
28           var button = document.getElementById('share-button');
29           button.disabled = true;
30         }
31       });
33   /**
34    * @return {Promise} A promise that resolves to the id of the current
35    * containing tab/window.
36    */
37   var getCurrentId = function () {
38     if (base.isAppsV2()) {
39       return Promise.resolve(chrome.app.window.current().id);
40     }
42     /**
43      * @param {function(*=):void} resolve
44      * @param {function(*=):void} reject
45      */
46     return new Promise(function(resolve, reject) {
47       /** @param {chrome.Tab} tab */
48       chrome.tabs.getCurrent(function(tab){
49         if (tab) {
50           resolve(String(tab.id));
51         }
52         reject('Cannot retrieve the current tab.');
53       });
54     });
55   };
57   var onLoad = function() {
58     // Parse URL parameters.
59     var urlParams = base.getUrlParameters();
60     if ('mode' in urlParams) {
61       if (urlParams['mode'] === 'me2me') {
62         var hostId = urlParams['hostId'];
63         remoting.connectMe2Me(hostId);
64         return;
65       } else if (urlParams['mode'] === 'hangout') {
66         getCurrentId().then(
67             /** @param {*} id */
68             function(id) {
69               /** @type {string} */
70               var accessCode = urlParams['accessCode'];
71               var connector = remoting.app.getSessionConnector();
72               remoting.setMode(remoting.AppMode.CLIENT_CONNECTING);
73               connector.connectIT2Me(accessCode);
75               document.body.classList.add('hangout-remote-desktop');
76               var senderId = /** @type {string} */ (String(id));
77               var hangoutSession = new remoting.HangoutSession(senderId);
78               hangoutSession.init();
79             });
80         return;
81       }
82     }
83     // No valid URL parameters, start up normally.
84     remoting.initHomeScreenUi();
85   }
86   remoting.hostList.load(onLoad);
89 /**
90  * Returns whether Host mode is supported on this platform for It2Me.
91  *
92  * @return {Promise} Resolves to true if Host mode is supported.
93  */
94 function isHostModeSupported_() {
95   if (remoting.HostInstaller.canInstall()) {
96     return Promise.resolve(true);
97   }
99   return remoting.HostInstaller.isInstalled();
103  * initHomeScreenUi is called if the app is not starting up in session mode,
104  * and also if the user cancels pin entry or the connection in session mode.
105  */
106 remoting.initHomeScreenUi = function() {
107   remoting.hostController = new remoting.HostController();
108   remoting.setMode(remoting.AppMode.HOME);
109   remoting.hostSetupDialog =
110       new remoting.HostSetupDialog(remoting.hostController);
111   var dialog = document.getElementById('paired-clients-list');
112   var message = document.getElementById('paired-client-manager-message');
113   var deleteAll = document.getElementById('delete-all-paired-clients');
114   var close = document.getElementById('close-paired-client-manager-dialog');
115   var working = document.getElementById('paired-client-manager-dialog-working');
116   var error = document.getElementById('paired-client-manager-dialog-error');
117   var noPairedClients = document.getElementById('no-paired-clients');
118   remoting.pairedClientManager =
119       new remoting.PairedClientManager(remoting.hostController, dialog, message,
120                                        deleteAll, close, noPairedClients,
121                                        working, error);
122   // Display the cached host list, then asynchronously update and re-display it.
123   remoting.updateLocalHostState();
124   remoting.hostList.refresh(remoting.updateLocalHostState);
125   remoting.butterBar = new remoting.ButterBar();
129  * Fetches local host state and updates the DOM accordingly.
130  */
131 remoting.updateLocalHostState = function() {
132   /**
133    * @param {remoting.HostController.State} state Host state.
134    */
135   var onHostState = function(state) {
136     if (state == remoting.HostController.State.STARTED) {
137       remoting.hostController.getLocalHostId(onHostId.bind(null, state));
138     } else {
139       onHostId(state, null);
140     }
141   };
143   /**
144    * @param {remoting.HostController.State} state Host state.
145    * @param {string?} hostId Host id.
146    */
147   var onHostId = function(state, hostId) {
148     remoting.hostList.setLocalHostStateAndId(state, hostId);
149     remoting.hostList.display();
150   };
152   /**
153    * @param {boolean} response True if the feature is present.
154    */
155   var onHasFeatureResponse = function(response) {
156     /**
157      * @param {remoting.Error} error
158      */
159     var onError = function(error) {
160       console.error('Failed to get pairing status: ' + error);
161       remoting.pairedClientManager.setPairedClients([]);
162     };
164     if (response) {
165       remoting.hostController.getPairedClients(
166           remoting.pairedClientManager.setPairedClients.bind(
167               remoting.pairedClientManager),
168           onError);
169     } else {
170       console.log('Pairing registry not supported by host.');
171       remoting.pairedClientManager.setPairedClients([]);
172     }
173   };
175   remoting.hostController.hasFeature(
176       remoting.HostController.Feature.PAIRING_REGISTRY, onHasFeatureResponse);
177   remoting.hostController.getLocalHostState(onHostState);
181  * Entry point for test code. In order to make test and production
182  * code as similar as possible, the same entry point is used for
183  * production code, but since production code should never have
184  * 'source' set to 'test', it continue with initialization
185  * immediately. As a fail-safe, the mechanism by which initialization
186  * completes when under test is controlled by a simple UI, making it
187  * possible to use the app even if the previous assumption fails to
188  * hold in some corner cases.
189  */
190 remoting.startDesktopRemotingForTesting = function() {
191   var urlParams = base.getUrlParameters();
192   if (urlParams['source'] === 'test') {
193     document.getElementById('browser-test-continue-init').addEventListener(
194         'click', remoting.startDesktopRemoting, false);
195     document.getElementById('browser-test-deferred-init').hidden = false;
196   } else {
197     remoting.startDesktopRemoting();
198   }
202 remoting.startDesktopRemoting = function() {
203   remoting.app = new remoting.Application(remoting.app_capabilities());
204   var desktop_remoting = new remoting.DesktopRemoting(remoting.app);
205   remoting.app.start();
208 window.addEventListener('load', remoting.startDesktopRemotingForTesting, false);