Commit 1c38f9fb authored by tbarzic's avatar tbarzic Committed by Commit bot

Use delegate object for chrome API calls in cws_widget_container

This would make the widget container code independent of the
chrome APIs
available/used by the extension embedding the widget.

TEST=Verified installing file handlers and file system providers works
BUG=477106

Review URL: https://codereview.chromium.org/1091943002

Cr-Commit-Position: refs/heads/master@{#327345}
parent 15525c4f
...@@ -6,10 +6,14 @@ ...@@ -6,10 +6,14 @@
* Manage the installation of apps. * Manage the installation of apps.
* *
* @param {string} itemId Item id to be installed. * @param {string} itemId Item id to be installed.
* @param {!CWSWidgetContainer.PlatformDelegate} delegate Delegate for accessing
* Chrome platform APIs.
* @constructor * @constructor
* @struct * @struct
*/ */
function AppInstaller(itemId) { function AppInstaller(itemId, delegate) {
/** @private {!CWSWidgetContainer.PlatformDelegate} */
this.delegate_ = delegate;
this.itemId_ = itemId; this.itemId_ = itemId;
this.callback_ = null; this.callback_ = null;
} }
...@@ -43,12 +47,9 @@ AppInstaller.USER_CANCELLED_ERROR_STR_ = 'User cancelled install'; ...@@ -43,12 +47,9 @@ AppInstaller.USER_CANCELLED_ERROR_STR_ = 'User cancelled install';
*/ */
AppInstaller.prototype.install = function(callback) { AppInstaller.prototype.install = function(callback) {
this.callback_ = callback; this.callback_ = callback;
chrome.fileManagerPrivate.installWebstoreItem( this.delegate_.installWebstoreItem(
this.itemId_, this.itemId_,
false, // Shows installation prompt. this.onInstallCompleted_.bind(this));
function() {
this.onInstallCompleted_(chrome.runtime.lastError);
}.bind(this));
}; };
/** /**
...@@ -63,8 +64,8 @@ AppInstaller.prototype.cancel = function() { ...@@ -63,8 +64,8 @@ AppInstaller.prototype.cancel = function() {
/** /**
* Called when the installation is completed. * Called when the installation is completed.
* *
* @param {!Object|undefined} error Undefined if the installation is success, * @param {?string} error Null if the installation is success,
* otherwise an object which contains error message. * otherwise error message.
* @private * @private
*/ */
AppInstaller.prototype.onInstallCompleted_ = function(error) { AppInstaller.prototype.onInstallCompleted_ = function(error) {
...@@ -72,14 +73,12 @@ AppInstaller.prototype.onInstallCompleted_ = function(error) { ...@@ -72,14 +73,12 @@ AppInstaller.prototype.onInstallCompleted_ = function(error) {
return; return;
var installerResult = AppInstaller.Result.SUCCESS; var installerResult = AppInstaller.Result.SUCCESS;
var errorMessage = ''; if (error !== null) {
if (error) {
installerResult = installerResult =
error.message == AppInstaller.USER_CANCELLED_ERROR_STR_ ? error == AppInstaller.USER_CANCELLED_ERROR_STR_ ?
AppInstaller.Result.CANCELLED : AppInstaller.Result.CANCELLED :
AppInstaller.Result.ERROR; AppInstaller.Result.ERROR;
errorMessage = error.message;
} }
this.callback_(installerResult, errorMessage); this.callback_(installerResult, error || '');
this.callback_ = null; this.callback_ = null;
}; };
...@@ -9,10 +9,15 @@ ...@@ -9,10 +9,15 @@
* @param {string} url Share Url for an entry. * @param {string} url Share Url for an entry.
* @param {string} target Target (scheme + host + port) of the widget. * @param {string} target Target (scheme + host + port) of the widget.
* @param {Object<string, *>} options Options to be sent to the dialog host. * @param {Object<string, *>} options Options to be sent to the dialog host.
* @param {!CWSWidgetContainer.PlatformDelegate} delegate Delegate for accessing
* Chrome platform APIs.
* @constructor * @constructor
* @extends {cr.EventTarget} * @extends {cr.EventTarget}
*/ */
function CWSContainerClient(webView, width, height, url, target, options) { function CWSContainerClient(webView, width, height, url, target, options,
delegate) {
/** @private {!CWSWidgetContainer.PlatformDelegate} */
this.delegate_ = delegate;
this.webView_ = webView; this.webView_ = webView;
this.width_ = width; this.width_ = width;
this.height_ = height; this.height_ = height;
...@@ -194,23 +199,26 @@ CWSContainerClient.prototype.postInstallSuccessMessage_ = function(itemId) { ...@@ -194,23 +199,26 @@ CWSContainerClient.prototype.postInstallSuccessMessage_ = function(itemId) {
*/ */
CWSContainerClient.prototype.postInitializeMessage_ = function() { CWSContainerClient.prototype.postInitializeMessage_ = function() {
new Promise(function(fulfill, reject) { new Promise(function(fulfill, reject) {
chrome.fileManagerPrivate.getProvidingExtensions(function(items) { this.delegate_.getInstalledItems(
if (chrome.runtime.lastError) { /**
reject(chrome.runtime.lastError.message); * @param {?Array.<!string>} items Installed items.
return; * Null on error.
} */
fulfill(items.map(function(item) { function(items) {
return item.extensionId; if (!items) {
})); reject('Failed to retrive installed items.');
return;
}
fulfill(items);
}) })
}).then( }.bind(this)).then(
/** /**
* @param {!Array<string>} preinstalledExtensionIDs * @param {!Array<string>} preinstalledExtensionIDs
*/ */
function(preinstalledExtensionIDs) { function(preinstalledExtensionIDs) {
var message = { var message = {
message: 'initialize', message: 'initialize',
hl: util.getCurrentLocaleOrDefault(), hl: this.delegate_.strings.UI_LOCALE,
width: this.width_, width: this.width_,
height: this.height_, height: this.height_,
preinstalled_items: preinstalledExtensionIDs, preinstalled_items: preinstalledExtensionIDs,
......
...@@ -33,7 +33,8 @@ function SuggestAppsDialog(parentNode, state) { ...@@ -33,7 +33,8 @@ function SuggestAppsDialog(parentNode, state) {
* @const {!CWSWidgetContainer} * @const {!CWSWidgetContainer}
* @private * @private
*/ */
this.widget_ = new CWSWidgetContainer(this.document_, widgetRoot, state); this.widget_ = new CWSWidgetContainer(
this.document_, widgetRoot, this.createWidgetPlatformDelegate_(), state);
this.initialFocusElement_ = this.widget_.getInitiallyFocusedElement(); this.initialFocusElement_ = this.widget_.getInitiallyFocusedElement();
...@@ -114,6 +115,71 @@ SuggestAppsDialog.prototype.showProviders = function(onDialogClosed) { ...@@ -114,6 +115,71 @@ SuggestAppsDialog.prototype.showProviders = function(onDialogClosed) {
onDialogClosed); onDialogClosed);
}; };
/**
* Creates platform delegate for CWSWidgetContainer.
* @return {!CWSWidgetContainer.PlatformDelegate}
* @private
*/
SuggestAppsDialog.prototype.createWidgetPlatformDelegate_ = function() {
return {
strings: {
UI_LOCALE: util.getCurrentLocaleOrDefault(),
LINK_TO_WEBSTORE: str('SUGGEST_DIALOG_LINK_TO_WEBSTORE'),
INSTALLATION_FAILED_MESSAGE: str('SUGGEST_DIALOG_INSTALLATION_FAILED')
},
metricsImpl: metrics,
/**
* @param {string} itemId,
* @param {function(?string)} callback Callback argument is set to error
* message (null on success)
*/
installWebstoreItem: function(itemId, callback) {
chrome.fileManagerPrivate.installWebstoreItem(
itemId,
false /* show installation prompt */,
function() {
callback(chrome.runtime.lastError ?
chrome.runtime.lastError.message || 'UNKNOWN ERROR' : null);
});
},
/**
* @param {function(?Array.<!string>)} callback Callback
* argument is a list of installed item ids (null on error).
*/
getInstalledItems: function(callback) {
chrome.fileManagerPrivate.getProvidingExtensions(function(items) {
if (chrome.runtime.lastError) {
console.error('Failed to get installed items: ' +
chrome.runtime.lastError.message);
callback(null);
return;
}
callback(items.map(function(item) {
return item.extensionId;
}));
});
},
/**
* @param {function(?string)} callback Callback argument is the requested
* token (null on error).
*/
requestWebstoreAccessToken: function(callback) {
chrome.fileManagerPrivate.requestWebStoreAccessToken(function(token) {
if (chrome.runtime.lastError) {
console.error(chrome.runtime.lastError.message);
callback(null);
return;
}
callback(token);
});
}
};
};
/** /**
* Internal method to show a dialog. This should be called only from 'Suggest. * Internal method to show a dialog. This should be called only from 'Suggest.
* appDialog.showXxxx()' functions. * appDialog.showXxxx()' functions.
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment