Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(308)

Side by Side Diff: remoting/webapp/crd/js/host_list.js

Issue 944183002: HostTableEntry refactor (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Address Jamie's feedback Created 5 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « remoting/webapp/crd/js/crd_main.js ('k') | remoting/webapp/crd/js/host_table_entry.js » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 /** 5 /**
6 * @fileoverview 6 * @fileoverview
7 * Class representing the host-list portion of the home screen UI. 7 * Class representing the host-list portion of the home screen UI.
8 */ 8 */
9 9
10 'use strict'; 10 'use strict';
(...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after
65 * @type {Array<remoting.Host>} 65 * @type {Array<remoting.Host>}
66 * @private 66 * @private
67 */ 67 */
68 this.hosts_ = []; 68 this.hosts_ = [];
69 /** 69 /**
70 * @type {string} 70 * @type {string}
71 * @private 71 * @private
72 */ 72 */
73 this.lastError_ = ''; 73 this.lastError_ = '';
74 /** 74 /**
75 * @type {remoting.Host?} 75 * @type {remoting.LocalHostSection}
76 * @private 76 * @private
77 */ 77 */
78 this.localHost_ = null; 78 this.localHostSection_ = new remoting.LocalHostSection(
79 /** 79 /** @type {HTMLElement} */ (document.querySelector('.daemon-control')),
80 * @type {remoting.HostController.State} 80 new remoting.LocalHostSection.Controller(
81 * @private 81 this, new remoting.HostSetupDialog(remoting.hostController)));
82 */
83 this.localHostState_ = remoting.HostController.State.UNKNOWN;
84 82
85 /** 83 /**
86 * @type {number} 84 * @type {number}
87 * @private 85 * @private
88 */ 86 */
89 this.webappMajorVersion_ = parseInt(chrome.runtime.getManifest().version, 10); 87 this.webappMajorVersion_ = parseInt(chrome.runtime.getManifest().version, 10);
90 88
91 this.errorButton_.addEventListener('click', 89 this.errorButton_.addEventListener('click',
92 this.onErrorClick_.bind(this), 90 this.onErrorClick_.bind(this),
93 false); 91 false);
94 var reloadButton = this.loadingIndicator_.firstElementChild; 92 var reloadButton = this.loadingIndicator_.firstElementChild;
95 /** @type {remoting.HostList} */ 93 /** @type {remoting.HostList} */
96 var that = this; 94 var that = this;
97 /** @param {Event} event */ 95 /** @param {Event} event */
98 function refresh(event) { 96 function refresh(event) {
99 event.preventDefault(); 97 event.preventDefault();
100 that.refresh(that.display.bind(that)); 98 that.refresh(that.display.bind(that));
101 }; 99 }
102 reloadButton.addEventListener('click', refresh, false); 100 reloadButton.addEventListener('click', refresh, false);
103 }; 101 };
104 102
105 /** 103 /**
106 * Load the host-list asynchronously from local storage. 104 * Load the host-list asynchronously from local storage.
107 * 105 *
108 * @param {function():void} onDone Completion callback. 106 * @param {function():void} onDone Completion callback.
109 */ 107 */
110 remoting.HostList.prototype.load = function(onDone) { 108 remoting.HostList.prototype.load = function(onDone) {
111 // Load the cache of the last host-list, if present. 109 // Load the cache of the last host-list, if present.
(...skipping 139 matching lines...) Expand 10 before | Expand all | Expand 10 after
251 /*i18n-content*/'RETRY'); 249 /*i18n-content*/'RETRY');
252 } 250 }
253 } else { 251 } else {
254 for (var i = 0; i < this.hosts_.length; ++i) { 252 for (var i = 0; i < this.hosts_.length; ++i) {
255 /** @type {remoting.Host} */ 253 /** @type {remoting.Host} */
256 var host = this.hosts_[i]; 254 var host = this.hosts_[i];
257 // Validate the entry to make sure it has all the fields we expect and is 255 // Validate the entry to make sure it has all the fields we expect and is
258 // not the local host (which is displayed separately). NB: if the host has 256 // not the local host (which is displayed separately). NB: if the host has
259 // never sent a heartbeat, then there will be no jabberId. 257 // never sent a heartbeat, then there will be no jabberId.
260 if (host.hostName && host.hostId && host.status && host.publicKey && 258 if (host.hostName && host.hostId && host.status && host.publicKey &&
261 (!this.localHost_ || host.hostId != this.localHost_.hostId)) { 259 (host.hostId != this.localHostSection_.getHostId())) {
262 var hostTableEntry = new remoting.HostTableEntry( 260 var hostTableEntry = new remoting.HostTableEntry(
263 host, this.webappMajorVersion_, 261 this.webappMajorVersion_,
264 this.renameHost_.bind(this), this.deleteHost_.bind(this)); 262 remoting.connectMe2Me,
265 hostTableEntry.createDom(); 263 this.renameHost.bind(this),
264 this.deleteHost_.bind(this));
265 hostTableEntry.setHost(host);
266 this.hostTableEntries_[i] = hostTableEntry; 266 this.hostTableEntries_[i] = hostTableEntry;
267 this.table_.appendChild(hostTableEntry.tableRow); 267 this.table_.appendChild(hostTableEntry.element());
268 } 268 }
269 } 269 }
270 } 270 }
271 271
272 this.errorMsg_.parentNode.hidden = (this.lastError_ == ''); 272 this.errorMsg_.parentNode.hidden = (this.lastError_ == '');
273
274 // The local host cannot be stopped or started if the host controller is not
275 // implemented for this platform. Additionally, it cannot be started if there
276 // is an error (in many error states, the start operation will fail anyway,
277 // but even if it succeeds, the chance of a related but hard-to-diagnose
278 // future error is high).
279 var state = this.localHostState_;
280 var enabled = (state == remoting.HostController.State.STARTING) ||
281 (state == remoting.HostController.State.STARTED);
282 var canChangeLocalHostState =
283 (state != remoting.HostController.State.NOT_IMPLEMENTED) &&
284 (state != remoting.HostController.State.UNKNOWN) &&
285 (state != remoting.HostController.State.NOT_INSTALLED ||
286 remoting.isMe2MeInstallable()) &&
287 (enabled || this.lastError_ == '');
288
289 remoting.updateModalUi(enabled ? 'enabled' : 'disabled', 'data-daemon-state');
290 var element = document.getElementById('daemon-control');
291 element.hidden = !canChangeLocalHostState;
292
293 if (noHostsRegistered) { 273 if (noHostsRegistered) {
294 this.showHostListEmptyMessage_(canChangeLocalHostState); 274 this.showHostListEmptyMessage_(this.localHostSection_.canChangeState());
295 } 275 }
296 }; 276 };
297 277
298 /** 278 /**
299 * Displays a message to the user when the host list is empty. 279 * Displays a message to the user when the host list is empty.
300 * 280 *
301 * @param {boolean} hostingSupported 281 * @param {boolean} hostingSupported
302 * @return {void} 282 * @return {void}
303 * @private 283 * @private
304 */ 284 */
(...skipping 25 matching lines...) Expand all
330 ); 310 );
331 }; 311 };
332 312
333 /** 313 /**
334 * Remove a host from the list, and deregister it. 314 * Remove a host from the list, and deregister it.
335 * @param {remoting.HostTableEntry} hostTableEntry The host to be removed. 315 * @param {remoting.HostTableEntry} hostTableEntry The host to be removed.
336 * @return {void} Nothing. 316 * @return {void} Nothing.
337 * @private 317 * @private
338 */ 318 */
339 remoting.HostList.prototype.deleteHost_ = function(hostTableEntry) { 319 remoting.HostList.prototype.deleteHost_ = function(hostTableEntry) {
340 this.table_.removeChild(hostTableEntry.tableRow); 320 this.table_.removeChild(hostTableEntry.element());
341 var index = this.hostTableEntries_.indexOf(hostTableEntry); 321 var index = this.hostTableEntries_.indexOf(hostTableEntry);
342 if (index != -1) { 322 if (index != -1) {
343 this.hostTableEntries_.splice(index, 1); 323 this.hostTableEntries_.splice(index, 1);
344 } 324 }
345 remoting.HostList.unregisterHostById(hostTableEntry.host.hostId); 325 remoting.HostList.unregisterHostById(hostTableEntry.host.hostId);
346 }; 326 };
347 327
348 /** 328 /**
349 * Prepare a host for renaming by replacing its name with an edit box. 329 * Prepare a host for renaming by replacing its name with an edit box.
350 * @param {remoting.HostTableEntry} hostTableEntry The host to be renamed. 330 * @param {remoting.HostTableEntry} hostTableEntry The host to be renamed.
351 * @return {void} Nothing. 331 * @return {void} Nothing.
352 * @private
353 */ 332 */
354 remoting.HostList.prototype.renameHost_ = function(hostTableEntry) { 333 remoting.HostList.prototype.renameHost = function(hostTableEntry) {
355 for (var i = 0; i < this.hosts_.length; ++i) { 334 for (var i = 0; i < this.hosts_.length; ++i) {
356 if (this.hosts_[i].hostId == hostTableEntry.host.hostId) { 335 if (this.hosts_[i].hostId == hostTableEntry.host.hostId) {
357 this.hosts_[i].hostName = hostTableEntry.host.hostName; 336 this.hosts_[i].hostName = hostTableEntry.host.hostName;
358 break; 337 break;
359 } 338 }
360 } 339 }
361 this.save_(); 340 this.save_();
362 341
363 remoting.hostListApi.put(hostTableEntry.host.hostId, 342 remoting.hostListApi.put(hostTableEntry.host.hostId,
364 hostTableEntry.host.hostName, 343 hostTableEntry.host.hostName,
365 hostTableEntry.host.publicKey, 344 hostTableEntry.host.publicKey,
366 function() {}, 345 function() {},
367 remoting.showErrorMessage); 346 remoting.showErrorMessage);
368 }; 347 };
369 348
370 /** 349 /**
371 * Unregister a host. 350 * Unregister a host.
372 * @param {string} hostId The id of the host to be removed. 351 * @param {string} hostId The id of the host to be removed.
373 * @return {void} Nothing. 352 * @return {void} Nothing.
374 */ 353 */
375 remoting.HostList.unregisterHostById = function(hostId) { 354 remoting.HostList.unregisterHostById = function(hostId) {
376 remoting.hostListApi.remove(hostId, function() {}, remoting.showErrorMessage); 355 remoting.hostListApi.remove(hostId, function() {}, remoting.showErrorMessage);
377 }; 356 };
378 357
379 /** 358 /**
380 * Set tool-tips for the 'connect' action. We can't just set this on the
381 * parent element because the button has no tool-tip, and therefore would
382 * inherit connectStr.
383 *
384 * @return {void} Nothing.
385 * @private
386 */
387 remoting.HostList.prototype.setTooltips_ = function() {
388 var connectStr = '';
389 if (this.localHost_) {
390 chrome.i18n.getMessage(/*i18n-content*/'TOOLTIP_CONNECT',
391 this.localHost_.hostName);
392 }
393 document.getElementById('this-host-name').title = connectStr;
394 document.getElementById('this-host-icon').title = connectStr;
395 };
396
397 /**
398 * Set the state of the local host and localHostId if any. 359 * Set the state of the local host and localHostId if any.
399 * 360 *
400 * @param {remoting.HostController.State} state State of the local host. 361 * @param {remoting.HostController.State} state State of the local host.
401 * @param {string?} hostId ID of the local host, or null. 362 * @param {string?} hostId ID of the local host, or null.
402 * @return {void} Nothing. 363 * @return {void} Nothing.
403 */ 364 */
404 remoting.HostList.prototype.setLocalHostStateAndId = function(state, hostId) { 365 remoting.HostList.prototype.setLocalHostStateAndId = function(state, hostId) {
405 this.localHostState_ = state; 366 var host = hostId ? this.getHostForId(hostId) : null;
406 this.setLocalHost_(hostId ? this.getHostForId(hostId) : null); 367 this.localHostSection_.setModel(host, state, this.lastError_ !== '');
407 } 368 };
408
409 /**
410 * Set the host object that corresponds to the local computer, if any.
411 *
412 * @param {remoting.Host?} host The host, or null if not registered.
413 * @return {void} Nothing.
414 * @private
415 */
416 remoting.HostList.prototype.setLocalHost_ = function(host) {
417 this.localHost_ = host;
418 this.setTooltips_();
419 /** @type {remoting.HostList} */
420 var that = this;
421 if (host) {
422 /** @param {remoting.HostTableEntry} host */
423 var renameHost = function(host) {
424 that.renameHost_(host);
425 that.setTooltips_();
426 };
427 if (!this.localHostTableEntry_) {
428 /** @type {remoting.HostTableEntry} @private */
429 this.localHostTableEntry_ = new remoting.HostTableEntry(
430 host, this.webappMajorVersion_, renameHost);
431 this.localHostTableEntry_.init(
432 document.getElementById('this-host-connect'),
433 document.getElementById('this-host-warning'),
434 document.getElementById('this-host-name'),
435 document.getElementById('this-host-rename'));
436 } else {
437 // TODO(jamiewalch): This is hack to prevent multiple click handlers being
438 // registered for the same DOM elements if this method is called more than
439 // once. A better solution would be to let HostTable create the daemon row
440 // like it creates the rows for non-local hosts.
441 this.localHostTableEntry_.host = host;
442 }
443 } else {
444 this.localHostTableEntry_ = null;
445 }
446 }
447 369
448 /** 370 /**
449 * Called by the HostControlled after the local host has been started. 371 * Called by the HostControlled after the local host has been started.
450 * 372 *
451 * @param {string} hostName Host name. 373 * @param {string} hostName Host name.
452 * @param {string} hostId ID of the local host. 374 * @param {string} hostId ID of the local host.
453 * @param {string} publicKey Public key. 375 * @param {string} publicKey Public key.
454 * @return {void} Nothing. 376 * @return {void} Nothing.
455 */ 377 */
456 remoting.HostList.prototype.onLocalHostStarted = function( 378 remoting.HostList.prototype.onLocalHostStarted = function(
457 hostName, hostId, publicKey) { 379 hostName, hostId, publicKey) {
458 // Create a dummy remoting.Host instance to represent the local host. 380 // Create a dummy remoting.Host instance to represent the local host.
459 // Refreshing the list is no good in general, because the directory 381 // Refreshing the list is no good in general, because the directory
460 // information won't be in sync for several seconds. We don't know the 382 // information won't be in sync for several seconds. We don't know the
461 // host JID, but it can be missing from the cache with no ill effects. 383 // host JID, but it can be missing from the cache with no ill effects.
462 // It will be refreshed if the user tries to connect to the local host, 384 // It will be refreshed if the user tries to connect to the local host,
463 // and we hope that the directory will have been updated by that point. 385 // and we hope that the directory will have been updated by that point.
464 var localHost = new remoting.Host(); 386 var localHost = new remoting.Host();
465 localHost.hostName = hostName; 387 localHost.hostName = hostName;
466 // Provide a version number to avoid warning about this dummy host being 388 // Provide a version number to avoid warning about this dummy host being
467 // out-of-date. 389 // out-of-date.
468 localHost.hostVersion = String(this.webappMajorVersion_) + ".x" 390 localHost.hostVersion = String(this.webappMajorVersion_) + ".x"
469 localHost.hostId = hostId; 391 localHost.hostId = hostId;
470 localHost.publicKey = publicKey; 392 localHost.publicKey = publicKey;
471 localHost.status = 'ONLINE'; 393 localHost.status = 'ONLINE';
472 this.hosts_.push(localHost); 394 this.hosts_.push(localHost);
473 this.save_(); 395 this.save_();
474 this.setLocalHost_(localHost); 396 this.localHostSection_.setModel(localHost,
397 remoting.HostController.State.STARTED,
398 this.lastError_ !== '');
475 }; 399 };
476 400
477 /** 401 /**
478 * Called when the user clicks the button next to the error message. The action 402 * Called when the user clicks the button next to the error message. The action
479 * depends on the error. 403 * depends on the error.
480 * 404 *
481 * @private 405 * @private
482 */ 406 */
483 remoting.HostList.prototype.onErrorClick_ = function() { 407 remoting.HostList.prototype.onErrorClick_ = function() {
484 if (this.lastError_ == remoting.Error.AUTHENTICATION_FAILED) { 408 if (this.lastError_ == remoting.Error.AUTHENTICATION_FAILED) {
(...skipping 15 matching lines...) Expand all
500 } 424 }
501 }; 425 };
502 426
503 /** 427 /**
504 * Key name under which Me2Me hosts are cached. 428 * Key name under which Me2Me hosts are cached.
505 */ 429 */
506 remoting.HostList.HOSTS_KEY = 'me2me-cached-hosts'; 430 remoting.HostList.HOSTS_KEY = 'me2me-cached-hosts';
507 431
508 /** @type {remoting.HostList} */ 432 /** @type {remoting.HostList} */
509 remoting.hostList = null; 433 remoting.hostList = null;
OLDNEW
« no previous file with comments | « remoting/webapp/crd/js/crd_main.js ('k') | remoting/webapp/crd/js/host_table_entry.js » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698