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

Unified Diff: remoting/webapp/base/js/message_window.js

Issue 1139543002: [Chromoting] Show any startup errors in the LoadingWindow. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Check types for optional fields. Created 5 years, 7 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 side-by-side diff with in-line comments
Download patch
Index: remoting/webapp/base/js/message_window.js
diff --git a/remoting/webapp/base/js/message_window.js b/remoting/webapp/base/js/message_window.js
index f2c36347eb9ffb6128916a5189d4fe056d515ab2..c25f6e367d89d5f13bfe91760465d8d0ef47ff5b 100644
--- a/remoting/webapp/base/js/message_window.js
+++ b/remoting/webapp/base/js/message_window.js
@@ -44,19 +44,16 @@ MessageWindowImpl.prototype.sendReply_ = function(
};
/**
- * Initializes the button with the label and the click handler.
+ * Updates the button label text.
* Hides the button if the label is null or undefined.
*
* @param{HTMLElement} button
* @param{?string} label
- * @param{Function} clickHandler
* @private
*/
-MessageWindowImpl.prototype.initButton_ =
- function(button, label, clickHandler) {
+MessageWindowImpl.prototype.updateButton_ = function(button, label) {
if (label) {
button.innerText = label;
- button.addEventListener('click', clickHandler, false);
}
button.hidden = !Boolean(label);
};
@@ -69,86 +66,89 @@ MessageWindowImpl.prototype.initButton_ =
* @private
*/
MessageWindowImpl.prototype.onMessage_ = function(event) {
- switch (event.data['command']) {
- case 'show':
- // Validate the message.
- var messageId = /** @type {number} */ (event.data['id']);
- var title = /** @type {string} */ (event.data['title']);
- var message = /** @type {string} */ (event.data['message']);
- var infobox = /** @type {string} */ (event.data['infobox']);
- var buttonLabel = /** @type {string} */ (event.data['buttonLabel']);
- /** @type {string} */
- var cancelButtonLabel = (event.data['cancelButtonLabel']);
- var showSpinner = /** @type {boolean} */ (event.data['showSpinner']);
- if (typeof(messageId) != 'number' ||
- typeof(title) != 'string' ||
- typeof(message) != 'string' ||
- typeof(infobox) != 'string' ||
- typeof(buttonLabel) != 'string' ||
- typeof(showSpinner) != 'boolean') {
- console.log('Bad show message:', event.data);
- break;
- }
-
- // Set the dialog text.
- var button = document.getElementById('button-primary');
- var cancelButton = document.getElementById('button-secondary');
- var messageDiv = document.getElementById('message');
- var infoboxDiv = document.getElementById('infobox');
-
- document.getElementById('title').innerText = title;
- document.querySelector('title').innerText = title;
- messageDiv.innerHTML = message;
-
- if (showSpinner) {
- messageDiv.classList.add('waiting');
- messageDiv.classList.add('prominent');
- }
- if (infobox != '') {
- infoboxDiv.innerText = infobox;
- } else {
- infoboxDiv.hidden = true;
- }
-
- this.initButton_(
- button,
- buttonLabel,
- this.sendReply_.bind(this, event.source, messageId, 1));
-
- this.initButton_(
- cancelButton,
- cancelButtonLabel,
- this.sendReply_.bind(this, event.source, messageId, 0));
-
- var buttonToFocus = (cancelButtonLabel) ? cancelButton : button;
- buttonToFocus.focus();
-
- // Add a close handler in case the window is closed without clicking one
- // of the buttons. This will send a 0 as the result.
- // Note that when a button is pressed, this will result in sendReply_
- // being called multiple times (once for the button, once for close).
- chrome.app.window.current().onClosed.addListener(
- this.sendReply_.bind(this, event.source, messageId, 0));
-
- base.resizeWindowToContent(true);
- chrome.app.window.current().show();
- break;
-
- case 'update_message':
- var message = /** @type {string} */ (event.data['message']);
- if (typeof(message) != 'string') {
- console.log('Bad update_message message:', event.data);
- break;
- }
-
- var messageDiv = document.getElementById('message');
- messageDiv.innerText = message;
-
- base.resizeWindowToContent(true);
- break;
-
- default:
- console.error('Unexpected message:', event.data);
+ var command = /** @type {string} */ (event.data['command']);
+ if (command !== 'show' && command !== 'update') {
+ console.error('Unexpected message: ' + command);
+ return;
+ }
+
+ // Validate the message.
+ var messageId = /** @type {number} */ (event.data['id']);
+ var title = /** @type {string} */ (event.data['title']);
+ var message = /** @type {string} */ (event.data['message']);
+ var infobox = /** @type {string} */ (event.data['infobox']);
+ var buttonLabel = /** @type {string} */ (event.data['buttonLabel']);
+ var cancelButtonLabel = /** @type {string} */
+ (event.data['cancelButtonLabel']);
+ var showSpinner = /** @type {boolean} */ (event.data['showSpinner']);
+
+ // Many of these fields are optional for either the 'show' or 'update'
+ // message. These vars are used to mark the optional fields to allow
+ // them to be undefined.
+ var optionalFieldShow = command === 'show';
+ var optionalFieldUpdate = command === 'update';
+ if (isNumber(messageId) ||
+ isString(title, optionalFieldUpdate) ||
+ isString(message, optionalFieldUpdate) ||
+ isString(infobox, optionalFieldUpdate) ||
+ isString(buttonLabel, optionalFieldUpdate) ||
+ isString(cancelButtonLabel, optionalFieldShow || optionalFieldUpdate) ||
+ isBoolean(showSpinner, optionalFieldUpdate) {
+ console.log('Bad ' + command + ' message: ' + event.data);
+ return;
+ }
+
+ var button = document.getElementById('button-primary');
+ var cancelButton = document.getElementById('button-secondary');
+ var messageDiv = document.getElementById('message');
+ var infoboxDiv = document.getElementById('infobox');
+
+ if (isString(title)) {
Jamie 2015/05/13 23:43:17 I think (title !== undefined) would be a more natu
garykac 2015/05/15 01:29:36 Done.
+ document.getElementById('title').innerText = title;
+ document.querySelector('title').innerText = title;
+ }
+ if (isString(message) {
+ messageDiv.innerText = message;
+ }
+ if (isString(infobox)) {
+ if (infobox != '') {
+ infoboxDiv.innerText = infobox;
+ } else {
+ infoboxDiv.hidden = true;
+ }
+ }
+ if (isBoolean(showSpinner)) {
+ if (showSpinner) {
+ messageDiv.classList.add('waiting');
+ messageDiv.classList.add('prominent');
+ } else {
+ messageDiv.classList.remove('waiting');
+ messageDiv.classList.remove('prominent');
+ }
+ }
+ this.updateButton_(button, buttonLabel);
+ this.updateButton_(cancelButton, cancelButtonLabel);
+
+ base.resizeWindowToContent(true);
+
+ if (command === 'show') {
+ // Set up click-handlers for the buttons.
+ button.addEventListener(
+ 'click', this.sendReply_.bind(this, event.source, messageId, 1), false);
+ cancelButton.addEventListener(
+ 'click', this.sendReply_.bind(this, event.source, messageId, 0), false);
+
+ var buttonToFocus = (cancelButtonLabel) ? cancelButton : button;
+ buttonToFocus.focus();
+
+ // Add a close handler in case the window is closed without clicking one
+ // of the buttons. This will send a 0 as the result.
+ // Note that when a button is pressed, this will result in sendReply_
+ // being called multiple times (once for the button, once for close).
+ chrome.app.window.current().onClosed.addListener(
+ this.sendReply_.bind(this, event.source, messageId, 0));
+
+ chrome.app.window.current().show();
}
};

Powered by Google App Engine
This is Rietveld 408576698