Commit b95648fd authored by Anand K. Mistry's avatar Anand K. Mistry Committed by Commit Bot

[Extensions Functions] Migrate imageWriterPrivate API to ExtensionFunction

Bug: 634140
Change-Id: I177393c1950a475c44e0d35f0f27b48fecf800e2
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2056190Reviewed-by: default avatarKaran Bhatia <karandeepb@chromium.org>
Commit-Queue: Anand Mistry <amistry@chromium.org>
Cr-Commit-Position: refs/heads/master@{#743331}
parent 9c725992
...@@ -24,9 +24,10 @@ ImageWriterPrivateBaseFunction::~ImageWriterPrivateBaseFunction() {} ...@@ -24,9 +24,10 @@ ImageWriterPrivateBaseFunction::~ImageWriterPrivateBaseFunction() {}
void ImageWriterPrivateBaseFunction::OnComplete(bool success, void ImageWriterPrivateBaseFunction::OnComplete(bool success,
const std::string& error) { const std::string& error) {
if (!success) if (success)
error_ = error; Respond(NoArguments());
SendResponse(success); else
Respond(Error(error));
} }
ImageWriterPrivateWriteFromUrlFunction:: ImageWriterPrivateWriteFromUrlFunction::
...@@ -37,12 +38,13 @@ ImageWriterPrivateWriteFromUrlFunction:: ...@@ -37,12 +38,13 @@ ImageWriterPrivateWriteFromUrlFunction::
~ImageWriterPrivateWriteFromUrlFunction() { ~ImageWriterPrivateWriteFromUrlFunction() {
} }
bool ImageWriterPrivateWriteFromUrlFunction::RunAsync() { ExtensionFunction::ResponseAction
ImageWriterPrivateWriteFromUrlFunction::Run() {
#if defined(OS_CHROMEOS) #if defined(OS_CHROMEOS)
if (GetProfile()->GetPrefs()->GetBoolean(prefs::kExternalStorageDisabled) || Profile* profile = Profile::FromBrowserContext(browser_context());
GetProfile()->GetPrefs()->GetBoolean(prefs::kExternalStorageReadOnly)) { if (profile->GetPrefs()->GetBoolean(prefs::kExternalStorageDisabled) ||
error_ = image_writer::error::kDeviceWriteError; profile->GetPrefs()->GetBoolean(prefs::kExternalStorageReadOnly)) {
return false; return RespondNow(Error(image_writer::error::kDeviceWriteError));
} }
#endif #endif
std::unique_ptr<image_writer_api::WriteFromUrl::Params> params( std::unique_ptr<image_writer_api::WriteFromUrl::Params> params(
...@@ -50,22 +52,20 @@ bool ImageWriterPrivateWriteFromUrlFunction::RunAsync() { ...@@ -50,22 +52,20 @@ bool ImageWriterPrivateWriteFromUrlFunction::RunAsync() {
EXTENSION_FUNCTION_VALIDATE(params.get()); EXTENSION_FUNCTION_VALIDATE(params.get());
GURL url(params->image_url); GURL url(params->image_url);
if (!url.is_valid()) { if (!url.is_valid())
error_ = image_writer::error::kUrlInvalid; return RespondNow(Error(image_writer::error::kUrlInvalid));
return false;
}
std::string hash; std::string hash;
if (params->options.get() && params->options->image_hash.get()) { if (params->options.get() && params->options->image_hash.get()) {
hash = *params->options->image_hash; hash = *params->options->image_hash;
} }
image_writer::OperationManager::Get(GetProfile()) image_writer::OperationManager::Get(browser_context())
->StartWriteFromUrl( ->StartWriteFromUrl(
extension_id(), url, hash, params->storage_unit_id, extension_id(), url, hash, params->storage_unit_id,
base::BindOnce(&ImageWriterPrivateWriteFromUrlFunction::OnComplete, base::BindOnce(&ImageWriterPrivateWriteFromUrlFunction::OnComplete,
this)); this));
return true; return RespondLater();
} }
ImageWriterPrivateWriteFromFileFunction:: ImageWriterPrivateWriteFromFileFunction::
...@@ -76,12 +76,13 @@ ImageWriterPrivateWriteFromFileFunction:: ...@@ -76,12 +76,13 @@ ImageWriterPrivateWriteFromFileFunction::
~ImageWriterPrivateWriteFromFileFunction() { ~ImageWriterPrivateWriteFromFileFunction() {
} }
bool ImageWriterPrivateWriteFromFileFunction::RunAsync() { ExtensionFunction::ResponseAction
ImageWriterPrivateWriteFromFileFunction::Run() {
#if defined(OS_CHROMEOS) #if defined(OS_CHROMEOS)
if (GetProfile()->GetPrefs()->GetBoolean(prefs::kExternalStorageDisabled) || Profile* profile = Profile::FromBrowserContext(browser_context());
GetProfile()->GetPrefs()->GetBoolean(prefs::kExternalStorageReadOnly)) { if (profile->GetPrefs()->GetBoolean(prefs::kExternalStorageDisabled) ||
error_ = image_writer::error::kDeviceWriteError; profile->GetPrefs()->GetBoolean(prefs::kExternalStorageReadOnly)) {
return false; return RespondNow(Error(image_writer::error::kDeviceWriteError));
} }
#endif #endif
std::string filesystem_name; std::string filesystem_name;
...@@ -94,17 +95,19 @@ bool ImageWriterPrivateWriteFromFileFunction::RunAsync() { ...@@ -94,17 +95,19 @@ bool ImageWriterPrivateWriteFromFileFunction::RunAsync() {
base::FilePath path; base::FilePath path;
std::string error;
if (!extensions::app_file_handler_util::ValidateFileEntryAndGetPath( if (!extensions::app_file_handler_util::ValidateFileEntryAndGetPath(
filesystem_name, filesystem_path, source_process_id(), &path, filesystem_name, filesystem_path, source_process_id(), &path,
&error_)) &error)) {
return false; return RespondNow(Error(error));
}
image_writer::OperationManager::Get(GetProfile()) image_writer::OperationManager::Get(browser_context())
->StartWriteFromFile( ->StartWriteFromFile(
extension_id(), path, storage_unit_id, extension_id(), path, storage_unit_id,
base::BindOnce(&ImageWriterPrivateWriteFromFileFunction::OnComplete, base::BindOnce(&ImageWriterPrivateWriteFromFileFunction::OnComplete,
this)); this));
return true; return RespondLater();
} }
ImageWriterPrivateCancelWriteFunction::ImageWriterPrivateCancelWriteFunction() { ImageWriterPrivateCancelWriteFunction::ImageWriterPrivateCancelWriteFunction() {
...@@ -114,13 +117,13 @@ ImageWriterPrivateCancelWriteFunction:: ...@@ -114,13 +117,13 @@ ImageWriterPrivateCancelWriteFunction::
~ImageWriterPrivateCancelWriteFunction() { ~ImageWriterPrivateCancelWriteFunction() {
} }
bool ImageWriterPrivateCancelWriteFunction::RunAsync() { ExtensionFunction::ResponseAction ImageWriterPrivateCancelWriteFunction::Run() {
image_writer::OperationManager::Get(GetProfile()) image_writer::OperationManager::Get(browser_context())
->CancelWrite( ->CancelWrite(
extension_id(), extension_id(),
base::BindOnce(&ImageWriterPrivateCancelWriteFunction::OnComplete, base::BindOnce(&ImageWriterPrivateCancelWriteFunction::OnComplete,
this)); this));
return true; return RespondLater();
} }
ImageWriterPrivateDestroyPartitionsFunction:: ImageWriterPrivateDestroyPartitionsFunction::
...@@ -131,12 +134,13 @@ ImageWriterPrivateDestroyPartitionsFunction:: ...@@ -131,12 +134,13 @@ ImageWriterPrivateDestroyPartitionsFunction::
~ImageWriterPrivateDestroyPartitionsFunction() { ~ImageWriterPrivateDestroyPartitionsFunction() {
} }
bool ImageWriterPrivateDestroyPartitionsFunction::RunAsync() { ExtensionFunction::ResponseAction
ImageWriterPrivateDestroyPartitionsFunction::Run() {
#if defined(OS_CHROMEOS) #if defined(OS_CHROMEOS)
if (GetProfile()->GetPrefs()->GetBoolean(prefs::kExternalStorageDisabled) || Profile* profile = Profile::FromBrowserContext(browser_context());
GetProfile()->GetPrefs()->GetBoolean(prefs::kExternalStorageReadOnly)) { if (profile->GetPrefs()->GetBoolean(prefs::kExternalStorageDisabled) ||
error_ = image_writer::error::kDeviceWriteError; profile->GetPrefs()->GetBoolean(prefs::kExternalStorageReadOnly)) {
return false; return RespondNow(Error(image_writer::error::kDeviceWriteError));
} }
#endif #endif
...@@ -144,12 +148,12 @@ bool ImageWriterPrivateDestroyPartitionsFunction::RunAsync() { ...@@ -144,12 +148,12 @@ bool ImageWriterPrivateDestroyPartitionsFunction::RunAsync() {
image_writer_api::DestroyPartitions::Params::Create(*args_)); image_writer_api::DestroyPartitions::Params::Create(*args_));
EXTENSION_FUNCTION_VALIDATE(params.get()); EXTENSION_FUNCTION_VALIDATE(params.get());
image_writer::OperationManager::Get(GetProfile()) image_writer::OperationManager::Get(browser_context())
->DestroyPartitions( ->DestroyPartitions(
extension_id(), params->storage_unit_id, extension_id(), params->storage_unit_id,
base::BindOnce( base::BindOnce(
&ImageWriterPrivateDestroyPartitionsFunction::OnComplete, this)); &ImageWriterPrivateDestroyPartitionsFunction::OnComplete, this));
return true; return RespondLater();
} }
ImageWriterPrivateListRemovableStorageDevicesFunction:: ImageWriterPrivateListRemovableStorageDevicesFunction::
...@@ -160,31 +164,31 @@ ImageWriterPrivateListRemovableStorageDevicesFunction:: ...@@ -160,31 +164,31 @@ ImageWriterPrivateListRemovableStorageDevicesFunction::
~ImageWriterPrivateListRemovableStorageDevicesFunction() { ~ImageWriterPrivateListRemovableStorageDevicesFunction() {
} }
bool ImageWriterPrivateListRemovableStorageDevicesFunction::RunAsync() { ExtensionFunction::ResponseAction
ImageWriterPrivateListRemovableStorageDevicesFunction::Run() {
#if defined(OS_CHROMEOS) #if defined(OS_CHROMEOS)
if (GetProfile()->GetPrefs()->GetBoolean(prefs::kExternalStorageDisabled)) { Profile* profile = Profile::FromBrowserContext(browser_context());
if (profile->GetPrefs()->GetBoolean(prefs::kExternalStorageDisabled)) {
// Return an empty device list. // Return an empty device list.
OnDeviceListReady(base::MakeRefCounted<StorageDeviceList>()); OnDeviceListReady(base::MakeRefCounted<StorageDeviceList>());
return true; return AlreadyResponded();
} }
#endif #endif
RemovableStorageProvider::GetAllDevices(base::BindOnce( RemovableStorageProvider::GetAllDevices(base::BindOnce(
&ImageWriterPrivateListRemovableStorageDevicesFunction::OnDeviceListReady, &ImageWriterPrivateListRemovableStorageDevicesFunction::OnDeviceListReady,
this)); this));
return true; return RespondLater();
} }
void ImageWriterPrivateListRemovableStorageDevicesFunction::OnDeviceListReady( void ImageWriterPrivateListRemovableStorageDevicesFunction::OnDeviceListReady(
scoped_refptr<StorageDeviceList> device_list) { scoped_refptr<StorageDeviceList> device_list) {
const bool success = device_list.get() != nullptr; const bool success = device_list.get() != nullptr;
if (success) { if (success) {
SetResultList( Respond(ArgumentList(
image_writer_api::ListRemovableStorageDevices::Results::Create( image_writer_api::ListRemovableStorageDevices::Results::Create(
device_list->data)); device_list->data)));
SendResponse(true);
} else { } else {
error_ = image_writer::error::kDeviceListError; Respond(Error(image_writer::error::kDeviceListError));
SendResponse(false);
} }
} }
......
...@@ -5,12 +5,12 @@ ...@@ -5,12 +5,12 @@
#define CHROME_BROWSER_EXTENSIONS_API_IMAGE_WRITER_PRIVATE_IMAGE_WRITER_PRIVATE_API_H_ #define CHROME_BROWSER_EXTENSIONS_API_IMAGE_WRITER_PRIVATE_IMAGE_WRITER_PRIVATE_API_H_
#include "chrome/browser/extensions/api/image_writer_private/removable_storage_provider.h" #include "chrome/browser/extensions/api/image_writer_private/removable_storage_provider.h"
#include "chrome/browser/extensions/chrome_extension_function.h"
#include "chrome/common/extensions/api/image_writer_private.h" #include "chrome/common/extensions/api/image_writer_private.h"
#include "extensions/browser/extension_function.h"
namespace extensions { namespace extensions {
class ImageWriterPrivateBaseFunction : public ChromeAsyncExtensionFunction { class ImageWriterPrivateBaseFunction : public ExtensionFunction {
public: public:
ImageWriterPrivateBaseFunction(); ImageWriterPrivateBaseFunction();
...@@ -32,7 +32,7 @@ class ImageWriterPrivateWriteFromUrlFunction ...@@ -32,7 +32,7 @@ class ImageWriterPrivateWriteFromUrlFunction
private: private:
~ImageWriterPrivateWriteFromUrlFunction() override; ~ImageWriterPrivateWriteFromUrlFunction() override;
bool RunAsync() override; ResponseAction Run() override;
}; };
class ImageWriterPrivateWriteFromFileFunction class ImageWriterPrivateWriteFromFileFunction
...@@ -44,7 +44,7 @@ class ImageWriterPrivateWriteFromFileFunction ...@@ -44,7 +44,7 @@ class ImageWriterPrivateWriteFromFileFunction
private: private:
~ImageWriterPrivateWriteFromFileFunction() override; ~ImageWriterPrivateWriteFromFileFunction() override;
bool RunAsync() override; ResponseAction Run() override;
}; };
class ImageWriterPrivateCancelWriteFunction class ImageWriterPrivateCancelWriteFunction
...@@ -56,7 +56,7 @@ class ImageWriterPrivateCancelWriteFunction ...@@ -56,7 +56,7 @@ class ImageWriterPrivateCancelWriteFunction
private: private:
~ImageWriterPrivateCancelWriteFunction() override; ~ImageWriterPrivateCancelWriteFunction() override;
bool RunAsync() override; ResponseAction Run() override;
}; };
class ImageWriterPrivateDestroyPartitionsFunction class ImageWriterPrivateDestroyPartitionsFunction
...@@ -68,11 +68,11 @@ class ImageWriterPrivateDestroyPartitionsFunction ...@@ -68,11 +68,11 @@ class ImageWriterPrivateDestroyPartitionsFunction
private: private:
~ImageWriterPrivateDestroyPartitionsFunction() override; ~ImageWriterPrivateDestroyPartitionsFunction() override;
bool RunAsync() override; ResponseAction Run() override;
}; };
class ImageWriterPrivateListRemovableStorageDevicesFunction class ImageWriterPrivateListRemovableStorageDevicesFunction
: public ChromeAsyncExtensionFunction { : public ExtensionFunction {
public: public:
DECLARE_EXTENSION_FUNCTION("imageWriterPrivate.listRemovableStorageDevices", DECLARE_EXTENSION_FUNCTION("imageWriterPrivate.listRemovableStorageDevices",
IMAGEWRITER_LISTREMOVABLESTORAGEDEVICES) IMAGEWRITER_LISTREMOVABLESTORAGEDEVICES)
...@@ -80,7 +80,7 @@ class ImageWriterPrivateListRemovableStorageDevicesFunction ...@@ -80,7 +80,7 @@ class ImageWriterPrivateListRemovableStorageDevicesFunction
private: private:
~ImageWriterPrivateListRemovableStorageDevicesFunction() override; ~ImageWriterPrivateListRemovableStorageDevicesFunction() override;
bool RunAsync() override; ResponseAction Run() override;
void OnDeviceListReady(scoped_refptr<StorageDeviceList> device_list); void OnDeviceListReady(scoped_refptr<StorageDeviceList> device_list);
}; };
......
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