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.
7 /** @suppress {duplicate} */
8 var remoting = remoting || {};
11 * Initialize the host list.
13 * @param {function(string)} handleConnect Function to call to connect to the
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,
27 isHostModeSupported_().then(function(/** boolean */ supported) {
29 var noShare = document.getElementById('unsupported-platform-message');
30 noShare.parentNode.removeChild(noShare);
32 var button = document.getElementById('share-button');
33 button.disabled = true;
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);
47 // No valid URL parameters, start up normally.
48 remoting.initHomeScreenUi();
50 remoting.hostList.load(onLoad);
54 * Returns whether Host mode is supported on this platform for It2Me.
56 * @return {Promise} Resolves to true if Host mode is supported.
58 function isHostModeSupported_() {
59 if (remoting.HostInstaller.canInstall()) {
60 return Promise.resolve(true);
62 return remoting.HostInstaller.isInstalled();
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.
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,
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();
89 * Fetches local host state and updates the DOM accordingly.
91 remoting.updateLocalHostState = function() {
93 * @param {remoting.HostController.State} state Host state.
95 var onHostState = function(state) {
96 if (state == remoting.HostController.State.STARTED) {
97 remoting.hostController.getLocalHostId(onHostId.bind(null, state));
99 onHostId(state, null);
104 * @param {remoting.HostController.State} state Host state.
105 * @param {string?} hostId Host id.
107 var onHostId = function(state, hostId) {
108 remoting.hostList.setLocalHostStateAndId(state, hostId);
109 remoting.hostList.display();
113 * @param {boolean} response True if the feature is present.
115 var onHasFeatureResponse = function(response) {
117 * @param {!remoting.Error} error
119 var onError = function(error) {
120 console.error('Failed to get pairing status: ' + error.toString());
121 remoting.pairedClientManager.setPairedClients([]);
125 remoting.hostController.getPairedClients(
126 remoting.pairedClientManager.setPairedClients.bind(
127 remoting.pairedClientManager),
130 console.log('Pairing registry not supported by host.');
131 remoting.pairedClientManager.setPairedClients([]);
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.
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;
158 remoting.startDesktopRemoting();
163 * @param {!remoting.Error} error The failure reason.
165 remoting.showErrorMessage = function(error) {
166 l10n.localizeElementFromTag(
167 document.getElementById('token-refresh-error-message'),
169 var auth_failed = (error.hasTag(remoting.Error.Tag.AUTHENTICATION_FAILED));
170 if (auth_failed && base.isAppsV2()) {
171 remoting.handleAuthFailureAndRelaunch();
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);
180 remoting.startDesktopRemoting = function() {
181 remoting.app = new remoting.DesktopRemoting();
182 remoting.app.start();
185 window.addEventListener('load', remoting.startDesktopRemotingForTesting, false);