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"
12 namespace image_writer_api = extensions::api::image_writer_private;
14 namespace extensions {
16 ImageWriterPrivateWriteFromUrlFunction::
17 ImageWriterPrivateWriteFromUrlFunction() {
20 ImageWriterPrivateWriteFromUrlFunction::
21 ~ImageWriterPrivateWriteFromUrlFunction() {
24 bool ImageWriterPrivateWriteFromUrlFunction::RunImpl() {
25 scoped_ptr<image_writer_api::WriteFromUrl::Params> params(
26 image_writer_api::WriteFromUrl::Params::Create(*args_));
27 EXTENSION_FUNCTION_VALIDATE(params.get());
29 GURL url(params->image_url);
30 if (!url.is_valid()) {
31 error_ = image_writer::error::kUrlInvalid;
36 if (params->options.get() && params->options->image_hash.get()) {
37 hash = *params->options->image_hash;
40 image_writer::OperationManager::Get(GetProfile())->StartWriteFromUrl(
44 params->storage_unit_id,
45 base::Bind(&ImageWriterPrivateWriteFromUrlFunction::OnWriteStarted,
50 void ImageWriterPrivateWriteFromUrlFunction::OnWriteStarted(
52 const std::string& error) {
57 SendResponse(success);
60 ImageWriterPrivateWriteFromFileFunction::
61 ImageWriterPrivateWriteFromFileFunction() {
64 ImageWriterPrivateWriteFromFileFunction::
65 ~ImageWriterPrivateWriteFromFileFunction() {
68 bool ImageWriterPrivateWriteFromFileFunction::RunImpl() {
69 std::string filesystem_name;
70 std::string filesystem_path;
71 std::string storage_unit_id;
73 EXTENSION_FUNCTION_VALIDATE(args_->GetString(0, &storage_unit_id));
74 EXTENSION_FUNCTION_VALIDATE(args_->GetString(1, &filesystem_name));
75 EXTENSION_FUNCTION_VALIDATE(args_->GetString(2, &filesystem_path));
79 if (!extensions::app_file_handler_util::ValidateFileEntryAndGetPath(
87 image_writer::OperationManager::Get(GetProfile())->StartWriteFromFile(
91 base::Bind(&ImageWriterPrivateWriteFromFileFunction::OnWriteStarted,
96 void ImageWriterPrivateWriteFromFileFunction::OnWriteStarted(
98 const std::string& error) {
102 SendResponse(success);
105 ImageWriterPrivateCancelWriteFunction::ImageWriterPrivateCancelWriteFunction() {
108 ImageWriterPrivateCancelWriteFunction::
109 ~ImageWriterPrivateCancelWriteFunction() {
112 bool ImageWriterPrivateCancelWriteFunction::RunImpl() {
113 image_writer::OperationManager::Get(GetProfile())->CancelWrite(
115 base::Bind(&ImageWriterPrivateCancelWriteFunction::OnWriteCancelled,
120 void ImageWriterPrivateCancelWriteFunction::OnWriteCancelled(
122 const std::string& error) {
126 SendResponse(success);
129 ImageWriterPrivateDestroyPartitionsFunction::
130 ImageWriterPrivateDestroyPartitionsFunction() {
133 ImageWriterPrivateDestroyPartitionsFunction::
134 ~ImageWriterPrivateDestroyPartitionsFunction() {
137 bool ImageWriterPrivateDestroyPartitionsFunction::RunImpl() {
138 scoped_ptr<image_writer_api::DestroyPartitions::Params> params(
139 image_writer_api::DestroyPartitions::Params::Create(*args_));
140 EXTENSION_FUNCTION_VALIDATE(params.get());
142 image_writer::OperationManager::Get(GetProfile())->DestroyPartitions(
144 params->storage_unit_id,
146 &ImageWriterPrivateDestroyPartitionsFunction::OnDestroyComplete,
151 void ImageWriterPrivateDestroyPartitionsFunction::OnDestroyComplete(
153 const std::string& error) {
158 SendResponse(success);
161 ImageWriterPrivateListRemovableStorageDevicesFunction::
162 ImageWriterPrivateListRemovableStorageDevicesFunction() {
165 ImageWriterPrivateListRemovableStorageDevicesFunction::
166 ~ImageWriterPrivateListRemovableStorageDevicesFunction() {
169 bool ImageWriterPrivateListRemovableStorageDevicesFunction::RunImpl() {
170 RemovableStorageProvider::GetAllDevices(
172 &ImageWriterPrivateListRemovableStorageDevicesFunction::OnDeviceListReady,
177 void ImageWriterPrivateListRemovableStorageDevicesFunction::OnDeviceListReady(
178 scoped_refptr<StorageDeviceList> device_list,
182 image_writer_api::ListRemovableStorageDevices::Results::Create(
183 device_list.get()->data);
186 error_ = image_writer::error::kDeviceListError;
191 } // namespace extensions