1// Copyright 2014 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.
4
5'use strict';
6
7/** @suppress {duplicate} */
8var remoting = remoting || {};
9
10/**
11 * HostInstallDialog prompts the user to install host components.
12 *
13 * @constructor
14 */
15remoting.HostInstallDialog = function() {
16  this.continueInstallButton_ = document.getElementById(
17      'host-install-continue');
18  this.cancelInstallButton_ = document.getElementById(
19      'host-install-dismiss');
20  this.retryInstallButton_ = document.getElementById(
21      'host-install-retry');
22
23  this.onOkClickedHandler_ = this.onOkClicked_.bind(this);
24  this.onCancelClickedHandler_ = this.onCancelClicked_.bind(this);
25  this.onRetryClickedHandler_ = this.onRetryClicked_.bind(this);
26
27  this.continueInstallButton_.disabled = false;
28  this.cancelInstallButton_.disabled = false;
29
30  /** @private*/
31  this.onDoneHandler_ = function() {};
32
33  /** @param {remoting.Error} error @private */
34  this.onErrorHandler_ = function(error) {};
35
36  /**
37   * @type {remoting.HostInstaller}
38   * @private
39   */
40  this.hostInstaller_ = new remoting.HostInstaller();
41};
42
43/**
44 * Starts downloading host components and shows installation prompt.
45 *
46 * @param {function():void} onDone Callback called when user clicks Ok,
47 * presumably after installing the host. The handler must verify that the host
48 * has been installed and call tryAgain() otherwise.
49 * @param {function(remoting.Error):void} onError Callback called when user
50 *    clicks Cancel button or there is some other unexpected error.
51 * @return {void}
52 */
53remoting.HostInstallDialog.prototype.show = function(onDone, onError) {
54  this.continueInstallButton_.addEventListener(
55      'click', this.onOkClickedHandler_, false);
56  this.cancelInstallButton_.addEventListener(
57      'click', this.onCancelClickedHandler_, false);
58  remoting.setMode(remoting.AppMode.HOST_INSTALL_PROMPT);
59
60  /** @type {function():void} */
61  this.onDoneHandler_ = onDone;
62
63  /** @type {function(remoting.Error):void} */
64  this.onErrorHandler_ = onError;
65
66  /** @type {remoting.HostInstaller} */
67  var hostInstaller = new remoting.HostInstaller();
68
69  /** @type {remoting.HostInstallDialog} */
70  var that = this;
71
72  this.hostInstaller_.downloadAndWaitForInstall().then(function() {
73    that.continueInstallButton_.click();
74    that.hostInstaller_.cancel();
75  }, function(){
76    that.onErrorHandler_(remoting.Error.CANCELLED);
77    that.hostInstaller_.cancel();
78  });
79};
80
81/**
82 * In manual host installation, onDone handler must call this method if it
83 * detects that the host components are still unavailable. The same onDone
84 * and onError callbacks will be used when user clicks Ok or Cancel.
85 */
86remoting.HostInstallDialog.prototype.tryAgain = function() {
87  this.retryInstallButton_.addEventListener(
88      'click', this.onRetryClickedHandler_.bind(this), false);
89  remoting.setMode(remoting.AppMode.HOST_INSTALL_PENDING);
90  this.continueInstallButton_.disabled = false;
91  this.cancelInstallButton_.disabled = false;
92};
93
94remoting.HostInstallDialog.prototype.onOkClicked_ = function() {
95  this.continueInstallButton_.removeEventListener(
96      'click', this.onOkClickedHandler_, false);
97  this.cancelInstallButton_.removeEventListener(
98      'click', this.onCancelClickedHandler_, false);
99  this.continueInstallButton_.disabled = true;
100  this.cancelInstallButton_.disabled = true;
101
102  this.onDoneHandler_();
103};
104
105remoting.HostInstallDialog.prototype.onCancelClicked_ = function() {
106  this.continueInstallButton_.removeEventListener(
107      'click', this.onOkClickedHandler_, false);
108  this.cancelInstallButton_.removeEventListener(
109      'click', this.onCancelClickedHandler_, false);
110  this.hostInstaller_.cancel();
111  this.onErrorHandler_(remoting.Error.CANCELLED);
112};
113
114remoting.HostInstallDialog.prototype.onRetryClicked_ = function() {
115  this.retryInstallButton_.removeEventListener(
116      'click', this.onRetryClickedHandler_.bind(this), false);
117  this.continueInstallButton_.addEventListener(
118      'click', this.onOkClickedHandler_, false);
119  this.cancelInstallButton_.addEventListener(
120      'click', this.onCancelClickedHandler_, false);
121  remoting.setMode(remoting.AppMode.HOST_INSTALL_PROMPT);
122};
123