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.
5 // Namespace for the share dialog mock.
13 shareDialog.EMBEDDER_ORIGIN =
14 'chrome-extension://hhaomjibdihmijegdhdafkllkbggdgoj';
17 * Target width of the sharing window in pixels.
21 shareDialog.TARGET_WIDTH = 350;
24 * Target height of the sharing window in pixels.
28 shareDialog.TARGET_HEIGHT = 250;
31 * Target window of Files.app. Used to communicate over messages. Filled out
32 * once the first message from the embedder arrives.
35 shareDialog.embedderTarget = null;
38 * List of pending messages enqueued to be sent before establishing the target.
39 * @type {Array<Object>}
41 shareDialog.pendingMessages = [];
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
48 * @param {string} type Message identifier
49 * @param {Object=} opt_args Arguments for the message.
52 shareDialog.sendMessage_ = function(type, opt_args) {
53 if (!shareDialog.embedderTarget) {
54 shareDialog.pendingMessages.push({type: type, args: opt_args});
63 shareDialog.embedderTarget.postMessage(JSON.stringify(data),
64 shareDialog.EMBEDDER_ORIGIN);
68 * Handles a request from the embedder to make the body visible.
71 shareDialog.onMakeBodyVisible_ = function() {
72 document.body.style.display = '';
76 * Handles an event from the embedder than preparation to show the contents
80 shareDialog.onPrepareComplete_ = function() {
85 * Handles an event from the embedder than preparation resize the window is
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';
96 * Changes the visibility of the dialog.
97 * @param {boolean} visible True to set the dialog visible, false to set it
100 shareDialog.setVisible = function(visible) {
101 shareDialog.sendMessage_('setVisible', {visible: visible});
105 * Prepares the embedder to make the contents visible.
107 shareDialog.prepareForVisible = function() {
108 shareDialog.sendMessage_('prepareForVisible');
112 * Resizes the embedder to the content window dimensions.
114 shareDialog.resize = function() {
115 shareDialog.sendMessage_('prepareForResize');
119 * Handles messages sent by the embedder. If it is the first message, then
120 * the target is established and all enqueued messages to be sent to the
121 * embedder are sent before handling the message from the embedder.
123 * @param {Event} message Message event.
126 shareDialog.onMessage_ = function(message) {
127 if (message.origin != shareDialog.EMBEDDER_ORIGIN)
130 if (!shareDialog.embedderTarget) {
131 shareDialog.embedderTarget = message.source;
132 for (var i = 0; i < shareDialog.pendingMessages.length; i++) {
133 shareDialog.sendMessage_(shareDialog.pendingMessages[i].type,
134 shareDialog.pendingMessages[i].args);
136 shareDialog.pendingMessages = [];
139 var packet = JSON.parse(message.data)
140 var type = packet.type;
141 var args = packet.args;
144 case 'makeBodyVisible':
145 shareDialog.onMakeBodyVisible_(args);
147 case 'prepareComplete':
148 shareDialog.onPrepareComplete_(args);
150 case 'resizeComplete':
151 shareDialog.onResizeComplete_(args);
157 * Initializes the mocked share dialog.
159 shareDialog.initialize = function() {
160 window.addEventListener('message', shareDialog.onMessage_);
161 shareDialog.prepareForVisible();
164 window.addEventListener('load', shareDialog.initialize);