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