Update V8 to version 3.30.4 (based on bleeding_edge revision r24443)
[chromium-blink-merge.git] / remoting / webapp / background / message_window_helper.js
blobcbc379ef0167d6f7387aa0e60e3c82eae3bc8749
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 /**
11 * Create a new message window.
13 * @param {Object} options Message window create options
14 * @constructor
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);
22 /** @type {number} */
23 var duration = 0;
24 if (/** @type {number?} */(options.duration)) {
25 duration = /** @type {number} */(options.duration);
27 /** @type {string} */
28 var infobox = '';
29 if (/** @type {string?} */(options.infobox)) {
30 infobox = /** @type {string} */(options.infobox);
32 var onTimeout = /** @type {?function():void} */ (options.onTimeout);
34 /** @type {number} */
35 this.id_ = remoting.MessageWindowManager.addMessageWindow(this);
37 /** @type {?function(number):void} */
38 this.onResult_ = onResult;
40 /** @type {Window} */
41 this.window_ = null;
43 /** @type {number} */
44 this.timer_ = 0;
46 /** @type {Array.<function():void>} */
47 this.pendingWindowOperations_ = [];
49 /**
50 * Callback to call when the timeout expires.
51 * @type {?function():void}
53 this.onTimeout_ = onTimeout;
55 var message_struct = {
56 command: 'show',
57 id: this.id_,
58 title: title,
59 message: message,
60 infobox: infobox,
61 buttonLabel: okButtonLabel,
62 cancelButtonLabel: cancelButtonLabel,
63 showSpinner: (duration != 0)
66 var windowAttributes = {
67 bounds: {
68 width: 400,
69 height: 100
71 resizable: false
74 /** @type {remoting.MessageWindow} */
75 var that = this;
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);
88 if (duration != 0) {
89 this.timer_ = window.setTimeout(this.onTimeoutHandler_.bind(this),
90 duration);
94 /**
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() {
99 this.close();
100 if (this.onTimeout_) {
101 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) {
112 if (!this.window_) {
113 this.pendingWindowOperations_.push(this.updateMessage.bind(this, message));
114 return;
117 var message_struct = {
118 command: 'update_message',
119 message: 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() {
128 if (!this.window_) {
129 this.pendingWindowOperations_.push(this.close.bind(this));
130 return;
133 if (this.timer_) {
134 window.clearTimeout(this.timer_);
136 this.timer_ = 0;
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();
143 this.window_ = null;
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
161 * @private
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];
168 pendingOperation();
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) {
186 var options = {
187 title: title,
188 message: message,
189 buttonLabel: okButtonLabel,
190 cancelButtonLabel: cancelButtonLabel,
191 onResult: onResult
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) {
208 var options = {
209 title: title,
210 message: message,
211 buttonLabel: buttonLabel,
212 onResult: onResult
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) {
226 var options = {
227 title: title,
228 message: 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) {
251 var options = {
252 title: title,
253 message: message,
254 infobox: infobox,
255 buttonLabel: buttonLabel,
256 onResult: onResult,
257 duration: duration,
258 onTimeout: onTimeout
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();
270 window.close();