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 12namespace image_writer_api = extensions::api::image_writer_private; 13 14namespace extensions { 15 16ImageWriterPrivateWriteFromUrlFunction:: 17 ImageWriterPrivateWriteFromUrlFunction() { 18} 19 20ImageWriterPrivateWriteFromUrlFunction:: 21 ~ImageWriterPrivateWriteFromUrlFunction() { 22} 23 24bool 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()); 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 50void ImageWriterPrivateWriteFromUrlFunction::OnWriteStarted( 51 bool success, 52 const std::string& error) { 53 if (!success) { 54 error_ = error; 55 } 56 57 SendResponse(success); 58} 59 60ImageWriterPrivateWriteFromFileFunction:: 61 ImageWriterPrivateWriteFromFileFunction() { 62} 63 64ImageWriterPrivateWriteFromFileFunction:: 65 ~ImageWriterPrivateWriteFromFileFunction() { 66} 67 68bool ImageWriterPrivateWriteFromFileFunction::RunAsync() { 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 96void ImageWriterPrivateWriteFromFileFunction::OnWriteStarted( 97 bool success, 98 const std::string& error) { 99 if (!success) { 100 error_ = error; 101 } 102 SendResponse(success); 103} 104 105ImageWriterPrivateCancelWriteFunction::ImageWriterPrivateCancelWriteFunction() { 106} 107 108ImageWriterPrivateCancelWriteFunction:: 109 ~ImageWriterPrivateCancelWriteFunction() { 110} 111 112bool ImageWriterPrivateCancelWriteFunction::RunAsync() { 113 image_writer::OperationManager::Get(GetProfile())->CancelWrite( 114 extension_id(), 115 base::Bind(&ImageWriterPrivateCancelWriteFunction::OnWriteCancelled, 116 this)); 117 return true; 118} 119 120void ImageWriterPrivateCancelWriteFunction::OnWriteCancelled( 121 bool success, 122 const std::string& error) { 123 if (!success) { 124 error_ = error; 125 } 126 SendResponse(success); 127} 128 129ImageWriterPrivateDestroyPartitionsFunction:: 130 ImageWriterPrivateDestroyPartitionsFunction() { 131} 132 133ImageWriterPrivateDestroyPartitionsFunction:: 134 ~ImageWriterPrivateDestroyPartitionsFunction() { 135} 136 137bool 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()); 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 151void ImageWriterPrivateDestroyPartitionsFunction::OnDestroyComplete( 152 bool success, 153 const std::string& error) { 154 if (!success) { 155 error_ = error; 156 } 157 158 SendResponse(success); 159} 160 161ImageWriterPrivateListRemovableStorageDevicesFunction:: 162 ImageWriterPrivateListRemovableStorageDevicesFunction() { 163} 164 165ImageWriterPrivateListRemovableStorageDevicesFunction:: 166 ~ImageWriterPrivateListRemovableStorageDevicesFunction() { 167} 168 169bool ImageWriterPrivateListRemovableStorageDevicesFunction::RunAsync() { 170 RemovableStorageProvider::GetAllDevices( 171 base::Bind( 172 &ImageWriterPrivateListRemovableStorageDevicesFunction::OnDeviceListReady, 173 this)); 174 return true; 175} 176 177void 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 192