OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "webkit/fileapi/local_file_system_test_helper.h" | 5 #include "webkit/fileapi/sandbox_file_system_test_helper.h" |
6 | 6 |
7 #include "base/file_util.h" | 7 #include "base/file_util.h" |
8 #include "base/message_loop.h" | 8 #include "base/message_loop.h" |
9 #include "base/message_loop_proxy.h" | 9 #include "base/message_loop_proxy.h" |
10 #include "googleurl/src/gurl.h" | 10 #include "googleurl/src/gurl.h" |
11 #include "webkit/browser/fileapi/file_system_file_util.h" | 11 #include "webkit/browser/fileapi/file_system_file_util.h" |
12 #include "webkit/browser/fileapi/file_system_usage_cache.h" | 12 #include "webkit/browser/fileapi/file_system_usage_cache.h" |
13 #include "webkit/browser/fileapi/sandbox_mount_point_provider.h" | 13 #include "webkit/browser/fileapi/sandbox_mount_point_provider.h" |
14 #include "webkit/fileapi/file_system_context.h" | 14 #include "webkit/fileapi/file_system_context.h" |
15 #include "webkit/fileapi/file_system_operation_context.h" | 15 #include "webkit/fileapi/file_system_operation_context.h" |
16 #include "webkit/fileapi/file_system_task_runners.h" | 16 #include "webkit/fileapi/file_system_task_runners.h" |
17 #include "webkit/fileapi/file_system_url.h" | 17 #include "webkit/fileapi/file_system_url.h" |
18 #include "webkit/fileapi/file_system_util.h" | 18 #include "webkit/fileapi/file_system_util.h" |
19 #include "webkit/fileapi/local_file_system_operation.h" | 19 #include "webkit/fileapi/local_file_system_operation.h" |
20 #include "webkit/fileapi/mock_file_system_context.h" | 20 #include "webkit/fileapi/mock_file_system_context.h" |
21 #include "webkit/quota/mock_special_storage_policy.h" | 21 #include "webkit/quota/mock_special_storage_policy.h" |
22 | 22 |
23 namespace fileapi { | 23 namespace fileapi { |
24 | 24 |
25 LocalFileSystemTestOriginHelper::LocalFileSystemTestOriginHelper( | 25 SandboxFileSystemTestHelper::SandboxFileSystemTestHelper( |
26 const GURL& origin, FileSystemType type) | 26 const GURL& origin, FileSystemType type) |
27 : origin_(origin), type_(type), file_util_(NULL) { | 27 : origin_(origin), type_(type), file_util_(NULL) { |
28 } | 28 } |
29 | 29 |
30 LocalFileSystemTestOriginHelper::LocalFileSystemTestOriginHelper() | 30 SandboxFileSystemTestHelper::SandboxFileSystemTestHelper() |
31 : origin_(GURL("http://foo.com")), | 31 : origin_(GURL("http://foo.com")), |
32 type_(kFileSystemTypeTemporary), | 32 type_(kFileSystemTypeTemporary), |
33 file_util_(NULL) { | 33 file_util_(NULL) { |
34 } | 34 } |
35 | 35 |
36 LocalFileSystemTestOriginHelper::~LocalFileSystemTestOriginHelper() { | 36 SandboxFileSystemTestHelper::~SandboxFileSystemTestHelper() { |
37 } | 37 } |
38 | 38 |
39 void LocalFileSystemTestOriginHelper::SetUp(const base::FilePath& base_dir) { | 39 void SandboxFileSystemTestHelper::SetUp(const base::FilePath& base_dir) { |
40 SetUp(base_dir, NULL); | 40 SetUp(base_dir, NULL); |
41 } | 41 } |
42 | 42 |
43 void LocalFileSystemTestOriginHelper::SetUp( | 43 void SandboxFileSystemTestHelper::SetUp( |
44 FileSystemContext* file_system_context) { | 44 FileSystemContext* file_system_context) { |
45 file_system_context_ = file_system_context; | 45 file_system_context_ = file_system_context; |
46 | 46 |
47 SetUpFileUtil(); | 47 SetUpFileSystem(); |
48 | |
49 // Prepare the origin's root directory. | |
50 file_system_context_->GetMountPointProvider(type_)-> | |
51 GetFileSystemRootPathOnFileThread(CreateURL(base::FilePath()), | |
52 true /* create */); | |
53 | |
54 // Initialize the usage cache file. | |
55 base::FilePath usage_cache_path = GetUsageCachePath(); | |
56 if (!usage_cache_path.empty()) | |
57 usage_cache()->UpdateUsage(usage_cache_path, 0); | |
58 } | 48 } |
59 | 49 |
60 void LocalFileSystemTestOriginHelper::SetUp( | 50 void SandboxFileSystemTestHelper::SetUp( |
61 const base::FilePath& base_dir, | 51 const base::FilePath& base_dir, |
62 quota::QuotaManagerProxy* quota_manager_proxy) { | 52 quota::QuotaManagerProxy* quota_manager_proxy) { |
63 file_system_context_ = CreateFileSystemContextForTesting( | 53 file_system_context_ = CreateFileSystemContextForTesting( |
64 quota_manager_proxy, base_dir); | 54 quota_manager_proxy, base_dir); |
65 | 55 |
66 SetUpFileUtil(); | 56 SetUpFileSystem(); |
67 | |
68 // Prepare the origin's root directory. | |
69 FileSystemMountPointProvider* mount_point_provider = | |
70 file_system_context_->GetMountPointProvider(type_); | |
71 mount_point_provider->GetFileSystemRootPathOnFileThread( | |
72 CreateURL(base::FilePath()), true /* create */); | |
73 | |
74 // Initialize the usage cache file. | |
75 base::FilePath usage_cache_path = GetUsageCachePath(); | |
76 if (!usage_cache_path.empty()) | |
77 usage_cache()->UpdateUsage(usage_cache_path, 0); | |
78 } | 57 } |
79 | 58 |
80 void LocalFileSystemTestOriginHelper::TearDown() { | 59 void SandboxFileSystemTestHelper::TearDown() { |
81 file_system_context_ = NULL; | 60 file_system_context_ = NULL; |
82 base::MessageLoop::current()->RunUntilIdle(); | 61 base::MessageLoop::current()->RunUntilIdle(); |
83 } | 62 } |
84 | 63 |
85 base::FilePath LocalFileSystemTestOriginHelper::GetOriginRootPath() const { | 64 base::FilePath SandboxFileSystemTestHelper::GetOriginRootPath() const { |
86 return file_system_context_->GetMountPointProvider(type_)-> | 65 return file_system_context_->sandbox_provider()-> |
87 GetFileSystemRootPathOnFileThread(CreateURL(base::FilePath()), false); | 66 GetBaseDirectoryForOriginAndType(origin_, type_, false); |
88 } | 67 } |
89 | 68 |
90 base::FilePath LocalFileSystemTestOriginHelper::GetLocalPath( | 69 base::FilePath SandboxFileSystemTestHelper::GetLocalPath( |
91 const base::FilePath& path) { | 70 const base::FilePath& path) { |
92 DCHECK(file_util_); | 71 DCHECK(file_util_); |
93 base::FilePath local_path; | 72 base::FilePath local_path; |
94 scoped_ptr<FileSystemOperationContext> context(NewOperationContext()); | 73 scoped_ptr<FileSystemOperationContext> context(NewOperationContext()); |
95 file_util_->GetLocalFilePath(context.get(), CreateURL(path), &local_path); | 74 file_util_->GetLocalFilePath(context.get(), CreateURL(path), &local_path); |
96 return local_path; | 75 return local_path; |
97 } | 76 } |
98 | 77 |
99 base::FilePath LocalFileSystemTestOriginHelper::GetLocalPathFromASCII( | 78 base::FilePath SandboxFileSystemTestHelper::GetLocalPathFromASCII( |
100 const std::string& path) { | 79 const std::string& path) { |
101 return GetLocalPath(base::FilePath().AppendASCII(path)); | 80 return GetLocalPath(base::FilePath().AppendASCII(path)); |
102 } | 81 } |
103 | 82 |
104 base::FilePath LocalFileSystemTestOriginHelper::GetUsageCachePath() const { | 83 base::FilePath SandboxFileSystemTestHelper::GetUsageCachePath() const { |
105 if (type_ != kFileSystemTypeTemporary && | |
106 type_ != kFileSystemTypePersistent && | |
107 type_ != kFileSystemTypeSyncable) | |
108 return base::FilePath(); | |
109 return file_system_context_-> | 84 return file_system_context_-> |
110 sandbox_provider()->GetUsageCachePathForOriginAndType(origin_, type_); | 85 sandbox_provider()->GetUsageCachePathForOriginAndType(origin_, type_); |
111 } | 86 } |
112 | 87 |
113 FileSystemURL LocalFileSystemTestOriginHelper::CreateURL( | 88 FileSystemURL SandboxFileSystemTestHelper::CreateURL( |
114 const base::FilePath& path) const { | 89 const base::FilePath& path) const { |
115 return file_system_context_->CreateCrackedFileSystemURL(origin_, type_, path); | 90 return file_system_context_->CreateCrackedFileSystemURL(origin_, type_, path); |
116 } | 91 } |
117 | 92 |
118 int64 LocalFileSystemTestOriginHelper::GetCachedOriginUsage() const { | 93 int64 SandboxFileSystemTestHelper::GetCachedOriginUsage() const { |
119 return file_system_context_->GetQuotaUtil(type_)->GetOriginUsageOnFileThread( | 94 return file_system_context_->GetQuotaUtil(type_)->GetOriginUsageOnFileThread( |
120 file_system_context_, origin_, type_); | 95 file_system_context_, origin_, type_); |
121 } | 96 } |
122 | 97 |
123 int64 LocalFileSystemTestOriginHelper::ComputeCurrentOriginUsage() { | 98 int64 SandboxFileSystemTestHelper::ComputeCurrentOriginUsage() { |
124 usage_cache()->CloseCacheFiles(); | 99 usage_cache()->CloseCacheFiles(); |
125 int64 size = file_util::ComputeDirectorySize(GetOriginRootPath()); | 100 int64 size = file_util::ComputeDirectorySize(GetOriginRootPath()); |
126 if (file_util::PathExists(GetUsageCachePath())) | 101 if (file_util::PathExists(GetUsageCachePath())) |
127 size -= FileSystemUsageCache::kUsageFileSize; | 102 size -= FileSystemUsageCache::kUsageFileSize; |
128 return size; | 103 return size; |
129 } | 104 } |
130 | 105 |
131 int64 | 106 int64 |
132 LocalFileSystemTestOriginHelper::ComputeCurrentDirectoryDatabaseUsage() const { | 107 SandboxFileSystemTestHelper::ComputeCurrentDirectoryDatabaseUsage() const { |
133 return file_util::ComputeDirectorySize( | 108 return file_util::ComputeDirectorySize( |
134 GetOriginRootPath().AppendASCII("Paths")); | 109 GetOriginRootPath().AppendASCII("Paths")); |
135 } | 110 } |
136 | 111 |
137 LocalFileSystemOperation* LocalFileSystemTestOriginHelper::NewOperation() { | 112 LocalFileSystemOperation* SandboxFileSystemTestHelper::NewOperation() { |
138 DCHECK(file_system_context_.get()); | 113 DCHECK(file_system_context_.get()); |
139 DCHECK(file_util_); | 114 DCHECK(file_util_); |
140 scoped_ptr<FileSystemOperationContext> operation_context( | |
141 NewOperationContext()); | |
142 LocalFileSystemOperation* operation = static_cast<LocalFileSystemOperation*>( | 115 LocalFileSystemOperation* operation = static_cast<LocalFileSystemOperation*>( |
143 file_system_context_->CreateFileSystemOperation( | 116 file_system_context_->CreateFileSystemOperation( |
144 CreateURL(base::FilePath()), NULL)); | 117 CreateURL(base::FilePath()), NULL)); |
145 return operation; | 118 return operation; |
146 } | 119 } |
147 | 120 |
148 FileSystemOperationContext* | 121 FileSystemOperationContext* |
149 LocalFileSystemTestOriginHelper::NewOperationContext() { | 122 SandboxFileSystemTestHelper::NewOperationContext() { |
150 DCHECK(file_system_context_.get()); | 123 DCHECK(file_system_context_.get()); |
151 FileSystemOperationContext* context = | 124 FileSystemOperationContext* context = |
152 new FileSystemOperationContext(file_system_context_.get()); | 125 new FileSystemOperationContext(file_system_context_.get()); |
153 context->set_update_observers( | 126 context->set_update_observers( |
154 *file_system_context_->GetUpdateObservers(type_)); | 127 *file_system_context_->GetUpdateObservers(type_)); |
155 return context; | 128 return context; |
156 } | 129 } |
157 | 130 |
158 FileSystemUsageCache* LocalFileSystemTestOriginHelper::usage_cache() { | 131 FileSystemUsageCache* SandboxFileSystemTestHelper::usage_cache() { |
159 return file_system_context()->sandbox_provider()->usage_cache(); | 132 return file_system_context()->sandbox_provider()->usage_cache(); |
160 } | 133 } |
161 | 134 |
162 void LocalFileSystemTestOriginHelper::SetUpFileUtil() { | 135 void SandboxFileSystemTestHelper::SetUpFileSystem() { |
163 DCHECK(file_system_context_); | 136 DCHECK(file_system_context_); |
| 137 DCHECK(file_system_context_->sandbox_provider()->CanHandleType(type_)); |
| 138 |
164 file_util_ = file_system_context_->GetFileUtil(type_); | 139 file_util_ = file_system_context_->GetFileUtil(type_); |
165 DCHECK(file_util_); | 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); |
166 } | 151 } |
167 | 152 |
168 } // namespace fileapi | 153 } // namespace fileapi |
OLD | NEW |