OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "webkit/browser/fileapi/sandbox_file_system_backend.h" | 5 #include "webkit/browser/fileapi/sandbox_file_system_backend.h" |
6 | 6 |
7 #include "base/bind.h" | 7 #include "base/bind.h" |
8 #include "base/file_util.h" | 8 #include "base/file_util.h" |
9 #include "base/logging.h" | 9 #include "base/logging.h" |
10 #include "base/metrics/histogram.h" | 10 #include "base/metrics/histogram.h" |
11 #include "base/task_runner_util.h" | 11 #include "base/task_runner_util.h" |
12 #include "url/gurl.h" | 12 #include "url/gurl.h" |
13 #include "webkit/browser/fileapi/async_file_util_adapter.h" | 13 #include "webkit/browser/fileapi/async_file_util_adapter.h" |
14 #include "webkit/browser/fileapi/copy_or_move_file_validator.h" | 14 #include "webkit/browser/fileapi/copy_or_move_file_validator.h" |
15 #include "webkit/browser/fileapi/file_system_context.h" | 15 #include "webkit/browser/fileapi/file_system_context.h" |
16 #include "webkit/browser/fileapi/file_system_file_stream_reader.h" | 16 #include "webkit/browser/fileapi/file_system_file_stream_reader.h" |
17 #include "webkit/browser/fileapi/file_system_operation_context.h" | 17 #include "webkit/browser/fileapi/file_system_operation_context.h" |
18 #include "webkit/browser/fileapi/file_system_operation_impl.h" | 18 #include "webkit/browser/fileapi/file_system_operation_impl.h" |
19 #include "webkit/browser/fileapi/file_system_options.h" | 19 #include "webkit/browser/fileapi/file_system_options.h" |
20 #include "webkit/browser/fileapi/file_system_usage_cache.h" | 20 #include "webkit/browser/fileapi/file_system_usage_cache.h" |
21 #include "webkit/browser/fileapi/obfuscated_file_util.h" | 21 #include "webkit/browser/fileapi/obfuscated_file_util.h" |
22 #include "webkit/browser/fileapi/sandbox_context.h" | |
23 #include "webkit/browser/fileapi/sandbox_file_stream_writer.h" | 22 #include "webkit/browser/fileapi/sandbox_file_stream_writer.h" |
| 23 #include "webkit/browser/fileapi/sandbox_file_system_backend_delegate.h" |
24 #include "webkit/browser/fileapi/sandbox_quota_observer.h" | 24 #include "webkit/browser/fileapi/sandbox_quota_observer.h" |
25 #include "webkit/browser/quota/quota_manager.h" | 25 #include "webkit/browser/quota/quota_manager.h" |
26 #include "webkit/common/fileapi/file_system_types.h" | 26 #include "webkit/common/fileapi/file_system_types.h" |
27 #include "webkit/common/fileapi/file_system_util.h" | 27 #include "webkit/common/fileapi/file_system_util.h" |
28 | 28 |
29 using quota::QuotaManagerProxy; | 29 using quota::QuotaManagerProxy; |
30 using quota::SpecialStoragePolicy; | 30 using quota::SpecialStoragePolicy; |
31 | 31 |
32 namespace fileapi { | 32 namespace fileapi { |
33 | 33 |
34 namespace { | 34 namespace { |
35 | 35 |
36 const char kTemporaryOriginsCountLabel[] = "FileSystem.TemporaryOriginsCount"; | 36 const char kTemporaryOriginsCountLabel[] = "FileSystem.TemporaryOriginsCount"; |
37 const char kPersistentOriginsCountLabel[] = "FileSystem.PersistentOriginsCount"; | 37 const char kPersistentOriginsCountLabel[] = "FileSystem.PersistentOriginsCount"; |
38 | 38 |
39 } // anonymous namespace | 39 } // anonymous namespace |
40 | 40 |
41 SandboxFileSystemBackend::SandboxFileSystemBackend( | 41 SandboxFileSystemBackend::SandboxFileSystemBackend( |
42 SandboxContext* sandbox_context) | 42 SandboxFileSystemBackendDelegate* delegate) |
43 : sandbox_context_(sandbox_context), | 43 : delegate_(delegate), |
44 enable_temporary_file_system_in_incognito_(false) { | 44 enable_temporary_file_system_in_incognito_(false) { |
45 } | 45 } |
46 | 46 |
47 SandboxFileSystemBackend::~SandboxFileSystemBackend() { | 47 SandboxFileSystemBackend::~SandboxFileSystemBackend() { |
48 } | 48 } |
49 | 49 |
50 bool SandboxFileSystemBackend::CanHandleType(FileSystemType type) const { | 50 bool SandboxFileSystemBackend::CanHandleType(FileSystemType type) const { |
51 return type == kFileSystemTypeTemporary || | 51 return type == kFileSystemTypeTemporary || |
52 type == kFileSystemTypePersistent; | 52 type == kFileSystemTypePersistent; |
53 } | 53 } |
54 | 54 |
55 void SandboxFileSystemBackend::Initialize(FileSystemContext* context) { | 55 void SandboxFileSystemBackend::Initialize(FileSystemContext* context) { |
56 // Set quota observers. | 56 // Set quota observers. |
57 update_observers_ = update_observers_.AddObserver( | 57 update_observers_ = update_observers_.AddObserver( |
58 sandbox_context_->quota_observer(), | 58 delegate_->quota_observer(), |
59 sandbox_context_->file_task_runner()); | 59 delegate_->file_task_runner()); |
60 access_observers_ = access_observers_.AddObserver( | 60 access_observers_ = access_observers_.AddObserver( |
61 sandbox_context_->quota_observer(), NULL); | 61 delegate_->quota_observer(), NULL); |
62 } | 62 } |
63 | 63 |
64 void SandboxFileSystemBackend::OpenFileSystem( | 64 void SandboxFileSystemBackend::OpenFileSystem( |
65 const GURL& origin_url, | 65 const GURL& origin_url, |
66 fileapi::FileSystemType type, | 66 fileapi::FileSystemType type, |
67 OpenFileSystemMode mode, | 67 OpenFileSystemMode mode, |
68 const OpenFileSystemCallback& callback) { | 68 const OpenFileSystemCallback& callback) { |
69 DCHECK(CanHandleType(type)); | 69 DCHECK(CanHandleType(type)); |
70 DCHECK(sandbox_context_); | 70 DCHECK(delegate_); |
71 if (sandbox_context_->file_system_options().is_incognito() && | 71 if (delegate_->file_system_options().is_incognito() && |
72 !(type == kFileSystemTypeTemporary && | 72 !(type == kFileSystemTypeTemporary && |
73 enable_temporary_file_system_in_incognito_)) { | 73 enable_temporary_file_system_in_incognito_)) { |
74 // TODO(kinuko): return an isolated temporary directory. | 74 // TODO(kinuko): return an isolated temporary directory. |
75 callback.Run(GURL(), std::string(), base::PLATFORM_FILE_ERROR_SECURITY); | 75 callback.Run(GURL(), std::string(), base::PLATFORM_FILE_ERROR_SECURITY); |
76 return; | 76 return; |
77 } | 77 } |
78 | 78 |
79 sandbox_context_->OpenFileSystem( | 79 delegate_->OpenFileSystem( |
80 origin_url, type, mode, callback, | 80 origin_url, type, mode, callback, |
81 GetFileSystemRootURI(origin_url, type)); | 81 GetFileSystemRootURI(origin_url, type)); |
82 } | 82 } |
83 | 83 |
84 FileSystemFileUtil* SandboxFileSystemBackend::GetFileUtil( | 84 FileSystemFileUtil* SandboxFileSystemBackend::GetFileUtil( |
85 FileSystemType type) { | 85 FileSystemType type) { |
86 return sandbox_context_->sync_file_util(); | 86 return delegate_->sync_file_util(); |
87 } | 87 } |
88 | 88 |
89 AsyncFileUtil* SandboxFileSystemBackend::GetAsyncFileUtil( | 89 AsyncFileUtil* SandboxFileSystemBackend::GetAsyncFileUtil( |
90 FileSystemType type) { | 90 FileSystemType type) { |
91 DCHECK(sandbox_context_); | 91 DCHECK(delegate_); |
92 return sandbox_context_->file_util(); | 92 return delegate_->file_util(); |
93 } | 93 } |
94 | 94 |
95 CopyOrMoveFileValidatorFactory* | 95 CopyOrMoveFileValidatorFactory* |
96 SandboxFileSystemBackend::GetCopyOrMoveFileValidatorFactory( | 96 SandboxFileSystemBackend::GetCopyOrMoveFileValidatorFactory( |
97 FileSystemType type, | 97 FileSystemType type, |
98 base::PlatformFileError* error_code) { | 98 base::PlatformFileError* error_code) { |
99 DCHECK(error_code); | 99 DCHECK(error_code); |
100 *error_code = base::PLATFORM_FILE_OK; | 100 *error_code = base::PLATFORM_FILE_OK; |
101 return NULL; | 101 return NULL; |
102 } | 102 } |
103 | 103 |
104 FileSystemOperation* SandboxFileSystemBackend::CreateFileSystemOperation( | 104 FileSystemOperation* SandboxFileSystemBackend::CreateFileSystemOperation( |
105 const FileSystemURL& url, | 105 const FileSystemURL& url, |
106 FileSystemContext* context, | 106 FileSystemContext* context, |
107 base::PlatformFileError* error_code) const { | 107 base::PlatformFileError* error_code) const { |
108 DCHECK(CanHandleType(url.type())); | 108 DCHECK(CanHandleType(url.type())); |
109 DCHECK(sandbox_context_); | 109 DCHECK(delegate_); |
110 if (!sandbox_context_->IsAccessValid(url)) { | 110 if (!delegate_->IsAccessValid(url)) { |
111 *error_code = base::PLATFORM_FILE_ERROR_SECURITY; | 111 *error_code = base::PLATFORM_FILE_ERROR_SECURITY; |
112 return NULL; | 112 return NULL; |
113 } | 113 } |
114 | 114 |
115 scoped_ptr<FileSystemOperationContext> operation_context( | 115 scoped_ptr<FileSystemOperationContext> operation_context( |
116 new FileSystemOperationContext(context)); | 116 new FileSystemOperationContext(context)); |
117 operation_context->set_update_observers(update_observers_); | 117 operation_context->set_update_observers(update_observers_); |
118 operation_context->set_change_observers(change_observers_); | 118 operation_context->set_change_observers(change_observers_); |
119 | 119 |
120 SpecialStoragePolicy* policy = sandbox_context_->special_storage_policy(); | 120 SpecialStoragePolicy* policy = delegate_->special_storage_policy(); |
121 if (policy && policy->IsStorageUnlimited(url.origin())) | 121 if (policy && policy->IsStorageUnlimited(url.origin())) |
122 operation_context->set_quota_limit_type(quota::kQuotaLimitTypeUnlimited); | 122 operation_context->set_quota_limit_type(quota::kQuotaLimitTypeUnlimited); |
123 else | 123 else |
124 operation_context->set_quota_limit_type(quota::kQuotaLimitTypeLimited); | 124 operation_context->set_quota_limit_type(quota::kQuotaLimitTypeLimited); |
125 | 125 |
126 return new FileSystemOperationImpl(url, context, operation_context.Pass()); | 126 return new FileSystemOperationImpl(url, context, operation_context.Pass()); |
127 } | 127 } |
128 | 128 |
129 scoped_ptr<webkit_blob::FileStreamReader> | 129 scoped_ptr<webkit_blob::FileStreamReader> |
130 SandboxFileSystemBackend::CreateFileStreamReader( | 130 SandboxFileSystemBackend::CreateFileStreamReader( |
131 const FileSystemURL& url, | 131 const FileSystemURL& url, |
132 int64 offset, | 132 int64 offset, |
133 const base::Time& expected_modification_time, | 133 const base::Time& expected_modification_time, |
134 FileSystemContext* context) const { | 134 FileSystemContext* context) const { |
135 DCHECK(CanHandleType(url.type())); | 135 DCHECK(CanHandleType(url.type())); |
136 DCHECK(sandbox_context_); | 136 DCHECK(delegate_); |
137 if (!sandbox_context_->IsAccessValid(url)) | 137 if (!delegate_->IsAccessValid(url)) |
138 return scoped_ptr<webkit_blob::FileStreamReader>(); | 138 return scoped_ptr<webkit_blob::FileStreamReader>(); |
139 return scoped_ptr<webkit_blob::FileStreamReader>( | 139 return scoped_ptr<webkit_blob::FileStreamReader>( |
140 new FileSystemFileStreamReader( | 140 new FileSystemFileStreamReader( |
141 context, url, offset, expected_modification_time)); | 141 context, url, offset, expected_modification_time)); |
142 } | 142 } |
143 | 143 |
144 scoped_ptr<fileapi::FileStreamWriter> | 144 scoped_ptr<fileapi::FileStreamWriter> |
145 SandboxFileSystemBackend::CreateFileStreamWriter( | 145 SandboxFileSystemBackend::CreateFileStreamWriter( |
146 const FileSystemURL& url, | 146 const FileSystemURL& url, |
147 int64 offset, | 147 int64 offset, |
148 FileSystemContext* context) const { | 148 FileSystemContext* context) const { |
149 DCHECK(CanHandleType(url.type())); | 149 DCHECK(CanHandleType(url.type())); |
150 DCHECK(sandbox_context_); | 150 DCHECK(delegate_); |
151 if (!sandbox_context_->IsAccessValid(url)) | 151 if (!delegate_->IsAccessValid(url)) |
152 return scoped_ptr<fileapi::FileStreamWriter>(); | 152 return scoped_ptr<fileapi::FileStreamWriter>(); |
153 return scoped_ptr<fileapi::FileStreamWriter>( | 153 return scoped_ptr<fileapi::FileStreamWriter>( |
154 new SandboxFileStreamWriter(context, url, offset, update_observers_)); | 154 new SandboxFileStreamWriter(context, url, offset, update_observers_)); |
155 } | 155 } |
156 | 156 |
157 FileSystemQuotaUtil* SandboxFileSystemBackend::GetQuotaUtil() { | 157 FileSystemQuotaUtil* SandboxFileSystemBackend::GetQuotaUtil() { |
158 return this; | 158 return this; |
159 } | 159 } |
160 | 160 |
161 SandboxContext::OriginEnumerator* | 161 SandboxFileSystemBackendDelegate::OriginEnumerator* |
162 SandboxFileSystemBackend::CreateOriginEnumerator() { | 162 SandboxFileSystemBackend::CreateOriginEnumerator() { |
163 DCHECK(sandbox_context_); | 163 DCHECK(delegate_); |
164 return sandbox_context_->CreateOriginEnumerator(); | 164 return delegate_->CreateOriginEnumerator(); |
165 } | 165 } |
166 | 166 |
167 base::PlatformFileError | 167 base::PlatformFileError |
168 SandboxFileSystemBackend::DeleteOriginDataOnFileThread( | 168 SandboxFileSystemBackend::DeleteOriginDataOnFileThread( |
169 FileSystemContext* file_system_context, | 169 FileSystemContext* file_system_context, |
170 QuotaManagerProxy* proxy, | 170 QuotaManagerProxy* proxy, |
171 const GURL& origin_url, | 171 const GURL& origin_url, |
172 fileapi::FileSystemType type) { | 172 fileapi::FileSystemType type) { |
173 DCHECK(CanHandleType(type)); | 173 DCHECK(CanHandleType(type)); |
174 DCHECK(sandbox_context_); | 174 DCHECK(delegate_); |
175 return sandbox_context_->DeleteOriginDataOnFileThread( | 175 return delegate_->DeleteOriginDataOnFileThread( |
176 file_system_context, proxy, origin_url, type); | 176 file_system_context, proxy, origin_url, type); |
177 } | 177 } |
178 | 178 |
179 void SandboxFileSystemBackend::GetOriginsForTypeOnFileThread( | 179 void SandboxFileSystemBackend::GetOriginsForTypeOnFileThread( |
180 fileapi::FileSystemType type, std::set<GURL>* origins) { | 180 fileapi::FileSystemType type, std::set<GURL>* origins) { |
181 DCHECK(CanHandleType(type)); | 181 DCHECK(CanHandleType(type)); |
182 DCHECK(sandbox_context_); | 182 DCHECK(delegate_); |
183 sandbox_context_->GetOriginsForTypeOnFileThread(type, origins); | 183 delegate_->GetOriginsForTypeOnFileThread(type, origins); |
184 switch (type) { | 184 switch (type) { |
185 case kFileSystemTypeTemporary: | 185 case kFileSystemTypeTemporary: |
186 UMA_HISTOGRAM_COUNTS(kTemporaryOriginsCountLabel, origins->size()); | 186 UMA_HISTOGRAM_COUNTS(kTemporaryOriginsCountLabel, origins->size()); |
187 break; | 187 break; |
188 case kFileSystemTypePersistent: | 188 case kFileSystemTypePersistent: |
189 UMA_HISTOGRAM_COUNTS(kPersistentOriginsCountLabel, origins->size()); | 189 UMA_HISTOGRAM_COUNTS(kPersistentOriginsCountLabel, origins->size()); |
190 break; | 190 break; |
191 default: | 191 default: |
192 break; | 192 break; |
193 } | 193 } |
194 } | 194 } |
195 | 195 |
196 void SandboxFileSystemBackend::GetOriginsForHostOnFileThread( | 196 void SandboxFileSystemBackend::GetOriginsForHostOnFileThread( |
197 fileapi::FileSystemType type, const std::string& host, | 197 fileapi::FileSystemType type, const std::string& host, |
198 std::set<GURL>* origins) { | 198 std::set<GURL>* origins) { |
199 DCHECK(CanHandleType(type)); | 199 DCHECK(CanHandleType(type)); |
200 DCHECK(sandbox_context_); | 200 DCHECK(delegate_); |
201 sandbox_context_->GetOriginsForHostOnFileThread(type, host, origins); | 201 delegate_->GetOriginsForHostOnFileThread(type, host, origins); |
202 } | 202 } |
203 | 203 |
204 int64 SandboxFileSystemBackend::GetOriginUsageOnFileThread( | 204 int64 SandboxFileSystemBackend::GetOriginUsageOnFileThread( |
205 FileSystemContext* file_system_context, | 205 FileSystemContext* file_system_context, |
206 const GURL& origin_url, | 206 const GURL& origin_url, |
207 fileapi::FileSystemType type) { | 207 fileapi::FileSystemType type) { |
208 DCHECK(CanHandleType(type)); | 208 DCHECK(CanHandleType(type)); |
209 DCHECK(sandbox_context_); | 209 DCHECK(delegate_); |
210 return sandbox_context_->GetOriginUsageOnFileThread( | 210 return delegate_->GetOriginUsageOnFileThread( |
211 file_system_context, origin_url, type); | 211 file_system_context, origin_url, type); |
212 } | 212 } |
213 | 213 |
214 void SandboxFileSystemBackend::AddFileUpdateObserver( | 214 void SandboxFileSystemBackend::AddFileUpdateObserver( |
215 FileSystemType type, | 215 FileSystemType type, |
216 FileUpdateObserver* observer, | 216 FileUpdateObserver* observer, |
217 base::SequencedTaskRunner* task_runner) { | 217 base::SequencedTaskRunner* task_runner) { |
218 DCHECK(CanHandleType(type)); | 218 DCHECK(CanHandleType(type)); |
219 UpdateObserverList* list = &update_observers_; | 219 UpdateObserverList* list = &update_observers_; |
220 *list = list->AddObserver(observer, task_runner); | 220 *list = list->AddObserver(observer, task_runner); |
(...skipping 28 matching lines...) Expand all Loading... |
249 return &change_observers_; | 249 return &change_observers_; |
250 } | 250 } |
251 | 251 |
252 const AccessObserverList* SandboxFileSystemBackend::GetAccessObservers( | 252 const AccessObserverList* SandboxFileSystemBackend::GetAccessObservers( |
253 FileSystemType type) const { | 253 FileSystemType type) const { |
254 DCHECK(CanHandleType(type)); | 254 DCHECK(CanHandleType(type)); |
255 return &access_observers_; | 255 return &access_observers_; |
256 } | 256 } |
257 | 257 |
258 } // namespace fileapi | 258 } // namespace fileapi |
OLD | NEW |