| OLD | NEW |
| (Empty) |
| 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. | |
| 4 | |
| 5 #include "webkit/fileapi/sandbox_file_system_test_helper.h" | |
| 6 | |
| 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" | |
| 22 | |
| 23 namespace fileapi { | |
| 24 | |
| 25 SandboxFileSystemTestHelper::SandboxFileSystemTestHelper( | |
| 26 const GURL& origin, FileSystemType type) | |
| 27 : origin_(origin), type_(type), file_util_(NULL) { | |
| 28 } | |
| 29 | |
| 30 SandboxFileSystemTestHelper::SandboxFileSystemTestHelper() | |
| 31 : origin_(GURL("http://foo.com")), | |
| 32 type_(kFileSystemTypeTemporary), | |
| 33 file_util_(NULL) { | |
| 34 } | |
| 35 | |
| 36 SandboxFileSystemTestHelper::~SandboxFileSystemTestHelper() { | |
| 37 } | |
| 38 | |
| 39 void SandboxFileSystemTestHelper::SetUp(const base::FilePath& base_dir) { | |
| 40 SetUp(base_dir, NULL); | |
| 41 } | |
| 42 | |
| 43 void SandboxFileSystemTestHelper::SetUp( | |
| 44 FileSystemContext* file_system_context) { | |
| 45 file_system_context_ = file_system_context; | |
| 46 | |
| 47 SetUpFileSystem(); | |
| 48 } | |
| 49 | |
| 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); | |
| 55 | |
| 56 SetUpFileSystem(); | |
| 57 } | |
| 58 | |
| 59 void SandboxFileSystemTestHelper::TearDown() { | |
| 60 file_system_context_ = NULL; | |
| 61 base::MessageLoop::current()->RunUntilIdle(); | |
| 62 } | |
| 63 | |
| 64 base::FilePath SandboxFileSystemTestHelper::GetOriginRootPath() const { | |
| 65 return file_system_context_->sandbox_provider()-> | |
| 66 GetBaseDirectoryForOriginAndType(origin_, type_, false); | |
| 67 } | |
| 68 | |
| 69 base::FilePath SandboxFileSystemTestHelper::GetLocalPath( | |
| 70 const base::FilePath& path) { | |
| 71 DCHECK(file_util_); | |
| 72 base::FilePath local_path; | |
| 73 scoped_ptr<FileSystemOperationContext> context(NewOperationContext()); | |
| 74 file_util_->GetLocalFilePath(context.get(), CreateURL(path), &local_path); | |
| 75 return local_path; | |
| 76 } | |
| 77 | |
| 78 base::FilePath SandboxFileSystemTestHelper::GetLocalPathFromASCII( | |
| 79 const std::string& path) { | |
| 80 return GetLocalPath(base::FilePath().AppendASCII(path)); | |
| 81 } | |
| 82 | |
| 83 base::FilePath SandboxFileSystemTestHelper::GetUsageCachePath() const { | |
| 84 return file_system_context_-> | |
| 85 sandbox_provider()->GetUsageCachePathForOriginAndType(origin_, type_); | |
| 86 } | |
| 87 | |
| 88 FileSystemURL SandboxFileSystemTestHelper::CreateURL( | |
| 89 const base::FilePath& path) const { | |
| 90 return file_system_context_->CreateCrackedFileSystemURL(origin_, type_, path); | |
| 91 } | |
| 92 | |
| 93 int64 SandboxFileSystemTestHelper::GetCachedOriginUsage() const { | |
| 94 return file_system_context_->GetQuotaUtil(type_)->GetOriginUsageOnFileThread( | |
| 95 file_system_context_, origin_, type_); | |
| 96 } | |
| 97 | |
| 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; | |
| 103 return size; | |
| 104 } | |
| 105 | |
| 106 int64 | |
| 107 SandboxFileSystemTestHelper::ComputeCurrentDirectoryDatabaseUsage() const { | |
| 108 return file_util::ComputeDirectorySize( | |
| 109 GetOriginRootPath().AppendASCII("Paths")); | |
| 110 } | |
| 111 | |
| 112 LocalFileSystemOperation* SandboxFileSystemTestHelper::NewOperation() { | |
| 113 DCHECK(file_system_context_.get()); | |
| 114 DCHECK(file_util_); | |
| 115 LocalFileSystemOperation* operation = static_cast<LocalFileSystemOperation*>( | |
| 116 file_system_context_->CreateFileSystemOperation( | |
| 117 CreateURL(base::FilePath()), NULL)); | |
| 118 return operation; | |
| 119 } | |
| 120 | |
| 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_)); | |
| 128 return context; | |
| 129 } | |
| 130 | |
| 131 FileSystemUsageCache* SandboxFileSystemTestHelper::usage_cache() { | |
| 132 return file_system_context()->sandbox_provider()->usage_cache(); | |
| 133 } | |
| 134 | |
| 135 void SandboxFileSystemTestHelper::SetUpFileSystem() { | |
| 136 DCHECK(file_system_context_); | |
| 137 DCHECK(file_system_context_->sandbox_provider()->CanHandleType(type_)); | |
| 138 | |
| 139 file_util_ = file_system_context_->GetFileUtil(type_); | |
| 140 DCHECK(file_util_); | |
| 141 | |
| 142 // Prepare the origin's root directory. | |
| 143 file_system_context_->sandbox_provider()-> | |
| 144 GetFileSystemRootPathOnFileThread(CreateURL(base::FilePath()), | |
| 145 true /* create */); | |
| 146 | |
| 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); | |
| 151 } | |
| 152 | |
| 153 } // namespace fileapi | |
| OLD | NEW |