summaryrefslogtreecommitdiffstats
path: root/remoting/webapp/paired_client_manager.js
blob: d001cf31b5c7e38d29abf74152d8f8fcd3503bb7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
// Copyright 2013 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

/**
 * @fileoverview
 * Dialog for showing the list of clients that are paired with this host.
 */

'use strict';

/** @suppress {duplicate} */
var remoting = remoting || {};

/**
 * Extract the appropriate fields from the input parameter, if present. Use the
 * isValid() method to determine whether or not a valid paired client instance
 * was provided.
 *
 * @param {Object} pairedClient The paired client, as returned by the native
 *     host instance.
 * @constructor
 */
remoting.PairedClient = function(pairedClient) {
  if (!pairedClient || typeof(pairedClient) != 'object') {
    return;
  }

  this.clientId = /** @type {string} */ (pairedClient['clientId']);
  this.clientName = /** @type {string} */ (pairedClient['clientName']);
  this.createdTime = /** @type {number} */ (pairedClient['createdTime']);

  /** @type {Element} */
  this.tableRow = null;
  /** @type {Element} */
  this.deleteButton = null;
};

/**
 * Create the DOM elements representing this client in the paired client
 * manager dialog.
 *
 * @param {remoting.PairedClientManager} parent The paired client manager
 *     dialog containing this row.
 * @param {Element} tbody The <tbody> element to which to append the row.
 */
remoting.PairedClient.prototype.createDom = function(parent, tbody) {
  this.tableRow = document.createElement('tr');
  var td = document.createElement('td');
  td.innerText = new Date(this.createdTime).toLocaleDateString();
  this.tableRow.appendChild(td);
  td = document.createElement('td');
  td.innerText = this.clientName;
  this.tableRow.appendChild(td);
  td = document.createElement('td');
  this.deleteButton = document.createElement('a');
  this.deleteButton.href = '#';
  this.deleteButton.innerText = chrome.i18n.getMessage(
      /*i18n-content*/'DELETE_PAIRED_CLIENT');
  this.deleteButton.id = 'delete-client-' + this.clientId;
  this.deleteButton.addEventListener(
      'click',
      parent.deletePairedClient.bind(parent, this),
      false);
  td.appendChild(this.deleteButton);
  this.tableRow.appendChild(td);
  tbody.appendChild(this.tableRow);
};

/**
 * Show or hide the "Delete" button for this row.
 *
 * @param {boolean} show True to show the button; false to hide it.
 */
remoting.PairedClient.prototype.showButton = function(show) {
  this.deleteButton.hidden = !show;
};

/**
 * @return {boolean} True if the constructor parameter was a well-formed
 *     paired client instance.
 */
remoting.PairedClient.prototype.isValid = function() {
  return typeof(this.clientId) == 'string' &&
         typeof(this.clientName) == 'string' &&
         typeof(this.createdTime) == 'number';
};

/**
 * Converts a raw object to an array of PairedClient instances. Returns null if
 * the input object is incorrectly formatted.
 *
 * @param {*} pairedClients The object to convert.
 * @return {Array.<remoting.PairedClient>} The converted result.
 */
remoting.PairedClient.convertToPairedClientArray = function(pairedClients) {
  if (!(pairedClients instanceof Array)) {
    console.error('pairedClients is not an Array:', pairedClients);
    return null;
  }

  var result = [];
  for (var i = 0; i < pairedClients.length; i++) {
    var pairedClient = new remoting.PairedClient(pairedClients[i]);
    if (!pairedClient.isValid()) {
      console.error('pairedClient[' + i + '] has incorrect format:',
                    /** @type {*} */(pairedClients[i]));
      return null;
    }
    result.push(pairedClient);
  }
  return result;
}

/**
 * @param {remoting.HostController} hostController
 * @param {HTMLElement} listContainer HTML <div> to contain the list of paired
 *     clients.
 * @param {HTMLElement} message HTML <div> containing the message notifying
 *     the user that clients are paired and containing the link to open the
 *     dialog.
 * @param {HTMLElement} deleteAllButton HTML <button> inititating the "delete
 *     all" action.
 * @param {HTMLElement} closeButton HTML <button> to close the dialog.
 * @param {HTMLElement} noPairedClients HTML <div> containing a message shown
 *     when all clients have been deleted.
 * @param {HTMLElement} workingSpinner HTML element containing a spinner
 *     graphic shown while a deletion is in progress.
 * @param {HTMLElement} errorDiv HTML <div> containing an error message shown
 *     if a delete operation fails.
 * @constructor
 */
