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/fileapi/sandbox_file_system_test_helper.h"
7 #include "base/file_util.h"
8 #include "base/message_loop.h"
9 #include "base/message_loop_proxy.h"
10 #include "googleurl/src/gurl.h"
11 #include "webkit/browser/fileapi/file_system_file_util.h"
12 #include "webkit/browser/fileapi/file_system_task_runners.h"
13 #include "webkit/browser/fileapi/file_system_usage_cache.h"
14 #include "webkit/browser/fileapi/local_file_system_operation.h"
15 #include "webkit/browser/fileapi/mock_file_system_context.h"
16 #include "webkit/browser/fileapi/sandbox_mount_point_provider.h"
17 #include "webkit/fileapi/file_system_context.h"
18 #include "webkit/fileapi/file_system_operation_context.h"
19 #include "webkit/fileapi/file_system_url.h"
20 #include "webkit/fileapi/file_system_util.h"
21 #include "webkit/quota/mock_special_storage_policy.h"
25 SandboxFileSystemTestHelper::SandboxFileSystemTestHelper(
26 const GURL
& origin
, FileSystemType type
)
27 : origin_(origin
), type_(type
), file_util_(NULL
) {
30 SandboxFileSystemTestHelper::SandboxFileSystemTestHelper()
31 : origin_(GURL("http://foo.com")),
32 type_(kFileSystemTypeTemporary
),
36 SandboxFileSystemTestHelper::~SandboxFileSystemTestHelper() {
39 void SandboxFileSystemTestHelper::SetUp(const base::FilePath
& base_dir
) {
40 SetUp(base_dir
, NULL
);
43 void SandboxFileSystemTestHelper::SetUp(
44 FileSystemContext
* file_system_context
) {
45 file_system_context_
= file_system_context
;
50 void SandboxFileSystemTestHelper::SetUp(
51 const base::FilePath
& base_dir
,
52 quota::QuotaManagerProxy
* quota_manager_proxy
) {
53 file_system_context_
= CreateFileSystemContextForTesting(
54 quota_manager_proxy
, base_dir
);
59 void SandboxFileSystemTestHelper::TearDown() {
60 file_system_context_
= NULL
;
61 base::MessageLoop::current()->RunUntilIdle();
64 base::FilePath
SandboxFileSystemTestHelper::GetOriginRootPath() const {
65 return file_system_context_
->sandbox_provider()->
66 GetBaseDirectoryForOriginAndType(origin_
, type_
, false);
69 base::FilePath
SandboxFileSystemTestHelper::GetLocalPath(
70 const base::FilePath
& path
) {
72 base::FilePath local_path
;
73 scoped_ptr
<FileSystemOperationContext
> context(NewOperationContext());
74 file_util_
->GetLocalFilePath(context
.get(), CreateURL(path
), &local_path
);
78 base::FilePath
SandboxFileSystemTestHelper::GetLocalPathFromASCII(
79 const std::string
& path
) {
80 return GetLocalPath(base::FilePath().AppendASCII(path
));
83 base::FilePath
SandboxFileSystemTestHelper::GetUsageCachePath() const {
84 return file_system_context_
->
85 sandbox_provider()->GetUsageCachePathForOriginAndType(origin_
, type_
);
88 FileSystemURL
SandboxFileSystemTestHelper::CreateURL(
89 const base::FilePath
& path
) const {
90 return file_system_context_
->CreateCrackedFileSystemURL(origin_
, type_
, path
);
93 int64
SandboxFileSystemTestHelper::GetCachedOriginUsage() const {
94 return file_system_context_
->GetQuotaUtil(type_
)->GetOriginUsageOnFileThread(
95 file_system_context_
, origin_
, type_
);
98 int64
SandboxFileSystemTestHelper::ComputeCurrentOriginUsage() {
99 usage_cache()->CloseCacheFiles();
100 int64 size
= file_util::ComputeDirectorySize(GetOriginRootPath());
101 if (file_util::PathExists(GetUsageCachePath()))
102 size
-= FileSystemUsageCache::kUsageFileSize
;
107 SandboxFileSystemTestHelper::ComputeCurrentDirectoryDatabaseUsage() const {
108 return file_util::ComputeDirectorySize(
109 GetOriginRootPath().AppendASCII("Paths"));
112 LocalFileSystemOperation
* SandboxFileSystemTestHelper::NewOperation() {
113 DCHECK(file_system_context_
.get());
115 LocalFileSystemOperation
* operation
= static_cast<LocalFileSystemOperation
*>(
116 file_system_context_
->CreateFileSystemOperation(
117 CreateURL(base::FilePath()), NULL
));
121 FileSystemOperationContext
*
122 SandboxFileSystemTestHelper::NewOperationContext() {
123 DCHECK(file_system_context_
.get());
124 FileSystemOperationContext
* context
=
125 new FileSystemOperationContext(file_system_context_
.get());
126 context
->set_update_observers(
127 *file_system_context_
->GetUpdateObservers(type_
));
131 FileSystemUsageCache
* SandboxFileSystemTestHelper::usage_cache() {
132 return file_system_context()->sandbox_provider()->usage_cache();
135 void SandboxFileSystemTestHelper::SetUpFileSystem() {
136 DCHECK(file_system_context_
);
137 DCHECK(file_system_context_
->sandbox_provider()->CanHandleType(type_
));
139 file_util_
= file_system_context_
->GetFileUtil(type_
);
142 // Prepare the origin's root directory.
143 file_system_context_
->sandbox_provider()->
144 GetFileSystemRootPathOnFileThread(CreateURL(base::FilePath()),
147 // Initialize the usage cache file.
148 base::FilePath usage_cache_path
= GetUsageCachePath();
149 if (!usage_cache_path
.empty())
150 usage_cache()->UpdateUsage(usage_cache_path
, 0);
153 } // namespace fileapi