Roll src/third_party/WebKit d9c6159:8139f33 (svn 201974:201975)
[chromium-blink-merge.git] / remoting / webapp / base / js / message_window_helper.js
blob936f9a7f1bed9680325b85ccd4b2c235ebf22164
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.
5 'use strict';
7 /** @suppress {duplicate} */
8 var remoting = remoting || {};
10 /** @constructor */
11 remoting.MessageWindowOptions = function() {
12 /** @type {string} */
13 this.title = '';
15 /** @type {string} */
16 this.message = '';
18 /** @type {string} */
19 this.buttonLabel = '';
21 /** @type {string} */
22 this.cancelButtonLabel = '';
24 /** @type {function(number):void} */
25 this.onResult = function() {};
27 /** @type {number} */
28 this.duration = 0;
30 /** @type {string} */
31 this.infobox = '';
33 /** @type {?function():void} */
34 this.onTimeout = function() {};
36 /** @type {string} */
37 this.htmlFile = '';
39 /** @type {string} */
40 this.frame = '';
42 /** @type {number} */
43 this.minimumWidth = 0;
46 /**
47 * Create a new message window.
49 * @param {remoting.MessageWindowOptions} options Message window create options
50 * @constructor
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;
58 var duration = 0;
59 if (options.duration) {
60 duration = options.duration;
62 var infobox = '';
63 if (options.infobox) {
64 infobox = options.infobox;
66 var onTimeout = options.onTimeout;
68 /** @type {number} */
69 this.id_ = remoting.messageWindowManager.addMessageWindow(this);
71 /** @type {?function(number):void} */
72 this.onResult_ = onResult;
74 /** @type {Window} */
75 this.window_ = null;
77 /** @type {number} */
78 this.timer_ = 0;
80 /** @type {Array<function():void>} */
81 this.pendingWindowOperations_ = [];
83 /**
84 * Callback to call when the timeout expires.
85 * @type {?function():void}
87 this.onTimeout_ = onTimeout;
89 var message_struct = {
90 command: 'show',
91 id: this.id_,
92 title: title,
93 message: message,
94 infobox: infobox,
95 buttonLabel: okButtonLabel,
96 cancelButtonLabel: cancelButtonLabel,
97 showSpinner: (duration != 0)
100 var windowAttributes = {
101 bounds: {
102 width: options.minimumWidth || 400,
103 height: 100,
104 top: undefined,
105 left: undefined
107 resizable: false,
108 frame: options.frame || 'chrome'
111 /** @type {remoting.MessageWindow} */
112 var that = this;
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);
128 if (duration != 0) {
129 this.timer_ = window.setTimeout(this.onTimeoutHandler_.bind(this),
130 duration);
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.
139 * @type {string}
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() {
148 this.close();
149 if (this.onTimeout_) {
150 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) {
161 if (!this.window_) {
162 this.pendingWindowOperations_.push(this.updateMessage.bind(this, message));
163 return;
166 var message_struct = {
167 command: 'update_message',
168 message: 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() {
177 if (!this.window_) {
178 this.pendingWindowOperations_.push(this.close.bind(this));
179 return;
182 if (this.timer_) {
183 window.clearTimeout(this.timer_);
185 this.timer_ = 0;
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();
192 this.window_ = null;
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
210 * @private
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];
217 pendingOperation();
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} */ ({
236 title: title,
237 message: message,
238 buttonLabel: okButtonLabel,
239 cancelButtonLabel: cancelButtonLabel,
240 onResult: onResult
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} */ ({
258 title: title,
259 message: message,
260 buttonLabel: buttonLabel,
261 onResult: onResult
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} */ ({
276 title: title,
277 message: message,
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();
291 window.close();