1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291
|
// Copyright 2013 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "chrome/browser/chromeos/extensions/file_system_provider/file_system_provider_api.h"
#include <string>
#include <vector>
#include "base/debug/trace_event.h"
#include "base/memory/linked_ptr.h"
#include "base/memory/scoped_ptr.h"
#include "base/values.h"
#include "chrome/browser/chromeos/file_system_provider/provided_file_system_interface.h"
#include "chrome/browser/chromeos/file_system_provider/request_manager.h"
#include "chrome/browser/chromeos/file_system_provider/request_value.h"
#include "chrome/browser/chromeos/file_system_provider/service.h"
#include "chrome/common/extensions/api/file_system_provider.h"
#include "chrome/common/extensions/api/file_system_provider_internal.h"
#include "storage/browser/fileapi/watcher_manager.h"
using chromeos::file_system_provider::MountOptions;
using chromeos::file_system_provider::ProvidedFileSystemInfo;
using chromeos::file_system_provider::ProvidedFileSystemInterface;
using chromeos::file_system_provider::ProvidedFileSystemObserver;
using chromeos::file_system_provider::RequestValue;
using chromeos::file_system_provider::Service;
namespace extensions {
namespace {
typedef std::vector<linked_ptr<api::file_system_provider::Change>> IDLChanges;
// Converts the change type from the IDL type to a native type. |changed_type|
// must be specified (not CHANGE_TYPE_NONE).
storage::WatcherManager::ChangeType ParseChangeType(
const api::file_system_provider::ChangeType& change_type) {
switch (change_type) {
case api::file_system_provider::CHANGE_TYPE_CHANGED:
return storage::WatcherManager::CHANGED;
case api::file_system_provider::CHANGE_TYPE_DELETED:
return storage::WatcherManager::DELETED;
default:
break;
}
NOTREACHED();
return storage::WatcherManager::CHANGED;
}
// Convert the change from the IDL type to a native type. The reason IDL types
// are not used is since they are imperfect, eg. paths are stored as strings.
ProvidedFileSystemObserver::Change ParseChange(
const api::file_system_provider::Change& change) {
ProvidedFileSystemObserver::Change result;
result.entry_path = base::FilePath::FromUTF8Unsafe(change.entry_path);
result.change_type = ParseChangeType(change.change_type);
return result;
}
// Converts a list of child changes from the IDL type to a native type.
scoped_ptr<ProvidedFileSystemObserver::Changes> ParseChanges(
const IDLChanges& changes) {
scoped_ptr<ProvidedFileSystemObserver::Changes> results(
new ProvidedFileSystemObserver::Changes);
for (const auto& change : changes) {
results->push_back(ParseChange(*change));
}
return results;
}
} // namespace
bool FileSystemProviderMountFunction::RunSync() {
using api::file_system_provider::Mount::Params;
const scoped_ptr<Params> params(Params::Create(*args_));
EXTENSION_FUNCTION_VALIDATE(params);
// It's an error if the file system Id is empty.
if (params->options.file_system_id.empty()) {
SetError(FileErrorToString(base::File::FILE_ERROR_INVALID_OPERATION));
return false;
}
// It's an error if the display name is empty.
if (params->options.display_name.empty()) {
SetError(FileErrorToString(base::File::FILE_ERROR_INVALID_OPERATION));
return false;
}
// If the opened files limit is set, then it must be larger or equal than 0.
if (params->options.opened_files_limit.get() &&
*params->options.opened_files_limit.get() < 0) {
SetError(FileErrorToString(base::File::FILE_ERROR_INVALID_OPERATION));
return false;
}
Service* const service = Service::Get(GetProfile());
DCHECK(service);
MountOptions options;
options.file_system_id = params->options.file_system_id;
options.display_name = params->options.display_name;
options.writable = params->options.writable;
options.opened_files_limit = params->options.opened_files_limit.get()
? *params->options.opened_files_limit.get()
: 0;
options.supports_notify_tag = params->options.supports_notify_tag;
const base::File::Error result =
service->MountFileSystem(extension_id(), options);
if (result != base::File::FILE_OK) {
SetError(FileErrorToString(result));
return false;
}
return true;
}
bool FileSystemProviderUnmountFunction::RunSync() {
using api::file_system_provider::Unmount::Params;
scoped_ptr<Params> params(Params::Create(*args_));
EXTENSION_FUNCTION_VALIDATE(params);
Service* const service = Service::Get(GetProfile());
DCHECK(service);
const base::File::Error result =
service->UnmountFileSystem(extension_id(), params->options.file_system_id,
Service::UNMOUNT_REASON_USER);
if (result != base::File::FILE_OK) {
SetError(FileErrorToString(result));
return false;
}
return true;
}
bool FileSystemProviderGetAllFunction::RunSync() {
using api::file_system_provider::FileSystemInfo;
using api::file_system_provider::Watcher;
Service* const service = Service::Get(GetProfile());
DCHECK(service);
const std::vector<ProvidedFileSystemInfo> file_systems =
service->GetProvidedFileSystemInfoList();
std::vector<linked_ptr<FileSystemInfo>> items;
for (const auto& file_system_info : file_systems) {
if (file_system_info.extension_id() == extension_id()) {
const linked_ptr<FileSystemInfo> item(new FileSystemInfo);
item->file_system_id = file_system_info.file_system_id();
item->display_name = file_system_info.display_name();
item->writable = file_system_info.writable();
chromeos::file_system_provider::ProvidedFileSystemInterface* const
file_system =
service->GetProvidedFileSystem(file_system_info.extension_id(),
file_system_info.file_system_id());
DCHECK(file_system);
std::vector<linked_ptr<Watcher>> watcher_items;
chromeos::file_system_provider::Watchers* const watchers =
file_system->GetWatchers();
DCHECK(watchers);
for (const auto& watcher : *watchers) {
const linked_ptr<Watcher> watcher_item(new Watcher);
watcher_item->entry_path = watcher.second.entry_path.value();
watcher_item->recursive = watcher.second.recursive;
if (!watcher.second.last_tag.empty())
watcher_item->last_tag.reset(
new std::string(watcher.second.last_tag));
watcher_items.push_back(watcher_item);
}
item->watchers = watcher_items;
items.push_back(item);
}
}
SetResultList(api::file_system_provider::GetAll::Results::Create(items));
return true;
}
bool FileSystemProviderNotifyFunction::RunAsync() {
using api::file_system_provider::Notify::Params;
scoped_ptr<Params> params(Params::Create(*args_));
EXTENSION_FUNCTION_VALIDATE(params);
Service* const service = Service::Get(GetProfile());
DCHECK(service);
ProvidedFileSystemInterface* const file_system =
service->GetProvidedFileSystem(extension_id(),
params->options.file_system_id);
if (!file_system) {
SetError(FileErrorToString(base::File::FILE_ERROR_NOT_FOUND));
return false;
}
file_system->Notify(
base::FilePath::FromUTF8Unsafe(params->options.observed_path),
params->options.recursive, ParseChangeType(params->options.change_type),
params->options.changes.get()
? ParseChanges(*params->options.changes.get())
: make_scoped_ptr(new ProvidedFileSystemObserver::Changes),
params->options.tag.get() ? *params->options.tag.get() : "",
base::Bind(&FileSystemProviderNotifyFunction::OnNotifyCompleted, this));
return true;
}
void FileSystemProviderNotifyFunction::OnNotifyCompleted(
base::File::Error result) {
if (result != base::File::FILE_OK) {
SetError(FileErrorToString(result));
SendResponse(false);
return;
}
SendResponse(true);
}
bool FileSystemProviderInternalUnmountRequestedSuccessFunction::RunWhenValid() {
using api::file_system_provider_internal::UnmountRequestedSuccess::Params;
scoped_ptr<Params> params(Params::Create(*args_));
EXTENSION_FUNCTION_VALIDATE(params);
return FulfillRequest(RequestValue::CreateForUnmountSuccess(params.Pass()),
false /* has_more */);
}
bool
FileSystemProviderInternalGetMetadataRequestedSuccessFunction::RunWhenValid() {
using api::file_system_provider_internal::GetMetadataRequestedSuccess::Params;
scoped_ptr<Params> params(Params::Create(*args_));
EXTENSION_FUNCTION_VALIDATE(params);
return FulfillRequest(
RequestValue::CreateForGetMetadataSuccess(params.Pass()),
false /* has_more */);
}
bool FileSystemProviderInternalReadDirectoryRequestedSuccessFunction::
RunWhenValid() {
using api::file_system_provider_internal::ReadDirectoryRequestedSuccess::
Params;
scoped_ptr<Params> params(Params::Create(*args_));
EXTENSION_FUNCTION_VALIDATE(params);
const bool has_more = params->has_more;
return FulfillRequest(
RequestValue::CreateForReadDirectorySuccess(params.Pass()), has_more);
}
bool
FileSystemProviderInternalReadFileRequestedSuccessFunction::RunWhenValid() {
TRACE_EVENT0("file_system_provider", "ReadFileRequestedSuccess");
using api::file_system_provider_internal::ReadFileRequestedSuccess::Params;
scoped_ptr<Params> params(Params::Create(*args_));
EXTENSION_FUNCTION_VALIDATE(params);
const bool has_more = params->has_more;
return FulfillRequest(RequestValue::CreateForReadFileSuccess(params.Pass()),
has_more);
}
bool
FileSystemProviderInternalOperationRequestedSuccessFunction::RunWhenValid() {
using api::file_system_provider_internal::OperationRequestedSuccess::Params;
scoped_ptr<Params> params(Params::Create(*args_));
EXTENSION_FUNCTION_VALIDATE(params);
return FulfillRequest(
scoped_ptr<RequestValue>(
RequestValue::CreateForOperationSuccess(params.Pass())),
false /* has_more */);
}
bool FileSystemProviderInternalOperationRequestedErrorFunction::RunWhenValid() {
using api::file_system_provider_internal::OperationRequestedError::Params;
scoped_ptr<Params> params(Params::Create(*args_));
EXTENSION_FUNCTION_VALIDATE(params);
const base::File::Error error = ProviderErrorToFileError(params->error);
return RejectRequest(RequestValue::CreateForOperationError(params.Pass()),
error);
}
} // namespace extensions
|