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"
8 #include "base/file_util.h"
9 #include "base/logging.h"
10 #include "base/metrics/histogram.h"
11 #include "base/task_runner_util.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
;
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(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()));
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
);
77 delegate_
->OpenFileSystem(url
.origin(),
81 GetFileSystemRootURI(url
.origin(), url
.type()));
84 AsyncFileUtil
* SandboxFileSystemBackend::GetAsyncFileUtil(
85 FileSystemType type
) {
87 return delegate_
->file_util();
90 CopyOrMoveFileValidatorFactory
*
91 SandboxFileSystemBackend::GetCopyOrMoveFileValidatorFactory(
93 base::File::Error
* error_code
) {
95 *error_code
= base::File::FILE_OK
;
99 FileSystemOperation
* SandboxFileSystemBackend::CreateFileSystemOperation(
100 const FileSystemURL
& url
,
101 FileSystemContext
* context
,
102 base::File::Error
* error_code
) const {
103 DCHECK(CanHandleType(url
.type()));
107 scoped_ptr
<FileSystemOperationContext
> operation_context
=
108 delegate_
->CreateFileSystemOperationContext(url
, context
, error_code
);
109 if (!operation_context
)
112 SpecialStoragePolicy
* policy
= delegate_
->special_storage_policy();
113 if (policy
&& policy
->IsStorageUnlimited(url
.origin()))
114 operation_context
->set_quota_limit_type(quota::kQuotaLimitTypeUnlimited
);
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 {
126 scoped_ptr
<webkit_blob::FileStreamReader
>
127 SandboxFileSystemBackend::CreateFileStreamReader(
128 const FileSystemURL
& url
,
130 const base::Time
& expected_modification_time
,
131 FileSystemContext
* context
) const {
132 DCHECK(CanHandleType(url
.type()));
134 return delegate_
->CreateFileStreamReader(
135 url
, offset
, expected_modification_time
, context
);
138 scoped_ptr
<fileapi::FileStreamWriter
>
139 SandboxFileSystemBackend::CreateFileStreamWriter(
140 const FileSystemURL
& url
,
142 FileSystemContext
* context
) const {
143 DCHECK(CanHandleType(url
.type()));
145 return delegate_
->CreateFileStreamWriter(url
, offset
, context
, url
.type());
148 FileSystemQuotaUtil
* SandboxFileSystemBackend::GetQuotaUtil() {
152 SandboxFileSystemBackendDelegate::OriginEnumerator
*
153 SandboxFileSystemBackend::CreateOriginEnumerator() {
155 return delegate_
->CreateOriginEnumerator();
158 } // namespace fileapi