Supervised user whitelists: Cleanup
[chromium-blink-merge.git] / remoting / webapp / base / js / message_window.js
blob452b95ee9513779a7dc20a0fe7aa5188cf995deb
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.
5 'use strict';
7 /**
8 * @constructor
9 */
10 function MessageWindowImpl() {
11 /**
12 * Used to prevent multiple responses due to the closeWindow handler.
14 * @private {boolean}
16 this.sentReply_ = false;
18 window.addEventListener('message', this.onMessage_.bind(this), false);
21 /**
22 * @param {Window} parentWindow The id of the window that showed the message.
23 * @param {number} messageId The identifier of the message, as supplied by the
24 * parent.
25 * @param {number} result 0 if window was closed without pressing a button;
26 * otherwise the index of the button pressed (e.g., 1 = primary).
27 * @private
29 MessageWindowImpl.prototype.sendReply_ = function(
30 parentWindow, messageId, result) {
31 // Only forward the first reply that we receive.
32 if (!this.sentReply_) {
33 var message = {
34 command: 'messageWindowResult',
35 id: messageId,
36 result: result
38 parentWindow.postMessage(message, '*');
39 this.sentReply_ = true;
40 } else {
41 // Make sure that the reply we're ignoring is from the window close.
42 base.debug.assert(result == 0);
46 /**
47 * Initializes the button with the label and the click handler.
48 * Hides the button if the label is null or undefined.
50 * @param{HTMLElement} button
51 * @param{?string} label
52 * @param{Function} clickHandler
53 * @private
55 MessageWindowImpl.prototype.initButton_ =
56 function(button, label, clickHandler) {
57 if (label) {
58 button.innerText = label;
59 button.addEventListener('click', clickHandler, false);
61 button.hidden = !Boolean(label);
64 /**
65 * Event-handler callback, invoked when the parent window supplies the
66 * message content.
68 * @param{Event} event
69 * @private
71 MessageWindowImpl.prototype.onMessage_ = function(event) {
72 switch (event.data['command']) {
73 case 'show':
74 // Validate the message.
75 var messageId = /** @type {number} */ (event.data['id']);
76 var title = /** @type {string} */ (event.data['title']);
77 var message = /** @type {string} */ (event.data['message']);
78 var infobox = /** @type {string} */ (event.data['infobox']);
79 var buttonLabel = /** @type {string} */ (event.data['buttonLabel']);
80 /** @type {string} */
81 var cancelButtonLabel = (event.data['cancelButtonLabel']);
82 var showSpinner = /** @type {boolean} */ (event.data['showSpinner']);
83 if (typeof(messageId) != 'number' ||
84 typeof(title) != 'string' ||
85 typeof(message) != 'string' ||
86 typeof(infobox) != 'string' ||
87 typeof(buttonLabel) != 'string' ||
88 typeof(showSpinner) != 'boolean') {
89 console.log('Bad show message:', event.data);
90 break;
93 // Set the dialog text.
94 var button = document.getElementById('button-primary');
95 var cancelButton = document.getElementById('button-secondary');
96 var messageDiv = document.getElementById('message');
97 var infoboxDiv = document.getElementById('infobox');
99 document.getElementById('title').innerText = title;
100 document.querySelector('title').innerText = title;
101 messageDiv.innerHTML = message;
103 if (showSpinner) {
104 messageDiv.classList.add('waiting');
105 messageDiv.classList.add('prominent');
107 if (infobox != '') {
108 infoboxDiv.innerText = infobox;
109 } else {
110 infoboxDiv.hidden = true;
113 this.initButton_(
114 button,
115 buttonLabel,
116 this.sendReply_.bind(this, event.source, messageId, 1));
118 this.initButton_(
119 cancelButton,
120 cancelButtonLabel,
121 this.sendReply_.bind(this, event.source, messageId, 0));
123 var buttonToFocus = (cancelButtonLabel) ? cancelButton : button;
124 buttonToFocus.focus();
126 // Add a close handler in case the window is closed without clicking one
127 // of the buttons. This will send a 0 as the result.
128 // Note that when a button is pressed, this will result in sendReply_
129 // being called multiple times (once for the button, once for close).
130 chrome.app.window.current().onClosed.addListener(
131 this.sendReply_.bind(this, event.source, messageId, 0));
133 base.resizeWindowToContent();
134 chrome.app.window.current().show();
135 break;
137 case 'update_message':
138 var message = /** @type {string} */ (event.data['message']);
139 if (typeof(message) != 'string') {
140 console.log('Bad update_message message:', event.data);
141 break;
144 var messageDiv = document.getElementById('message');
145 messageDiv.innerText = message;
147 base.resizeWindowToContent();
148 break;
150 default:
151 console.error('Unexpected message:', event.data);
155 var messageWindow = new MessageWindowImpl();