e89d460dea9ad0370d8cbce8f91086395e27ac39
[platform/framework/web/crosswalk.git] / src / chrome / browser / extensions / api / image_writer_private / image_writer_private_api.cc
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.
4
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
12 namespace image_writer_api = extensions::api::image_writer_private;
13
14 namespace extensions {
15
16 ImageWriterPrivateWriteFromUrlFunction::
17     ImageWriterPrivateWriteFromUrlFunction() {
18 }
19
20 ImageWriterPrivateWriteFromUrlFunction::
21     ~ImageWriterPrivateWriteFromUrlFunction() {
22 }
23
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());
28
29   GURL url(params->image_url);
30   if (!url.is_valid()) {
31     error_ = image_writer::error::kUrlInvalid;
32     return false;
33   }
34
35   std::string hash;
36   if (params->options.get() && params->options->image_hash.get()) {
37     hash = *params->options->image_hash;
38   }
39
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;
48 }
49
50 void ImageWriterPrivateWriteFromUrlFunction::OnWriteStarted(
51     bool success,
52     const std::string& error) {
53   if (!success) {
54     error_ = error;
55   }
56
57   SendResponse(success);
58 }
59
60 ImageWriterPrivateWriteFromFileFunction::
61     ImageWriterPrivateWriteFromFileFunction() {
62 }
63
64 ImageWriterPrivateWriteFromFileFunction::
65     ~ImageWriterPrivateWriteFromFileFunction() {
66 }
67
68 bool ImageWriterPrivateWriteFromFileFunction::RunImpl() {
69   std::string filesystem_name;
70   std::string filesystem_path;
71   std::string storage_unit_id;
72
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));
76
77   base::FilePath path;
78
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;
86
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;
94 }
95
96 void ImageWriterPrivateWriteFromFileFunction::OnWriteStarted(
97     bool success,
98     const std::string& error) {
99   if (!success) {
100     error_ = error;
101   }
102   SendResponse(success);
103 }
104
105 ImageWriterPrivateCancelWriteFunction::ImageWriterPrivateCancelWriteFunction() {
106 }
107
108 ImageWriterPrivateCancelWriteFunction::
109     ~ImageWriterPrivateCancelWriteFunction() {
110 }
111
112 bool ImageWriterPrivateCancelWriteFunction::RunImpl() {
113   image_writer::OperationManager::Get(GetProfile())->CancelWrite(
114       extension_id(),
115       base::Bind(&ImageWriterPrivateCancelWriteFunction::OnWriteCancelled,
116                  this));
117   return true;
118 }
119
120 void ImageWriterPrivateCancelWriteFunction::OnWriteCancelled(
121     bool success,
122     const std::string& error) {
123   if (!success) {
124     error_ = error;
125   }
126   SendResponse(success);
127 }
128
129 ImageWriterPrivateDestroyPartitionsFunction::
130     ImageWriterPrivateDestroyPartitionsFunction() {
131 }
132
133 ImageWriterPrivateDestroyPartitionsFunction::
134     ~ImageWriterPrivateDestroyPartitionsFunction() {
135 }
136
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());
141
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;
149 }
150
151 void ImageWriterPrivateDestroyPartitionsFunction::OnDestroyComplete(
152     bool success,
153     const std::string& error) {
154   if (!success) {
155     error_ = error;
156   }
157
158   SendResponse(success);
159 }
160
161 ImageWriterPrivateListRemovableStorageDevicesFunction::
162   ImageWriterPrivateListRemovableStorageDevicesFunction() {
163 }
164
165 ImageWriterPrivateListRemovableStorageDevicesFunction::
166   ~ImageWriterPrivateListRemovableStorageDevicesFunction() {
167 }
168
169 bool ImageWriterPrivateListRemovableStorageDevicesFunction::RunImpl() {
170   RemovableStorageProvider::GetAllDevices(
171     base::Bind(
172       &ImageWriterPrivateListRemovableStorageDevicesFunction::OnDeviceListReady,
173       this));
174   return true;
175 }
176
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);
188   }
189 }
190
191 }  // namespace extensions