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 * Namespace for window manager functions.
14 remoting
.MessageWindowManager
= {};
17 * Mapping from window id to corresponding MessageWindow.
19 * @private {Object<number, remoting.MessageWindow>}
21 remoting
.MessageWindowManager
.messageWindows_
= {};
24 * The next window id to auto-assign.
27 remoting
.MessageWindowManager
.nextId_
= 1;
30 * @param {remoting.MessageWindow} window The window to associate
32 * @return {number} The window id.
34 remoting
.MessageWindowManager
.addMessageWindow = function(window
) {
35 var id
= ++remoting
.MessageWindowManager
.nextId_
;
36 remoting
.MessageWindowManager
.messageWindows_
[id
] = window
;
41 * @param {number} id The window id.
42 * @return {remoting.MessageWindow}
44 remoting
.MessageWindowManager
.getMessageWindow = function(id
) {
45 return remoting
.MessageWindowManager
.messageWindows_
[id
];
49 * @param {number} id The window id to delete.
51 remoting
.MessageWindowManager
.deleteMessageWindow = function(id
) {
52 delete remoting
.MessageWindowManager
.messageWindows_
[id
];
56 * Close all of the registered MessageWindows
58 remoting
.MessageWindowManager
.closeAllMessageWindows = function() {
59 /** @type {Array<remoting.MessageWindow>} */
61 // Make a list of the windows to close.
62 // We don't delete the window directly in this loop because close() can
63 // call deleteMessageWindow which will update messageWindows_.
64 for (var win_id
in remoting
.MessageWindowManager
.messageWindows_
) {
65 /** @type {remoting.MessageWindow} */
66 var win
= remoting
.MessageWindowManager
.getMessageWindow(
67 parseInt(win_id
, 10));
68 base
.debug
.assert(win
!= null);
71 for (var i
= 0; i
< windows
.length
; i
++) {
72 /** @type {remoting.MessageWindow} */(windows
[i
]).close();
77 * Dispatch a message box result to the appropriate callback.
79 * @param {Event} event
82 remoting
.MessageWindowManager
.onMessage_ = function(event
) {
83 if (typeof(event
.data
) != 'object') {
87 if (event
.data
['command'] == 'messageWindowResult') {
88 var id
= /** @type {number} */ (event
.data
['id']);
89 var result
= /** @type {number} */ (event
.data
['result']);
91 if (typeof(id
) != 'number' || typeof(result
) != 'number') {
92 console
.log('Poorly formatted id or result');
96 var messageWindow
= remoting
.MessageWindowManager
.getMessageWindow(id
);
98 console
.log('Ignoring unknown message window id:', id
);
102 messageWindow
.handleResult(result
);
103 messageWindow
.close();
108 window
.addEventListener('message', remoting
.MessageWindowManager
.onMessage_
,