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