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/image_writer_private/error_messages.h"
7 #include "chrome/browser/extensions/api/image_writer_private/image_writer_private_api.h"
8 #include "chrome/browser/extensions/api/image_writer_private/operation_manager.h"
10 namespace image_writer_api = extensions::api::image_writer_private;
12 namespace extensions {
14 ImageWriterPrivateWriteFromUrlFunction::
15 ImageWriterPrivateWriteFromUrlFunction() {
18 ImageWriterPrivateWriteFromUrlFunction::
19 ~ImageWriterPrivateWriteFromUrlFunction() {
22 bool ImageWriterPrivateWriteFromUrlFunction::RunImpl() {
23 scoped_ptr<image_writer_api::WriteFromUrl::Params> params(
24 image_writer_api::WriteFromUrl::Params::Create(*args_));
25 EXTENSION_FUNCTION_VALIDATE(params.get());
27 GURL url(params->image_url);
28 if (!url.is_valid()) {
29 error_ = image_writer::error::kInvalidUrl;
33 #if defined(OS_CHROMEOS)
34 // The Chrome OS temporary partition is too small for Chrome OS images, thus
35 // we must always use the downloads folder.
36 bool save_image_as_download = true;
38 bool save_image_as_download = false;
39 if (params->options.get() && params->options->save_as_download.get()) {
40 save_image_as_download = true;
45 if (params->options.get() && params->options->image_hash.get()) {
46 hash = *params->options->image_hash;
49 image_writer::OperationManager::Get(GetProfile())->StartWriteFromUrl(
54 save_image_as_download,
55 params->storage_unit_id,
56 base::Bind(&ImageWriterPrivateWriteFromUrlFunction::OnWriteStarted,
61 void ImageWriterPrivateWriteFromUrlFunction::OnWriteStarted(
63 const std::string& error) {
68 SendResponse(success);
71 ImageWriterPrivateWriteFromFileFunction::
72 ImageWriterPrivateWriteFromFileFunction() {
75 ImageWriterPrivateWriteFromFileFunction::
76 ~ImageWriterPrivateWriteFromFileFunction() {
79 bool ImageWriterPrivateWriteFromFileFunction::RunImpl() {
80 scoped_ptr<image_writer_api::WriteFromFile::Params> params(
81 image_writer_api::WriteFromFile::Params::Create(*args_));
82 EXTENSION_FUNCTION_VALIDATE(params.get());
84 image_writer::OperationManager::Get(GetProfile())->StartWriteFromFile(
86 params->storage_unit_id,
87 base::Bind(&ImageWriterPrivateWriteFromFileFunction::OnWriteStarted,
92 void ImageWriterPrivateWriteFromFileFunction::OnWriteStarted(
94 const std::string& error) {
98 SendResponse(success);
101 ImageWriterPrivateCancelWriteFunction::ImageWriterPrivateCancelWriteFunction() {
104 ImageWriterPrivateCancelWriteFunction::
105 ~ImageWriterPrivateCancelWriteFunction() {
108 bool ImageWriterPrivateCancelWriteFunction::RunImpl() {
109 image_writer::OperationManager::Get(GetProfile())->CancelWrite(
111 base::Bind(&ImageWriterPrivateCancelWriteFunction::OnWriteCancelled,
116 void ImageWriterPrivateCancelWriteFunction::OnWriteCancelled(
118 const std::string& error) {
122 SendResponse(success);
125 ImageWriterPrivateDestroyPartitionsFunction::
126 ImageWriterPrivateDestroyPartitionsFunction() {
129 ImageWriterPrivateDestroyPartitionsFunction::
130 ~ImageWriterPrivateDestroyPartitionsFunction() {
133 bool ImageWriterPrivateDestroyPartitionsFunction::RunImpl() {
134 scoped_ptr<image_writer_api::DestroyPartitions::Params> params(
135 image_writer_api::DestroyPartitions::Params::Create(*args_));
136 EXTENSION_FUNCTION_VALIDATE(params.get());
142 ImageWriterPrivateListRemovableStorageDevicesFunction::
143 ImageWriterPrivateListRemovableStorageDevicesFunction() {
146 ImageWriterPrivateListRemovableStorageDevicesFunction::
147 ~ImageWriterPrivateListRemovableStorageDevicesFunction() {
150 bool ImageWriterPrivateListRemovableStorageDevicesFunction::RunImpl() {
151 RemovableStorageProvider::GetAllDevices(
153 &ImageWriterPrivateListRemovableStorageDevicesFunction::OnDeviceListReady,
158 void ImageWriterPrivateListRemovableStorageDevicesFunction::OnDeviceListReady(
159 scoped_refptr<StorageDeviceList> device_list,
163 image_writer_api::ListRemovableStorageDevices::Results::Create(
164 device_list.get()->data);
167 error_ = image_writer::error::kDeviceList;
172 } // namespace extensions