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