OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 'use strict'; | 5 'use strict'; |
6 | 6 |
7 /** | 7 /** |
8 * @constructor | 8 * @constructor |
9 */ | 9 */ |
10 function MessageWindowImpl() { | 10 function MessageWindowImpl() { |
11 /** | 11 /** |
12 * Used to prevent multiple responses due to the closeWindow handler. | 12 * Used to prevent multiple responses due to the closeWindow handler. |
13 * | 13 * |
14 * @type {boolean} | 14 * @type {boolean} |
15 * @private | 15 * @private |
16 */ | 16 */ |
17 this.sentReply_ = false; | 17 this.sentReply_ = false; |
18 | 18 |
19 window.addEventListener('message', this.onMessage_.bind(this), false); | 19 window.addEventListener('message', this.onMessage_.bind(this), false); |
20 }; | 20 }; |
21 | 21 |
22 /** | 22 /** |
23 * @param {Window} parentWindow The id of the window that showed the message. | 23 * @param {Window} parentWindow The id of the window that showed the message. |
24 * @param {string} messageId The identifier of the message, as supplied by the | 24 * @param {number} messageId The identifier of the message, as supplied by the |
25 * parent. | 25 * parent. |
26 * @param {number} result 0 if window was closed without pressing a button; | 26 * @param {number} result 0 if window was closed without pressing a button; |
27 * otherwise the index of the button pressed (e.g., 1 = primary). | 27 * otherwise the index of the button pressed (e.g., 1 = primary). |
28 * @private | 28 * @private |
29 */ | 29 */ |
30 MessageWindowImpl.prototype.sendReply_ = function( | 30 MessageWindowImpl.prototype.sendReply_ = function( |
31 parentWindow, messageId, result) { | 31 parentWindow, messageId, result) { |
32 // Only forward the first reply that we receive. | 32 // Only forward the first reply that we receive. |
33 if (!this.sentReply_) { | 33 if (!this.sentReply_) { |
34 var message = { | 34 var message = { |
(...skipping 83 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
118 if (showSpinner) { | 118 if (showSpinner) { |
119 messageDiv.classList.add('waiting'); | 119 messageDiv.classList.add('waiting'); |
120 messageDiv.classList.add('prominent'); | 120 messageDiv.classList.add('prominent'); |
121 } | 121 } |
122 if (infobox != '') { | 122 if (infobox != '') { |
123 infoboxDiv.innerText = infobox; | 123 infoboxDiv.innerText = infobox; |
124 } else { | 124 } else { |
125 infoboxDiv.hidden = true; | 125 infoboxDiv.hidden = true; |
126 } | 126 } |
127 | 127 |
128 var messageIdStr = messageId.toString(); | |
129 | |
130 this.initButton_( | 128 this.initButton_( |
131 button, | 129 button, |
132 buttonLabel, | 130 buttonLabel, |
133 this.sendReply_.bind(this, event.source, messageIdStr, 1)); | 131 this.sendReply_.bind(this, event.source, messageId, 1)); |
134 | 132 |
135 this.initButton_( | 133 this.initButton_( |
136 cancelButton, | 134 cancelButton, |
137 cancelButtonLabel, | 135 cancelButtonLabel, |
138 this.sendReply_.bind(this, event.source, messageIdStr, 0)); | 136 this.sendReply_.bind(this, event.source, messageId, 0)); |
139 | 137 |
140 var buttonToFocus = (cancelButtonLabel) ? cancelButton : button; | 138 var buttonToFocus = (cancelButtonLabel) ? cancelButton : button; |
141 buttonToFocus.focus(); | 139 buttonToFocus.focus(); |
142 | 140 |
143 // Add a close handler in case the window is closed without clicking one | 141 // Add a close handler in case the window is closed without clicking one |
144 // of the buttons. This will send a 0 as the result. | 142 // of the buttons. This will send a 0 as the result. |
145 // Note that when a button is pressed, this will result in sendReply_ | 143 // Note that when a button is pressed, this will result in sendReply_ |
146 // being called multiple times (once for the button, once for close). | 144 // being called multiple times (once for the button, once for close). |
147 chrome.app.window.current().onClosed.addListener( | 145 chrome.app.window.current().onClosed.addListener( |
148 this.sendReply_.bind(this, event.source, messageIdStr, 0)); | 146 this.sendReply_.bind(this, event.source, messageId, 0)); |
149 | 147 |
150 this.updateSize_(); | 148 this.updateSize_(); |
151 chrome.app.window.current().show(); | 149 chrome.app.window.current().show(); |
152 break; | 150 break; |
153 | 151 |
154 case 'update_message': | 152 case 'update_message': |
155 var message = /** @type {string} */ (event.data['message']); | 153 var message = /** @type {string} */ (event.data['message']); |
156 if (typeof(message) != 'string') { | 154 if (typeof(message) != 'string') { |
157 console.log('Bad update_message message:', event.data); | 155 console.log('Bad update_message message:', event.data); |
158 break; | 156 break; |
159 } | 157 } |
160 | 158 |
161 var messageDiv = document.getElementById('message'); | 159 var messageDiv = document.getElementById('message'); |
162 messageDiv.innerText = message; | 160 messageDiv.innerText = message; |
163 | 161 |
164 this.updateSize_(); | 162 this.updateSize_(); |
165 break; | 163 break; |
166 | 164 |
167 default: | 165 default: |
168 console.error('Unexpected message:', event.data); | 166 console.error('Unexpected message:', event.data); |
169 } | 167 } |
170 }; | 168 }; |
171 | 169 |
172 var messageWindow = new MessageWindowImpl(); | 170 var messageWindow = new MessageWindowImpl(); |
OLD | NEW |