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