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

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: Reviewer's feedbacks with unit tests 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
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 document.getElementById('daemon-control'),
80 * @type {remoting.HostController.State} 80 new remoting.LocalHostSection.Controller(this));
Jamie 2015/02/27 18:19:40 These elements (or a suitable parent through which
kelvinp 2015/03/03 21:57:52 Acknowledged. I simply moved the line to query the
Jamie 2015/03/04 00:09:24 "this" being what, in this context? Refactoring th
kelvinp 2015/03/04 02:02:28 Correct, what I meant is that I can refactor the D
81 * @private
82 */
83 this.localHostState_ = remoting.HostController.State.UNKNOWN;
84 81
85 /** 82 /**
86 * @type {number} 83 * @type {number}
87 * @private 84 * @private
88 */ 85 */
89 this.webappMajorVersion_ = parseInt(chrome.runtime.getManifest().version, 10); 86 this.webappMajorVersion_ = parseInt(chrome.runtime.getManifest().version, 10);
90 87
91 this.errorButton_.addEventListener('click', 88 this.errorButton_.addEventListener('click',
92 this.onErrorClick_.bind(this), 89 this.onErrorClick_.bind(this),
93 false); 90 false);
94 var reloadButton = this.loadingIndicator_.firstElementChild; 91 var reloadButton = this.loadingIndicator_.firstElementChild;
95 /** @type {remoting.HostList} */ 92 /** @type {remoting.HostList} */
96 var that = this; 93 var that = this;
97 /** @param {Event} event */ 94 /** @param {Event} event */
98 function refresh(event) { 95 function refresh(event) {
99 event.preventDefault(); 96 event.preventDefault();
100 that.refresh(that.display.bind(that)); 97 that.refresh(that.display.bind(that));
101 }; 98 }
102 reloadButton.addEventListener('click', refresh, false); 99 reloadButton.addEventListener('click', refresh, false);
103 }; 100 };
104 101
105 /** 102 /**
106 * Load the host-list asynchronously from local storage. 103 * Load the host-list asynchronously from local storage.
107 * 104 *
108 * @param {function():void} onDone Completion callback. 105 * @param {function():void} onDone Completion callback.
109 */ 106 */
110 remoting.HostList.prototype.load = function(onDone) { 107 remoting.HostList.prototype.load = function(onDone) {
111 // Load the cache of the last host-list, if present. 108 // 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'); 248 /*i18n-content*/'RETRY');
252 } 249 }
253 } else { 250 } else {
254 for (var i = 0; i < this.hosts_.length; ++i) { 251 for (var i = 0; i < this.hosts_.length; ++i) {
255 /** @type {remoting.Host} */ 252 /** @type {remoting.Host} */
256 var host = this.hosts_[i]; 253 var host = this.hosts_[i];
257 // Validate the entry to make sure it has all the fields we expect and is 254 // 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 255 // not the local host (which is displayed separately). NB: if the host has
259 // never sent a heartbeat, then there will be no jabberId. 256 // never sent a heartbeat, then there will be no jabberId.
260 if (host.hostName && host.hostId && host.status && host.publicKey && 257 if (host.hostName && host.hostId && host.status && host.publicKey &&
261 (!this.localHost_ || host.hostId != this.localHost_.hostId)) { 258 (host.hostId != this.localHostSection_.getHostId())) {
262 var hostTableEntry = new remoting.HostTableEntry( 259 var hostTableEntry = new remoting.HostTableEntry(
263 host, this.webappMajorVersion_, 260 this.webappMajorVersion_,
264 this.renameHost_.bind(this), this.deleteHost_.bind(this)); 261 remoting.connectMe2Me,
265 hostTableEntry.createDom(); 262 this.renameHost.bind(this),
263 this.deleteHost_.bind(this));
264 hostTableEntry.setHost(host);
266 this.hostTableEntries_[i] = hostTableEntry; 265 this.hostTableEntries_[i] = hostTableEntry;
267 this.table_.appendChild(hostTableEntry.tableRow); 266 this.table_.appendChild(hostTableEntry.element());
268 } 267 }
269 } 268 }
270 } 269 }
271 270
272 this.errorMsg_.parentNode.hidden = (this.lastError_ == ''); 271 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) { 272 if (noHostsRegistered) {
294 this.showHostListEmptyMessage_(canChangeLocalHostState); 273 this.showHostListEmptyMessage_(this.localHostSection_.canChangeState());
295 } 274 }
296 }; 275 };
297 276
298 /** 277 /**
299 * Displays a message to the user when the host list is empty. 278 * Displays a message to the user when the host list is empty.
300 * 279 *
301 * @param {boolean} hostingSupported 280 * @param {boolean} hostingSupported
302 * @return {void} 281 * @return {void}
303 * @private 282 * @private
304 */ 283 */
(...skipping 25 matching lines...) Expand all
330 ); 309 );
331 }; 310 };
332 311
333 /** 312 /**
334 * Remove a host from the list, and deregister it. 313 * Remove a host from the list, and deregister it.
335 * @param {remoting.HostTableEntry} hostTableEntry The host to be removed. 314 * @param {remoting.HostTableEntry} hostTableEntry The host to be removed.
336 * @return {void} Nothing. 315 * @return {void} Nothing.
337 * @private 316 * @private
338 */ 317 */
339 remoting.HostList.prototype.deleteHost_ = function(hostTableEntry) { 318 remoting.HostList.prototype.deleteHost_ = function(hostTableEntry) {
340 this.table_.removeChild(hostTableEntry.tableRow); 319 this.table_.removeChild(hostTableEntry.element());
341 var index = this.hostTableEntries_.indexOf(hostTableEntry); 320 var index = this.hostTableEntries_.indexOf(hostTableEntry);
342 if (index != -1) { 321 if (index != -1) {
343 this.hostTableEntries_.splice(index, 1); 322 this.hostTableEntries_.splice(index, 1);
344 } 323 }
345 remoting.HostList.unregisterHostById(hostTableEntry.host.hostId); 324 remoting.HostList.unregisterHostById(hostTableEntry.host.hostId);
346 }; 325 };
347 326
348 /** 327 /**
349 * Prepare a host for renaming by replacing its name with an edit box. 328 * Prepare a host for renaming by replacing its name with an edit box.
350 * @param {remoting.HostTableEntry} hostTableEntry The host to be renamed. 329 * @param {remoting.HostTableEntry} hostTableEntry The host to be renamed.
351 * @return {void} Nothing. 330 * @return {void} Nothing.
352 * @private
353 */ 331 */
354 remoting.HostList.prototype.renameHost_ = function(hostTableEntry) { 332 remoting.HostList.prototype.renameHost = function(hostTableEntry) {
355 for (var i = 0; i < this.hosts_.length; ++i) { 333 for (var i = 0; i < this.hosts_.length; ++i) {
356 if (this.hosts_[i].hostId == hostTableEntry.host.hostId) { 334 if (this.hosts_[i].hostId == hostTableEntry.host.hostId) {
357 this.hosts_[i].hostName = hostTableEntry.host.hostName; 335 this.hosts_[i].hostName = hostTableEntry.host.hostName;
358 break; 336 break;
359 } 337 }
360 } 338 }
361 this.save_(); 339 this.save_();
362 340
363 remoting.hostListApi.put(hostTableEntry.host.hostId, 341 remoting.hostListApi.put(hostTableEntry.host.hostId,
364 hostTableEntry.host.hostName, 342 hostTableEntry.host.hostName,
365 hostTableEntry.host.publicKey, 343 hostTableEntry.host.publicKey,
366 function() {}, 344 function() {},
367 remoting.showErrorMessage); 345 remoting.showErrorMessage);
368 }; 346 };
369 347
370 /** 348 /**
371 * Unregister a host. 349 * Unregister a host.
372 * @param {string} hostId The id of the host to be removed. 350 * @param {string} hostId The id of the host to be removed.
373 * @return {void} Nothing. 351 * @return {void} Nothing.
374 */ 352 */
375 remoting.HostList.unregisterHostById = function(hostId) { 353 remoting.HostList.unregisterHostById = function(hostId) {
376 remoting.hostListApi.remove(hostId, function() {}, remoting.showErrorMessage); 354 remoting.hostListApi.remove(hostId, function() {}, remoting.showErrorMessage);
377 }; 355 };
378 356
379 /** 357 /**
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. 358 * Set the state of the local host and localHostId if any.
399 * 359 *
400 * @param {remoting.HostController.State} state State of the local host. 360 * @param {remoting.HostController.State} state State of the local host.
401 * @param {string?} hostId ID of the local host, or null. 361 * @param {string?} hostId ID of the local host, or null.
402 * @return {void} Nothing. 362 * @return {void} Nothing.
403 */ 363 */
404 remoting.HostList.prototype.setLocalHostStateAndId = function(state, hostId) { 364 remoting.HostList.prototype.setLocalHostStateAndId = function(state, hostId) {
405 this.localHostState_ = state; 365 var host = hostId ? this.getHostForId(hostId) : null;
406 this.setLocalHost_(hostId ? this.getHostForId(hostId) : null); 366 this.localHostSection_.setModel(host, state, this.lastError_ !== '');
407 } 367 };
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 368
448 /** 369 /**
449 * Called by the HostControlled after the local host has been started. 370 * Called by the HostControlled after the local host has been started.
450 * 371 *
451 * @param {string} hostName Host name. 372 * @param {string} hostName Host name.
452 * @param {string} hostId ID of the local host. 373 * @param {string} hostId ID of the local host.
453 * @param {string} publicKey Public key. 374 * @param {string} publicKey Public key.
454 * @return {void} Nothing. 375 * @return {void} Nothing.
455 */ 376 */
456 remoting.HostList.prototype.onLocalHostStarted = function( 377 remoting.HostList.prototype.onLocalHostStarted = function(
457 hostName, hostId, publicKey) { 378 hostName, hostId, publicKey) {
458 // Create a dummy remoting.Host instance to represent the local host. 379 // Create a dummy remoting.Host instance to represent the local host.
459 // Refreshing the list is no good in general, because the directory 380 // 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 381 // 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. 382 // 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, 383 // 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. 384 // and we hope that the directory will have been updated by that point.
464 var localHost = new remoting.Host(); 385 var localHost = new remoting.Host();
465 localHost.hostName = hostName; 386 localHost.hostName = hostName;
466 // Provide a version number to avoid warning about this dummy host being 387 // Provide a version number to avoid warning about this dummy host being
467 // out-of-date. 388 // out-of-date.
468 localHost.hostVersion = String(this.webappMajorVersion_) + ".x" 389 localHost.hostVersion = String(this.webappMajorVersion_) + ".x"
469 localHost.hostId = hostId; 390 localHost.hostId = hostId;
470 localHost.publicKey = publicKey; 391 localHost.publicKey = publicKey;
471 localHost.status = 'ONLINE'; 392 localHost.status = 'ONLINE';
472 this.hosts_.push(localHost); 393 this.hosts_.push(localHost);
473 this.save_(); 394 this.save_();
474 this.setLocalHost_(localHost); 395 this.localHostSection_.setModel(localHost,
396 remoting.HostController.State.STARTED,
397 this.lastError_ !== '');
475 }; 398 };
476 399
477 /** 400 /**
478 * Called when the user clicks the button next to the error message. The action 401 * Called when the user clicks the button next to the error message. The action
479 * depends on the error. 402 * depends on the error.
480 * 403 *
481 * @private 404 * @private
482 */ 405 */
483 remoting.HostList.prototype.onErrorClick_ = function() { 406 remoting.HostList.prototype.onErrorClick_ = function() {
484 if (this.lastError_ == remoting.Error.AUTHENTICATION_FAILED) { 407 if (this.lastError_ == remoting.Error.AUTHENTICATION_FAILED) {
(...skipping 15 matching lines...) Expand all
500 } 423 }
501 }; 424 };
502 425
503 /** 426 /**
504 * Key name under which Me2Me hosts are cached. 427 * Key name under which Me2Me hosts are cached.
505 */ 428 */
506 remoting.HostList.HOSTS_KEY = 'me2me-cached-hosts'; 429 remoting.HostList.HOSTS_KEY = 'me2me-cached-hosts';
507 430
508 /** @type {remoting.HostList} */ 431 /** @type {remoting.HostList} */
509 remoting.hostList = null; 432 remoting.hostList = null;
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698