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 * Target origin of the embedded dialog.
29 ShareClient.SHARE_TARGET = 'https://drive.google.com';
32 * Observes for state changes of the embedded dialog.
35 ShareClient.Observer = function() {
39 * Notifies about the embedded dialog being loaded.
41 ShareClient.Observer.prototype.onLoaded = function() {
45 * Notifies when the the embedded dialog failed to load.
47 ShareClient.Observer.prototype.onLoadingFailed = function() {
51 * Notifies about changed dimensions of the embedded dialog.
52 * @param {number} width Width in pixels.
53 * @param {number} height Height in pixels.
54 * @param {function()} callback Completion callback. Call when finished
55 * handling the resize.
57 ShareClient.Observer.prototype.onResized = function(width, height, callback) {
61 * Notifies about the embedded dialog being closed.
63 ShareClient.Observer.prototype.onClosed = function() {
67 * Handles messages from the embedded dialog.
68 * @param {Event} e Message event.
71 ShareClient.prototype.onMessage_ = function(e) {
72 if (e.origin != ShareClient.SHARE_TARGET && !window.IN_TEST) {
73 // Logs added temporarily to track crbug.com/288783.
74 console.debug('Received a message from an illegal origin: ' + e.origin);
78 var data = JSON.parse(e.data);
79 // Logs added temporarily to track crbug.com/288783.
80 console.debug('Received message: ' + data.type);
84 this.observer_.onResized(data.args.width,
86 this.postMessage_.bind(this, 'resizeComplete'));
88 case 'prepareForVisible':
89 this.postMessage_('prepareComplete');
91 this.loading_ = false;
93 this.observer_.onLoaded();
97 if (!data.args.visible)
98 this.observer_.onClosed();
104 * Handles completion of the web view request.
105 * @param {Event} e Message event.
108 ShareClient.prototype.onLoadStop_ = function(e) {
109 // Logs added temporarily to track crbug.com/288783.
110 console.debug('Web View loaded.');
112 this.postMessage_('makeBodyVisible');
116 * Handles termination of the web view request.
117 * @param {Event} e Message event.
120 ShareClient.prototype.onLoadAbort_ = function(e) {
121 // Logs added temporarily to track crbug.com/288783.
122 console.debug('Web View failed to load with error: ' + e.reason + ', url: ' +
123 e.url + ' while requested: ' + this.url_);
125 this.observer_.onLoadFailed();
129 * Sends a message to the embedded dialog.
130 * @param {string} type Message type.
131 * @param {Object=} opt_args Optional arguments.
134 ShareClient.prototype.postMessage_ = function(type, opt_args) {
135 // Logs added temporarily to track crbug.com/288783.
136 console.debug('Sending message: ' + type);
142 this.webView_.contentWindow.postMessage(
143 JSON.stringify(message),
144 !window.IN_TEST ? ShareClient.SHARE_TARGET : '*');
148 * Loads the embedded dialog. Can be called only one.
150 ShareClient.prototype.load = function() {
151 if (this.loading_ || this.loaded_)
152 throw new Error('Already loaded.');
153 this.loading_ = true;
155 // Logs added temporarily to track crbug.com/288783.
156 console.debug('Loading.');
158 window.addEventListener('message', this.onMessageBound_);
159 this.webView_.addEventListener('loadstop', this.onLoadStopBound_);
160 this.webView_.addEventListener('loadabort', this.onLoadAbortBound_);
161 this.webView_.setAttribute('src', this.url_);
165 * Aborts loading of the embedded dialog and performs cleanup.
167 ShareClient.prototype.abort = function() {
168 window.removeEventListener('message', this.onMessageBound_);
169 this.webView_.removeEventListener('loadstop', this.onLoadStopBound_);
170 this.webView_.removeEventListener(
171 'loadabort', this.onLoadAbortBound_);
172 this.webView_.stop();
176 * Cleans the dialog by removing all handlers.
178 ShareClient.prototype.dispose = function() {