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(
125 remoting.MessageWindow.htmlFilePrefix + htmlFile,
126 windowAttributes, onCreate);
129 this.timer_ = window.setTimeout(this.onTimeoutHandler_.bind(this),
135 * This string is prepended to the htmlFile when message windows are created.
136 * Normally, this should be left empty, but the shared module needs to specify
137 * this so that the shared HTML files can be found when running in the
138 * context of the app stub.
141 remoting.MessageWindow.htmlFilePrefix = "";
144 * Called when the timer runs out. This in turn calls the window's
145 * timeout handler (if any).
147 remoting.MessageWindow.prototype.onTimeoutHandler_ = function() {
149 if (this.onTimeout_) {
155 * Update the message being shown in the window. This should only be called
156 * after the window has been shown.
158 * @param {string} message The message.
160 remoting.MessageWindow.prototype.updateMessage = function(message) {
162 this.pendingWindowOperations_.push(this.updateMessage.bind(this, message));
166 var message_struct = {
167 command: 'update_message',
170 this.window_.postMessage(message_struct, '*');
174 * Close the message box and unregister it with the window manager.
176 remoting.MessageWindow.prototype.close = function() {
178 this.pendingWindowOperations_.push(this.close.bind(this));
183 window.clearTimeout(this.timer_);
187 // Unregister the window with the window manager.
188 // After this call, events sent to this window will no longer trigger the
189 // onResult callback.
190 remoting.messageWindowManager.deleteMessageWindow(this.id_);
191 this.window_.close();
196 * Dispatch a message box result to the registered callback.
198 * @param {number} result The dialog result.
200 remoting.MessageWindow.prototype.handleResult = function(result) {
201 if (this.onResult_) {
202 this.onResult_(result);
207 * Set the window handle and run any pending operations that require it.
209 * @param {Window} window
212 remoting.MessageWindow.prototype.setWindow_ = function(window) {
213 console.assert(this.window_ == null, 'Duplicate call to setWindow_().');
214 this.window_ = window;
215 for (var i = 0; i < this.pendingWindowOperations_.length; ++i) {
216 var pendingOperation = this.pendingWindowOperations_[i];
219 this.pendingWindowOperations_ = [];
223 * Static method to create and show a confirm message box.
225 * @param {string} title The title of the message box.
226 * @param {string} message The message.
227 * @param {string} okButtonLabel The text for the primary button.
228 * @param {string} cancelButtonLabel The text for the secondary button.
229 * @param {function(number):void} onResult The callback to invoke when the
230 * user closes the message window.
231 * @return {remoting.MessageWindow}
233 remoting.MessageWindow.showConfirmWindow = function(
234 title, message, okButtonLabel, cancelButtonLabel, onResult) {
235 var options = /** @type {remoting.MessageWindowOptions} */ ({
238 buttonLabel: okButtonLabel,
239 cancelButtonLabel: cancelButtonLabel,
242 return new remoting.MessageWindow(options);
246 * Static method to create and show a simple message box.
248 * @param {string} title The title of the message box.
249 * @param {string} message The message.
250 * @param {string} buttonLabel The text for the primary button.
251 * @param {function(number):void} onResult The callback to invoke when the
252 * user closes the message window.
253 * @return {remoting.MessageWindow}
255 remoting.MessageWindow.showMessageWindow = function(
256 title, message, buttonLabel, onResult) {
257 var options = /** @type {remoting.MessageWindowOptions} */ ({
260 buttonLabel: buttonLabel,
263 return new remoting.MessageWindow(options);
267 * Static method to create and show an error message box with an "OK" button.
268 * The app will close when the user dismisses the message window.
270 * @param {string} title The title of the message box.
271 * @param {string} message The message.
272 * @return {remoting.MessageWindow}
274 remoting.MessageWindow.showErrorMessage = function(title, message) {
275 var options = /** @type {remoting.MessageWindowOptions} */ ({
278 buttonLabel: chrome.i18n.getMessage(/*i18n-content*/'OK'),
279 onResult: remoting.MessageWindow.quitApp
281 return new remoting.MessageWindow(options);
285 * Cancel the current connection and close all app windows.
287 * @param {number} result The dialog result.
289 remoting.MessageWindow.quitApp = function(result) {
290 remoting.messageWindowManager.closeAllMessageWindows();