1 // Copyright (c) 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 * SuggestAppsDialog contains a list box to select an app to be opened the file
9 * with. This dialog should be used as action picker for file operations.
13 * The width of the widget (in pixel).
17 var WEBVIEW_WIDTH = 735;
19 * The height of the widget (in pixel).
23 var WEBVIEW_HEIGHT = 480;
26 * The URL of the widget.
31 'https://clients5.google.com/webstore/wall/cros-widget-container';
33 * The origin of the widget.
37 var CWS_WIDGET_ORIGIN = 'https://clients5.google.com';
40 * Creates dialog in DOM tree.
42 * @param {HTMLElement} parentNode Node to be parent for this dialog.
43 * @param {Object} state Static state of suggest app dialog.
45 * @extends {FileManagerDialogBase}
47 function SuggestAppsDialog(parentNode, state) {
48 FileManagerDialogBase.call(this, parentNode);
50 this.frame_.id = 'suggest-app-dialog';
52 this.webviewContainer_ = this.document_.createElement('div');
53 this.webviewContainer_.id = 'webview-container';
54 this.webviewContainer_.style.width = WEBVIEW_WIDTH + 'px';
55 this.webviewContainer_.style.height = WEBVIEW_HEIGHT + 'px';
56 this.frame_.insertBefore(this.webviewContainer_, this.text_.nextSibling);
58 var spinnerLayer = this.document_.createElement('div');
59 spinnerLayer.className = 'spinner-layer';
60 this.webviewContainer_.appendChild(spinnerLayer);
62 this.buttons_ = this.document_.createElement('div');
63 this.buttons_.id = 'buttons';
64 this.frame_.appendChild(this.buttons_);
66 this.webstoreButton_ = this.document_.createElement('div');
67 this.webstoreButton_.id = 'webstore-button';
68 this.webstoreButton_.innerHTML = str('SUGGEST_DIALOG_LINK_TO_WEBSTORE');
69 this.webstoreButton_.addEventListener(
70 'click', this.onWebstoreLinkClicked_.bind(this));
71 this.buttons_.appendChild(this.webstoreButton_);
73 this.initialFocusElement_ = this.webviewContainer_;
76 this.accessToken_ = null;
78 state.overrideCwsContainerUrlForTest || CWS_WIDGET_URL;
80 state.overrideCwsContainerOriginForTest || CWS_WIDGET_ORIGIN;
82 this.extension_ = null;
84 this.installingItemId_ = null;
85 this.state_ = SuggestAppsDialog.State.UNINITIALIZED;
87 this.initializationTask_ = new AsyncUtil.Group();
88 this.initializationTask_.add(this.retrieveAuthorizeToken_.bind(this));
89 this.initializationTask_.run();
92 SuggestAppsDialog.prototype = {
93 __proto__: FileManagerDialogBase.prototype
100 SuggestAppsDialog.State = {
101 UNINITIALIZED: 'SuggestAppsDialog.State.UNINITIALIZED',
102 INITIALIZING: 'SuggestAppsDialog.State.INITIALIZING',
103 INITIALIZE_FAILED_CLOSING:
104 'SuggestAppsDialog.State.INITIALIZE_FAILED_CLOSING',
105 INITIALIZED: 'SuggestAppsDialog.State.INITIALIZED',
106 INSTALLING: 'SuggestAppsDialog.State.INSTALLING',
107 INSTALLED_CLOSING: 'SuggestAppsDialog.State.INSTALLED_CLOSING',
108 OPENING_WEBSTORE_CLOSING: 'SuggestAppsDialog.State.OPENING_WEBSTORE_CLOSING',
109 CANCELED_CLOSING: 'SuggestAppsDialog.State.CANCELED_CLOSING'
111 Object.freeze(SuggestAppsDialog.State);
117 SuggestAppsDialog.Result = {
118 // Install is done. The install app should be opened.
119 INSTALL_SUCCESSFUL: 'SuggestAppsDialog.Result.INSTALL_SUCCESSFUL',
120 // User cancelled the suggest app dialog. No message should be shown.
121 USER_CANCELL: 'SuggestAppsDialog.Result.USER_CANCELL',
122 // User clicked the link to web store so the dialog is closed.
123 WEBSTORE_LINK_OPENED: 'SuggestAppsDialog.Result.WEBSTORE_LINK_OPENED',
124 // Failed to load the widget. Error message should be shown.
125 FAILED: 'SuggestAppsDialog.Result.FAILED'
127 Object.freeze(SuggestAppsDialog.Result);
132 SuggestAppsDialog.prototype.onInputFocus = function() {
133 this.webviewContainer_.select();
137 * Injects headers into the passed request.
139 * @param {Event} e Request event.
140 * @return {{requestHeaders: HttpHeaders}} Modified headers.
143 SuggestAppsDialog.prototype.authorizeRequest_ = function(e) {
144 e.requestHeaders.push({
145 name: 'Authorization',
146 value: 'Bearer ' + this.accessToken_
148 return {requestHeaders: e.requestHeaders};
152 * Retrieves the authorize token. This method should be called in
153 * initialization of the dialog.
155 * @param {function()} callback Called when the token is retrieved.
158 SuggestAppsDialog.prototype.retrieveAuthorizeToken_ = function(callback) {
159 if (window.IN_TEST) {
160 // In test, use a dummy string as token. This must be a non-empty string.
161 this.accessToken_ = 'DUMMY_ACCESS_TOKEN_FOR_TEST';
164 if (this.accessToken_) {
169 // Fetch or update the access token.
170 chrome.fileBrowserPrivate.requestWebStoreAccessToken(
171 function(accessToken) {
172 // In case of error, this.accessToken_ will be set to null.
173 this.accessToken_ = accessToken;
179 * Dummy function for SuggestAppsDialog.show() not to be called unintentionally.
181 SuggestAppsDialog.prototype.show = function() {
182 console.error('SuggestAppsDialog.show() shouldn\'t be called directly.');
186 * Shows suggest-apps dialog by file extension and mime.
188 * @param {string} extension Extension of the file.
189 * @param {string} mime Mime of the file.
190 * @param {function(boolean)} onDialogClosed Called when the dialog is closed.
191 * The argument is the result of installation: true if an app is installed,
194 SuggestAppsDialog.prototype.showByExtensionAndMime =
195 function(extension, mime, onDialogClosed) {
196 this.text_.hidden = true;
197 this.dialogText_ = '';
198 this.showInternal_(null, extension, mime, onDialogClosed);
202 * Shows suggest-apps dialog by the filename.
204 * @param {string} filename Filename (without extension) of the file.
205 * @param {function(boolean)} onDialogClosed Called when the dialog is closed.
206 * The argument is the result of installation: true if an app is installed,
209 SuggestAppsDialog.prototype.showByFilename =
210 function(filename, onDialogClosed) {
211 this.text_.hidden = false;
212 this.dialogText_ = str('SUGGEST_DIALOG_MESSAGE_FOR_EXECUTABLE');
213 this.showInternal_(filename, null, null, onDialogClosed);
217 * Internal method to show a dialog. This should be called only from 'Suggest.
218 * appDialog.showXxxx()' functions.
220 * @param {string} filename Filename (without extension) of the file.
221 * @param {string} extension Extension of the file.
222 * @param {string} mime Mime of the file.
223 * @param {function(boolean)} onDialogClosed Called when the dialog is closed.
224 * The argument is the result of installation: true if an app is installed,
228 SuggestAppsDialog.prototype.showInternal_ =
229 function(filename, extension, mime, onDialogClosed) {
230 if (this.state_ != SuggestAppsDialog.State.UNINITIALIZED) {
231 console.error('Invalid state.');
235 this.extension_ = extension;
236 this.mimeType_ = mime;
237 this.onDialogClosed_ = onDialogClosed;
238 this.state_ = SuggestAppsDialog.State.INITIALIZING;
240 SuggestAppsDialog.Metrics.recordShowDialog();
241 SuggestAppsDialog.Metrics.startLoad();
243 // Makes it sure that the initialization is completed.
244 this.initializationTask_.run(function() {
245 if (!this.accessToken_) {
246 this.state_ = SuggestAppsDialog.State.INITIALIZE_FAILED_CLOSING;
251 var title = str('SUGGEST_DIALOG_TITLE');
252 var show = this.dialogText_ ?
253 FileManagerDialogBase.prototype.showTitleAndTextDialog.call(
254 this, title, this.dialogText_) :
255 FileManagerDialogBase.prototype.showTitleOnlyDialog.call(
258 console.error('SuggestAppsDialog can\'t be shown');
259 this.state_ = SuggestAppsDialog.State.UNINITIALIZED;
264 this.webview_ = this.document_.createElement('webview');
265 this.webview_.id = 'cws-widget';
266 this.webview_.partition = 'persist:cwswidgets';
267 this.webview_.style.width = WEBVIEW_WIDTH + 'px';
268 this.webview_.style.height = WEBVIEW_HEIGHT + 'px';
269 this.webview_.request.onBeforeSendHeaders.addListener(
270 this.authorizeRequest_.bind(this),
271 {urls: [this.widgetOrigin_ + '/*']},
272 ['blocking', 'requestHeaders']);
273 this.webview_.addEventListener('newwindow', function(event) {
274 // Discard the window object and reopen in an external window.
275 event.window.discard();
276 util.visitURL(event.targetUrl);
277 event.preventDefault();
279 this.webviewContainer_.appendChild(this.webview_);
281 this.frame_.classList.add('show-spinner');
283 this.webviewClient_ = new CWSContainerClient(
285 extension, mime, filename,
286 WEBVIEW_WIDTH, WEBVIEW_HEIGHT,
287 this.widgetUrl_, this.widgetOrigin_);
288 this.webviewClient_.addEventListener(CWSContainerClient.Events.LOADED,
289 this.onWidgetLoaded_.bind(this));
290 this.webviewClient_.addEventListener(CWSContainerClient.Events.LOAD_FAILED,
291 this.onWidgetLoadFailed_.bind(this));
292 this.webviewClient_.addEventListener(
293 CWSContainerClient.Events.REQUEST_INSTALL,
294 this.onInstallRequest_.bind(this));
295 this.webviewClient_.load();
300 * Called when the 'See more...' link is clicked to be navigated to Webstore.
301 * @param {Event} e Event.
304 SuggestAppsDialog.prototype.onWebstoreLinkClicked_ = function(e) {
306 FileTasks.createWebStoreLink(this.extension_, this.mimeType_);
307 util.visitURL(webStoreUrl);
308 this.state_ = SuggestAppsDialog.State.OPENING_WEBSTORE_CLOSING;
313 * Called when the widget is loaded successfully.
314 * @param {Event} event Event.
317 SuggestAppsDialog.prototype.onWidgetLoaded_ = function(event) {
318 SuggestAppsDialog.Metrics.finishLoad();
319 SuggestAppsDialog.Metrics.recordLoad(
320 SuggestAppsDialog.Metrics.LOAD.SUCCEEDED);
322 this.frame_.classList.remove('show-spinner');
323 this.state_ = SuggestAppsDialog.State.INITIALIZED;
325 this.webview_.focus();
329 * Called when the widget is failed to load.
330 * @param {Event} event Event.
333 SuggestAppsDialog.prototype.onWidgetLoadFailed_ = function(event) {
334 SuggestAppsDialog.Metrics.recordLoad(SuggestAppsDialog.Metrics.LOAD.FAILURE);
336 this.frame_.classList.remove('show-spinner');
337 this.state_ = SuggestAppsDialog.State.INITIALIZE_FAILED_CLOSING;
343 * Called when the connection status is changed.
344 * @param {VolumeManagerCommon.DriveConnectionType} connectionType Current
347 SuggestAppsDialog.prototype.onDriveConnectionChanged =
348 function(connectionType) {
349 if (this.state_ !== SuggestAppsDialog.State.UNINITIALIZED &&
350 connectionType === VolumeManagerCommon.DriveConnectionType.OFFLINE) {
351 this.state_ = SuggestAppsDialog.State.INITIALIZE_FAILED_CLOSING;
357 * Called when receiving the install request from the webview client.
358 * @param {Event} e Event.
361 SuggestAppsDialog.prototype.onInstallRequest_ = function(e) {
362 var itemId = e.itemId;
363 this.installingItemId_ = itemId;
365 this.appInstaller_ = new AppInstaller(itemId);
366 this.appInstaller_.install(this.onInstallCompleted_.bind(this));
368 this.frame_.classList.add('show-spinner');
369 this.state_ = SuggestAppsDialog.State.INSTALLING;
373 * Called when the installation is completed from the app installer.
374 * @param {AppInstaller.Result} result Result of the installation.
375 * @param {string} error Detail of the error.
378 SuggestAppsDialog.prototype.onInstallCompleted_ = function(result, error) {
379 var success = (result === AppInstaller.Result.SUCCESS);
381 this.frame_.classList.remove('show-spinner');
382 this.state_ = success ?
383 SuggestAppsDialog.State.INSTALLED_CLOSING :
384 SuggestAppsDialog.State.INITIALIZED; // Back to normal state.
385 this.webviewClient_.onInstallCompleted(success, this.installingItemId_);
386 this.installingItemId_ = null;
389 case AppInstaller.Result.SUCCESS:
390 SuggestAppsDialog.Metrics.recordInstall(
391 SuggestAppsDialog.Metrics.INSTALL.SUCCESS);
394 case AppInstaller.Result.CANCELLED:
395 SuggestAppsDialog.Metrics.recordInstall(
396 SuggestAppsDialog.Metrics.INSTALL.CANCELLED);
397 // User cancelled the installation. Do nothing.
399 case AppInstaller.Result.ERROR:
400 SuggestAppsDialog.Metrics.recordInstall(
401 SuggestAppsDialog.Metrics.INSTALL.FAILED);
402 fileManager.error.show(str('SUGGEST_DIALOG_INSTALLATION_FAILED'));
410 SuggestAppsDialog.prototype.hide = function(opt_originalOnHide) {
411 switch (this.state_) {
412 case SuggestAppsDialog.State.INSTALLING:
413 // Install is being aborted. Send the failure result.
414 // Cancels the install.
415 if (this.webviewClient_)
416 this.webviewClient_.onInstallCompleted(false, this.installingItemId_);
417 this.installingItemId_ = null;
419 // Assumes closing the dialog as canceling the install.
420 this.state_ = SuggestAppsDialog.State.CANCELED_CLOSING;
422 case SuggestAppsDialog.State.INITIALIZING:
423 SuggestAppsDialog.Metrics.recordLoad(
424 SuggestAppsDialog.Metrics.LOAD.CANCELLED);
425 this.state_ = SuggestAppsDialog.State.CANCELED_CLOSING;
427 case SuggestAppsDialog.State.INSTALLED_CLOSING:
428 case SuggestAppsDialog.State.INITIALIZE_FAILED_CLOSING:
429 case SuggestAppsDialog.State.OPENING_WEBSTORE_CLOSING:
432 case SuggestAppsDialog.State.INITIALIZED:
433 this.state_ = SuggestAppsDialog.State.CANCELED_CLOSING;
436 this.state_ = SuggestAppsDialog.State.CANCELED_CLOSING;
437 console.error('Invalid state.');
440 if (this.webviewClient_) {
441 this.webviewClient_.dispose();
442 this.webviewClient_ = null;
445 this.webviewContainer_.removeChild(this.webview_);
446 this.webview_ = null;
447 this.extension_ = null;
450 FileManagerDialogBase.prototype.hide.call(
452 this.onHide_.bind(this, opt_originalOnHide));
456 * @param {function()=} opt_originalOnHide Original onHide function passed to
457 * SuggestAppsDialog.hide().
460 SuggestAppsDialog.prototype.onHide_ = function(opt_originalOnHide) {
461 // Calls the callback after the dialog hides.
462 if (opt_originalOnHide)
463 opt_originalOnHide();
466 switch (this.state_) {
467 case SuggestAppsDialog.State.INSTALLED_CLOSING:
468 result = SuggestAppsDialog.Result.INSTALL_SUCCESSFUL;
469 SuggestAppsDialog.Metrics.recordCloseDialog(
470 SuggestAppsDialog.Metrics.CLOSE_DIALOG.ITEM_INSTALLED);
472 case SuggestAppsDialog.State.INITIALIZE_FAILED_CLOSING:
473 result = SuggestAppsDialog.Result.FAILED;
475 case SuggestAppsDialog.State.CANCELED_CLOSING:
476 result = SuggestAppsDialog.Result.USER_CANCELL;
477 SuggestAppsDialog.Metrics.recordCloseDialog(
478 SuggestAppsDialog.Metrics.CLOSE_DIALOG.USER_CANCELL);
480 case SuggestAppsDialog.State.OPENING_WEBSTORE_CLOSING:
481 result = SuggestAppsDialog.Result.WEBSTORE_LINK_OPENED;
482 SuggestAppsDialog.Metrics.recordCloseDialog(
483 SuggestAppsDialog.Metrics.CLOSE_DIALOG.WEB_STORE_LINK);
486 result = SuggestAppsDialog.Result.USER_CANCELL;
487 SuggestAppsDialog.Metrics.recordCloseDialog(
488 SuggestAppsDialog.Metrics.CLOSE_DIALOG.UNKNOWN_ERROR);
489 console.error('Invalid state.');
491 this.state_ = SuggestAppsDialog.State.UNINITIALIZED;
493 this.onDialogClosed_(result);
497 * Utility methods and constants to record histograms.
499 SuggestAppsDialog.Metrics = Object.freeze({
500 LOAD: Object.freeze({
507 * @param {SuggestAppsDialog.Metrics.LOAD} result Result of load.
509 recordLoad: function(result) {
510 if (0 <= result && result < 3)
511 metrics.recordEnum('SuggestApps.Load', result, 3);
514 CLOSE_DIALOG: Object.freeze({
518 WEBSTORE_LINK_OPENED: 3,
522 * @param {SuggestAppsDialog.Metrics.CLOSE_DIALOG} reason Reason of closing
525 recordCloseDialog: function(reason) {
526 if (0 <= reason && reason < 4)
527 metrics.recordEnum('SuggestApps.CloseDialog', reason, 4);
530 INSTALL: Object.freeze({
537 * @param {SuggestAppsDialog.Metrics.INSTALL} result Result of installation.
539 recordInstall: function(result) {
540 if (0 <= result && result < 3)
541 metrics.recordEnum('SuggestApps.Install', result, 3);
544 recordShowDialog: function() {
545 metrics.recordUserAction('SuggestApps.ShowDialog');
548 startLoad: function() {
549 metrics.startInterval('SuggestApps.LoadTime');
552 finishLoad: function() {
553 metrics.recordInterval('SuggestApps.LoadTime');