-
Notifications
You must be signed in to change notification settings - Fork 8
/
message_window_manager.js
111 lines (95 loc) · 2.97 KB
/
message_window_manager.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
// Copyright 2014 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
'use strict';
/** @suppress {duplicate} */
var remoting = remoting || {};
/**
* Namespace for window manager functions.
* @type {Object}
*/
remoting.MessageWindowManager = {};
/**
* Mapping from window id to corresponding MessageWindow.
*
* @type {Object<number, remoting.MessageWindow>}
* @private
*/
remoting.MessageWindowManager.messageWindows_ = {};
/**
* The next window id to auto-assign.
* @type {number}
* @private
*/
remoting.MessageWindowManager.nextId_ = 1;
/**
* @param {remoting.MessageWindow} window The window to associate
* with the window id.
* @return {number} The window id.
*/
remoting.MessageWindowManager.addMessageWindow = function(window) {
var id = ++remoting.MessageWindowManager.nextId_;
remoting.MessageWindowManager.messageWindows_[id] = window;
return id;
};
/**
* @param {number} id The window id.
* @return {remoting.MessageWindow}
*/
remoting.MessageWindowManager.getMessageWindow = function(id) {
return remoting.MessageWindowManager.messageWindows_[id];
};
/**
* @param {number} id The window id to delete.
*/
remoting.MessageWindowManager.deleteMessageWindow = function(id) {
delete remoting.MessageWindowManager.messageWindows_[id];
};
/**
* Close all of the registered MessageWindows
*/
remoting.MessageWindowManager.closeAllMessageWindows = function() {
/** @type {Array<remoting.MessageWindow>} */
var windows = [];
// Make a list of the windows to close.
// We don't delete the window directly in this loop because close() can
// call deleteMessageWindow which will update messageWindows_.
for (var win_id in remoting.MessageWindowManager.messageWindows_) {
/** @type {remoting.MessageWindow} */
var win = remoting.MessageWindowManager.getMessageWindow(
parseInt(win_id, 10));
base.debug.assert(win != null);
windows.push(win);
}
for (var i = 0; i < windows.length; i++) {
/** @type {remoting.MessageWindow} */(windows[i]).close();
}
};
/**
* Dispatch a message box result to the appropriate callback.
*
* @param {Event} event
* @private
*/
remoting.MessageWindowManager.onMessage_ = function(event) {
if (typeof(event.data) != 'object') {
return;
}
if (event.data['command'] == 'messageWindowResult') {
var id = /** @type {number} */ (event.data['id']);
var result = /** @type {number} */ (event.data['result']);
if (typeof(id) != 'number' || typeof(result) != 'number') {
console.log('Poorly formatted id or result');
return;
}
var messageWindow = remoting.MessageWindowManager.getMessageWindow(id);
if (!messageWindow) {
console.log('Ignoring unknown message window id:', id);
return;
}
messageWindow.handleResult(result);
messageWindow.close();
}
};
window.addEventListener('message', remoting.MessageWindowManager.onMessage_,
false);