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.
10 function MessageWindowImpl() {
12 * Used to prevent multiple responses due to the closeWindow handler.
16 this.sentReply_
= false;
18 window
.addEventListener('message', this.onMessage_
.bind(this), false);
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
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).
29 MessageWindowImpl
.prototype.sendReply_ = function(
30 parentWindow
, messageId
, result
) {
31 // Only forward the first reply that we receive.
32 if (!this.sentReply_
) {
34 command
: 'messageWindowResult',
38 parentWindow
.postMessage(message
, '*');
39 this.sentReply_
= true;
41 // Make sure that the reply we're ignoring is from the window close.
42 base
.debug
.assert(result
== 0);
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
55 MessageWindowImpl
.prototype.initButton_
=
56 function(button
, label
, clickHandler
) {
58 button
.innerText
= label
;
59 button
.addEventListener('click', clickHandler
, false);
61 button
.hidden
= !Boolean(label
);
65 * Event-handler callback, invoked when the parent window supplies the
71 MessageWindowImpl
.prototype.onMessage_ = function(event
) {
72 switch (event
.data
['command']) {
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']);
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
);
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
;
104 messageDiv
.classList
.add('waiting');
105 messageDiv
.classList
.add('prominent');
108 infoboxDiv
.innerText
= infobox
;
110 infoboxDiv
.hidden
= true;
116 this.sendReply_
.bind(this, event
.source
, messageId
, 1));
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();
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
);
144 var messageDiv
= document
.getElementById('message');
145 messageDiv
.innerText
= message
;
147 base
.resizeWindowToContent();
151 console
.error('Unexpected message:', event
.data
);
155 var messageWindow
= new MessageWindowImpl();