When enabling new profile management programmatically, make sure to set the
[chromium-blink-merge.git] / webkit / browser / fileapi / sandbox_file_system_backend.cc
blobcb288aaab910e40eefbe15c1ac4419663afa14a0
1 // Copyright (c) 2012 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.
5 #include "webkit/browser/fileapi/sandbox_file_system_backend.h"
7 #include "base/bind.h"
8 #include "base/file_util.h"
9 #include "base/logging.h"
10 #include "base/metrics/histogram.h"
11 #include "base/task_runner_util.h"
12 #include "url/gurl.h"
13 #include "webkit/browser/blob/file_stream_reader.h"
14 #include "webkit/browser/fileapi/async_file_util_adapter.h"
15 #include "webkit/browser/fileapi/copy_or_move_file_validator.h"
16 #include "webkit/browser/fileapi/file_stream_writer.h"
17 #include "webkit/browser/fileapi/file_system_context.h"
18 #include "webkit/browser/fileapi/file_system_operation.h"
19 #include "webkit/browser/fileapi/file_system_operation_context.h"
20 #include "webkit/browser/fileapi/file_system_options.h"
21 #include "webkit/browser/fileapi/file_system_usage_cache.h"
22 #include "webkit/browser/fileapi/obfuscated_file_util.h"
23 #include "webkit/browser/fileapi/sandbox_file_system_backend_delegate.h"
24 #include "webkit/browser/fileapi/sandbox_quota_observer.h"
25 #include "webkit/browser/quota/quota_manager.h"
26 #include "webkit/common/fileapi/file_system_types.h"
27 #include "webkit/common/fileapi/file_system_util.h"
29 using quota::QuotaManagerProxy;
30 using quota::SpecialStoragePolicy;
32 namespace fileapi {
34 SandboxFileSystemBackend::SandboxFileSystemBackend(
35 SandboxFileSystemBackendDelegate* delegate)
36 : delegate_(delegate),
37 enable_temporary_file_system_in_incognito_(false) {
40 SandboxFileSystemBackend::~SandboxFileSystemBackend() {
43 bool SandboxFileSystemBackend::CanHandleType(FileSystemType type) const {
44 return type == kFileSystemTypeTemporary ||
45 type == kFileSystemTypePersistent;
48 void SandboxFileSystemBackend::Initialize(FileSystemContext* context) {
49 DCHECK(delegate_);
51 // Set quota observers.
52 delegate_->RegisterQuotaUpdateObserver(fileapi::kFileSystemTypeTemporary);
53 delegate_->AddFileAccessObserver(
54 fileapi::kFileSystemTypeTemporary,
55 delegate_->quota_observer(), NULL);
57 delegate_->RegisterQuotaUpdateObserver(fileapi::kFileSystemTypePersistent);
58 delegate_->AddFileAccessObserver(
59 fileapi::kFileSystemTypePersistent,
60 delegate_->quota_observer(), NULL);
63 void SandboxFileSystemBackend::ResolveURL(
64 const FileSystemURL& url,
65 OpenFileSystemMode mode,
66 const OpenFileSystemCallback& callback) {
67 DCHECK(CanHandleType(url.type()));
68 DCHECK(delegate_);
69 if (delegate_->file_system_options().is_incognito() &&
70 !(url.type() == kFileSystemTypeTemporary &&
71 enable_temporary_file_system_in_incognito_)) {
72 // TODO(kinuko): return an isolated temporary directory.
73 callback.Run(GURL(), std::string(), base::File::FILE_ERROR_SECURITY);
74 return;
77 delegate_->OpenFileSystem(url.origin(),
78 url.type(),
79 mode,
80 callback,
81 GetFileSystemRootURI(url.origin(), url.type()));
84 AsyncFileUtil* SandboxFileSystemBackend::GetAsyncFileUtil(
85 FileSystemType type) {
86 DCHECK(delegate_);
87 return delegate_->file_util();
90 CopyOrMoveFileValidatorFactory*
91 SandboxFileSystemBackend::GetCopyOrMoveFileValidatorFactory(
92 FileSystemType type,
93 base::File::Error* error_code) {
94 DCHECK(error_code);
95 *error_code = base::File::FILE_OK;
96 return NULL;
99 FileSystemOperation* SandboxFileSystemBackend::CreateFileSystemOperation(
100 const FileSystemURL& url,
101 FileSystemContext* context,
102 base::File::Error* error_code) const {
103 DCHECK(CanHandleType(url.type()));
104 DCHECK(error_code);
106 DCHECK(delegate_);
107 scoped_ptr<FileSystemOperationContext> operation_context =
108 delegate_->CreateFileSystemOperationContext(url, context, error_code);
109 if (!operation_context)
110 return NULL;
112 SpecialStoragePolicy* policy = delegate_->special_storage_policy();
113 if (policy && policy->IsStorageUnlimited(url.origin()))
114 operation_context->set_quota_limit_type(quota::kQuotaLimitTypeUnlimited);
115 else
116 operation_context->set_quota_limit_type(quota::kQuotaLimitTypeLimited);
118 return FileSystemOperation::Create(url, context, operation_context.Pass());
121 bool SandboxFileSystemBackend::SupportsStreaming(
122 const fileapi::FileSystemURL& url) const {
123 return false;
126 scoped_ptr<webkit_blob::FileStreamReader>
127 SandboxFileSystemBackend::CreateFileStreamReader(
128 const FileSystemURL& url,
129 int64 offset,
130 const base::Time& expected_modification_time,
131 FileSystemContext* context) const {
132 DCHECK(CanHandleType(url.type()));
133 DCHECK(delegate_);
134 return delegate_->CreateFileStreamReader(
135 url, offset, expected_modification_time, context);
138 scoped_ptr<fileapi::FileStreamWriter>
139 SandboxFileSystemBackend::CreateFileStreamWriter(
140 const FileSystemURL& url,
141 int64 offset,
142 FileSystemContext* context) const {
143 DCHECK(CanHandleType(url.type()));
144 DCHECK(delegate_);
145 return delegate_->CreateFileStreamWriter(url, offset, context, url.type());
148 FileSystemQuotaUtil* SandboxFileSystemBackend::GetQuotaUtil() {
149 return delegate_;
152 SandboxFileSystemBackendDelegate::OriginEnumerator*
153 SandboxFileSystemBackend::CreateOriginEnumerator() {
154 DCHECK(delegate_);
155 return delegate_->CreateOriginEnumerator();
158 } // namespace fileapi