Add entry to histograms.xml for OverlyLargeOriginLength.
[chromium-blink-merge.git] / webkit / browser / fileapi / sandbox_file_system_backend.cc
blobcc022788aa475878783ce5aa25818edab7e9cc99
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::OpenFileSystem(
64 const GURL& origin_url,
65 fileapi::FileSystemType type,
66 OpenFileSystemMode mode,
67 const OpenFileSystemCallback& callback) {
68 DCHECK(CanHandleType(type));
69 DCHECK(delegate_);
70 if (delegate_->file_system_options().is_incognito() &&
71 !(type == kFileSystemTypeTemporary &&
72 enable_temporary_file_system_in_incognito_)) {
73 // TODO(kinuko): return an isolated temporary directory.
74 callback.Run(GURL(), std::string(), base::PLATFORM_FILE_ERROR_SECURITY);
75 return;
78 delegate_->OpenFileSystem(
79 origin_url, type, mode, callback,
80 GetFileSystemRootURI(origin_url, type));
83 AsyncFileUtil* SandboxFileSystemBackend::GetAsyncFileUtil(
84 FileSystemType type) {
85 DCHECK(delegate_);
86 return delegate_->file_util();
89 CopyOrMoveFileValidatorFactory*
90 SandboxFileSystemBackend::GetCopyOrMoveFileValidatorFactory(
91 FileSystemType type,
92 base::PlatformFileError* error_code) {
93 DCHECK(error_code);
94 *error_code = base::PLATFORM_FILE_OK;
95 return NULL;
98 FileSystemOperation* SandboxFileSystemBackend::CreateFileSystemOperation(
99 const FileSystemURL& url,
100 FileSystemContext* context,
101 base::PlatformFileError* error_code) const {
102 DCHECK(CanHandleType(url.type()));
103 DCHECK(error_code);
105 DCHECK(delegate_);
106 scoped_ptr<FileSystemOperationContext> operation_context =
107 delegate_->CreateFileSystemOperationContext(url, context, error_code);
108 if (!operation_context)
109 return NULL;
111 SpecialStoragePolicy* policy = delegate_->special_storage_policy();
112 if (policy && policy->IsStorageUnlimited(url.origin()))
113 operation_context->set_quota_limit_type(quota::kQuotaLimitTypeUnlimited);
114 else
115 operation_context->set_quota_limit_type(quota::kQuotaLimitTypeLimited);
117 return FileSystemOperation::Create(url, context, operation_context.Pass());
120 scoped_ptr<webkit_blob::FileStreamReader>
121 SandboxFileSystemBackend::CreateFileStreamReader(
122 const FileSystemURL& url,
123 int64 offset,
124 const base::Time& expected_modification_time,
125 FileSystemContext* context) const {
126 DCHECK(CanHandleType(url.type()));
127 DCHECK(delegate_);
128 return delegate_->CreateFileStreamReader(
129 url, offset, expected_modification_time, context);
132 scoped_ptr<fileapi::FileStreamWriter>
133 SandboxFileSystemBackend::CreateFileStreamWriter(
134 const FileSystemURL& url,
135 int64 offset,
136 FileSystemContext* context) const {
137 DCHECK(CanHandleType(url.type()));
138 DCHECK(delegate_);
139 return delegate_->CreateFileStreamWriter(url, offset, context, url.type());
142 FileSystemQuotaUtil* SandboxFileSystemBackend::GetQuotaUtil() {
143 return delegate_;
146 SandboxFileSystemBackendDelegate::OriginEnumerator*
147 SandboxFileSystemBackend::CreateOriginEnumerator() {
148 DCHECK(delegate_);
149 return delegate_->CreateOriginEnumerator();
152 } // namespace fileapi