Content settings: remove some plugin-related code/resources when... there are no...
[chromium-blink-merge.git] / content / public / test / async_file_test_helper.cc
blob935e7ea1b29bb50a76c6f808fb5d9d34793f8404
1 // Copyright 2014 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.
5 #include "base/bind.h"
6 #include "base/files/file_util.h"
7 #include "base/files/scoped_temp_dir.h"
8 #include "base/run_loop.h"
9 #include "content/public/test/async_file_test_helper.h"
10 #include "storage/browser/fileapi/file_system_backend.h"
11 #include "storage/browser/fileapi/file_system_context.h"
12 #include "storage/browser/fileapi/file_system_operation_runner.h"
13 #include "storage/browser/fileapi/file_system_url.h"
14 #include "storage/browser/quota/quota_manager.h"
15 #include "storage/common/fileapi/file_system_util.h"
16 #include "testing/gtest/include/gtest/gtest.h"
18 namespace content {
20 typedef storage::FileSystemOperation::FileEntryList FileEntryList;
22 namespace {
24 void AssignAndQuit(base::RunLoop* run_loop,
25 base::File::Error* result_out,
26 base::File::Error result) {
27 *result_out = result;
28 run_loop->Quit();
31 base::Callback<void(base::File::Error)>
32 AssignAndQuitCallback(base::RunLoop* run_loop,
33 base::File::Error* result) {
34 return base::Bind(&AssignAndQuit, run_loop, base::Unretained(result));
37 void GetMetadataCallback(base::RunLoop* run_loop,
38 base::File::Error* result_out,
39 base::File::Info* file_info_out,
40 base::File::Error result,
41 const base::File::Info& file_info) {
42 *result_out = result;
43 if (file_info_out)
44 *file_info_out = file_info;
45 run_loop->Quit();
48 void CreateSnapshotFileCallback(
49 base::RunLoop* run_loop,
50 base::File::Error* result_out,
51 base::FilePath* platform_path_out,
52 base::File::Error result,
53 const base::File::Info& file_info,
54 const base::FilePath& platform_path,
55 const scoped_refptr<storage::ShareableFileReference>& file_ref) {
56 DCHECK(!file_ref.get());
57 *result_out = result;
58 if (platform_path_out)
59 *platform_path_out = platform_path;
60 run_loop->Quit();
63 void ReadDirectoryCallback(base::RunLoop* run_loop,
64 base::File::Error* result_out,
65 FileEntryList* entries_out,
66 base::File::Error result,
67 const FileEntryList& entries,
68 bool has_more) {
69 *result_out = result;
70 entries_out->insert(entries_out->end(), entries.begin(), entries.end());
71 if (result != base::File::FILE_OK || !has_more)
72 run_loop->Quit();
75 void DidGetUsageAndQuota(storage::QuotaStatusCode* status_out,
76 int64* usage_out,
77 int64* quota_out,
78 storage::QuotaStatusCode status,
79 int64 usage,
80 int64 quota) {
81 if (status_out)
82 *status_out = status;
83 if (usage_out)
84 *usage_out = usage;
85 if (quota_out)
86 *quota_out = quota;
89 } // namespace
91 const int64 AsyncFileTestHelper::kDontCheckSize = -1;
93 base::File::Error AsyncFileTestHelper::Copy(
94 storage::FileSystemContext* context,
95 const storage::FileSystemURL& src,
96 const storage::FileSystemURL& dest) {
97 return CopyWithProgress(context, src, dest, CopyProgressCallback());
100 base::File::Error AsyncFileTestHelper::CopyWithProgress(
101 storage::FileSystemContext* context,
102 const storage::FileSystemURL& src,
103 const storage::FileSystemURL& dest,
104 const CopyProgressCallback& progress_callback) {
105 base::File::Error result = base::File::FILE_ERROR_FAILED;
106 base::RunLoop run_loop;
107 context->operation_runner()->Copy(
108 src, dest, storage::FileSystemOperation::OPTION_NONE,
109 storage::FileSystemOperation::ERROR_BEHAVIOR_ABORT, progress_callback,
110 AssignAndQuitCallback(&run_loop, &result));
111 run_loop.Run();
112 return result;
115 base::File::Error AsyncFileTestHelper::Move(
116 storage::FileSystemContext* context,
117 const storage::FileSystemURL& src,
118 const storage::FileSystemURL& dest) {
119 base::File::Error result = base::File::FILE_ERROR_FAILED;
120 base::RunLoop run_loop;
121 context->operation_runner()->Move(src,
122 dest,
123 storage::FileSystemOperation::OPTION_NONE,
124 AssignAndQuitCallback(&run_loop, &result));
125 run_loop.Run();
126 return result;
129 base::File::Error AsyncFileTestHelper::Remove(
130 storage::FileSystemContext* context,
131 const storage::FileSystemURL& url,
132 bool recursive) {
133 base::File::Error result = base::File::FILE_ERROR_FAILED;
134 base::RunLoop run_loop;
135 context->operation_runner()->Remove(
136 url, recursive, AssignAndQuitCallback(&run_loop, &result));
137 run_loop.Run();
138 return result;
141 base::File::Error AsyncFileTestHelper::ReadDirectory(
142 storage::FileSystemContext* context,
143 const storage::FileSystemURL& url,
144 FileEntryList* entries) {
145 base::File::Error result = base::File::FILE_ERROR_FAILED;
146 DCHECK(entries);
147 entries->clear();
148 base::RunLoop run_loop;
149 context->operation_runner()->ReadDirectory(
150 url, base::Bind(&ReadDirectoryCallback, &run_loop, &result, entries));
151 run_loop.Run();
152 return result;
155 base::File::Error AsyncFileTestHelper::CreateDirectory(
156 storage::FileSystemContext* context,
157 const storage::FileSystemURL& url) {
158 base::File::Error result = base::File::FILE_ERROR_FAILED;
159 base::RunLoop run_loop;
160 context->operation_runner()->CreateDirectory(
161 url,
162 false /* exclusive */,
163 false /* recursive */,
164 AssignAndQuitCallback(&run_loop, &result));
165 run_loop.Run();
166 return result;
169 base::File::Error AsyncFileTestHelper::CreateFile(
170 storage::FileSystemContext* context,
171 const storage::FileSystemURL& url) {
172 base::File::Error result = base::File::FILE_ERROR_FAILED;
173 base::RunLoop run_loop;
174 context->operation_runner()->CreateFile(
175 url, false /* exclusive */,
176 AssignAndQuitCallback(&run_loop, &result));
177 run_loop.Run();
178 return result;
181 base::File::Error AsyncFileTestHelper::CreateFileWithData(
182 storage::FileSystemContext* context,
183 const storage::FileSystemURL& url,
184 const char* buf,
185 int buf_size) {
186 base::ScopedTempDir dir;
187 if (!dir.CreateUniqueTempDir())
188 return base::File::FILE_ERROR_FAILED;
189 base::FilePath local_path = dir.path().AppendASCII("tmp");
190 if (buf_size != base::WriteFile(local_path, buf, buf_size))
191 return base::File::FILE_ERROR_FAILED;
192 base::File::Error result = base::File::FILE_ERROR_FAILED;
193 base::RunLoop run_loop;
194 context->operation_runner()->CopyInForeignFile(
195 local_path, url, AssignAndQuitCallback(&run_loop, &result));
196 run_loop.Run();
197 return result;
200 base::File::Error AsyncFileTestHelper::TruncateFile(
201 storage::FileSystemContext* context,
202 const storage::FileSystemURL& url,
203 size_t size) {
204 base::RunLoop run_loop;
205 base::File::Error result = base::File::FILE_ERROR_FAILED;
206 context->operation_runner()->Truncate(
207 url, size, AssignAndQuitCallback(&run_loop, &result));
208 run_loop.Run();
209 return result;
212 base::File::Error AsyncFileTestHelper::GetMetadata(
213 storage::FileSystemContext* context,
214 const storage::FileSystemURL& url,
215 base::File::Info* file_info) {
216 base::File::Error result = base::File::FILE_ERROR_FAILED;
217 base::RunLoop run_loop;
218 context->operation_runner()->GetMetadata(
219 url, base::Bind(&GetMetadataCallback, &run_loop, &result,
220 file_info));
221 run_loop.Run();
222 return result;
225 base::File::Error AsyncFileTestHelper::GetPlatformPath(
226 storage::FileSystemContext* context,
227 const storage::FileSystemURL& url,
228 base::FilePath* platform_path) {
229 base::File::Error result = base::File::FILE_ERROR_FAILED;
230 base::RunLoop run_loop;
231 context->operation_runner()->CreateSnapshotFile(
232 url, base::Bind(&CreateSnapshotFileCallback, &run_loop, &result,
233 platform_path));
234 run_loop.Run();
235 return result;
238 bool AsyncFileTestHelper::FileExists(storage::FileSystemContext* context,
239 const storage::FileSystemURL& url,
240 int64 expected_size) {
241 base::File::Info file_info;
242 base::File::Error result = GetMetadata(context, url, &file_info);
243 if (result != base::File::FILE_OK || file_info.is_directory)
244 return false;
245 return expected_size == kDontCheckSize || file_info.size == expected_size;
248 bool AsyncFileTestHelper::DirectoryExists(storage::FileSystemContext* context,
249 const storage::FileSystemURL& url) {
250 base::File::Info file_info;
251 base::File::Error result = GetMetadata(context, url, &file_info);
252 return (result == base::File::FILE_OK) && file_info.is_directory;
255 storage::QuotaStatusCode AsyncFileTestHelper::GetUsageAndQuota(
256 storage::QuotaManager* quota_manager,
257 const GURL& origin,
258 storage::FileSystemType type,
259 int64* usage,
260 int64* quota) {
261 storage::QuotaStatusCode status = storage::kQuotaStatusUnknown;
262 quota_manager->GetUsageAndQuota(
263 origin,
264 FileSystemTypeToQuotaStorageType(type),
265 base::Bind(&DidGetUsageAndQuota, &status, usage, quota));
266 base::RunLoop().RunUntilIdle();
267 return status;
270 } // namespace storage