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 * @type {Object.<number, remoting.MessageWindow>}
22 remoting.MessageWindowManager.messageWindows_ = {};
25 * The next window id to auto-assign.
29 remoting.MessageWindowManager.nextId_ = 1;
32 * @param {remoting.MessageWindow} window The window to associate
34 * @return {number} The window id.
36 remoting.MessageWindowManager.addMessageWindow = function(window) {
37 var id = ++remoting.MessageWindowManager.nextId_;
38 remoting.MessageWindowManager.messageWindows_[id] = window;
43 * @param {number} id The window id.
44 * @return {remoting.MessageWindow}
46 remoting.MessageWindowManager.getMessageWindow = function(id) {
47 return remoting.MessageWindowManager.messageWindows_[id];
51 * @param {number} id The window id to delete.
53 remoting.MessageWindowManager.deleteMessageWindow = function(id) {
54 delete remoting.MessageWindowManager.messageWindows_[id];
58 * Close all of the registered MessageWindows
60 remoting.MessageWindowManager.closeAllMessageWindows = function() {
61 /** @type {Array.<remoting.MessageWindow>} */
63 // Make a list of the windows to close.
64 // We don't delete the window directly in this loop because close() can
65 // call deleteMessageWindow which will update messageWindows_.
66 for (var win_id in remoting.MessageWindowManager.messageWindows_) {
67 /** @type {remoting.MessageWindow} */
68 var win = remoting.MessageWindowManager.getMessageWindow(
69 /** @type {number} */(win_id));
70 base.debug.assert(win != null);
73 for (var i = 0; i < windows.length; i++) {
74 /** @type {remoting.MessageWindow} */(windows[i]).close();
79 * Dispatch a message box result to the appropriate callback.
81 * @param {Event} event
84 remoting.MessageWindowManager.onMessage_ = function(event) {
85 if (typeof(event.data) != 'object') {
89 if (event.data['command'] == 'messageWindowResult') {
90 var id = /** @type {number} */ (event.data['id']);
91 var result = /** @type {number} */ (event.data['result']);
93 if (typeof(id) != 'number' || typeof(result) != 'number') {
94 console.log('Poorly formatted id or result');
98 var messageWindow = remoting.MessageWindowManager.getMessageWindow(id);
100 console.log('Ignoring unknown message window id:', id);
104 messageWindow.handleResult(result);
105 messageWindow.close();
110 window.addEventListener('message', remoting.MessageWindowManager.onMessage_,