Home | History | Annotate | Download | only in fileapi
      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/browser/fileapi/sandbox_file_system_test_helper.h"
      6 
      7 #include "base/file_util.h"
      8 #include "base/message_loop/message_loop.h"
      9 #include "base/message_loop/message_loop_proxy.h"
     10 #include "url/gurl.h"
     11 #include "webkit/browser/fileapi/file_system_context.h"
     12 #include "webkit/browser/fileapi/file_system_file_util.h"
     13 #include "webkit/browser/fileapi/file_system_operation_context.h"
     14 #include "webkit/browser/fileapi/file_system_operation_runner.h"
     15 #include "webkit/browser/fileapi/file_system_url.h"
     16 #include "webkit/browser/fileapi/file_system_usage_cache.h"
     17 #include "webkit/browser/fileapi/mock_file_system_context.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 
     22 namespace fileapi {
     23 
     24 SandboxFileSystemTestHelper::SandboxFileSystemTestHelper(
     25     const GURL& origin, FileSystemType type)
     26     : origin_(origin), type_(type), file_util_(NULL) {
     27 }
     28 
     29 SandboxFileSystemTestHelper::SandboxFileSystemTestHelper()
     30     : origin_(GURL("http://foo.com")),
     31       type_(kFileSystemTypeTemporary),
     32       file_util_(NULL) {
     33 }
     34 
     35 SandboxFileSystemTestHelper::~SandboxFileSystemTestHelper() {
     36 }
     37 
     38 void SandboxFileSystemTestHelper::SetUp(const base::FilePath& base_dir) {
     39   SetUp(base_dir, NULL);
     40 }
     41 
     42 void SandboxFileSystemTestHelper::SetUp(
     43     FileSystemContext* file_system_context) {
     44   file_system_context_ = file_system_context;
     45 
     46   SetUpFileSystem();
     47 }
     48 
     49 void 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 
     58 void SandboxFileSystemTestHelper::TearDown() {
     59   file_system_context_ = NULL;
     60   base::MessageLoop::current()->RunUntilIdle();
     61 }
     62 
     63 base::FilePath SandboxFileSystemTestHelper::GetOriginRootPath() {
     64   return file_system_context_->sandbox_context()->
     65       GetBaseDirectoryForOriginAndType(origin_, type_, false);
     66 }
     67 
     68 base::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 
     77 base::FilePath SandboxFileSystemTestHelper::GetLocalPathFromASCII(
     78     const std::string& path) {
     79   return GetLocalPath(base::FilePath().AppendASCII(path));
     80 }
     81 
     82 base::FilePath SandboxFileSystemTestHelper::GetUsageCachePath() const {
     83   return file_system_context_->
     84       sandbox_context()->GetUsageCachePathForOriginAndType(origin_, type_);
     85 }
     86 
     87 FileSystemURL SandboxFileSystemTestHelper::CreateURL(
     88     const base::FilePath& path) const {
     89   return file_system_context_->CreateCrackedFileSystemURL(origin_, type_, path);
     90 }
     91 
     92 int64 SandboxFileSystemTestHelper::GetCachedOriginUsage() const {
     93   return file_system_context_->GetQuotaUtil(type_)
     94       ->GetOriginUsageOnFileThread(file_system_context_.get(), origin_, type_);
     95 }
     96 
     97 int64 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 
    105 int64
    106 SandboxFileSystemTestHelper::ComputeCurrentDirectoryDatabaseUsage() {
    107   return base::ComputeDirectorySize(
    108       GetOriginRootPath().AppendASCII("Paths"));
    109 }
    110 
    111 FileSystemOperationRunner* SandboxFileSystemTestHelper::operation_runner() {
    112   return file_system_context_->operation_runner();
    113 }
    114 
    115 FileSystemOperationContext*
    116 SandboxFileSystemTestHelper::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 
    125 void SandboxFileSystemTestHelper::AddFileChangeObserver(
    126     FileChangeObserver* observer) {
    127   file_system_context_->sandbox_backend()->
    128       AddFileChangeObserver(type_, observer, NULL);
    129 }
    130 
    131 FileSystemUsageCache* SandboxFileSystemTestHelper::usage_cache() {
    132   return file_system_context()->sandbox_context()->usage_cache();
    133 }
    134 
    135 void SandboxFileSystemTestHelper::SetUpFileSystem() {
    136   DCHECK(file_system_context_.get());
    137   DCHECK(file_system_context_->sandbox_backend()->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_context()->
    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