-
Notifications
You must be signed in to change notification settings - Fork 8
/
crd_main.js
208 lines (188 loc) · 7.01 KB
/
crd_main.js
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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
// 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.
*/
remoting.initHostlist_ = function() {
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'));
isHostModeSupported_().then(
/** @param {Boolean} supported */
function(supported) {
if (supported) {
var noShare = document.getElementById('unsupported-platform-message');
noShare.parentNode.removeChild(noShare);
} else {
var button = document.getElementById('share-button');
button.disabled = true;
}
});
/**
* @return {Promise} A promise that resolves to the id of the current
* containing tab/window.
*/
var getCurrentId = function () {
if (base.isAppsV2()) {
return Promise.resolve(chrome.app.window.current().id);
}
/**
* @param {function(*=):void} resolve
* @param {function(*=):void} reject
*/
return new Promise(function(resolve, reject) {
/** @param {chrome.Tab} tab */
chrome.tabs.getCurrent(function(tab){
if (tab) {
resolve(String(tab.id));
}
reject('Cannot retrieve the current tab.');
});
});
};
var onLoad = function() {
// Parse URL parameters.
var urlParams = base.getUrlParameters();
if ('mode' in urlParams) {
if (urlParams['mode'] === 'me2me') {
var hostId = urlParams['hostId'];
remoting.connectMe2Me(hostId);
return;
} else if (urlParams['mode'] === 'hangout') {
getCurrentId().then(
/** @param {*} id */
function(id) {
/** @type {string} */
var accessCode = urlParams['accessCode'];
var connector = remoting.app.getSessionConnector();
remoting.setMode(remoting.AppMode.CLIENT_CONNECTING);
connector.connectIT2Me(accessCode);
document.body.classList.add('hangout-remote-desktop');
var senderId = /** @type {string} */ (String(id));
var hangoutSession = new remoting.HangoutSession(senderId);
hangoutSession.init();
});
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.hostController = new remoting.HostController();
remoting.setMode(remoting.AppMode.HOME);
remoting.hostSetupDialog =
new remoting.HostSetupDialog(remoting.hostController);
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(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);
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, 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();
}
}
remoting.startDesktopRemoting = function() {
remoting.app = new remoting.Application(remoting.app_capabilities());
var desktop_remoting = new remoting.DesktopRemoting(remoting.app);
remoting.app.start();
};
window.addEventListener('load', remoting.startDesktopRemotingForTesting, false);