Chromium Code Reviews| OLD | NEW |
|---|---|
| 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 Loading... | |
| 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)); |
| 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); |
| (...skipping 157 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 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 this.renameHost.bind(this), |
| 265 hostTableEntry.createDom(); | 262 this.deleteHost_.bind(this)); |
| 263 hostTableEntry.setHost(host); | |
| 266 this.hostTableEntries_[i] = hostTableEntry; | 264 this.hostTableEntries_[i] = hostTableEntry; |
| 267 this.table_.appendChild(hostTableEntry.tableRow); | 265 this.table_.appendChild(hostTableEntry.element()); |
| 268 } | 266 } |
| 269 } | 267 } |
| 270 } | 268 } |
| 271 | 269 |
| 272 this.errorMsg_.parentNode.hidden = (this.lastError_ == ''); | 270 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) { | 271 if (noHostsRegistered) { |
| 294 this.showHostListEmptyMessage_(canChangeLocalHostState); | 272 this.showHostListEmptyMessage_(this.localHostSection_.canChangeState()); |
| 295 } | 273 } |
| 296 }; | 274 }; |
| 297 | 275 |
| 298 /** | 276 /** |
| 299 * Displays a message to the user when the host list is empty. | 277 * Displays a message to the user when the host list is empty. |
| 300 * | 278 * |
| 301 * @param {boolean} hostingSupported | 279 * @param {boolean} hostingSupported |
| 302 * @return {void} | 280 * @return {void} |
| 303 * @private | 281 * @private |
| 304 */ | 282 */ |
| (...skipping 25 matching lines...) Expand all Loading... | |
| 330 ); | 308 ); |
| 331 }; | 309 }; |
| 332 | 310 |
| 333 /** | 311 /** |
| 334 * Remove a host from the list, and deregister it. | 312 * Remove a host from the list, and deregister it. |
| 335 * @param {remoting.HostTableEntry} hostTableEntry The host to be removed. | 313 * @param {remoting.HostTableEntry} hostTableEntry The host to be removed. |
| 336 * @return {void} Nothing. | 314 * @return {void} Nothing. |
| 337 * @private | 315 * @private |
| 338 */ | 316 */ |
| 339 remoting.HostList.prototype.deleteHost_ = function(hostTableEntry) { | 317 remoting.HostList.prototype.deleteHost_ = function(hostTableEntry) { |
| 340 this.table_.removeChild(hostTableEntry.tableRow); | 318 this.table_.removeChild(hostTableEntry.element()); |
| 341 var index = this.hostTableEntries_.indexOf(hostTableEntry); | 319 var index = this.hostTableEntries_.indexOf(hostTableEntry); |
| 342 if (index != -1) { | 320 if (index != -1) { |
| 343 this.hostTableEntries_.splice(index, 1); | 321 this.hostTableEntries_.splice(index, 1); |
| 344 } | 322 } |
| 345 remoting.HostList.unregisterHostById(hostTableEntry.host.hostId); | 323 remoting.HostList.unregisterHostById(hostTableEntry.host.hostId); |
| 346 }; | 324 }; |
| 347 | 325 |
| 348 /** | 326 /** |
| 349 * Prepare a host for renaming by replacing its name with an edit box. | 327 * Prepare a host for renaming by replacing its name with an edit box. |
| 350 * @param {remoting.HostTableEntry} hostTableEntry The host to be renamed. | 328 * @param {remoting.HostTableEntry} hostTableEntry The host to be renamed. |
| 351 * @return {void} Nothing. | 329 * @return {void} Nothing. |
| 352 * @private | |
| 353 */ | 330 */ |
| 354 remoting.HostList.prototype.renameHost_ = function(hostTableEntry) { | 331 remoting.HostList.prototype.renameHost = function(hostTableEntry) { |
| 355 for (var i = 0; i < this.hosts_.length; ++i) { | 332 for (var i = 0; i < this.hosts_.length; ++i) { |
| 356 if (this.hosts_[i].hostId == hostTableEntry.host.hostId) { | 333 if (this.hosts_[i].hostId == hostTableEntry.host.hostId) { |
| 357 this.hosts_[i].hostName = hostTableEntry.host.hostName; | 334 this.hosts_[i].hostName = hostTableEntry.host.hostName; |
| 358 break; | 335 break; |
| 359 } | 336 } |
| 360 } | 337 } |
| 361 this.save_(); | 338 this.save_(); |
| 362 | 339 |
| 363 remoting.hostListApi.put(hostTableEntry.host.hostId, | 340 remoting.hostListApi.put(hostTableEntry.host.hostId, |
| 364 hostTableEntry.host.hostName, | 341 hostTableEntry.host.hostName, |
| 365 hostTableEntry.host.publicKey, | 342 hostTableEntry.host.publicKey, |
| 366 function() {}, | 343 function() {}, |
| 367 remoting.showErrorMessage); | 344 remoting.showErrorMessage); |
| 368 }; | 345 }; |
| 369 | 346 |
| 370 /** | 347 /** |
| 371 * Unregister a host. | 348 * Unregister a host. |
| 372 * @param {string} hostId The id of the host to be removed. | 349 * @param {string} hostId The id of the host to be removed. |
| 373 * @return {void} Nothing. | 350 * @return {void} Nothing. |
| 374 */ | 351 */ |
| 375 remoting.HostList.unregisterHostById = function(hostId) { | 352 remoting.HostList.unregisterHostById = function(hostId) { |
| 376 remoting.hostListApi.remove(hostId, function() {}, remoting.showErrorMessage); | 353 remoting.hostListApi.remove(hostId, function() {}, remoting.showErrorMessage); |
| 377 }; | 354 }; |
| 378 | 355 |
| 379 /** | 356 /** |
| 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. | 357 * Set the state of the local host and localHostId if any. |
| 399 * | 358 * |
| 400 * @param {remoting.HostController.State} state State of the local host. | 359 * @param {remoting.HostController.State} state State of the local host. |
| 401 * @param {string?} hostId ID of the local host, or null. | 360 * @param {string?} hostId ID of the local host, or null. |
| 402 * @return {void} Nothing. | 361 * @return {void} Nothing. |
| 403 */ | 362 */ |
| 404 remoting.HostList.prototype.setLocalHostStateAndId = function(state, hostId) { | 363 remoting.HostList.prototype.setLocalHostStateAndId = function(state, hostId) { |
| 405 this.localHostState_ = state; | 364 var host = hostId ? this.getHostForId(hostId) : null; |
| 406 this.setLocalHost_(hostId ? this.getHostForId(hostId) : null); | 365 if (this.lastError_ !== '') { |
| 407 } | 366 state = remoting.HostController.State.UNKNOWN; |
|
Jamie
2015/02/23 22:28:11
This appears to be a change in behaviour. Is it in
kelvinp
2015/02/24 22:04:44
Done.
| |
| 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 } | 367 } |
| 446 } | 368 this.localHostSection_.setModel(host, state); |
| 369 }; | |
| 447 | 370 |
| 448 /** | 371 /** |
| 449 * Called by the HostControlled after the local host has been started. | 372 * Called by the HostControlled after the local host has been started. |
| 450 * | 373 * |
| 451 * @param {string} hostName Host name. | 374 * @param {string} hostName Host name. |
| 452 * @param {string} hostId ID of the local host. | 375 * @param {string} hostId ID of the local host. |
| 453 * @param {string} publicKey Public key. | 376 * @param {string} publicKey Public key. |
| 454 * @return {void} Nothing. | 377 * @return {void} Nothing. |
| 455 */ | 378 */ |
| 456 remoting.HostList.prototype.onLocalHostStarted = function( | 379 remoting.HostList.prototype.onLocalHostStarted = function( |
| 457 hostName, hostId, publicKey) { | 380 hostName, hostId, publicKey) { |
| 458 // Create a dummy remoting.Host instance to represent the local host. | 381 // Create a dummy remoting.Host instance to represent the local host. |
| 459 // Refreshing the list is no good in general, because the directory | 382 // 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 | 383 // 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. | 384 // 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, | 385 // 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. | 386 // and we hope that the directory will have been updated by that point. |
| 464 var localHost = new remoting.Host(); | 387 var localHost = new remoting.Host(); |
| 465 localHost.hostName = hostName; | 388 localHost.hostName = hostName; |
| 466 // Provide a version number to avoid warning about this dummy host being | 389 // Provide a version number to avoid warning about this dummy host being |
| 467 // out-of-date. | 390 // out-of-date. |
| 468 localHost.hostVersion = String(this.webappMajorVersion_) + ".x" | 391 localHost.hostVersion = String(this.webappMajorVersion_) + ".x" |
| 469 localHost.hostId = hostId; | 392 localHost.hostId = hostId; |
| 470 localHost.publicKey = publicKey; | 393 localHost.publicKey = publicKey; |
| 471 localHost.status = 'ONLINE'; | 394 localHost.status = 'ONLINE'; |
| 472 this.hosts_.push(localHost); | 395 this.hosts_.push(localHost); |
| 473 this.save_(); | 396 this.save_(); |
| 474 this.setLocalHost_(localHost); | 397 this.localHostSection_.setModel(localHost, |
| 398 remoting.HostController.State.STARTED); | |
| 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 Loading... | |
| 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; |
| OLD | NEW |