Upstreaming browser/ui/uikit_ui_util from iOS.
[chromium-blink-merge.git] / content / public / test / sandbox_file_system_test_helper.cc
blob9e52ee8693bba9299c4cc02722c6b5292d6541ee
1 // Copyright 2013 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 "content/public/test/sandbox_file_system_test_helper.h"
7 #include "base/files/file_util.h"
8 #include "base/run_loop.h"
9 #include "content/public/test/mock_special_storage_policy.h"
10 #include "content/public/test/test_file_system_context.h"
11 #include "storage/browser/fileapi/file_system_context.h"
12 #include "storage/browser/fileapi/file_system_file_util.h"
13 #include "storage/browser/fileapi/file_system_operation_context.h"
14 #include "storage/browser/fileapi/file_system_operation_runner.h"
15 #include "storage/browser/fileapi/file_system_url.h"
16 #include "storage/browser/fileapi/file_system_usage_cache.h"
17 #include "storage/browser/fileapi/sandbox_file_system_backend.h"
18 #include "storage/browser/quota/quota_manager_proxy.h"
19 #include "storage/common/fileapi/file_system_util.h"
20 #include "url/gurl.h"
22 using storage::FileSystemContext;
23 using storage::FileSystemOperationContext;
24 using storage::FileSystemOperationRunner;
25 using storage::FileSystemURL;
27 namespace content {
29 SandboxFileSystemTestHelper::SandboxFileSystemTestHelper(
30 const GURL& origin,
31 storage::FileSystemType type)
32 : origin_(origin), type_(type), file_util_(NULL) {
35 SandboxFileSystemTestHelper::SandboxFileSystemTestHelper()
36 : origin_(GURL("http://foo.com")),
37 type_(storage::kFileSystemTypeTemporary),
38 file_util_(NULL) {
41 SandboxFileSystemTestHelper::~SandboxFileSystemTestHelper() {
44 void SandboxFileSystemTestHelper::SetUp(const base::FilePath& base_dir) {
45 SetUp(base_dir, NULL);
48 void SandboxFileSystemTestHelper::SetUp(
49 FileSystemContext* file_system_context) {
50 file_system_context_ = file_system_context;
52 SetUpFileSystem();
55 void SandboxFileSystemTestHelper::SetUp(
56 const base::FilePath& base_dir,
57 storage::QuotaManagerProxy* quota_manager_proxy) {
58 file_system_context_ = CreateFileSystemContextForTesting(
59 quota_manager_proxy, base_dir);
61 SetUpFileSystem();
64 void SandboxFileSystemTestHelper::TearDown() {
65 file_system_context_ = NULL;
66 base::RunLoop().RunUntilIdle();
69 base::FilePath SandboxFileSystemTestHelper::GetOriginRootPath() {
70 return file_system_context_->sandbox_delegate()->
71 GetBaseDirectoryForOriginAndType(origin_, type_, false);
74 base::FilePath SandboxFileSystemTestHelper::GetLocalPath(
75 const base::FilePath& path) {
76 DCHECK(file_util_);
77 base::FilePath local_path;
78 scoped_ptr<FileSystemOperationContext> context(NewOperationContext());
79 file_util_->GetLocalFilePath(context.get(), CreateURL(path), &local_path);
80 return local_path;
83 base::FilePath SandboxFileSystemTestHelper::GetLocalPathFromASCII(
84 const std::string& path) {
85 return GetLocalPath(base::FilePath().AppendASCII(path));
88 base::FilePath SandboxFileSystemTestHelper::GetUsageCachePath() const {
89 return file_system_context_->sandbox_delegate()->
90 GetUsageCachePathForOriginAndType(origin_, type_);
93 FileSystemURL SandboxFileSystemTestHelper::CreateURL(
94 const base::FilePath& path) const {
95 return file_system_context_->CreateCrackedFileSystemURL(origin_, type_, path);
98 int64 SandboxFileSystemTestHelper::GetCachedOriginUsage() const {
99 return file_system_context_->GetQuotaUtil(type_)
100 ->GetOriginUsageOnFileTaskRunner(
101 file_system_context_.get(), origin_, type_);
104 int64 SandboxFileSystemTestHelper::ComputeCurrentOriginUsage() {
105 usage_cache()->CloseCacheFiles();
106 int64 size = base::ComputeDirectorySize(GetOriginRootPath());
107 if (base::PathExists(GetUsageCachePath()))
108 size -= storage::FileSystemUsageCache::kUsageFileSize;
109 return size;
112 int64
113 SandboxFileSystemTestHelper::ComputeCurrentDirectoryDatabaseUsage() {
114 return base::ComputeDirectorySize(
115 GetOriginRootPath().AppendASCII("Paths"));
118 FileSystemOperationRunner* SandboxFileSystemTestHelper::operation_runner() {
119 return file_system_context_->operation_runner();
122 FileSystemOperationContext*
123 SandboxFileSystemTestHelper::NewOperationContext() {
124 DCHECK(file_system_context_.get());
125 FileSystemOperationContext* context =
126 new FileSystemOperationContext(file_system_context_.get());
127 context->set_update_observers(
128 *file_system_context_->GetUpdateObservers(type_));
129 return context;
132 void SandboxFileSystemTestHelper::AddFileChangeObserver(
133 storage::FileChangeObserver* observer) {
134 file_system_context_->sandbox_delegate()->AddFileChangeObserver(
135 type_, observer, NULL);
138 void SandboxFileSystemTestHelper::AddFileUpdateObserver(
139 storage::FileUpdateObserver* observer) {
140 file_system_context_->sandbox_delegate()->AddFileUpdateObserver(
141 type_, observer, NULL);
144 storage::FileSystemUsageCache* SandboxFileSystemTestHelper::usage_cache() {
145 return file_system_context()->sandbox_delegate()->usage_cache();
148 void SandboxFileSystemTestHelper::SetUpFileSystem() {
149 DCHECK(file_system_context_.get());
150 DCHECK(file_system_context_->sandbox_backend()->CanHandleType(type_));
152 file_util_ = file_system_context_->sandbox_delegate()->sync_file_util();
153 DCHECK(file_util_);
155 // Prepare the origin's root directory.
156 file_system_context_->sandbox_delegate()->
157 GetBaseDirectoryForOriginAndType(origin_, type_, true /* create */);
159 // Initialize the usage cache file.
160 base::FilePath usage_cache_path = GetUsageCachePath();
161 if (!usage_cache_path.empty())
162 usage_cache()->UpdateUsage(usage_cache_path, 0);
165 } // namespace content