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 "storage/browser/fileapi/sandbox_file_system_backend.h"
8 #include "base/files/file_util.h"
9 #include "base/logging.h"
10 #include "base/metrics/histogram.h"
11 #include "base/task_runner_util.h"
12 #include "storage/browser/fileapi/async_file_util_adapter.h"
13 #include "storage/browser/fileapi/copy_or_move_file_validator.h"
14 #include "storage/browser/fileapi/file_stream_reader.h"
15 #include "storage/browser/fileapi/file_stream_writer.h"
16 #include "storage/browser/fileapi/file_system_context.h"
17 #include "storage/browser/fileapi/file_system_operation.h"
18 #include "storage/browser/fileapi/file_system_operation_context.h"
19 #include "storage/browser/fileapi/file_system_options.h"
20 #include "storage/browser/fileapi/file_system_usage_cache.h"
21 #include "storage/browser/fileapi/obfuscated_file_util.h"
22 #include "storage/browser/fileapi/sandbox_quota_observer.h"
23 #include "storage/browser/quota/quota_manager.h"
24 #include "storage/common/fileapi/file_system_types.h"
25 #include "storage/common/fileapi/file_system_util.h"
28 using storage::QuotaManagerProxy
;
29 using storage::SpecialStoragePolicy
;
33 SandboxFileSystemBackend::SandboxFileSystemBackend(
34 SandboxFileSystemBackendDelegate
* delegate
)
35 : delegate_(delegate
),
36 enable_temporary_file_system_in_incognito_(false) {
39 SandboxFileSystemBackend::~SandboxFileSystemBackend() {
42 bool SandboxFileSystemBackend::CanHandleType(FileSystemType type
) const {
43 return type
== kFileSystemTypeTemporary
||
44 type
== kFileSystemTypePersistent
;
47 void SandboxFileSystemBackend::Initialize(FileSystemContext
* context
) {
50 // Set quota observers.
51 delegate_
->RegisterQuotaUpdateObserver(storage::kFileSystemTypeTemporary
);
52 delegate_
->AddFileAccessObserver(
53 storage::kFileSystemTypeTemporary
, delegate_
->quota_observer(), NULL
);
55 delegate_
->RegisterQuotaUpdateObserver(storage::kFileSystemTypePersistent
);
56 delegate_
->AddFileAccessObserver(
57 storage::kFileSystemTypePersistent
, delegate_
->quota_observer(), NULL
);
60 void SandboxFileSystemBackend::ResolveURL(
61 const FileSystemURL
& url
,
62 OpenFileSystemMode mode
,
63 const OpenFileSystemCallback
& callback
) {
64 DCHECK(CanHandleType(url
.type()));
66 if (delegate_
->file_system_options().is_incognito() &&
67 !(url
.type() == kFileSystemTypeTemporary
&&
68 enable_temporary_file_system_in_incognito_
)) {
69 // TODO(kinuko): return an isolated temporary directory.
70 callback
.Run(GURL(), std::string(), base::File::FILE_ERROR_SECURITY
);
74 delegate_
->OpenFileSystem(url
.origin(),
78 GetFileSystemRootURI(url
.origin(), url
.type()));
81 AsyncFileUtil
* SandboxFileSystemBackend::GetAsyncFileUtil(
82 FileSystemType type
) {
84 return delegate_
->file_util();
87 WatcherManager
* SandboxFileSystemBackend::GetWatcherManager(
88 FileSystemType type
) {
92 CopyOrMoveFileValidatorFactory
*
93 SandboxFileSystemBackend::GetCopyOrMoveFileValidatorFactory(
95 base::File::Error
* error_code
) {
97 *error_code
= base::File::FILE_OK
;
101 FileSystemOperation
* SandboxFileSystemBackend::CreateFileSystemOperation(
102 const FileSystemURL
& url
,
103 FileSystemContext
* context
,
104 base::File::Error
* error_code
) const {
105 DCHECK(CanHandleType(url
.type()));
109 scoped_ptr
<FileSystemOperationContext
> operation_context
=
110 delegate_
->CreateFileSystemOperationContext(url
, context
, error_code
);
111 if (!operation_context
)
114 SpecialStoragePolicy
* policy
= delegate_
->special_storage_policy();
115 if (policy
&& policy
->IsStorageUnlimited(url
.origin()))
116 operation_context
->set_quota_limit_type(storage::kQuotaLimitTypeUnlimited
);
118 operation_context
->set_quota_limit_type(storage::kQuotaLimitTypeLimited
);
120 return FileSystemOperation::Create(url
, context
, operation_context
.Pass());
123 bool SandboxFileSystemBackend::SupportsStreaming(
124 const storage::FileSystemURL
& url
) const {
128 bool SandboxFileSystemBackend::HasInplaceCopyImplementation(
129 storage::FileSystemType type
) const {
133 scoped_ptr
<storage::FileStreamReader
>
134 SandboxFileSystemBackend::CreateFileStreamReader(
135 const FileSystemURL
& url
,
137 int64 max_bytes_to_read
,
138 const base::Time
& expected_modification_time
,
139 FileSystemContext
* context
) const {
140 DCHECK(CanHandleType(url
.type()));
142 return delegate_
->CreateFileStreamReader(
143 url
, offset
, expected_modification_time
, context
);
146 scoped_ptr
<storage::FileStreamWriter
>
147 SandboxFileSystemBackend::CreateFileStreamWriter(
148 const FileSystemURL
& url
,
150 FileSystemContext
* context
) const {
151 DCHECK(CanHandleType(url
.type()));
153 return delegate_
->CreateFileStreamWriter(url
, offset
, context
, url
.type());
156 FileSystemQuotaUtil
* SandboxFileSystemBackend::GetQuotaUtil() {
160 const UpdateObserverList
* SandboxFileSystemBackend::GetUpdateObservers(
161 FileSystemType type
) const {
162 return delegate_
->GetUpdateObservers(type
);
165 const ChangeObserverList
* SandboxFileSystemBackend::GetChangeObservers(
166 FileSystemType type
) const {
167 return delegate_
->GetChangeObservers(type
);
170 const AccessObserverList
* SandboxFileSystemBackend::GetAccessObservers(
171 FileSystemType type
) const {
172 return delegate_
->GetAccessObservers(type
);
175 SandboxFileSystemBackendDelegate::OriginEnumerator
*
176 SandboxFileSystemBackend::CreateOriginEnumerator() {
178 return delegate_
->CreateOriginEnumerator();
181 } // namespace storage