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.
8 * @param {WebView} webView Web View tag.
9 * @param {string} url Share Url for an entry.
10 * @param {ShareClient.Observer} observer Observer instance.
13 function ShareClient(webView, url, observer) {
14 this.webView_ = webView;
16 this.observer_ = observer;
18 this.loading_ = false;
19 this.onMessageBound_ = this.onMessage_.bind(this);
20 this.onLoadStopBound_ = this.onLoadStop_.bind(this);
21 this.onLoadAbortBound_ = this.onLoadAbort_.bind(this);
25 * Source origin of the client.
29 ShareClient.SHARE_ORIGIN =
30 'chrome-extension://hhaomjibdihmijegdhdafkllkbggdgoj';
33 * Target origin of the embedded dialog.
37 ShareClient.SHARE_TARGET = 'https://drive.google.com';
40 * Observes for state changes of the embedded dialog.
43 ShareClient.Observer = function() {
47 * Notifies about the embedded dialog being loaded.
49 ShareClient.Observer.prototype.onLoaded = function() {
53 * Notifies when the the embedded dialog failed to load.
55 ShareClient.Observer.prototype.onLoadingFailed = function() {
59 * Notifies about changed dimensions of the embedded dialog.
60 * @param {number} width Width in pixels.
61 * @param {number} height Height in pixels.
62 * @param {function()} callback Completion callback. Call when finished
63 * handling the resize.
65 ShareClient.Observer.prototype.onResized = function(width, height, callback) {
69 * Notifies about the embedded dialog being closed.
71 ShareClient.Observer.prototype.onClosed = function() {
75 * Handles messages from the embedded dialog.
76 * @param {Event} e Message event.
79 ShareClient.prototype.onMessage_ = function(e) {
80 if (e.origin != ShareClient.SHARE_TARGET && !window.IN_TEST) {
81 // Logs added temporarily to track crbug.com/288783.
82 console.debug('Received a message from an illegal origin: ' + e.origin);
86 var data = JSON.parse(e.data);
87 // Logs added temporarily to track crbug.com/288783.
88 console.debug('Received message: ' + data.type);
92 this.observer_.onResized(data.args.width,
94 this.postMessage_.bind(this, 'resizeComplete'));
96 case 'prepareForVisible':
97 this.postMessage_('prepareComplete');
99 this.loading_ = false;
101 this.observer_.onLoaded();
105 if (!data.args.visible)
106 this.observer_.onClosed();
112 * Handles completion of the web view request.
113 * @param {Event} e Message event.
116 ShareClient.prototype.onLoadStop_ = function(e) {
117 // Logs added temporarily to track crbug.com/288783.
118 console.debug('Web View loaded.');
120 this.postMessage_('makeBodyVisible');
124 * Handles termination of the web view request.
125 * @param {Event} e Message event.
128 ShareClient.prototype.onLoadAbort_ = function(e) {
129 // Logs added temporarily to track crbug.com/288783.
130 console.debug('Web View failed to load with error: ' + e.reason + ', url: ' +
131 e.url + ' while requested: ' + this.url_);
133 this.observer_.onLoadFailed();
137 * Sends a message to the embedded dialog.
138 * @param {string} type Message type.
139 * @param {Object=} opt_args Optional arguments.
142 ShareClient.prototype.postMessage_ = function(type, opt_args) {
143 // Logs added temporarily to track crbug.com/288783.
144 console.debug('Sending message: ' + type);
150 this.webView_.contentWindow.postMessage(
151 JSON.stringify(message),
152 !window.IN_TEST ? ShareClient.SHARE_TARGET : '*');
156 * Loads the embedded dialog. Can be called only one.
158 ShareClient.prototype.load = function() {
159 if (this.loading_ || this.loaded_)
160 throw new Error('Already loaded.');
161 this.loading_ = true;
163 // Logs added temporarily to track crbug.com/288783.
164 console.debug('Loading.');
166 window.addEventListener('message', this.onMessageBound_);
167 this.webView_.addEventListener('loadstop', this.onLoadStopBound_);
168 this.webView_.addEventListener('loadabort', this.onLoadAbortBound_);
169 this.webView_.setAttribute('src', this.url_);
173 * Aborts loading of the embedded dialog and performs cleanup.
175 ShareClient.prototype.abort = function() {
176 window.removeEventListener('message', this.onMessageBound_);
177 this.webView_.removeEventListener('loadstop', this.onLoadStopBound_);
178 this.webView_.removeEventListener(
179 'loadabort', this.onLoadAbortBound_);
180 this.webView_.stop();
184 * Cleans the dialog by removing all handlers.
186 ShareClient.prototype.dispose = function() {