remoting.PairedClientManager = function(hostController, listContainer, message,
                                        deleteAllButton, closeButton,
                                        noPairedClients, workingSpinner,
                                        errorDiv) {
  /**
   * @private
   */
  this.hostController_ = hostController;
  /**
   * @private
   */
  this.message_ = message;
  /**
   * @private
   */
  this.deleteAllButton_ = deleteAllButton;
  /**
   * @private
   */
  this.closeButton_ = closeButton;
  /**
   * @private
   */
  this.noPairedClients_ = noPairedClients;
  /**
   * @private
   */
  this.workingSpinner_ = workingSpinner;
  /**
   * @private
   */
  this.errorDiv_ = errorDiv;
  /**
   * @type {Element}
   * @private
   */
  this.clientRows_ = listContainer.querySelector('tbody');
  /**
   * @type {Array.<remoting.PairedClient>}
   */
  this.pairedClients_ = [];

  this.deleteAllButton_.addEventListener('click',
                                         this.deleteAll_.bind(this),
                                         false);
};

/**
 * Populate the dialog with the list of paired clients and show or hide the
 * message as appropriate.
 *
 * @param {*} pairedClients The list of paired clients as returned by the
 *     native host component.
 * @return {void} Nothing.
 */
remoting.PairedClientManager.prototype.setPairedClients =
    function(pairedClients) {
  // Reset table.
  while (this.clientRows_.lastChild) {
    this.clientRows_.removeChild(this.clientRows_.lastChild);
  }

  this.pairedClients_ =
    remoting.PairedClient.convertToPairedClientArray(pairedClients);
  for (var i = 0; i < this.pairedClients_.length; ++i) {
    var client = this.pairedClients_[i];
    client.createDom(this, this.clientRows_);
  }

  // Show or hide the "this computer has paired clients" message.
  this.setWorking_(false)
};

/**
 * Enter or leave "working" mode. This indicates to the user that a delete
 * operation is in progress. All dialog UI is disabled until it completes.
 *
 * @param {boolean} working True to enter "working" mode; false to leave it.
 * @private
 */
remoting.PairedClientManager.prototype.setWorking_ = function(working) {
  var hasPairedClients = (this.pairedClients_.length != 0);
  for (var i = 0; i < this.pairedClients_.length; ++i) {
    this.pairedClients_[i].showButton(!working);
  }
  this.closeButton_.disabled = working;
  this.workingSpinner_.hidden = !working;
  this.errorDiv_.hidden = true;
  this.message_.hidden = !hasPairedClients;
  this.deleteAllButton_.disabled = working || !hasPairedClients;
  this.noPairedClients_.hidden = hasPairedClients;
};

/**
 * Error callback for delete operations.
 *
 * @param {remoting.Error} error The error message.
 * @private
 */
remoting.PairedClientManager.prototype.onError_ = function(error) {
  this.setWorking_(false);
  l10n.localizeElementFromTag(this.errorDiv_, error);
  this.errorDiv_.hidden = false;
};

/**
 * Delete a single paired client.
 *
 * @param {remoting.PairedClient} client The pairing to delete.
 */
remoting.PairedClientManager.prototype.deletePairedClient = function(client) {
  this.setWorking_(true);
  this.hostController_.deletePairedClient(client.clientId,
      this.setWorking_.bind(this, false),
      this.onError_.bind(this));
  this.clientRows_.removeChild(client.tableRow);
  for (var i = 0; i < this.pairedClients_.length; ++i) {
    if (this.pairedClients_[i] == client) {
      this.pairedClients_.splice(i, 1);
      break;
    }
  }
};

/**
 * Delete all paired clients.
 *
 * @private
 */
remoting.PairedClientManager.prototype.deleteAll_ = function() {
  this.setWorking_(true);
  this.hostController_.clearPairedClients(
      this.setWorking_.bind(this, false),
      this.onError_.bind(this));

  while (this.clientRows_.lastChild) {
    this.clientRows_.removeChild(this.clientRows_.lastChild);
  }
  this.pairedClients_ = [];
};

/**
 * Get the id of the first paired client for testing.
 *
 * @private
 * @return {string} The client id of the first paired client in the list.
 */
remoting.PairedClientManager.prototype.getFirstClientIdForTesting_ =
    function() {
  return this.pairedClients_.length > 0 ? this.pairedClients_[0].clientId : '';
};


/** @type {remoting.PairedClientManager} */
remoting.pairedClientManager = null;