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.
6 * @param {WebView} webView Web View tag.
7 * @param {string} url Share Url for an entry.
8 * @param {ShareClient.Observer} observer Observer instance.
11 function ShareClient(webView, url, observer) {
12 this.webView_ = webView;
14 this.observer_ = observer;
16 this.loading_ = false;
17 this.onMessageBound_ = this.onMessage_.bind(this);
18 this.onLoadStopBound_ = this.onLoadStop_.bind(this);
19 this.onLoadAbortBound_ = this.onLoadAbort_.bind(this);
23 * Target origin of the embedded dialog.
27 ShareClient.SHARE_TARGET = 'https://drive.google.com';
30 * Observes for state changes of the embedded dialog.
33 ShareClient.Observer = function() {
37 * Notifies about the embedded dialog being loaded.
39 ShareClient.Observer.prototype.onLoaded = function() {
43 * Notifies when the the embedded dialog failed to load.
45 ShareClient.Observer.prototype.onLoadFailed = function() {
49 * Notifies about changed dimensions of the embedded dialog.
50 * @param {number} width Width in pixels.
51 * @param {number} height Height in pixels.
52 * @param {function()} callback Completion callback. Call when finished
53 * handling the resize.
55 ShareClient.Observer.prototype.onResized = function(width, height, callback) {
59 * Notifies about the embedded dialog being closed.
61 ShareClient.Observer.prototype.onClosed = function() {
65 * Handles messages from the embedded dialog.
66 * @param {Event} e Message event.
69 ShareClient.prototype.onMessage_ = function(e) {
70 if (e.origin != ShareClient.SHARE_TARGET && !window.IN_TEST) {
71 // Logs added temporarily to track crbug.com/288783.
72 console.debug('Received a message from an illegal origin: ' + e.origin);
76 var data = JSON.parse(e.data);
77 // Logs added temporarily to track crbug.com/288783.
78 console.debug('Received message: ' + data.type);
82 this.observer_.onResized(data.args.width,
84 this.postMessage_.bind(this, 'resizeComplete'));
86 case 'prepareForVisible':
87 this.postMessage_('prepareComplete');
89 this.loading_ = false;
91 this.observer_.onLoaded();
95 if (!data.args.visible)
96 this.observer_.onClosed();
102 * Handles completion of the web view request.
103 * @param {Event} e Message event.
106 ShareClient.prototype.onLoadStop_ = function(e) {
107 // Logs added temporarily to track crbug.com/288783.
108 console.debug('Web View loaded.');
110 this.postMessage_('makeBodyVisible');
114 * Handles termination of the web view request.
115 * @param {Event} e Message event.
118 ShareClient.prototype.onLoadAbort_ = function(e) {
119 // Logs added temporarily to track crbug.com/288783.
120 console.debug('Web View failed to load with error: ' + e.reason + ', url: ' +
121 e.url + ' while requested: ' + this.url_);
123 this.observer_.onLoadFailed();
127 * Sends a message to the embedded dialog.
128 * @param {string} type Message type.
129 * @param {Object=} opt_args Optional arguments.
132 ShareClient.prototype.postMessage_ = function(type, opt_args) {
133 // Logs added temporarily to track crbug.com/288783.
134 console.debug('Sending message: ' + type);
140 this.webView_.contentWindow.postMessage(
141 JSON.stringify(message),
142 !window.IN_TEST ? ShareClient.SHARE_TARGET : '*');
146 * Loads the embedded dialog. Can be called only one.
148 ShareClient.prototype.load = function() {
149 if (this.loading_ || this.loaded_)
150 throw new Error('Already loaded.');
151 this.loading_ = true;
153 // Logs added temporarily to track crbug.com/288783.
154 console.debug('Loading.');
156 window.addEventListener('message', this.onMessageBound_);
157 this.webView_.addEventListener('loadstop', this.onLoadStopBound_);
158 this.webView_.addEventListener('loadabort', this.onLoadAbortBound_);
159 this.webView_.setAttribute('src', this.url_);
163 * Aborts loading of the embedded dialog and performs cleanup.
165 ShareClient.prototype.abort = function() {
166 window.removeEventListener('message', this.onMessageBound_);
167 this.webView_.removeEventListener('loadstop', this.onLoadStopBound_);
168 this.webView_.removeEventListener(
169 'loadabort', this.onLoadAbortBound_);
170 this.webView_.stop();
174 * Cleans the dialog by removing all handlers.
176 ShareClient.prototype.dispose = function() {