OLD | NEW |
(Empty) | |
| 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. |
| 4 |
| 5 'use strict'; |
| 6 |
| 7 /** @suppress {duplicate} */ |
| 8 var remoting = remoting || {}; |
| 9 |
| 10 /** |
| 11 * Namespace for window manager functions. |
| 12 * @type {Object} |
| 13 */ |
| 14 remoting.MessageWindowManager = {}; |
| 15 |
| 16 /** |
| 17 * Mapping from window id to corresponding MessageWindow. |
| 18 * |
| 19 * @type {Object.<number, remoting.MessageWindow>} |
| 20 * @private |
| 21 */ |
| 22 remoting.MessageWindowManager.messageWindows_ = {}; |
| 23 |
| 24 /** |
| 25 * The next window id to auto-assign. |
| 26 * @type {number} |
| 27 * @private |
| 28 */ |
| 29 remoting.MessageWindowManager.nextId_ = 1; |
| 30 |
| 31 /** |
| 32 * @param {remoting.MessageWindow} window The window to associate |
| 33 * with the window id. |
| 34 * @return {number} The window id. |
| 35 */ |
| 36 remoting.MessageWindowManager.addMessageWindow = function(window) { |
| 37 var id = ++remoting.MessageWindowManager.nextId_; |
| 38 remoting.MessageWindowManager.messageWindows_[id] = window; |
| 39 return id; |
| 40 }; |
| 41 |
| 42 /** |
| 43 * @param {number} id The window id. |
| 44 * @return {remoting.MessageWindow} |
| 45 */ |
| 46 remoting.MessageWindowManager.getMessageWindow = function(id) { |
| 47 return remoting.MessageWindowManager.messageWindows_[id]; |
| 48 }; |
| 49 |
| 50 /** |
| 51 * @param {number} id The window id to delete. |
| 52 */ |
| 53 remoting.MessageWindowManager.deleteMessageWindow = function(id) { |
| 54 delete remoting.MessageWindowManager.messageWindows_[id]; |
| 55 }; |
| 56 |
| 57 /** |
| 58 * Close all of the registered MessageWindows |
| 59 */ |
| 60 remoting.MessageWindowManager.closeAllMessageWindows = function() { |
| 61 /** @type {Array.<remoting.MessageWindow>} */ |
| 62 var windows = []; |
| 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); |
| 71 windows.push(win); |
| 72 } |
| 73 for (var i = 0; i < windows.length; i++) { |
| 74 /** @type {remoting.MessageWindow} */(windows[i]).close(); |
| 75 } |
| 76 }; |
| 77 |
| 78 /** |
| 79 * Dispatch a message box result to the appropriate callback. |
| 80 * |
| 81 * @param {Event} event |
| 82 * @private |
| 83 */ |
| 84 remoting.MessageWindowManager.onMessage_ = function(event) { |
| 85 if (typeof(event.data) != 'object') { |
| 86 return; |
| 87 } |
| 88 |
| 89 if (event.data['command'] == 'messageWindowResult') { |
| 90 var id = /** @type {number} */ (event.data['id']); |
| 91 var result = /** @type {number} */ (event.data['result']); |
| 92 |
| 93 if (typeof(id) != 'number' || typeof(result) != 'number') { |
| 94 console.log('Poorly formatted id or result'); |
| 95 return; |
| 96 } |
| 97 |
| 98 var messageWindow = remoting.MessageWindowManager.getMessageWindow(id); |
| 99 if (!messageWindow) { |
| 100 console.log('Ignoring unknown message window id:', id); |
| 101 return; |
| 102 } |
| 103 |
| 104 messageWindow.handleResult(result); |
| 105 messageWindow.close(); |
| 106 } |
| 107 }; |
| 108 |
| 109 |
| 110 window.addEventListener('message', remoting.MessageWindowManager.onMessage_, |
| 111 false); |
OLD | NEW |