- add sources.
[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/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"
9
10 namespace image_writer_api = extensions::api::image_writer_private;
11
12 namespace extensions {
13
14 ImageWriterPrivateWriteFromUrlFunction::
15     ImageWriterPrivateWriteFromUrlFunction() {
16 }
17
18 ImageWriterPrivateWriteFromUrlFunction::
19     ~ImageWriterPrivateWriteFromUrlFunction() {
20 }
21
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());
26
27   GURL url(params->image_url);
28   if (!url.is_valid()) {
29     error_ = image_writer::error::kInvalidUrl;
30     return false;
31   }
32
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;
37 #else
38   bool save_image_as_download = false;
39   if (params->options.get() && params->options->save_as_download.get()) {
40     save_image_as_download = true;
41   }
42 #endif
43
44   std::string hash;
45   if (params->options.get() && params->options->image_hash.get()) {
46     hash = *params->options->image_hash;
47   }
48
49   image_writer::OperationManager::Get(GetProfile())->StartWriteFromUrl(
50       extension_id(),
51       url,
52       render_view_host(),
53       hash,
54       save_image_as_download,
55       params->storage_unit_id,
56       base::Bind(&ImageWriterPrivateWriteFromUrlFunction::OnWriteStarted,
57                  this));
58   return true;
59 }
60
61 void ImageWriterPrivateWriteFromUrlFunction::OnWriteStarted(
62     bool success,
63     const std::string& error) {
64   if (!success) {
65     error_ = error;
66   }
67
68   SendResponse(success);
69 }
70
71 ImageWriterPrivateWriteFromFileFunction::
72     ImageWriterPrivateWriteFromFileFunction() {
73 }
74
75 ImageWriterPrivateWriteFromFileFunction::
76     ~ImageWriterPrivateWriteFromFileFunction() {
77 }
78
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());
83
84   image_writer::OperationManager::Get(GetProfile())->StartWriteFromFile(
85       extension_id(),
86       params->storage_unit_id,
87       base::Bind(&ImageWriterPrivateWriteFromFileFunction::OnWriteStarted,
88                  this));
89   return true;
90 }
91
92 void ImageWriterPrivateWriteFromFileFunction::OnWriteStarted(
93     bool success,
94     const std::string& error) {
95   if (!success) {
96     error_ = error;
97   }
98   SendResponse(success);
99 }
100
101 ImageWriterPrivateCancelWriteFunction::ImageWriterPrivateCancelWriteFunction() {
102 }
103
104 ImageWriterPrivateCancelWriteFunction::
105     ~ImageWriterPrivateCancelWriteFunction() {
106 }
107
108 bool ImageWriterPrivateCancelWriteFunction::RunImpl() {
109   image_writer::OperationManager::Get(GetProfile())->CancelWrite(
110       extension_id(),
111       base::Bind(&ImageWriterPrivateCancelWriteFunction::OnWriteCancelled,
112                  this));
113   return true;
114 }
115
116 void ImageWriterPrivateCancelWriteFunction::OnWriteCancelled(
117     bool success,
118     const std::string& error) {
119   if (!success) {
120     error_ = error;
121   }
122   SendResponse(success);
123 }
124
125 ImageWriterPrivateDestroyPartitionsFunction::
126     ImageWriterPrivateDestroyPartitionsFunction() {
127 }
128
129 ImageWriterPrivateDestroyPartitionsFunction::
130     ~ImageWriterPrivateDestroyPartitionsFunction() {
131 }
132
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());
137
138   SendResponse(true);
139   return true;
140 }
141
142 ImageWriterPrivateListRemovableStorageDevicesFunction::
143   ImageWriterPrivateListRemovableStorageDevicesFunction() {
144 }
145
146 ImageWriterPrivateListRemovableStorageDevicesFunction::
147   ~ImageWriterPrivateListRemovableStorageDevicesFunction() {
148 }
149
150 bool ImageWriterPrivateListRemovableStorageDevicesFunction::RunImpl() {
151   RemovableStorageProvider::GetAllDevices(
152     base::Bind(
153       &ImageWriterPrivateListRemovableStorageDevicesFunction::OnDeviceListReady,
154       this));
155   return true;
156 }
157
158 void ImageWriterPrivateListRemovableStorageDevicesFunction::OnDeviceListReady(
159     scoped_refptr<StorageDeviceList> device_list,
160     bool success) {
161   if (success) {
162     results_ =
163       image_writer_api::ListRemovableStorageDevices::Results::Create(
164         device_list.get()->data);
165     SendResponse(true);
166   } else {
167     error_ = image_writer::error::kDeviceList;
168     SendResponse(false);
169   }
170 }
171
172 }  // namespace extensions