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() {}
void ImageWriterPrivateBaseFunction::OnComplete(bool success,
const std::string& error) {
if (!success)
error_ = error;
SendResponse(success);
if (success)
Respond(NoArguments());
else
Respond(Error(error));
}
ImageWriterPrivateWriteFromUrlFunction::
......@@ -37,12 +38,13 @@ ImageWriterPrivateWriteFromUrlFunction::
~ImageWriterPrivateWriteFromUrlFunction() {
}
bool ImageWriterPrivateWriteFromUrlFunction::RunAsync() {
ExtensionFunction::ResponseAction
ImageWriterPrivateWriteFromUrlFunction::Run() {
#if defined(OS_CHROMEOS)
if (GetProfile()->GetPrefs()->GetBoolean(prefs::kExternalStorageDisabled) ||
GetProfile()->GetPrefs()->GetBoolean(prefs::kExternalStorageReadOnly)) {
error_ = image_writer::error::kDeviceWriteError;
return false;
Profile* profile = Profile::FromBrowserContext(browser_context());
if (profile->GetPrefs()->GetBoolean(prefs::kExternalStorageDisabled) ||
profile->GetPrefs()->GetBoolean(prefs::kExternalStorageReadOnly)) {
return RespondNow(Error(image_writer::error::kDeviceWriteError));
}
#endif
std::unique_ptr<image_writer_api::WriteFromUrl::Params> params(
......@@ -50,22 +52,20 @@ bool ImageWriterPrivateWriteFromUrlFunction::RunAsync() {
EXTENSION_FUNCTION_VALIDATE(params.get());
GURL url(params->image_url);
if (!url.is_valid()) {
error_ = image_writer::error::kUrlInvalid;
return false;
}
if (!url.is_valid())
return RespondNow(Error(image_writer::error::kUrlInvalid));
std::string hash;
if (params->options.get() && params->options->image_hash.get()) {
hash = *params->options->image_hash;
}
image_writer::OperationManager::Get(GetProfile())
image_writer::OperationManager::Get(browser_context())
->StartWriteFromUrl(
extension_id(), url, hash, params->storage_unit_id,
base::BindOnce(&ImageWriterPrivateWriteFromUrlFunction::OnComplete,
this));
return true;
return RespondLater();
}
ImageWriterPrivateWriteFromFileFunction::
......@@ -76,12 +76,13 @@ ImageWriterPrivateWriteFromFileFunction::
~ImageWriterPrivateWriteFromFileFunction() {
}
bool ImageWriterPrivateWriteFromFileFunction::RunAsync() {
ExtensionFunction::ResponseAction
ImageWriterPrivateWriteFromFileFunction::Run() {
#if defined(OS_CHROMEOS)
if (GetProfile()->GetPrefs()->GetBoolean(prefs::kExternalStorageDisabled) ||
GetProfile()->GetPrefs()->GetBoolean(prefs::kExternalStorageReadOnly)) {
error_ = image_writer::error::kDeviceWriteError;
return false;
Profile* profile = Profile::FromBrowserContext(browser_context());
if (profile->GetPrefs()->GetBoolean(prefs::kExternalStorageDisabled) ||
profile->GetPrefs()->GetBoolean(prefs::kExternalStorageReadOnly)) {
return RespondNow(Error(image_writer::error::kDeviceWriteError));
}
#endif
std::string filesystem_name;
......@@ -94,17 +95,19 @@ bool ImageWriterPrivateWriteFromFileFunction::RunAsync() {
base::FilePath path;
std::string error;
if (!extensions::app_file_handler_util::ValidateFileEntryAndGetPath(
filesystem_name, filesystem_path, source_process_id(), &path,
&error_))
return false;
&error)) {
return RespondNow(Error(error));
}
image_writer::OperationManager::Get(GetProfile())
image_writer::OperationManager::Get(browser_context())
->StartWriteFromFile(
extension_id(), path, storage_unit_id,
base::BindOnce(&ImageWriterPrivateWriteFromFileFunction::OnComplete,
this));
return true;
return RespondLater();
}
ImageWriterPrivateCancelWriteFunction::ImageWriterPrivateCancelWriteFunction() {
......@@ -114,13 +117,13 @@ ImageWriterPrivateCancelWriteFunction::
~ImageWriterPrivateCancelWriteFunction() {
}
bool ImageWriterPrivateCancelWriteFunction::RunAsync() {
image_writer::OperationManager::Get(GetProfile())
ExtensionFunction::ResponseAction ImageWriterPrivateCancelWriteFunction::Run() {
image_writer::OperationManager::Get(browser_context())
->CancelWrite(
extension_id(),
base::BindOnce(&ImageWriterPrivateCancelWriteFunction::OnComplete,
this));
return true;
return RespondLater();
}
ImageWriterPrivateDestroyPartitionsFunction::
......@@ -131,12 +134,13 @@ ImageWriterPrivateDestroyPartitionsFunction::
~ImageWriterPrivateDestroyPartitionsFunction() {
}
bool ImageWriterPrivateDestroyPartitionsFunction::RunAsync() {
ExtensionFunction::ResponseAction
ImageWriterPrivateDestroyPartitionsFunction::Run() {
#if defined(OS_CHROMEOS)
if (GetProfile()->GetPrefs()->GetBoolean(prefs::kExternalStorageDisabled) ||
GetProfile()->GetPrefs()->GetBoolean(prefs::kExternalStorageReadOnly)) {
error_ = image_writer::error::kDeviceWriteError;
return false;
Profile* profile = Profile::FromBrowserContext(browser_context());
if (profile->GetPrefs()->GetBoolean(prefs::kExternalStorageDisabled) ||
profile->GetPrefs()->GetBoolean(prefs::kExternalStorageReadOnly)) {
return RespondNow(Error(image_writer::error::kDeviceWriteError));
}
#endif
......@@ -144,12 +148,12 @@ bool ImageWriterPrivateDestroyPartitionsFunction::RunAsync() {
image_writer_api::DestroyPartitions::Params::Create(*args_));
EXTENSION_FUNCTION_VALIDATE(params.get());
image_writer::OperationManager::Get(GetProfile())
image_writer::OperationManager::Get(browser_context())
->DestroyPartitions(
extension_id(), params->storage_unit_id,
base::BindOnce(
&ImageWriterPrivateDestroyPartitionsFunction::OnComplete, this));
return true;
return RespondLater();
}
ImageWriterPrivateListRemovableStorageDevicesFunction::
......@@ -160,31 +164,31 @@ ImageWriterPrivateListRemovableStorageDevicesFunction::
~ImageWriterPrivateListRemovableStorageDevicesFunction() {
}
bool ImageWriterPrivateListRemovableStorageDevicesFunction::RunAsync() {
ExtensionFunction::ResponseAction
ImageWriterPrivateListRemovableStorageDevicesFunction::Run() {
#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.
OnDeviceListReady(base::MakeRefCounted<StorageDeviceList>());
return true;
return AlreadyResponded();
}
#endif
RemovableStorageProvider::GetAllDevices(base::BindOnce(
&ImageWriterPrivateListRemovableStorageDevicesFunction::OnDeviceListReady,
this));
return true;
return RespondLater();
}
void ImageWriterPrivateListRemovableStorageDevicesFunction::OnDeviceListReady(
scoped_refptr<StorageDeviceList> device_list) {
const bool success = device_list.get() != nullptr;
if (success) {
SetResultList(
Respond(ArgumentList(
image_writer_api::ListRemovableStorageDevices::Results::Create(
device_list->data));
SendResponse(true);
device_list->data)));
} else {
error_ = image_writer::error::kDeviceListError;
SendResponse(false);
Respond(Error(image_writer::error::kDeviceListError));
}
}
......
......@@ -5,12 +5,12 @@
#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/chrome_extension_function.h"
#include "chrome/common/extensions/api/image_writer_private.h"
#include "extensions/browser/extension_function.h"
namespace extensions {
class ImageWriterPrivateBaseFunction : public ChromeAsyncExtensionFunction {
class ImageWriterPrivateBaseFunction : public ExtensionFunction {
public:
ImageWriterPrivateBaseFunction();
......@@ -32,7 +32,7 @@ class ImageWriterPrivateWriteFromUrlFunction
private:
~ImageWriterPrivateWriteFromUrlFunction() override;
bool RunAsync() override;
ResponseAction Run() override;
};
class ImageWriterPrivateWriteFromFileFunction
......@@ -44,7 +44,7 @@ class ImageWriterPrivateWriteFromFileFunction
private:
~ImageWriterPrivateWriteFromFileFunction() override;
bool RunAsync() override;
ResponseAction Run() override;
};
class ImageWriterPrivateCancelWriteFunction
......@@ -56,7 +56,7 @@ class ImageWriterPrivateCancelWriteFunction
private:
~ImageWriterPrivateCancelWriteFunction() override;
bool RunAsync() override;
ResponseAction Run() override;
};
class ImageWriterPrivateDestroyPartitionsFunction
......@@ -68,20 +68,20 @@ class ImageWriterPrivateDestroyPartitionsFunction
private:
~ImageWriterPrivateDestroyPartitionsFunction() override;
bool RunAsync() override;
ResponseAction Run() override;
};
class ImageWriterPrivateListRemovableStorageDevicesFunction
: public ChromeAsyncExtensionFunction {
public:
DECLARE_EXTENSION_FUNCTION("imageWriterPrivate.listRemovableStorageDevices",
IMAGEWRITER_LISTREMOVABLESTORAGEDEVICES)
ImageWriterPrivateListRemovableStorageDevicesFunction();
private:
~ImageWriterPrivateListRemovableStorageDevicesFunction() override;
bool RunAsync() override;
void OnDeviceListReady(scoped_refptr<StorageDeviceList> device_list);
: public ExtensionFunction {
public:
DECLARE_EXTENSION_FUNCTION("imageWriterPrivate.listRemovableStorageDevices",
IMAGEWRITER_LISTREMOVABLESTORAGEDEVICES)
ImageWriterPrivateListRemovableStorageDevicesFunction();
private:
~ImageWriterPrivateListRemovableStorageDevicesFunction() override;
ResponseAction Run() override;
void OnDeviceListReady(scoped_refptr<StorageDeviceList> device_list);
};
} // namespace extensions
......
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