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_file_system_backend_delegate.h"
23 #include "storage/browser/fileapi/sandbox_quota_observer.h"
24 #include "storage/browser/quota/quota_manager.h"
25 #include "storage/common/fileapi/file_system_types.h"
26 #include "storage/common/fileapi/file_system_util.h"
29 using storage::QuotaManagerProxy
;
30 using storage::SpecialStoragePolicy
;
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
) {
51 // Set quota observers.
52 delegate_
->RegisterQuotaUpdateObserver(storage::kFileSystemTypeTemporary
);
53 delegate_
->AddFileAccessObserver(
54 storage::kFileSystemTypeTemporary
, delegate_
->quota_observer(), NULL
);
56 delegate_
->RegisterQuotaUpdateObserver(storage::kFileSystemTypePersistent
);
57 delegate_
->AddFileAccessObserver(
58 storage::kFileSystemTypePersistent
, delegate_
->quota_observer(), NULL
);
61 void SandboxFileSystemBackend::ResolveURL(
62 const FileSystemURL
& url
,
63 OpenFileSystemMode mode
,
64 const OpenFileSystemCallback
& callback
) {
65 DCHECK(CanHandleType(url
.type()));
67 if (delegate_
->file_system_options().is_incognito() &&
68 !(url
.type() == kFileSystemTypeTemporary
&&
69 enable_temporary_file_system_in_incognito_
)) {
70 // TODO(kinuko): return an isolated temporary directory.
71 callback
.Run(GURL(), std::string(), base::File::FILE_ERROR_SECURITY
);
75 delegate_
->OpenFileSystem(url
.origin(),
79 GetFileSystemRootURI(url
.origin(), url
.type()));
82 AsyncFileUtil
* SandboxFileSystemBackend::GetAsyncFileUtil(
83 FileSystemType type
) {
85 return delegate_
->file_util();
88 WatcherManager
* SandboxFileSystemBackend::GetWatcherManager(
89 FileSystemType type
) {
93 CopyOrMoveFileValidatorFactory
*
94 SandboxFileSystemBackend::GetCopyOrMoveFileValidatorFactory(
96 base::File::Error
* error_code
) {
98 *error_code
= base::File::FILE_OK
;
102 FileSystemOperation
* SandboxFileSystemBackend::CreateFileSystemOperation(
103 const FileSystemURL
& url
,
104 FileSystemContext
* context
,
105 base::File::Error
* error_code
) const {
106 DCHECK(CanHandleType(url
.type()));
110 scoped_ptr
<FileSystemOperationContext
> operation_context
=
111 delegate_
->CreateFileSystemOperationContext(url
, context
, error_code
);
112 if (!operation_context
)
115 SpecialStoragePolicy
* policy
= delegate_
->special_storage_policy();
116 if (policy
&& policy
->IsStorageUnlimited(url
.origin()))
117 operation_context
->set_quota_limit_type(storage::kQuotaLimitTypeUnlimited
);
119 operation_context
->set_quota_limit_type(storage::kQuotaLimitTypeLimited
);
121 return FileSystemOperation::Create(url
, context
, operation_context
.Pass());
124 bool SandboxFileSystemBackend::SupportsStreaming(
125 const storage::FileSystemURL
& url
) const {
129 bool SandboxFileSystemBackend::HasInplaceCopyImplementation(
130 storage::FileSystemType type
) const {
134 scoped_ptr
<storage::FileStreamReader
>
135 SandboxFileSystemBackend::CreateFileStreamReader(
136 const FileSystemURL
& url
,
138 int64 max_bytes_to_read
,
139 const base::Time
& expected_modification_time
,
140 FileSystemContext
* context
) const {
141 DCHECK(CanHandleType(url
.type()));
143 return delegate_
->CreateFileStreamReader(
144 url
, offset
, expected_modification_time
, context
);
147 scoped_ptr
<storage::FileStreamWriter
>
148 SandboxFileSystemBackend::CreateFileStreamWriter(
149 const FileSystemURL
& url
,
151 FileSystemContext
* context
) const {
152 DCHECK(CanHandleType(url
.type()));
154 return delegate_
->CreateFileStreamWriter(url
, offset
, context
, url
.type());
157 FileSystemQuotaUtil
* SandboxFileSystemBackend::GetQuotaUtil() {
161 const UpdateObserverList
* SandboxFileSystemBackend::GetUpdateObservers(
162 FileSystemType type
) const {
163 return delegate_
->GetUpdateObservers(type
);
166 const ChangeObserverList
* SandboxFileSystemBackend::GetChangeObservers(
167 FileSystemType type
) const {
168 return delegate_
->GetChangeObservers(type
);
171 const AccessObserverList
* SandboxFileSystemBackend::GetAccessObservers(
172 FileSystemType type
) const {
173 return delegate_
->GetAccessObservers(type
);
176 SandboxFileSystemBackendDelegate::OriginEnumerator
*
177 SandboxFileSystemBackend::CreateOriginEnumerator() {
179 return delegate_
->CreateOriginEnumerator();
182 } // namespace storage