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 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440
|
// 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/fileapi/file_system_backend.h"
#include "base/logging.h"
#include "base/memory/scoped_ptr.h"
#include "base/strings/stringprintf.h"
#include "chrome/browser/chromeos/fileapi/file_access_permissions.h"
#include "chrome/browser/chromeos/fileapi/file_system_backend_delegate.h"
#include "chrome/browser/media_galleries/fileapi/media_file_system_backend.h"
#include "chrome/common/url_constants.h"
#include "chromeos/dbus/cros_disks_client.h"
#include "storage/browser/blob/file_stream_reader.h"
#include "storage/browser/fileapi/async_file_util.h"
#include "storage/browser/fileapi/external_mount_points.h"
#include "storage/browser/fileapi/file_stream_writer.h"
#include "storage/browser/fileapi/file_system_context.h"
#include "storage/browser/fileapi/file_system_operation.h"
#include "storage/browser/fileapi/file_system_operation_context.h"
#include "storage/browser/fileapi/file_system_url.h"
#include "storage/common/fileapi/file_system_mount_option.h"
namespace chromeos {
// static
bool FileSystemBackend::CanHandleURL(const storage::FileSystemURL& url) {
if (!url.is_valid())
return false;
return url.type() == storage::kFileSystemTypeNativeLocal ||
url.type() == storage::kFileSystemTypeRestrictedNativeLocal ||
url.type() == storage::kFileSystemTypeDrive ||
url.type() == storage::kFileSystemTypeProvided ||
url.type() == storage::kFileSystemTypeDeviceMediaAsFileStorage;
}
FileSystemBackend::FileSystemBackend(
FileSystemBackendDelegate* drive_delegate,
FileSystemBackendDelegate* file_system_provider_delegate,
FileSystemBackendDelegate* mtp_delegate,
scoped_refptr<storage::SpecialStoragePolicy> special_storage_policy,
scoped_refptr<storage::ExternalMountPoints> mount_points,
storage::ExternalMountPoints* system_mount_points)
: special_storage_policy_(special_storage_policy),
file_access_permissions_(new FileAccessPermissions()),
local_file_util_(storage::AsyncFileUtil::CreateForLocalFileSystem()),
drive_delegate_(drive_delegate),
file_system_provider_delegate_(file_system_provider_delegate),
mtp_delegate_(mtp_delegate),
mount_points_(mount_points),
system_mount_points_(system_mount_points) {
}
FileSystemBackend::~FileSystemBackend() {
}
void FileSystemBackend::AddSystemMountPoints() {
// RegisterFileSystem() is no-op if the mount point with the same name
// already exists, hence it's safe to call without checking if a mount
// point already exists or not.
system_mount_points_->RegisterFileSystem(
"archive",
storage::kFileSystemTypeNativeLocal,
storage::FileSystemMountOption(),
chromeos::CrosDisksClient::GetArchiveMountPoint());
system_mount_points_->RegisterFileSystem(
"removable", storage::kFileSystemTypeNativeLocal,
storage::FileSystemMountOption(storage::FlushPolicy::FLUSH_ON_COMPLETION),
chromeos::CrosDisksClient::GetRemovableDiskMountPoint());
system_mount_points_->RegisterFileSystem(
"oem",
storage::kFileSystemTypeRestrictedNativeLocal,
storage::FileSystemMountOption(),
base::FilePath(FILE_PATH_LITERAL("/usr/share/oem")));
}
bool FileSystemBackend::CanHandleType(storage::FileSystemType type) const {
switch (type) {
case storage::kFileSystemTypeExternal:
case storage::kFileSystemTypeDrive:
case storage::kFileSystemTypeRestrictedNativeLocal:
case storage::kFileSystemTypeNativeLocal:
case storage::kFileSystemTypeNativeForPlatformApp:
case storage::kFileSystemTypeDeviceMediaAsFileStorage:
case storage::kFileSystemTypeProvided:
return true;
default:
return false;
}
}
void FileSystemBackend::Initialize(storage::FileSystemContext* context) {
}
void FileSystemBackend::ResolveURL(const storage::FileSystemURL& url,
storage::OpenFileSystemMode mode,
const OpenFileSystemCallback& callback) {
std::string id;
storage::FileSystemType type;
std::string cracked_id;
base::FilePath path;
storage::FileSystemMountOption option;
if (!mount_points_->CrackVirtualPath(
url.virtual_path(), &id, &type, &cracked_id, &path, &option) &&
!system_mount_points_->CrackVirtualPath(
url.virtual_path(), &id, &type, &cracked_id, &path, &option)) {
// Not under a mount point, so return an error, since the root is not
// accessible.
GURL root_url = GURL(storage::GetExternalFileSystemRootURIString(
url.origin(), std::string()));
callback.Run(root_url, std::string(), base::File::FILE_ERROR_SECURITY);
return;
}
std::string name;
// Construct a URL restricted to the found mount point.
std::string root_url =
storage::GetExternalFileSystemRootURIString(url.origin(), id);
// For removable and archives, the file system root is the external mount
// point plus the inner mount point.
if (id == "archive" || id == "removable") {
std::vector<std::string> components;
url.virtual_path().GetComponents(&components);
DCHECK_EQ(id, components.at(0));
if (components.size() < 2) {
// Unable to access /archive and /removable directories directly. The
// inner mount name must be specified.
callback.Run(
GURL(root_url), std::string(), base::File::FILE_ERROR_SECURITY);
return;
}
std::string inner_mount_name = components[1];
root_url += inner_mount_name + "/";
name = inner_mount_name;
} else {
name = id;
}
callback.Run(GURL(root_url), name, base::File::FILE_OK);
}
storage::FileSystemQuotaUtil* FileSystemBackend::GetQuotaUtil() {
// No quota support.
return NULL;
}
const storage::UpdateObserverList* FileSystemBackend::GetUpdateObservers(
storage::FileSystemType type) const {
return NULL;
}
const storage::ChangeObserverList* FileSystemBackend::GetChangeObservers(
storage::FileSystemType type) const {
return NULL;
}
const storage::AccessObserverList* FileSystemBackend::GetAccessObservers(
storage::FileSystemType type) const {
return NULL;
}
bool FileSystemBackend::IsAccessAllowed(
const storage::FileSystemURL& url) const {
if (!url.is_valid())
return false;
// No extra check is needed for isolated file systems.
if (url.mount_type() == storage::kFileSystemTypeIsolated)
return true;
if (!CanHandleURL(url))
return false;
std::string extension_id = url.origin().host();
// TODO(mtomasz): Temporarily whitelist TimeScapes. Remove this in M-31.
// See: crbug.com/271946
if (extension_id == "mlbmkoenclnokonejhlfakkeabdlmpek" &&
url.type() == storage::kFileSystemTypeRestrictedNativeLocal) {
return true;
}
// Check first to make sure this extension has fileBrowserHander permissions.
if (!special_storage_policy_.get() ||
!special_storage_policy_->IsFileHandler(extension_id))
return false;
return file_access_permissions_->HasAccessPermission(extension_id,
url.virtual_path());
}
void FileSystemBackend::GrantFullAccessToExtension(
const std::string& extension_id) {
if (!special_storage_policy_.get())
return;
if (!special_storage_policy_->IsFileHandler(extension_id)) {
NOTREACHED();
return;
}
file_access_permissions_->GrantFullAccessPermission(extension_id);
}
void FileSystemBackend::GrantFileAccessToExtension(
const std::string& extension_id, const base::FilePath& virtual_path) {
if (!special_storage_policy_.get())
return;
// All we care about here is access from extensions for now.
if (!special_storage_policy_->IsFileHandler(extension_id)) {
NOTREACHED();
return;
}
std::string id;
storage::FileSystemType type;
std::string cracked_id;
base::FilePath path;
storage::FileSystemMountOption option;
if (!mount_points_->CrackVirtualPath(virtual_path, &id, &type, &cracked_id,
&path, &option) &&
!system_mount_points_->CrackVirtualPath(virtual_path, &id, &type,
&cracked_id, &path, &option)) {
return;
}
if (type == storage::kFileSystemTypeRestrictedNativeLocal) {
LOG(ERROR) << "Can't grant access for restricted mount point";
return;
}
file_access_permissions_->GrantAccessPermission(extension_id, virtual_path);
}
void FileSystemBackend::RevokeAccessForExtension(
const std::string& extension_id) {
file_access_permissions_->RevokePermissions(extension_id);
}
std::vector<base::FilePath> FileSystemBackend::GetRootDirectories() const {
std::vector<storage::MountPoints::MountPointInfo> mount_points;
mount_points_->AddMountPointInfosTo(&mount_points);
system_mount_points_->AddMountPointInfosTo(&mount_points);
std::vector<base::FilePath> root_dirs;
for (size_t i = 0; i < mount_points.size(); ++i)
root_dirs.push_back(mount_points[i].path);
return root_dirs;
}
storage::AsyncFileUtil* FileSystemBackend::GetAsyncFileUtil(
storage::FileSystemType type) {
switch (type) {
case storage::kFileSystemTypeDrive:
return drive_delegate_->GetAsyncFileUtil(type);
case storage::kFileSystemTypeProvided:
return file_system_provider_delegate_->GetAsyncFileUtil(type);
case storage::kFileSystemTypeNativeLocal:
case storage::kFileSystemTypeRestrictedNativeLocal:
return local_file_util_.get();
case storage::kFileSystemTypeDeviceMediaAsFileStorage:
return mtp_delegate_->GetAsyncFileUtil(type);
default:
NOTREACHED();
}
return NULL;
}
storage::WatcherManager* FileSystemBackend::GetWatcherManager(
storage::FileSystemType type) {
if (type == storage::kFileSystemTypeProvided)
return file_system_provider_delegate_->GetWatcherManager(type);
// TODO(mtomasz): Add support for other backends.
return NULL;
}
storage::CopyOrMoveFileValidatorFactory*
FileSystemBackend::GetCopyOrMoveFileValidatorFactory(
storage::FileSystemType type,
base::File::Error* error_code) {
DCHECK(error_code);
*error_code = base::File::FILE_OK;
return NULL;
}
storage::FileSystemOperation* FileSystemBackend::CreateFileSystemOperation(
const storage::FileSystemURL& url,
storage::FileSystemContext* context,
base::File::Error* error_code) const {
DCHECK(url.is_valid());
if (!IsAccessAllowed(url)) {
*error_code = base::File::FILE_ERROR_SECURITY;
return NULL;
}
if (url.type() == storage::kFileSystemTypeDeviceMediaAsFileStorage) {
// MTP file operations run on MediaTaskRunner.
return storage::FileSystemOperation::Create(
url,
context,
make_scoped_ptr(new storage::FileSystemOperationContext(
context, MediaFileSystemBackend::MediaTaskRunner().get())));
}
DCHECK(url.type() == storage::kFileSystemTypeNativeLocal ||
url.type() == storage::kFileSystemTypeRestrictedNativeLocal ||
url.type() == storage::kFileSystemTypeDrive ||
url.type() == storage::kFileSystemTypeProvided);
return storage::FileSystemOperation::Create(
url,
context,
make_scoped_ptr(new storage::FileSystemOperationContext(context)));
}
bool FileSystemBackend::SupportsStreaming(
const storage::FileSystemURL& url) const {
return url.type() == storage::kFileSystemTypeDrive ||
url.type() == storage::kFileSystemTypeProvided ||
url.type() == storage::kFileSystemTypeDeviceMediaAsFileStorage;
}
bool FileSystemBackend::HasInplaceCopyImplementation(
storage::FileSystemType type) const {
switch (type) {
case storage::kFileSystemTypeDrive:
case storage::kFileSystemTypeProvided:
case storage::kFileSystemTypeDeviceMediaAsFileStorage:
return true;
case storage::kFileSystemTypeNativeLocal:
case storage::kFileSystemTypeRestrictedNativeLocal:
return false;
default:
NOTREACHED();
}
return true;
}
scoped_ptr<storage::FileStreamReader> FileSystemBackend::CreateFileStreamReader(
const storage::FileSystemURL& url,
int64 offset,
int64 max_bytes_to_read,
const base::Time& expected_modification_time,
storage::FileSystemContext* context) const {
DCHECK(url.is_valid());
if (!IsAccessAllowed(url))
return scoped_ptr<storage::FileStreamReader>();
switch (url.type()) {
case storage::kFileSystemTypeDrive:
return drive_delegate_->CreateFileStreamReader(
url, offset, max_bytes_to_read, expected_modification_time, context);
case storage::kFileSystemTypeProvided:
return file_system_provider_delegate_->CreateFileStreamReader(
url, offset, max_bytes_to_read, expected_modification_time, context);
case storage::kFileSystemTypeNativeLocal:
case storage::kFileSystemTypeRestrictedNativeLocal:
return scoped_ptr<storage::FileStreamReader>(
storage::FileStreamReader::CreateForFileSystemFile(
context, url, offset, expected_modification_time));
case storage::kFileSystemTypeDeviceMediaAsFileStorage:
return mtp_delegate_->CreateFileStreamReader(
url, offset, max_bytes_to_read, expected_modification_time, context);
default:
NOTREACHED();
}
return scoped_ptr<storage::FileStreamReader>();
}
scoped_ptr<storage::FileStreamWriter> FileSystemBackend::CreateFileStreamWriter(
const storage::FileSystemURL& url,
int64 offset,
storage::FileSystemContext* context) const {
DCHECK(url.is_valid());
if (!IsAccessAllowed(url))
return scoped_ptr<storage::FileStreamWriter>();
switch (url.type()) {
case storage::kFileSystemTypeDrive:
return drive_delegate_->CreateFileStreamWriter(url, offset, context);
case storage::kFileSystemTypeProvided:
return file_system_provider_delegate_->CreateFileStreamWriter(
url, offset, context);
case storage::kFileSystemTypeNativeLocal:
return scoped_ptr<storage::FileStreamWriter>(
storage::FileStreamWriter::CreateForLocalFile(
context->default_file_task_runner(),
url.path(),
offset,
storage::FileStreamWriter::OPEN_EXISTING_FILE));
case storage::kFileSystemTypeRestrictedNativeLocal:
// Restricted native local file system is read only.
return scoped_ptr<storage::FileStreamWriter>();
case storage::kFileSystemTypeDeviceMediaAsFileStorage:
return mtp_delegate_->CreateFileStreamWriter(url, offset, context);
default:
NOTREACHED();
}
return scoped_ptr<storage::FileStreamWriter>();
}
bool FileSystemBackend::GetVirtualPath(
const base::FilePath& filesystem_path,
base::FilePath* virtual_path) {
return mount_points_->GetVirtualPath(filesystem_path, virtual_path) ||
system_mount_points_->GetVirtualPath(filesystem_path, virtual_path);
}
void FileSystemBackend::GetRedirectURLForContents(
const storage::FileSystemURL& url,
const storage::URLCallback& callback) {
DCHECK(url.is_valid());
if (!IsAccessAllowed(url))
return callback.Run(GURL());
switch (url.type()) {
case storage::kFileSystemTypeDrive:
drive_delegate_->GetRedirectURLForContents(url, callback);
return;
case storage::kFileSystemTypeProvided:
file_system_provider_delegate_->GetRedirectURLForContents(url,
callback);
return;
case storage::kFileSystemTypeDeviceMediaAsFileStorage:
mtp_delegate_->GetRedirectURLForContents(url, callback);
return;
case storage::kFileSystemTypeNativeLocal:
case storage::kFileSystemTypeRestrictedNativeLocal:
callback.Run(GURL());
return;
default:
NOTREACHED();
}
callback.Run(GURL());
}
} // namespace chromeos
|