15d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)// Copyright 2014 The Chromium Authors. All rights reserved.
22a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)// Use of this source code is governed by a BSD-style license that can be
32a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)// found in the LICENSE file.
42a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
52a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "base/bind.h"
61320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci#include "base/files/file_util.h"
73551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)#include "base/files/scoped_temp_dir.h"
82a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "base/run_loop.h"
95d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)#include "content/public/test/async_file_test_helper.h"
101320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci#include "storage/browser/fileapi/file_system_backend.h"
111320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci#include "storage/browser/fileapi/file_system_context.h"
121320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci#include "storage/browser/fileapi/file_system_operation_runner.h"
131320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci#include "storage/browser/fileapi/file_system_url.h"
141320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci#include "storage/browser/quota/quota_manager.h"
151320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci#include "storage/common/fileapi/file_system_util.h"
162a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "testing/gtest/include/gtest/gtest.h"
172a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
185d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)namespace content {
192a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
2003b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)typedef storage::FileSystemOperation::FileEntryList FileEntryList;
212a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
22a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)namespace {
23a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)
242a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)void AssignAndQuit(base::RunLoop* run_loop,
255d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)                   base::File::Error* result_out,
265d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)                   base::File::Error result) {
272a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  *result_out = result;
282a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  run_loop->Quit();
292a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
302a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
315d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)base::Callback<void(base::File::Error)>
322a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)AssignAndQuitCallback(base::RunLoop* run_loop,
335d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)                      base::File::Error* result) {
342a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  return base::Bind(&AssignAndQuit, run_loop, base::Unretained(result));
352a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
362a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
372a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)void GetMetadataCallback(base::RunLoop* run_loop,
385d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)                         base::File::Error* result_out,
395d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)                         base::File::Info* file_info_out,
405d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)                         base::File::Error result,
415d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)                         const base::File::Info& file_info) {
422a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  *result_out = result;
432a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  if (file_info_out)
442a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    *file_info_out = file_info;
457d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)  run_loop->Quit();
467d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)}
477d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)
487d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)void CreateSnapshotFileCallback(
497d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)    base::RunLoop* run_loop,
505d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    base::File::Error* result_out,
517d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)    base::FilePath* platform_path_out,
525d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    base::File::Error result,
535d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    const base::File::Info& file_info,
547d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)    const base::FilePath& platform_path,
5503b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)    const scoped_refptr<storage::ShareableFileReference>& file_ref) {
56eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch  DCHECK(!file_ref.get());
577d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)  *result_out = result;
582a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  if (platform_path_out)
592a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    *platform_path_out = platform_path;
602a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  run_loop->Quit();
612a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
622a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
632a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)void ReadDirectoryCallback(base::RunLoop* run_loop,
645d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)                           base::File::Error* result_out,
652a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)                           FileEntryList* entries_out,
665d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)                           base::File::Error result,
672a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)                           const FileEntryList& entries,
682a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)                           bool has_more) {
692a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  *result_out = result;
70a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  entries_out->insert(entries_out->end(), entries.begin(), entries.end());
715d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  if (result != base::File::FILE_OK || !has_more)
722a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    run_loop->Quit();
732a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
742a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
7503b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)void DidGetUsageAndQuota(storage::QuotaStatusCode* status_out,
762a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)                         int64* usage_out,
772a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)                         int64* quota_out,
7803b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)                         storage::QuotaStatusCode status,
792a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)                         int64 usage,
802a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)                         int64 quota) {
812a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  if (status_out)
822a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    *status_out = status;
832a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  if (usage_out)
842a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    *usage_out = usage;
852a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  if (quota_out)
862a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    *quota_out = quota;
872a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
882a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
89a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)}  // namespace
90a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)
912a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)const int64 AsyncFileTestHelper::kDontCheckSize = -1;
922a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
935d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)base::File::Error AsyncFileTestHelper::Copy(
9403b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)    storage::FileSystemContext* context,
9503b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)    const storage::FileSystemURL& src,
9603b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)    const storage::FileSystemURL& dest) {
97d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)  return CopyWithProgress(context, src, dest, CopyProgressCallback());
98d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)}
99d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)
1005d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)base::File::Error AsyncFileTestHelper::CopyWithProgress(
10103b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)    storage::FileSystemContext* context,
10203b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)    const storage::FileSystemURL& src,
10303b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)    const storage::FileSystemURL& dest,
104d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)    const CopyProgressCallback& progress_callback) {
1055d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  base::File::Error result = base::File::FILE_ERROR_FAILED;
1062a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  base::RunLoop run_loop;
10703b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)  context->operation_runner()->Copy(src,
10803b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)                                    dest,
10903b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)                                    storage::FileSystemOperation::OPTION_NONE,
11003b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)                                    progress_callback,
11103b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)                                    AssignAndQuitCallback(&run_loop, &result));
1122a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  run_loop.Run();
1132a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  return result;
1142a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
1152a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
1165d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)base::File::Error AsyncFileTestHelper::Move(
11703b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)    storage::FileSystemContext* context,
11803b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)    const storage::FileSystemURL& src,
11903b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)    const storage::FileSystemURL& dest) {
1205d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  base::File::Error result = base::File::FILE_ERROR_FAILED;
1212a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  base::RunLoop run_loop;
12203b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)  context->operation_runner()->Move(src,
12303b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)                                    dest,
12403b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)                                    storage::FileSystemOperation::OPTION_NONE,
12503b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)                                    AssignAndQuitCallback(&run_loop, &result));
1262a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  run_loop.Run();
1272a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  return result;
1282a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
1292a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
1305d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)base::File::Error AsyncFileTestHelper::Remove(
13103b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)    storage::FileSystemContext* context,
13203b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)    const storage::FileSystemURL& url,
1332a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    bool recursive) {
1345d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  base::File::Error result = base::File::FILE_ERROR_FAILED;
1352a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  base::RunLoop run_loop;
136868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  context->operation_runner()->Remove(
137868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)      url, recursive, AssignAndQuitCallback(&run_loop, &result));
1382a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  run_loop.Run();
1392a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  return result;
1402a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
1412a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
1425d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)base::File::Error AsyncFileTestHelper::ReadDirectory(
14303b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)    storage::FileSystemContext* context,
14403b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)    const storage::FileSystemURL& url,
1452a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    FileEntryList* entries) {
1465d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  base::File::Error result = base::File::FILE_ERROR_FAILED;
1472a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  DCHECK(entries);
1482a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  entries->clear();
1492a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  base::RunLoop run_loop;
150868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  context->operation_runner()->ReadDirectory(
1512a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      url, base::Bind(&ReadDirectoryCallback, &run_loop, &result, entries));
1522a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  run_loop.Run();
1532a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  return result;
1542a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
1552a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
1565d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)base::File::Error AsyncFileTestHelper::CreateDirectory(
15703b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)    storage::FileSystemContext* context,
15803b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)    const storage::FileSystemURL& url) {
1595d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  base::File::Error result = base::File::FILE_ERROR_FAILED;
1602a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  base::RunLoop run_loop;
161868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  context->operation_runner()->CreateDirectory(
162868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)      url,
163868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)      false /* exclusive */,
164868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)      false /* recursive */,
165868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)      AssignAndQuitCallback(&run_loop, &result));
1662a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  run_loop.Run();
1672a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  return result;
1682a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
1692a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
1705d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)base::File::Error AsyncFileTestHelper::CreateFile(
17103b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)    storage::FileSystemContext* context,
17203b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)    const storage::FileSystemURL& url) {
1735d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  base::File::Error result = base::File::FILE_ERROR_FAILED;
174868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  base::RunLoop run_loop;
175868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  context->operation_runner()->CreateFile(
176868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)      url, false /* exclusive */,
177868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)      AssignAndQuitCallback(&run_loop, &result));
1782a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  run_loop.Run();
1792a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  return result;
1802a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
1812a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
1825d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)base::File::Error AsyncFileTestHelper::CreateFileWithData(
18303b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)    storage::FileSystemContext* context,
18403b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)    const storage::FileSystemURL& url,
1853551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)    const char* buf,
1863551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)    int buf_size) {
1873551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)  base::ScopedTempDir dir;
1883551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)  if (!dir.CreateUniqueTempDir())
1895d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    return base::File::FILE_ERROR_FAILED;
1903551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)  base::FilePath local_path = dir.path().AppendASCII("tmp");
191a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  if (buf_size != base::WriteFile(local_path, buf, buf_size))
1925d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    return base::File::FILE_ERROR_FAILED;
1935d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  base::File::Error result = base::File::FILE_ERROR_FAILED;
1943551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)  base::RunLoop run_loop;
1953551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)  context->operation_runner()->CopyInForeignFile(
1963551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)      local_path, url, AssignAndQuitCallback(&run_loop, &result));
1973551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)  run_loop.Run();
1983551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)  return result;
1993551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)}
2003551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)
2015d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)base::File::Error AsyncFileTestHelper::TruncateFile(
20203b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)    storage::FileSystemContext* context,
20303b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)    const storage::FileSystemURL& url,
2042a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    size_t size) {
2052a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  base::RunLoop run_loop;
2065d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  base::File::Error result = base::File::FILE_ERROR_FAILED;
207868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  context->operation_runner()->Truncate(
208868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)      url, size, AssignAndQuitCallback(&run_loop, &result));
2092a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  run_loop.Run();
2102a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  return result;
2112a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
2122a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
2135d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)base::File::Error AsyncFileTestHelper::GetMetadata(
21403b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)    storage::FileSystemContext* context,
21503b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)    const storage::FileSystemURL& url,
2165d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    base::File::Info* file_info) {
2175d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  base::File::Error result = base::File::FILE_ERROR_FAILED;
2182a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  base::RunLoop run_loop;
219868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  context->operation_runner()->GetMetadata(
220868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)      url, base::Bind(&GetMetadataCallback, &run_loop, &result,
2217d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)                      file_info));
2227d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)  run_loop.Run();
2237d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)  return result;
2247d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)}
2257d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)
2265d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)base::File::Error AsyncFileTestHelper::GetPlatformPath(
22703b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)    storage::FileSystemContext* context,
22803b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)    const storage::FileSystemURL& url,
2297d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)    base::FilePath* platform_path) {
2305d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  base::File::Error result = base::File::FILE_ERROR_FAILED;
2317d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)  base::RunLoop run_loop;
2327d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)  context->operation_runner()->CreateSnapshotFile(
2337d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)      url, base::Bind(&CreateSnapshotFileCallback, &run_loop, &result,
2347d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)                      platform_path));
2352a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  run_loop.Run();
2362a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  return result;
2372a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
2382a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
23903b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)bool AsyncFileTestHelper::FileExists(storage::FileSystemContext* context,
24003b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)                                     const storage::FileSystemURL& url,
24103b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)                                     int64 expected_size) {
2425d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  base::File::Info file_info;
2435d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  base::File::Error result = GetMetadata(context, url, &file_info);
2445d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  if (result != base::File::FILE_OK || file_info.is_directory)
2452a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    return false;
2462a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  return expected_size == kDontCheckSize || file_info.size == expected_size;
2472a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
2482a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
24903b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)bool AsyncFileTestHelper::DirectoryExists(storage::FileSystemContext* context,
25003b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)                                          const storage::FileSystemURL& url) {
2515d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  base::File::Info file_info;
2525d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  base::File::Error result = GetMetadata(context, url, &file_info);
2535d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  return (result == base::File::FILE_OK) && file_info.is_directory;
2542a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
2552a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
25603b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)storage::QuotaStatusCode AsyncFileTestHelper::GetUsageAndQuota(
25703b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)    storage::QuotaManager* quota_manager,
2582a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    const GURL& origin,
25903b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)    storage::FileSystemType type,
2602a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    int64* usage,
2612a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    int64* quota) {
26203b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)  storage::QuotaStatusCode status = storage::kQuotaStatusUnknown;
2632a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  quota_manager->GetUsageAndQuota(
2642a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      origin,
2652a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      FileSystemTypeToQuotaStorageType(type),
2662a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      base::Bind(&DidGetUsageAndQuota, &status, usage, quota));
267d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)  base::RunLoop().RunUntilIdle();
2682a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  return status;
2692a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
2702a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
27103b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)}  // namespace storage
272