Convert cacheinvalidation_unittests to run exclusively on Swarming
[chromium-blink-merge.git] / remoting / webapp / base / js / message_window_manager.js
blob582bfa587429e97a13056c87bc76ca6e7bbe57f0
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 * This class manages all the message windows (remoting.MessageWindow).
12 * @param {base.WindowMessageDispatcher} windowMessageDispatcher
13 * @constructor
14 * @implements {base.Disposable}
16 remoting.MessageWindowManager = function(windowMessageDispatcher) {
17 /**
18 * @type {!Object<number, remoting.MessageWindow>}
19 * @private
21 this.messageWindows_ = {};
23 /**
24 * The next window id to auto-assign.
25 * @private {number}
27 this.nextId_ = 1;
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');
40 /**
41 * @param {remoting.MessageWindow} window The window to associate
42 * with the window id.
43 * @return {number} The window id.
45 remoting.MessageWindowManager.prototype.addMessageWindow = function(window) {
46 var id = ++this.nextId_;
47 this.messageWindows_[id] = window;
48 return id;
51 /**
52 * @param {number} id The window id.
53 * @return {remoting.MessageWindow}
55 remoting.MessageWindowManager.prototype.getMessageWindow = function(id) {
56 return this.messageWindows_[id];
59 /**
60 * @param {number} id The window id to delete.
62 remoting.MessageWindowManager.prototype.deleteMessageWindow = function(id) {
63 delete this.messageWindows_[id];
66 /**
67 * Close all of the registered MessageWindows
69 remoting.MessageWindowManager.prototype.closeAllMessageWindows = function() {
70 /** @type {Array<remoting.MessageWindow>} */
71 var windows = [];
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 + '.');
79 windows.push(win);
81 for (var i = 0; i < windows.length; i++) {
82 /** @type {remoting.MessageWindow} */(windows[i]).close();
86 /**
87 * Dispatch a message box result to the appropriate callback.
89 * @param {Event} event
90 * @private
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');
105 return;
108 var messageWindow = this.getMessageWindow(id);
109 if (!messageWindow) {
110 console.log('Ignoring unknown message window id:', id);
111 return;
114 messageWindow.handleResult(result);
115 messageWindow.close();
119 /** @type {remoting.MessageWindowManager} */
120 remoting.messageWindowManager = null;