OLD | NEW |
(Empty) | |
| 1 // Copyright 2013 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 // Namespace for the share dialog mock. |
| 6 var shareDialog = {}; |
| 7 |
| 8 /** |
| 9 * Origin of Files.app. |
| 10 * @type {string} |
| 11 * @const |
| 12 */ |
| 13 shareDialog.EMBEDDER_ORIGIN = |
| 14 'chrome-extension://hhaomjibdihmijegdhdafkllkbggdgoj'; |
| 15 |
| 16 /** |
| 17 * Target width of the sharing window in pixels. |
| 18 * @type {number} |
| 19 * @const |
| 20 */ |
| 21 shareDialog.TARGET_WIDTH = 350; |
| 22 |
| 23 /** |
| 24 * Target height of the sharing window in pixels. |
| 25 * @type {number} |
| 26 * @const |
| 27 */ |
| 28 shareDialog.TARGET_HEIGHT = 250; |
| 29 |
| 30 /** |
| 31 * Target window of Files.app. Used to communicate over messages. Filled out |
| 32 * once the first message from the embedder arrives. |
| 33 * @type {Window} |
| 34 */ |
| 35 shareDialog.embedderTarget = null; |
| 36 |
| 37 /** |
| 38 * List of pending messages enqueued to be sent before establishing the target. |
| 39 * @type {Array.<Object>} |
| 40 */ |
| 41 shareDialog.pendingMessages = []; |
| 42 |
| 43 /** |
| 44 * Sends a message to the embedder. If the embedder target is not available, |
| 45 * then enqueues them. Such enqueued messages will be sent as soon as the target |
| 46 * is available. |
| 47 * |
| 48 * @param {string} type Message identifier |
| 49 * @param {Object=} opt_args Arguments for the message. |
| 50 * @private |
| 51 */ |
| 52 shareDialog.sendMessage_ = function(type, opt_args) { |
| 53 if (!shareDialog.embedderTarget) { |
| 54 shareDialog.pendingMessages.push({type: type, args: opt_args}); |
| 55 return; |
| 56 } |
| 57 |
| 58 var data = {}; |
| 59 data.type = type; |
| 60 if (opt_args) |
| 61 data.args = opt_args; |
| 62 |
| 63 shareDialog.embedderTarget.postMessage(JSON.stringify(data), |
| 64 shareDialog.EMBEDDER_ORIGIN); |
| 65 }; |
| 66 |
| 67 /** |
| 68 * Handles a request from the embedder to make the body visible. |
| 69 * @private |
| 70 */ |
| 71 shareDialog.onMakeBodyVisible_ = function() { |
| 72 document.body.style.display = ''; |
| 73 }; |
| 74 |
| 75 /** |
| 76 * Handles an event from the embedder than preparation to show the contents |
| 77 * is done. |
| 78 * @private |
| 79 */ |
| 80 shareDialog.onPrepareComplete_ = function() { |
| 81 shareDialog.resize(); |
| 82 }; |
| 83 |
| 84 /** |
| 85 * Handles an event from the embedder than preparation resize the window is |
| 86 * done. |
| 87 * @private |
| 88 */ |
| 89 shareDialog.onResizeComplete_ = function() { |
| 90 var container = document.querySelector('#container'); |
| 91 container.style.width = shareDialog.TARGET_WIDTH + 'px'; |
| 92 container.style.height = shareDialog.TARGET_HEIGHT + 'px'; |
| 93 }; |
| 94 |
| 95 /** |
| 96 * Prepares the embedder to make the contents visible. |
| 97 */ |
| 98 shareDialog.prepareForVisible = function() { |
| 99 shareDialog.sendMessage_('prepareForVisible'); |
| 100 }; |
| 101 |
| 102 /** |
| 103 * Resizes the embedder to the content window dimensions. |
| 104 */ |
| 105 shareDialog.resize = function() { |
| 106 shareDialog.sendMessage_('prepareForResize'); |
| 107 }; |
| 108 |
| 109 /** |
| 110 * Handles messages sent by the embedder. If it is the first message, then |
| 111 * the target is established and all enqueued messages to be sent to the |
| 112 * embedder are sent before handling the message from the embedder. |
| 113 * |
| 114 * @param {Event} message Message event. |
| 115 * @private |
| 116 */ |
| 117 shareDialog.onMessage_ = function(message) { |
| 118 if (message.origin != shareDialog.EMBEDDER_ORIGIN) |
| 119 return; |
| 120 |
| 121 if (!shareDialog.embedderTarget) { |
| 122 shareDialog.embedderTarget = message.source; |
| 123 for (var i = 0; i < shareDialog.pendingMessages.length; i++) { |
| 124 shareDialog.sendMessage_(shareDialog.pendingMessages[i].type, |
| 125 shareDialog.pendingMessages[i].args); |
| 126 } |
| 127 shareDialog.pendingMessages = []; |
| 128 } |
| 129 |
| 130 var packet = JSON.parse(message.data) |
| 131 var type = packet.type; |
| 132 var args = packet.args; |
| 133 |
| 134 switch (type) { |
| 135 case 'makeBodyVisible': |
| 136 shareDialog.onMakeBodyVisible_(args); |
| 137 break; |
| 138 case 'prepareComplete': |
| 139 shareDialog.onPrepareComplete_(args); |
| 140 break; |
| 141 case 'resizeComplete': |
| 142 shareDialog.onResizeComplete_(args); |
| 143 break; |
| 144 } |
| 145 }; |
| 146 |
| 147 /** |
| 148 * Initializes the mocked share dialog. |
| 149 */ |
| 150 shareDialog.initialize = function() { |
| 151 window.addEventListener('message', shareDialog.onMessage_); |
| 152 shareDialog.prepareForVisible(); |
| 153 }; |
| 154 |
| 155 window.addEventListener('load', shareDialog.initialize); |
OLD | NEW |