sandbox_file_system_test_helper.h 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#ifndef CONTENT_PUBLIC_TEST_SANDBOX_FILE_SYSTEM_TEST_HELPER_H_
6#define CONTENT_PUBLIC_TEST_SANDBOX_FILE_SYSTEM_TEST_HELPER_H_
7
8#include <string>
9
10#include "base/files/file_path.h"
11#include "base/memory/ref_counted.h"
12#include "url/gurl.h"
13#include "webkit/browser/fileapi/file_system_url.h"
14#include "webkit/browser/fileapi/file_system_usage_cache.h"
15#include "webkit/browser/fileapi/task_runner_bound_observer_list.h"
16#include "webkit/common/fileapi/file_system_types.h"
17#include "webkit/common/fileapi/file_system_util.h"
18#include "webkit/common/quota/quota_types.h"
19
20namespace base {
21class FilePath;
22}
23
24namespace quota {
25class QuotaManagerProxy;
26}
27
28namespace fileapi {
29
30class FileSystemContext;
31class FileSystemFileUtil;
32class FileSystemOperationContext;
33class FileSystemOperationRunner;
34
35// Filesystem test helper class that encapsulates test environment for
36// a given {origin, type} pair.  This helper only works for sandboxed
37// file systems (Temporary or Persistent).
38class SandboxFileSystemTestHelper {
39 public:
40  SandboxFileSystemTestHelper(const GURL& origin, FileSystemType type);
41  SandboxFileSystemTestHelper();
42  ~SandboxFileSystemTestHelper();
43
44  void SetUp(const base::FilePath& base_dir);
45  // If you want to use more than one SandboxFileSystemTestHelper in
46  // a single base directory, they have to share a context, so that they don't
47  // have multiple databases fighting over the lock to the origin directory
48  // [deep down inside ObfuscatedFileUtil].
49  void SetUp(FileSystemContext* file_system_context);
50  void SetUp(const base::FilePath& base_dir,
51             quota::QuotaManagerProxy* quota_manager_proxy);
52  void TearDown();
53
54  base::FilePath GetOriginRootPath();
55  base::FilePath GetLocalPath(const base::FilePath& path);
56  base::FilePath GetLocalPathFromASCII(const std::string& path);
57
58  // Returns empty path if filesystem type is neither temporary nor persistent.
59  base::FilePath GetUsageCachePath() const;
60
61  FileSystemURL CreateURL(const base::FilePath& path) const;
62  FileSystemURL CreateURLFromUTF8(const std::string& utf8) const {
63    return CreateURL(base::FilePath::FromUTF8Unsafe(utf8));
64  }
65
66  // This returns cached usage size returned by QuotaUtil.
67  int64 GetCachedOriginUsage() const;
68
69  // This doesn't work with OFSFU.
70  int64 ComputeCurrentOriginUsage();
71
72  int64 ComputeCurrentDirectoryDatabaseUsage();
73
74  FileSystemOperationRunner* operation_runner();
75  FileSystemOperationContext* NewOperationContext();
76
77  void AddFileChangeObserver(FileChangeObserver* observer);
78
79  FileSystemContext* file_system_context() const {
80    return file_system_context_.get();
81  }
82
83  const GURL& origin() const { return origin_; }
84  FileSystemType type() const { return type_; }
85  quota::StorageType storage_type() const {
86    return FileSystemTypeToQuotaStorageType(type_);
87  }
88  FileSystemFileUtil* file_util() const { return file_util_; }
89  FileSystemUsageCache* usage_cache();
90
91 private:
92  void SetUpFileSystem();
93
94  scoped_refptr<FileSystemContext> file_system_context_;
95
96  const GURL origin_;
97  const FileSystemType type_;
98  FileSystemFileUtil* file_util_;
99};
100
101}  // namespace fileapi
102
103#endif  // CONTENT_PUBLIC_TEST_SANDBOX_FILE_SYSTEM_TEST_HELPER_H_
104