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 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 */
25 var noShare = document.getElementById('unsupported-platform-message');
26 noShare.parentNode.removeChild(noShare);
28 var button = document.getElementById('share-button');
29 button.disabled = true;
34 * @return {Promise} A promise that resolves to the id of the current
35 * containing tab/window.
37 var getCurrentId = function () {
38 if (base.isAppsV2()) {
39 return Promise.resolve(chrome.app.window.current().id);
43 * @param {function(*=):void} resolve
44 * @param {function(*=):void} reject
46 return new Promise(function(resolve, reject) {
47 /** @param {chrome.Tab} tab */
48 chrome.tabs.getCurrent(function(tab){
50 resolve(String(tab.id));
52 reject('Cannot retrieve the current tab.');
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);
65 } else if (urlParams['mode'] === 'hangout') {
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();
83 // No valid URL parameters, start up normally.
84 remoting.initHomeScreenUi();
86 remoting.hostList.load(onLoad);
90 * Returns whether Host mode is supported on this platform for It2Me.
92 * @return {Promise} Resolves to true if Host mode is supported.
94 function isHostModeSupported_() {
95 if (remoting.HostInstaller.canInstall()) {
96 return Promise.resolve(true);
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.
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,
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.
131 remoting.updateLocalHostState = function() {
133 * @param {remoting.HostController.State} state Host state.
135 var onHostState = function(state) {
136 if (state == remoting.HostController.State.STARTED) {
137 remoting.hostController.getLocalHostId(onHostId.bind(null, state));
139 onHostId(state, null);
144 * @param {remoting.HostController.State} state Host state.
145 * @param {string?} hostId Host id.
147 var onHostId = function(state, hostId) {
148 remoting.hostList.setLocalHostStateAndId(state, hostId);
149 remoting.hostList.display();
153 * @param {boolean} response True if the feature is present.
155 var onHasFeatureResponse = function(response) {
157 * @param {remoting.Error} error
159 var onError = function(error) {
160 console.error('Failed to get pairing status: ' + error);
161 remoting.pairedClientManager.setPairedClients([]);
165 remoting.hostController.getPairedClients(
166 remoting.pairedClientManager.setPairedClients.bind(
167 remoting.pairedClientManager),
170 console.log('Pairing registry not supported by host.');
171 remoting.pairedClientManager.setPairedClients([]);
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.
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;
197 remoting.startDesktopRemoting();
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);