Supervised user import: Listen for profile creation/deletion
[chromium-blink-merge.git] / remoting / webapp / base / js / message_window_manager.js
blob596336ff8022065cf626238f9fbc9425ef607c47
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 * Namespace for window manager functions.
12 * @type {Object}
14 remoting.MessageWindowManager = {};
16 /**
17 * Mapping from window id to corresponding MessageWindow.
19 * @private {Object<number, remoting.MessageWindow>}
21 remoting.MessageWindowManager.messageWindows_ = {};
23 /**
24 * The next window id to auto-assign.
25 * @private {number}
27 remoting.MessageWindowManager.nextId_ = 1;
29 /**
30 * @param {remoting.MessageWindow} window The window to associate
31 * with the window id.
32 * @return {number} The window id.
34 remoting.MessageWindowManager.addMessageWindow = function(window) {
35 var id = ++remoting.MessageWindowManager.nextId_;
36 remoting.MessageWindowManager.messageWindows_[id] = window;
37 return id;
40 /**
41 * @param {number} id The window id.
42 * @return {remoting.MessageWindow}
44 remoting.MessageWindowManager.getMessageWindow = function(id) {
45 return remoting.MessageWindowManager.messageWindows_[id];
48 /**
49 * @param {number} id The window id to delete.
51 remoting.MessageWindowManager.deleteMessageWindow = function(id) {
52 delete remoting.MessageWindowManager.messageWindows_[id];
55 /**
56 * Close all of the registered MessageWindows
58 remoting.MessageWindowManager.closeAllMessageWindows = function() {
59 /** @type {Array<remoting.MessageWindow>} */
60 var windows = [];
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);
69 windows.push(win);
71 for (var i = 0; i < windows.length; i++) {
72 /** @type {remoting.MessageWindow} */(windows[i]).close();
76 /**
77 * Dispatch a message box result to the appropriate callback.
79 * @param {Event} event
80 * @private
82 remoting.MessageWindowManager.onMessage_ = function(event) {
83 if (typeof(event.data) != 'object') {
84 return;
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');
93 return;
96 var messageWindow = remoting.MessageWindowManager.getMessageWindow(id);
97 if (!messageWindow) {
98 console.log('Ignoring unknown message window id:', id);
99 return;
102 messageWindow.handleResult(result);
103 messageWindow.close();
108 window.addEventListener('message', remoting.MessageWindowManager.onMessage_,
109 false);