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 remoting.MessageWindowOptions = function() {
19 this.buttonLabel = '';
22 this.cancelButtonLabel = '';
24 /** @type {function(number):void} */
25 this.onResult = function() {};
33 /** @type {?function():void} */
34 this.onTimeout = function() {};
43 this.minimumWidth = 0;
47 * Create a new message window.
49 * @param {remoting.MessageWindowOptions} options Message window create options
52 remoting.MessageWindow = function(options) {
53 var title = options.title;
54 var message = options.message;
55 var okButtonLabel = options.buttonLabel;
56 var cancelButtonLabel = options.cancelButtonLabel;
57 var onResult = options.onResult;
59 if (options.duration) {
60 duration = options.duration;
63 if (options.infobox) {
64 infobox = options.infobox;
66 var onTimeout = options.onTimeout;
69 this.id_ = remoting.MessageWindowManager.addMessageWindow(this);
71 /** @type {?function(number):void} */
72 this.onResult_ = onResult;
80 /** @type {Array<function():void>} */
81 this.pendingWindowOperations_ = [];
84 * Callback to call when the timeout expires.
85 * @type {?function():void}
87 this.onTimeout_ = onTimeout;
89 var message_struct = {
95 buttonLabel: okButtonLabel,
96 cancelButtonLabel: cancelButtonLabel,
97 showSpinner: (duration != 0)
100 var windowAttributes = {
102 width: options.minimumWidth || 400,
108 frame: options.frame || 'chrome'
111 /** @type {remoting.MessageWindow} */
114 /** @param {chrome.app.window.AppWindow} appWindow */
115 var onCreate = function(appWindow) {
116 that.setWindow_(/** @type {Window} */(appWindow.contentWindow));
117 var onLoad = function() {
118 appWindow.contentWindow.postMessage(message_struct, '*');
120 appWindow.contentWindow.addEventListener('load', onLoad, false);
123 var htmlFile = options.htmlFile || 'message_window.html';
124 chrome.app.window.create(htmlFile, windowAttributes, onCreate);
127 this.timer_ = window.setTimeout(this.onTimeoutHandler_.bind(this),
133 * Called when the timer runs out. This in turn calls the window's
134 * timeout handler (if any).
136 remoting.MessageWindow.prototype.onTimeoutHandler_ = function() {
138 if (this.onTimeout_) {
144 * Update the message being shown in the window. This should only be called
145 * after the window has been shown.
147 * @param {string} message The message.
149 remoting.MessageWindow.prototype.updateMessage = function(message) {
151 this.pendingWindowOperations_.push(this.updateMessage.bind(this, message));
155 var message_struct = {
156 command: 'update_message',
159 this.window_.postMessage(message_struct, '*');
163 * Close the message box and unregister it with the window manager.
165 remoting.MessageWindow.prototype.close = function() {
167 this.pendingWindowOperations_.push(this.close.bind(this));
172 window.clearTimeout(this.timer_);
176 // Unregister the window with the window manager.
177 // After this call, events sent to this window will no longer trigger the
178 // onResult callback.
179 remoting.MessageWindowManager.deleteMessageWindow(this.id_);
180 this.window_.close();
185 * Dispatch a message box result to the registered callback.
187 * @param {number} result The dialog result.
189 remoting.MessageWindow.prototype.handleResult = function(result) {
190 if (this.onResult_) {
191 this.onResult_(result);
196 * Set the window handle and run any pending operations that require it.
198 * @param {Window} window
201 remoting.MessageWindow.prototype.setWindow_ = function(window) {
202 base.debug.assert(this.window_ == null);
203 this.window_ = window;
204 for (var i = 0; i < this.pendingWindowOperations_.length; ++i) {
205 var pendingOperation = this.pendingWindowOperations_[i];
208 this.pendingWindowOperations_ = [];
212 * Static method to create and show a confirm message box.
214 * @param {string} title The title of the message box.
215 * @param {string} message The message.
216 * @param {string} okButtonLabel The text for the primary button.
217 * @param {string} cancelButtonLabel The text for the secondary button.
218 * @param {function(number):void} onResult The callback to invoke when the
219 * user closes the message window.
220 * @return {remoting.MessageWindow}
222 remoting.MessageWindow.showConfirmWindow = function(
223 title, message, okButtonLabel, cancelButtonLabel, onResult) {
224 var options = /** @type {remoting.MessageWindowOptions} */ ({
227 buttonLabel: okButtonLabel,
228 cancelButtonLabel: cancelButtonLabel,
231 return new remoting.MessageWindow(options);
235 * Static method to create and show a simple message box.
237 * @param {string} title The title of the message box.
238 * @param {string} message The message.
239 * @param {string} buttonLabel The text for the primary button.
240 * @param {function(number):void} onResult The callback to invoke when the
241 * user closes the message window.
242 * @return {remoting.MessageWindow}
244 remoting.MessageWindow.showMessageWindow = function(
245 title, message, buttonLabel, onResult) {
246 var options = /** @type {remoting.MessageWindowOptions} */ ({
249 buttonLabel: buttonLabel,
252 return new remoting.MessageWindow(options);
256 * Static method to create and show an error message box with an "OK" button.
257 * The app will close when the user dismisses the message window.
259 * @param {string} title The title of the message box.
260 * @param {string} message The message.
261 * @return {remoting.MessageWindow}
263 remoting.MessageWindow.showErrorMessage = function(title, message) {
264 var options = /** @type {remoting.MessageWindowOptions} */ ({
267 buttonLabel: chrome.i18n.getMessage(/*i18n-content*/'OK'),
268 onResult: remoting.MessageWindow.quitApp
270 return new remoting.MessageWindow(options);
274 * Cancel the current connection and close all app windows.
276 * @param {number} result The dialog result.
278 remoting.MessageWindow.quitApp = function(result) {
279 remoting.MessageWindowManager.closeAllMessageWindows();