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.
7 /** @suppress {duplicate} */
8 var remoting = remoting || {};
11 * Create a new message window.
13 * @param {Object} options Message window create options
16 remoting.MessageWindow = function(options) {
17 var title = /** @type {string} */ (options.title);
18 var message = /** @type {string} */ (options.message);
19 var okButtonLabel = /** @type {string} */ (options.buttonLabel);
20 var cancelButtonLabel = /** @type {string} */ (options.cancelButtonLabel);
21 var onResult = /** @type {function(number):void} */(options.onResult);
24 if (/** @type {number?} */(options.duration)) {
25 duration = /** @type {number} */(options.duration);
29 if (/** @type {string?} */(options.infobox)) {
30 infobox = /** @type {string} */(options.infobox);
32 var onTimeout = /** @type {?function():void} */ (options.onTimeout);
35 this.id_ = remoting.MessageWindowManager.addMessageWindow(this);
37 /** @type {?function(number):void} */
38 this.onResult_ = onResult;
46 /** @type {Array.<function():void>} */
47 this.pendingWindowOperations_ = [];
50 * Callback to call when the timeout expires.
51 * @type {?function():void}
53 this.onTimeout_ = onTimeout;
55 var message_struct = {
61 buttonLabel: okButtonLabel,
62 cancelButtonLabel: cancelButtonLabel,
63 showSpinner: (duration != 0)
66 var windowAttributes = {
74 /** @type {remoting.MessageWindow} */
77 /** @param {AppWindow} appWindow */
78 var onCreate = function(appWindow) {
79 that.setWindow_(/** @type {Window} */(appWindow.contentWindow));
80 var onLoad = function() {
81 appWindow.contentWindow.postMessage(message_struct, '*');
83 appWindow.contentWindow.addEventListener('load', onLoad, false);
86 chrome.app.window.create('message_window.html', windowAttributes, onCreate);
89 this.timer_ = window.setTimeout(this.onTimeoutHandler_.bind(this),
95 * Called when the timer runs out. This in turn calls the window's
96 * timeout handler (if any).
98 remoting.MessageWindow.prototype.onTimeoutHandler_ = function() {
100 if (this.onTimeout_) {
106 * Update the message being shown in the window. This should only be called
107 * after the window has been shown.
109 * @param {string} message The message.
111 remoting.MessageWindow.prototype.updateMessage = function(message) {
113 this.pendingWindowOperations_.push(this.updateMessage.bind(this, message));
117 var message_struct = {
118 command: 'update_message',
121 this.window_.postMessage(message_struct, '*');
125 * Close the message box and unregister it with the window manager.
127 remoting.MessageWindow.prototype.close = function() {
129 this.pendingWindowOperations_.push(this.close.bind(this));
134 window.clearTimeout(this.timer_);
138 // Unregister the window with the window manager.
139 // After this call, events sent to this window will no longer trigger the
140 // onResult callback.
141 remoting.MessageWindowManager.deleteMessageWindow(this.id_);
142 this.window_.close();
147 * Dispatch a message box result to the registered callback.
149 * @param {number} result The dialog result.
151 remoting.MessageWindow.prototype.handleResult = function(result) {
152 if (this.onResult_) {
153 this.onResult_(result);
158 * Set the window handle and run any pending operations that require it.
160 * @param {Window} window
163 remoting.MessageWindow.prototype.setWindow_ = function(window) {
164 base.debug.assert(this.window_ == null);
165 this.window_ = window;
166 for (var i = 0; i < this.pendingWindowOperations_.length; ++i) {
167 var pendingOperation = this.pendingWindowOperations_[i];
170 this.pendingWindowOperations_ = [];
174 * Static method to create and show a confirm message box.
176 * @param {string} title The title of the message box.
177 * @param {string} message The message.
178 * @param {string} okButtonLabel The text for the primary button.
179 * @param {string} cancelButtonLabel The text for the secondary button.
180 * @param {function(number):void} onResult The callback to invoke when the
181 * user closes the message window.
182 * @return {remoting.MessageWindow}
184 remoting.MessageWindow.showConfirmWindow = function(
185 title, message, okButtonLabel, cancelButtonLabel, onResult) {
189 buttonLabel: okButtonLabel,
190 cancelButtonLabel: cancelButtonLabel,
193 return new remoting.MessageWindow(options);
197 * Static method to create and show a simple message box.
199 * @param {string} title The title of the message box.
200 * @param {string} message The message.
201 * @param {string} buttonLabel The text for the primary button.
202 * @param {function(number):void} onResult The callback to invoke when the
203 * user closes the message window.
204 * @return {remoting.MessageWindow}
206 remoting.MessageWindow.showMessageWindow = function(
207 title, message, buttonLabel, onResult) {
211 buttonLabel: buttonLabel,
214 return new remoting.MessageWindow(options);
218 * Static method to create and show an error message box with an "OK" button.
219 * The app will close when the user dismisses the message window.
221 * @param {string} title The title of the message box.
222 * @param {string} message The message.
223 * @return {remoting.MessageWindow}
225 remoting.MessageWindow.showErrorMessage = function(title, message) {
229 buttonLabel: chrome.i18n.getMessage(/**i18n-content*/'OK'),
230 onResult: remoting.MessageWindow.quitApp
232 return new remoting.MessageWindow(options);
236 * Static method to create and show a timed message box.
238 * @param {string} title The title of the message box.
239 * @param {string} message The message.
240 * @param {string} infobox Additional information to be displayed in an infobox,
241 * or the empty string if there is no additional information.
242 * @param {string} buttonLabel The text for the primary button.
243 * @param {function(number):void} onResult The callback to invoke when the
244 * user closes the message window.
245 * @param {number} duration Time for wait before calling onTime
246 * @param {?function():void} onTimeout Callback function.
247 * @return {remoting.MessageWindow}
249 remoting.MessageWindow.showTimedMessageWindow = function(
250 title, message, infobox, buttonLabel, onResult, duration, onTimeout) {
255 buttonLabel: buttonLabel,
260 return new remoting.MessageWindow(options);
264 * Cancel the current connection and close all app windows.
266 * @param {number} result The dialog result.
268 remoting.MessageWindow.quitApp = function(result) {
269 remoting.MessageWindowManager.closeAllMessageWindows();