| OLD | NEW |
| (Empty) |
| 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. | |
| 4 | |
| 5 #include "content/public/test/sandbox_file_system_test_helper.h" | |
| 6 | |
| 7 #include <memory> | |
| 8 | |
| 9 #include "base/files/file_util.h" | |
| 10 #include "base/run_loop.h" | |
| 11 #include "content/public/test/mock_special_storage_policy.h" | |
| 12 #include "content/public/test/test_file_system_context.h" | |
| 13 #include "storage/browser/fileapi/file_system_context.h" | |
| 14 #include "storage/browser/fileapi/file_system_file_util.h" | |
| 15 #include "storage/browser/fileapi/file_system_operation_context.h" | |
| 16 #include "storage/browser/fileapi/file_system_operation_runner.h" | |
| 17 #include "storage/browser/fileapi/file_system_url.h" | |
| 18 #include "storage/browser/fileapi/file_system_usage_cache.h" | |
| 19 #include "storage/browser/fileapi/sandbox_file_system_backend.h" | |
| 20 #include "storage/browser/quota/quota_manager_proxy.h" | |
| 21 #include "storage/common/fileapi/file_system_util.h" | |
| 22 #include "url/gurl.h" | |
| 23 | |
| 24 using storage::FileSystemContext; | |
| 25 using storage::FileSystemOperationContext; | |
| 26 using storage::FileSystemOperationRunner; | |
| 27 using storage::FileSystemURL; | |
| 28 | |
| 29 namespace content { | |
| 30 | |
| 31 SandboxFileSystemTestHelper::SandboxFileSystemTestHelper( | |
| 32 const GURL& origin, | |
| 33 storage::FileSystemType type) | |
| 34 : origin_(origin), type_(type), file_util_(NULL) { | |
| 35 } | |
| 36 | |
| 37 SandboxFileSystemTestHelper::SandboxFileSystemTestHelper() | |
| 38 : origin_(GURL("http://foo.com")), | |
| 39 type_(storage::kFileSystemTypeTemporary), | |
| 40 file_util_(NULL) { | |
| 41 } | |
| 42 | |
| 43 SandboxFileSystemTestHelper::~SandboxFileSystemTestHelper() { | |
| 44 } | |
| 45 | |
| 46 void SandboxFileSystemTestHelper::SetUp(const base::FilePath& base_dir) { | |
| 47 SetUp(base_dir, NULL); | |
| 48 } | |
| 49 | |
| 50 void SandboxFileSystemTestHelper::SetUp( | |
| 51 FileSystemContext* file_system_context) { | |
| 52 file_system_context_ = file_system_context; | |
| 53 | |
| 54 SetUpFileSystem(); | |
| 55 } | |
| 56 | |
| 57 void SandboxFileSystemTestHelper::SetUp( | |
| 58 const base::FilePath& base_dir, | |
| 59 storage::QuotaManagerProxy* quota_manager_proxy) { | |
| 60 file_system_context_ = CreateFileSystemContextForTesting( | |
| 61 quota_manager_proxy, base_dir); | |
| 62 | |
| 63 SetUpFileSystem(); | |
| 64 } | |
| 65 | |
| 66 void SandboxFileSystemTestHelper::TearDown() { | |
| 67 file_system_context_ = NULL; | |
| 68 base::RunLoop().RunUntilIdle(); | |
| 69 } | |
| 70 | |
| 71 base::FilePath SandboxFileSystemTestHelper::GetOriginRootPath() { | |
| 72 return file_system_context_->sandbox_delegate()-> | |
| 73 GetBaseDirectoryForOriginAndType(origin_, type_, false); | |
| 74 } | |
| 75 | |
| 76 base::FilePath SandboxFileSystemTestHelper::GetLocalPath( | |
| 77 const base::FilePath& path) { | |
| 78 DCHECK(file_util_); | |
| 79 base::FilePath local_path; | |
| 80 std::unique_ptr<FileSystemOperationContext> context(NewOperationContext()); | |
| 81 file_util_->GetLocalFilePath(context.get(), CreateURL(path), &local_path); | |
| 82 return local_path; | |
| 83 } | |
| 84 | |
| 85 base::FilePath SandboxFileSystemTestHelper::GetLocalPathFromASCII( | |
| 86 const std::string& path) { | |
| 87 return GetLocalPath(base::FilePath().AppendASCII(path)); | |
| 88 } | |
| 89 | |
| 90 base::FilePath SandboxFileSystemTestHelper::GetUsageCachePath() const { | |
| 91 return file_system_context_->sandbox_delegate()-> | |
| 92 GetUsageCachePathForOriginAndType(origin_, type_); | |
| 93 } | |
| 94 | |
| 95 FileSystemURL SandboxFileSystemTestHelper::CreateURL( | |
| 96 const base::FilePath& path) const { | |
| 97 return file_system_context_->CreateCrackedFileSystemURL(origin_, type_, path); | |
| 98 } | |
| 99 | |
| 100 int64_t SandboxFileSystemTestHelper::GetCachedOriginUsage() const { | |
| 101 return file_system_context_->GetQuotaUtil(type_) | |
| 102 ->GetOriginUsageOnFileTaskRunner( | |
| 103 file_system_context_.get(), origin_, type_); | |
| 104 } | |
| 105 | |
| 106 int64_t SandboxFileSystemTestHelper::ComputeCurrentOriginUsage() { | |
| 107 usage_cache()->CloseCacheFiles(); | |
| 108 int64_t size = base::ComputeDirectorySize(GetOriginRootPath()); | |
| 109 if (base::PathExists(GetUsageCachePath())) | |
| 110 size -= storage::FileSystemUsageCache::kUsageFileSize; | |
| 111 return size; | |
| 112 } | |
| 113 | |
| 114 int64_t SandboxFileSystemTestHelper::ComputeCurrentDirectoryDatabaseUsage() { | |
| 115 return base::ComputeDirectorySize( | |
| 116 GetOriginRootPath().AppendASCII("Paths")); | |
| 117 } | |
| 118 | |
| 119 FileSystemOperationRunner* SandboxFileSystemTestHelper::operation_runner() { | |
| 120 return file_system_context_->operation_runner(); | |
| 121 } | |
| 122 | |
| 123 FileSystemOperationContext* | |
| 124 SandboxFileSystemTestHelper::NewOperationContext() { | |
| 125 DCHECK(file_system_context_.get()); | |
| 126 FileSystemOperationContext* context = | |
| 127 new FileSystemOperationContext(file_system_context_.get()); | |
| 128 context->set_update_observers( | |
| 129 *file_system_context_->GetUpdateObservers(type_)); | |
| 130 return context; | |
| 131 } | |
| 132 | |
| 133 void SandboxFileSystemTestHelper::AddFileChangeObserver( | |
| 134 storage::FileChangeObserver* observer) { | |
| 135 file_system_context_->sandbox_delegate()->AddFileChangeObserver( | |
| 136 type_, observer, NULL); | |
| 137 } | |
| 138 | |
| 139 void SandboxFileSystemTestHelper::AddFileUpdateObserver( | |
| 140 storage::FileUpdateObserver* observer) { | |
| 141 file_system_context_->sandbox_delegate()->AddFileUpdateObserver( | |
| 142 type_, observer, NULL); | |
| 143 } | |
| 144 | |
| 145 storage::FileSystemUsageCache* SandboxFileSystemTestHelper::usage_cache() { | |
| 146 return file_system_context()->sandbox_delegate()->usage_cache(); | |
| 147 } | |
| 148 | |
| 149 void SandboxFileSystemTestHelper::SetUpFileSystem() { | |
| 150 DCHECK(file_system_context_.get()); | |
| 151 DCHECK(file_system_context_->sandbox_backend()->CanHandleType(type_)); | |
| 152 | |
| 153 file_util_ = file_system_context_->sandbox_delegate()->sync_file_util(); | |
| 154 DCHECK(file_util_); | |
| 155 | |
| 156 // Prepare the origin's root directory. | |
| 157 file_system_context_->sandbox_delegate()-> | |
| 158 GetBaseDirectoryForOriginAndType(origin_, type_, true /* create */); | |
| 159 | |
| 160 // Initialize the usage cache file. | |
| 161 base::FilePath usage_cache_path = GetUsageCachePath(); | |
| 162 if (!usage_cache_path.empty()) | |
| 163 usage_cache()->UpdateUsage(usage_cache_path, 0); | |
| 164 } | |
| 165 | |
| 166 } // namespace content | |
| OLD | NEW |