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 * This class manages all the message windows (remoting.MessageWindow).
12 * @param {base.WindowMessageDispatcher} windowMessageDispatcher
14 * @implements {base.Disposable}
16 remoting.MessageWindowManager = function(windowMessageDispatcher) {
18 * @type {!Object<number, remoting.MessageWindow>}
21 this.messageWindows_ = {};
24 * The next window id to auto-assign.
29 /** @private {base.WindowMessageDispatcher} */
30 this.windowMessageDispatcher_ = windowMessageDispatcher;
32 this.windowMessageDispatcher_.registerMessageHandler(
33 'message-window', this.onMessage_.bind(this));
36 remoting.MessageWindowManager.prototype.dispose = function() {
37 this.windowMessageDispatcher_.unregisterMessageHandler('message-window');
41 * @param {remoting.MessageWindow} window The window to associate
43 * @return {number} The window id.
45 remoting.MessageWindowManager.prototype.addMessageWindow = function(window) {
46 var id = ++this.nextId_;
47 this.messageWindows_[id] = window;
52 * @param {number} id The window id.
53 * @return {remoting.MessageWindow}
55 remoting.MessageWindowManager.prototype.getMessageWindow = function(id) {
56 return this.messageWindows_[id];
60 * @param {number} id The window id to delete.
62 remoting.MessageWindowManager.prototype.deleteMessageWindow = function(id) {
63 delete this.messageWindows_[id];
67 * Close all of the registered MessageWindows
69 remoting.MessageWindowManager.prototype.closeAllMessageWindows = function() {
70 /** @type {Array<remoting.MessageWindow>} */
72 // Make a list of the windows to close.
73 // We don't delete the window directly in this loop because close() can
74 // call deleteMessageWindow which will update messageWindows_.
75 for (var win_id in this.messageWindows_) {
76 /** @type {remoting.MessageWindow} */
77 var win = this.getMessageWindow(parseInt(win_id, 10));
78 console.assert(win != null, 'Unknown window id ' + win_id + '.');
81 for (var i = 0; i < windows.length; i++) {
82 /** @type {remoting.MessageWindow} */(windows[i]).close();
87 * Dispatch a message box result to the appropriate callback.
89 * @param {Event} event
92 remoting.MessageWindowManager.prototype.onMessage_ = function(event) {
93 console.assert(typeof event.data === 'object',
94 'Unexpected data. Expected object, got ' + event.data + '.');
95 console.assert(event.data['source'] == 'message-window',
96 'Bad event source: ' +
97 /** @type {string} */ (event.data['source']) + '.');
99 if (event.data['command'] == 'messageWindowResult') {
100 var id = /** @type {number} */ (event.data['id']);
101 var result = /** @type {number} */ (event.data['result']);
103 if (typeof(id) != 'number' || typeof(result) != 'number') {
104 console.log('Poorly formatted id or result');
108 var messageWindow = this.getMessageWindow(id);
109 if (!messageWindow) {
110 console.log('Ignoring unknown message window id:', id);
114 messageWindow.handleResult(result);
115 messageWindow.close();
119 /** @type {remoting.MessageWindowManager} */
120 remoting.messageWindowManager = null;