Add ENABLE_MEDIA_ROUTER define to builds other than Android and iOS.
[chromium-blink-merge.git] / chrome / browser / extensions / api / image_writer_private / image_writer_private_api.cc
blob54453c537f022c993178a2b7fd674da99ac72560
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::RunAsync() {
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;
32 return false;
35 std::string hash;
36 if (params->options.get() && params->options->image_hash.get()) {
37 hash = *params->options->image_hash;
40 image_writer::OperationManager::Get(GetProfile())->StartWriteFromUrl(
41 extension_id(),
42 url,
43 hash,
44 params->storage_unit_id,
45 base::Bind(&ImageWriterPrivateWriteFromUrlFunction::OnWriteStarted,
46 this));
47 return true;
50 void ImageWriterPrivateWriteFromUrlFunction::OnWriteStarted(
51 bool success,
52 const std::string& error) {
53 if (!success) {
54 error_ = error;
57 SendResponse(success);
60 ImageWriterPrivateWriteFromFileFunction::
61 ImageWriterPrivateWriteFromFileFunction() {
64 ImageWriterPrivateWriteFromFileFunction::
65 ~ImageWriterPrivateWriteFromFileFunction() {
68 bool ImageWriterPrivateWriteFromFileFunction::RunAsync() {
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));
77 base::FilePath path;
79 if (!extensions::app_file_handler_util::ValidateFileEntryAndGetPath(
80 filesystem_name,
81 filesystem_path,
82 render_view_host(),
83 &path,
84 &error_))
85 return false;
87 image_writer::OperationManager::Get(GetProfile())->StartWriteFromFile(
88 extension_id(),
89 path,
90 storage_unit_id,
91 base::Bind(&ImageWriterPrivateWriteFromFileFunction::OnWriteStarted,
92 this));
93 return true;
96 void ImageWriterPrivateWriteFromFileFunction::OnWriteStarted(
97 bool success,
98 const std::string& error) {
99 if (!success) {
100 error_ = error;
102 SendResponse(success);
105 ImageWriterPrivateCancelWriteFunction::ImageWriterPrivateCancelWriteFunction() {
108 ImageWriterPrivateCancelWriteFunction::
109 ~ImageWriterPrivateCancelWriteFunction() {
112 bool ImageWriterPrivateCancelWriteFunction::RunAsync() {
113 image_writer::OperationManager::Get(GetProfile())->CancelWrite(
114 extension_id(),
115 base::Bind(&ImageWriterPrivateCancelWriteFunction::OnWriteCancelled,
116 this));
117 return true;
120 void ImageWriterPrivateCancelWriteFunction::OnWriteCancelled(
121 bool success,
122 const std::string& error) {
123 if (!success) {
124 error_ = error;
126 SendResponse(success);
129 ImageWriterPrivateDestroyPartitionsFunction::
130 ImageWriterPrivateDestroyPartitionsFunction() {
133 ImageWriterPrivateDestroyPartitionsFunction::
134 ~ImageWriterPrivateDestroyPartitionsFunction() {
137 bool ImageWriterPrivateDestroyPartitionsFunction::RunAsync() {
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(
143 extension_id(),
144 params->storage_unit_id,
145 base::Bind(
146 &ImageWriterPrivateDestroyPartitionsFunction::OnDestroyComplete,
147 this));
148 return true;
151 void ImageWriterPrivateDestroyPartitionsFunction::OnDestroyComplete(
152 bool success,
153 const std::string& error) {
154 if (!success) {
155 error_ = error;
158 SendResponse(success);
161 ImageWriterPrivateListRemovableStorageDevicesFunction::
162 ImageWriterPrivateListRemovableStorageDevicesFunction() {
165 ImageWriterPrivateListRemovableStorageDevicesFunction::
166 ~ImageWriterPrivateListRemovableStorageDevicesFunction() {
169 bool ImageWriterPrivateListRemovableStorageDevicesFunction::RunAsync() {
170 RemovableStorageProvider::GetAllDevices(
171 base::Bind(
172 &ImageWriterPrivateListRemovableStorageDevicesFunction::OnDeviceListReady,
173 this));
174 return true;
177 void ImageWriterPrivateListRemovableStorageDevicesFunction::OnDeviceListReady(
178 scoped_refptr<StorageDeviceList> device_list,
179 bool success) {
180 if (success) {
181 results_ =
182 image_writer_api::ListRemovableStorageDevices::Results::Create(
183 device_list.get()->data);
184 SendResponse(true);
185 } else {
186 error_ = image_writer::error::kDeviceListError;
187 SendResponse(false);
191 } // namespace extensions