Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / chrome / browser / extensions / api / image_writer_private / image_writer_private_api.cc
blob5afa7be5810263d13d56105cf34520c308377d4a
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;
34 return false;
37 std::string hash;
38 if (params->options.get() && params->options->image_hash.get()) {
39 hash = *params->options->image_hash;
42 image_writer::OperationManager::Get(GetProfile())->StartWriteFromUrl(
43 extension_id(),
44 url,
45 hash,
46 params->storage_unit_id,
47 base::Bind(&ImageWriterPrivateWriteFromUrlFunction::OnWriteStarted,
48 this));
49 return true;
52 void ImageWriterPrivateWriteFromUrlFunction::OnWriteStarted(
53 bool success,
54 const std::string& error) {
55 if (!success) {
56 error_ = 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));
79 base::FilePath path;
81 if (!extensions::app_file_handler_util::ValidateFileEntryAndGetPath(
82 filesystem_name, filesystem_path,
83 render_frame_host()->GetProcess()->GetID(), &path, &error_))
84 return false;
86 image_writer::OperationManager::Get(GetProfile())->StartWriteFromFile(
87 extension_id(),
88 path,
89 storage_unit_id,
90 base::Bind(&ImageWriterPrivateWriteFromFileFunction::OnWriteStarted,
91 this));
92 return true;
95 void ImageWriterPrivateWriteFromFileFunction::OnWriteStarted(
96 bool success,
97 const std::string& error) {
98 if (!success) {
99 error_ = error;
101 SendResponse(success);
104 ImageWriterPrivateCancelWriteFunction::ImageWriterPrivateCancelWriteFunction() {
107 ImageWriterPrivateCancelWriteFunction::
108 ~ImageWriterPrivateCancelWriteFunction() {
111 bool ImageWriterPrivateCancelWriteFunction::RunAsync() {
112 image_writer::OperationManager::Get(GetProfile())->CancelWrite(
113 extension_id(),
114 base::Bind(&ImageWriterPrivateCancelWriteFunction::OnWriteCancelled,
115 this));
116 return true;
119 void ImageWriterPrivateCancelWriteFunction::OnWriteCancelled(
120 bool success,
121 const std::string& error) {
122 if (!success) {
123 error_ = 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(
142 extension_id(),
143 params->storage_unit_id,
144 base::Bind(
145 &ImageWriterPrivateDestroyPartitionsFunction::OnDestroyComplete,
146 this));
147 return true;
150 void ImageWriterPrivateDestroyPartitionsFunction::OnDestroyComplete(
151 bool success,
152 const std::string& error) {
153 if (!success) {
154 error_ = error;
157 SendResponse(success);
160 ImageWriterPrivateListRemovableStorageDevicesFunction::
161 ImageWriterPrivateListRemovableStorageDevicesFunction() {
164 ImageWriterPrivateListRemovableStorageDevicesFunction::
165 ~ImageWriterPrivateListRemovableStorageDevicesFunction() {
168 bool ImageWriterPrivateListRemovableStorageDevicesFunction::RunAsync() {
169 RemovableStorageProvider::GetAllDevices(
170 base::Bind(
171 &ImageWriterPrivateListRemovableStorageDevicesFunction::OnDeviceListReady,
172 this));
173 return true;
176 void ImageWriterPrivateListRemovableStorageDevicesFunction::OnDeviceListReady(
177 scoped_refptr<StorageDeviceList> device_list,
178 bool success) {
179 if (success) {
180 results_ =
181 image_writer_api::ListRemovableStorageDevices::Results::Create(
182 device_list.get()->data);
183 SendResponse(true);
184 } else {
185 error_ = image_writer::error::kDeviceListError;
186 SendResponse(false);
190 } // namespace extensions