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.
5 #include "base/logging.h"
6 #include "chrome/browser/extensions/api/file_handlers/app_file_handler_util.h"
7 #include "chrome/browser/extensions/api/image_writer_private/error_messages.h"
8 #include "chrome/browser/extensions/api/image_writer_private/image_writer_private_api.h"
9 #include "chrome/browser/extensions/api/image_writer_private/operation_manager.h"
10 #include "chrome/browser/profiles/profile.h"
11 #include "content/public/browser/render_frame_host.h"
12 #include "content/public/browser/render_process_host.h"
14 namespace image_writer_api
= extensions::api::image_writer_private
;
16 namespace extensions
{
18 ImageWriterPrivateWriteFromUrlFunction::
19 ImageWriterPrivateWriteFromUrlFunction() {
22 ImageWriterPrivateWriteFromUrlFunction::
23 ~ImageWriterPrivateWriteFromUrlFunction() {
26 bool ImageWriterPrivateWriteFromUrlFunction::RunAsync() {
27 scoped_ptr
<image_writer_api::WriteFromUrl::Params
> params(
28 image_writer_api::WriteFromUrl::Params::Create(*args_
));
29 EXTENSION_FUNCTION_VALIDATE(params
.get());
31 GURL
url(params
->image_url
);
32 if (!url
.is_valid()) {
33 error_
= image_writer::error::kUrlInvalid
;
38 if (params
->options
.get() && params
->options
->image_hash
.get()) {
39 hash
= *params
->options
->image_hash
;
42 image_writer::OperationManager::Get(GetProfile())->StartWriteFromUrl(
46 params
->storage_unit_id
,
47 base::Bind(&ImageWriterPrivateWriteFromUrlFunction::OnWriteStarted
,
52 void ImageWriterPrivateWriteFromUrlFunction::OnWriteStarted(
54 const std::string
& error
) {
59 SendResponse(success
);
62 ImageWriterPrivateWriteFromFileFunction::
63 ImageWriterPrivateWriteFromFileFunction() {
66 ImageWriterPrivateWriteFromFileFunction::
67 ~ImageWriterPrivateWriteFromFileFunction() {
70 bool ImageWriterPrivateWriteFromFileFunction::RunAsync() {
71 std::string filesystem_name
;
72 std::string filesystem_path
;
73 std::string storage_unit_id
;
75 EXTENSION_FUNCTION_VALIDATE(args_
->GetString(0, &storage_unit_id
));
76 EXTENSION_FUNCTION_VALIDATE(args_
->GetString(1, &filesystem_name
));
77 EXTENSION_FUNCTION_VALIDATE(args_
->GetString(2, &filesystem_path
));
81 if (!extensions::app_file_handler_util::ValidateFileEntryAndGetPath(
82 filesystem_name
, filesystem_path
,
83 render_frame_host()->GetProcess()->GetID(), &path
, &error_
))
86 image_writer::OperationManager::Get(GetProfile())->StartWriteFromFile(
90 base::Bind(&ImageWriterPrivateWriteFromFileFunction::OnWriteStarted
,
95 void ImageWriterPrivateWriteFromFileFunction::OnWriteStarted(
97 const std::string
& error
) {
101 SendResponse(success
);
104 ImageWriterPrivateCancelWriteFunction::ImageWriterPrivateCancelWriteFunction() {
107 ImageWriterPrivateCancelWriteFunction::
108 ~ImageWriterPrivateCancelWriteFunction() {
111 bool ImageWriterPrivateCancelWriteFunction::RunAsync() {
112 image_writer::OperationManager::Get(GetProfile())->CancelWrite(
114 base::Bind(&ImageWriterPrivateCancelWriteFunction::OnWriteCancelled
,
119 void ImageWriterPrivateCancelWriteFunction::OnWriteCancelled(
121 const std::string
& error
) {
125 SendResponse(success
);
128 ImageWriterPrivateDestroyPartitionsFunction::
129 ImageWriterPrivateDestroyPartitionsFunction() {
132 ImageWriterPrivateDestroyPartitionsFunction::
133 ~ImageWriterPrivateDestroyPartitionsFunction() {
136 bool ImageWriterPrivateDestroyPartitionsFunction::RunAsync() {
137 scoped_ptr
<image_writer_api::DestroyPartitions::Params
> params(
138 image_writer_api::DestroyPartitions::Params::Create(*args_
));
139 EXTENSION_FUNCTION_VALIDATE(params
.get());
141 image_writer::OperationManager::Get(GetProfile())->DestroyPartitions(
143 params
->storage_unit_id
,
145 &ImageWriterPrivateDestroyPartitionsFunction::OnDestroyComplete
,
150 void ImageWriterPrivateDestroyPartitionsFunction::OnDestroyComplete(
152 const std::string
& error
) {
157 SendResponse(success
);
160 ImageWriterPrivateListRemovableStorageDevicesFunction::
161 ImageWriterPrivateListRemovableStorageDevicesFunction() {
164 ImageWriterPrivateListRemovableStorageDevicesFunction::
165 ~ImageWriterPrivateListRemovableStorageDevicesFunction() {
168 bool ImageWriterPrivateListRemovableStorageDevicesFunction::RunAsync() {
169 RemovableStorageProvider::GetAllDevices(
171 &ImageWriterPrivateListRemovableStorageDevicesFunction::OnDeviceListReady
,
176 void ImageWriterPrivateListRemovableStorageDevicesFunction::OnDeviceListReady(
177 scoped_refptr
<StorageDeviceList
> device_list
,
181 image_writer_api::ListRemovableStorageDevices::Results::Create(
182 device_list
.get()->data
);
185 error_
= image_writer::error::kDeviceListError
;
190 } // namespace extensions