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 {HTMLElement} parentNode Node to be parent for this dialog.
10 * @extends {FileManagerDialogBase}
11 * @implements {ShareClient.Observer}
13 function ShareDialog(parentNode) {
14 this.queue_ = new AsyncUtil.Queue();
15 this.onQueueTaskFinished_ = null;
16 this.shareClient_ = null;
17 this.webViewWrapper_ = null;
19 this.failureTimeout_ = null;
20 this.callback_ = null;
22 FileManagerDialogBase.call(this, parentNode);
26 * Timeout for loading the share dialog before giving up.
30 ShareDialog.FAILURE_TIMEOUT = 10000;
33 * The result of opening the dialog.
37 ShareDialog.Result = Object.freeze({
38 // The dialog is closed normally. This includes user cancel.
40 // The dialog is closed by network error.
41 NETWORK_ERROR: 'networkError',
42 // The dialog is not opened because it is already showing.
43 ALREADY_SHOWING: 'alreadyShowing'
47 * Wraps a Web View element and adds authorization headers to it.
48 * @param {string} urlPattern Pattern of urls to be authorized.
49 * @param {WebView} webView Web View element to be wrapped.
52 ShareDialog.WebViewAuthorizer = function(urlPattern, webView) {
53 this.urlPattern_ = urlPattern;
54 this.webView_ = webView;
55 this.initialized_ = false;
56 this.accessToken_ = null;
60 * Initializes the web view by installing hooks injecting the authorization
62 * @param {function()} callback Completion callback.
64 ShareDialog.WebViewAuthorizer.prototype.initialize = function(callback) {
65 if (this.initialized_) {
70 var registerInjectionHooks = function() {
71 this.webView_.removeEventListener('loadstop', registerInjectionHooks);
72 this.webView_.request.onBeforeSendHeaders.addListener(
73 this.authorizeRequest_.bind(this),
74 {urls: [this.urlPattern_]},
75 ['blocking', 'requestHeaders']);
76 this.initialized_ = true;
80 this.webView_.addEventListener('loadstop', registerInjectionHooks);
81 this.webView_.setAttribute('src', 'data:text/html,');
85 * Authorizes the web view by fetching the freshest access tokens.
86 * @param {function()} callback Completion callback.
88 ShareDialog.WebViewAuthorizer.prototype.authorize = function(callback) {
89 // Fetch or update the access token.
90 chrome.fileManagerPrivate.requestAccessToken(false, // force_refresh
91 function(inAccessToken) {
92 this.accessToken_ = inAccessToken;
98 * Injects headers into the passed request.
99 * @param {Event} e Request event.
100 * @return {{requestHeaders: HttpHeaders}} Modified headers.
103 ShareDialog.WebViewAuthorizer.prototype.authorizeRequest_ = function(e) {
104 e.requestHeaders.push({
105 name: 'Authorization',
106 value: 'Bearer ' + this.accessToken_
108 return {requestHeaders: e.requestHeaders};
111 ShareDialog.prototype = {
112 __proto__: FileManagerDialogBase.prototype
116 * One-time initialization of DOM.
119 ShareDialog.prototype.initDom_ = function() {
120 FileManagerDialogBase.prototype.initDom_.call(this);
121 this.frame_.classList.add('share-dialog-frame');
123 this.webViewWrapper_ = this.document_.createElement('div');
124 this.webViewWrapper_.className = 'share-dialog-webview-wrapper';
125 this.cancelButton_.hidden = true;
126 this.okButton_.hidden = true;
127 this.frame_.insertBefore(this.webViewWrapper_,
128 this.frame_.querySelector('.cr-dialog-buttons'));
134 ShareDialog.prototype.onResized = function(width, height, callback) {
135 if (width && height) {
136 this.webViewWrapper_.style.width = width + 'px';
137 this.webViewWrapper_.style.height = height + 'px';
138 this.webView_.style.width = width + 'px';
139 this.webView_.style.height = height + 'px';
141 setTimeout(callback, 0);
147 ShareDialog.prototype.onClosed = function() {
154 ShareDialog.prototype.onLoaded = function() {
155 if (this.failureTimeout_) {
156 clearTimeout(this.failureTimeout_);
157 this.failureTimeout_ = null;
160 // Logs added temporarily to track crbug.com/288783.
161 console.debug('Loaded.');
163 this.okButton_.hidden = false;
164 this.webViewWrapper_.classList.add('loaded');
165 this.webView_.focus();
171 ShareDialog.prototype.onLoadFailed = function() {
172 this.hideWithResult(ShareDialog.Result.NETWORK_ERROR);
178 ShareDialog.prototype.hide = function(opt_onHide) {
179 this.hideWithResult(ShareDialog.Result.SUCCESS, opt_onHide);
183 * Hide the dialog with the result and the callback.
184 * @param {ShareDialog.Result} result Result passed to the closing callback.
185 * @param {function()=} opt_onHide Callback called at the end of hiding.
187 ShareDialog.prototype.hideWithResult = function(result, opt_onHide) {
188 if (!this.isShowing())
191 if (this.shareClient_) {
192 this.shareClient_.dispose();
193 this.shareClient_ = null;
196 this.webViewWrapper_.textContent = '';
197 if (this.failureTimeout_) {
198 clearTimeout(this.failureTimeout_);
199 this.failureTimeout_ = null;
202 FileManagerDialogBase.prototype.hide.call(
207 this.callback_(result);
208 this.callback_ = null;
214 * @param {FileEntry} entry Entry to share.
215 * @param {function(boolean)} callback Callback to be called when the showing
216 * task is completed. The argument is whether to succeed or not. Note that
217 * cancel is regarded as success.
219 ShareDialog.prototype.show = function(entry, callback) {
220 // If the dialog is already showing, return the error.
221 if (this.isShowing()) {
222 callback(ShareDialog.Result.ALREADY_SHOWING);
226 // Initialize the variables.
227 this.callback_ = callback;
228 this.webViewWrapper_.style.width = '';
229 this.webViewWrapper_.style.height = '';
230 this.webViewWrapper_.classList.remove('loaded');
232 // If the embedded share dialog is not started within some time, then
233 // give up and show an error message.
234 this.failureTimeout_ = setTimeout(function() {
235 this.hideWithResult(ShareDialog.Result.NETWORK_ERROR);
237 // Logs added temporarily to track crbug.com/288783.
238 console.debug('Timeout. Web View points at: ' + this.webView_.src);
239 }.bind(this), ShareDialog.FAILURE_TIMEOUT);
241 // TODO(mtomasz): Move to initDom_() once and reuse <webview> once it gets
242 // fixed. See: crbug.com/260622.
243 this.webView_ = util.createChild(
244 this.webViewWrapper_, 'share-dialog-webview', 'webview');
245 this.webView_.setAttribute('tabIndex', '-1');
246 this.webViewAuthorizer_ = new ShareDialog.WebViewAuthorizer(
247 !window.IN_TEST ? (ShareClient.SHARE_TARGET + '/*') : '<all_urls>',
249 this.webView_.addEventListener('newwindow', function(e) {
250 // Discard the window object and reopen in an external window.
252 util.visitURL(e.targetUrl);
255 var show = FileManagerDialogBase.prototype.showBlankDialog.call(this);
257 // The code shoundn't get here, since already-showing was handled before.
258 console.error('ShareDialog can\'t be shown.');
262 // Initialize and authorize the Web View tag asynchronously.
263 var group = new AsyncUtil.Group();
265 // Fetches an url to the sharing dialog.
267 group.add(function(inCallback) {
268 chrome.fileManagerPrivate.getShareUrl(
270 function(inShareUrl) {
271 if (!chrome.runtime.lastError)
272 shareUrl = inShareUrl;
274 console.error(chrome.runtime.lastError.message);
278 group.add(this.webViewAuthorizer_.initialize.bind(this.webViewAuthorizer_));
279 group.add(this.webViewAuthorizer_.authorize.bind(this.webViewAuthorizer_));
281 // Loads the share widget once all the previous async calls are finished.
282 group.run(function() {
283 // If the url is not obtained, return the network error.
285 // Logs added temporarily to track crbug.com/288783.
286 console.debug('The share URL is not available.');
288 this.hideWithResult(ShareDialog.Result.NETWORK_ERROR);
291 // Already inactive, therefore ignore.
292 if (!this.isShowing())
294 this.shareClient_ = new ShareClient(this.webView_,
297 this.shareClient_.load();
302 * Tells whether the share dialog is showing or not.
304 * @return {boolean} True since the show method is called and until the closing
305 * callback is invoked.
307 ShareDialog.prototype.isShowing = function() {
308 return !!this.callback_;