1 // Copyright 2015 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 var remoting = remoting || {};
12 * @param {HTMLElement} rootElement
13 * @param {remoting.LocalHostSection.Controller} controller
16 * @implements {base.Disposable}
18 remoting.LocalHostSection = function(rootElement, controller) {
20 this.rootElement_ = rootElement;
22 this.controller_ = controller;
23 /** @private {remoting.Host} */
25 /** @private {remoting.HostController.State} */
26 this.state_ = remoting.HostController.State.UNKNOWN;
28 var hostContainer = rootElement.querySelector('.host-entry');
30 this.hostTableEntry_ = new remoting.HostTableEntry(
31 parseInt(chrome.runtime.getManifest().version, 10),
32 controller.connect.bind(controller),
33 this.rename_.bind(this));
34 hostContainer.appendChild(this.hostTableEntry_.element());
35 this.hostTableEntry_.element().id = 'local-host-connect-button';
37 var startButton = rootElement.querySelector('.start-daemon');
38 var stopButton = rootElement.querySelector('.stop-daemon');
39 var stopLocalDaemonButton = rootElement.querySelector('.stop-local-daemon');
40 var changePINButton = rootElement.querySelector('.change-daemon-pin');
43 this.eventHooks_ = new base.Disposables(
44 new base.DomEventHook(startButton, 'click',
45 controller.start.bind(controller), false),
46 new base.DomEventHook(stopButton, 'click',
47 controller.stop.bind(controller), false),
48 new base.DomEventHook(stopLocalDaemonButton, 'click',
49 controller.stop.bind(controller), false),
50 new base.DomEventHook(changePINButton, 'click',
51 controller.changePIN.bind(controller), false));
53 this.hasError_ = false;
56 remoting.LocalHostSection.prototype.dispose = function() {
57 base.dispose(this.eventHooks_);
58 this.eventHooks_ = null;
62 * @param {remoting.Host} host
63 * @param {remoting.HostController.State} state
64 * @param {boolean} hasError Whether the host list is in an error state.
66 remoting.LocalHostSection.prototype.setModel = function(host, state, hasError) {
69 this.hasError_ = hasError;
76 remoting.LocalHostSection.prototype.getHostId = function() {
77 return this.host_ ? this.host_.hostId : null;
80 /** @return {boolean} */
81 remoting.LocalHostSection.prototype.isEnabled_ = function() {
82 return (this.state_ == remoting.HostController.State.STARTING) ||
83 (this.state_ == remoting.HostController.State.STARTED);
86 /** @return {boolean} */
87 remoting.LocalHostSection.prototype.canChangeState = function() {
88 // The local host cannot be stopped or started if the host controller is not
89 // implemented for this platform.
90 var state = this.state_;
91 if (state === remoting.HostController.State.NOT_IMPLEMENTED ||
92 state === remoting.HostController.State.UNKNOWN) {
96 // Return false if the host is uninstallable. The NOT_INSTALLED check is
97 // required to handle the special case for Ubuntu, as we report the host as
98 // uninstallable on Linux.
99 if (!this.isMe2MeInstallable_() &&
100 state === remoting.HostController.State.NOT_INSTALLED) {
104 // In addition, it cannot be started if there is an error (in many error
105 // states, the start operation will fail anyway, but even if it succeeds, the
106 // chance of a related but hard-to-diagnose future error is high).
107 return this.isEnabled_() || !this.hasError_;
111 * Returns true if the current platform is fully supported. It's only used when
112 * we detect that host native messaging components are not installed. In that
113 * case the result of this function determines if the webapp should show the
114 * controls that allow to install and enable Me2Me host.
119 remoting.LocalHostSection.prototype.isMe2MeInstallable_ = function() {
120 // The chromoting host is currently not installable on ChromeOS.
121 // For Linux, we have a install package for Ubuntu but not other distros.
122 // Since we cannot tell from javascript alone the Linux distro the client is
123 // on, we don't show the daemon-control UI for Linux unless the host is
125 return remoting.platformIsWindows() || remoting.platformIsMac();
129 remoting.LocalHostSection.prototype.updateUI_ = function() {
130 this.hostTableEntry_.setHost(this.host_);
133 var enabled = this.isEnabled_();
134 var canChangeLocalHostState = this.canChangeState();
135 var daemonState = '';
137 daemonState = 'disabled';
138 } else if (this.host_ !== null) {
139 daemonState = 'enabled';
141 daemonState = 'enabled-other-account';
143 remoting.updateModalUi(daemonState, 'data-daemon-state');
144 this.rootElement_.hidden = !canChangeLocalHostState;
147 remoting.LocalHostSection.prototype.rename_ = function() {
148 return this.controller_.rename(this.hostTableEntry_);
153 * @param {remoting.HostList} hostList
154 * @param {remoting.HostSetupDialog} setupDialog
155 * @param {function(string)} handleConnect Function to call to connect to the
156 * host with |hostId|.
158 remoting.LocalHostSection.Controller =
159 function(hostList, setupDialog, handleConnect) {
161 this.hostList_ = hostList;
163 this.setupDialog_ = setupDialog;
165 this.handleConnect_ = handleConnect;
168 remoting.LocalHostSection.Controller.prototype.start = function() {
169 this.setupDialog_.showForStart();
172 remoting.LocalHostSection.Controller.prototype.stop = function() {
173 this.setupDialog_.showForStop();
176 remoting.LocalHostSection.Controller.prototype.changePIN = function() {
177 this.setupDialog_.showForPin();
180 /** @param {remoting.HostTableEntry} host */
181 remoting.LocalHostSection.Controller.prototype.rename = function(host) {
182 this.hostList_.renameHost(host);
185 /** @param {string} hostId */
186 remoting.LocalHostSection.Controller.prototype.connect = function(hostId) {
187 this.handleConnect_(hostId);