12a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)// Copyright (c) 2013 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)
51320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci#ifndef STORAGE_BROWSER_FILEAPI_ASYNC_FILE_UTIL_H_
61320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci#define STORAGE_BROWSER_FILEAPI_ASYNC_FILE_UTIL_H_
72a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
84e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)#include <vector>
94e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)
102a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "base/basictypes.h"
112a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "base/callback_forward.h"
125d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)#include "base/files/file.h"
132a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "base/files/file_util_proxy.h"
14868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)#include "base/memory/scoped_ptr.h"
151320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci#include "storage/browser/fileapi/file_system_operation.h"
161320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci#include "storage/browser/storage_browser_export.h"
171320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci#include "storage/common/fileapi/directory_entry.h"
182a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
192a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)namespace base {
202a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)class Time;
212a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
222a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
2303b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)namespace storage {
24c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)class ShareableFileReference;
25c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)}
26c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
2703b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)namespace storage {
282a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
292a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)class FileSystemOperationContext;
302a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)class FileSystemURL;
312a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
322a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)// An interface which provides filesystem-specific file operations for
33a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)// FileSystemOperationImpl.
342a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)//
35a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)// Each filesystem which needs to be dispatched from FileSystemOperationImpl
362a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)// must implement this interface or a synchronous version of interface:
372a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)// FileSystemFileUtil.
382a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)//
397dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch// As far as an instance of this class is owned by a FileSystemBackend
40868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)// (which is owned by FileSystemContext), it's guaranteed that this instance's
41868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)// alive while FileSystemOperationContext given to each operation is kept
42868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)// alive. (Note that this instance might be freed on different thread
43868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)// from the thread it is created.)
447dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch//
457dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch// It is NOT valid to give null callback to this class, and implementors
467dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch// can assume that they don't get any null callbacks.
477dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch//
48d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)class AsyncFileUtil {
492a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) public:
505d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  typedef base::Callback<void(base::File::Error result)> StatusCallback;
512a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
527dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch  // |on_close_callback| will be called after the |file| is closed in the
537dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch  // child process. |on_close_callback|.is_null() can be true, if no operation
547dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch  // is needed on closing the file.
55eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch  typedef base::Callback<
56cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)      void(base::File file,
577dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch           const base::Closure& on_close_callback)> CreateOrOpenCallback;
582a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
592a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  typedef base::Callback<
605d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      void(base::File::Error result,
612a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)           bool created)> EnsureFileExistsCallback;
622a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
632a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  typedef base::Callback<
645d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      void(base::File::Error result,
655d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)           const base::File::Info& file_info)> GetFileInfoCallback;
662a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
6790dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  typedef std::vector<DirectoryEntry> EntryList;
682a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  typedef base::Callback<
695d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      void(base::File::Error result,
702a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)           const EntryList& file_list,
712a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)           bool has_more)> ReadDirectoryCallback;
722a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
732a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  typedef base::Callback<
745d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      void(base::File::Error result,
755d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)           const base::File::Info& file_info,
762a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)           const base::FilePath& platform_path,
7703b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)           const scoped_refptr<storage::ShareableFileReference>& file_ref)>
784e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)      CreateSnapshotFileCallback;
792a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
8058537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  typedef base::Callback<void(int64 size)> CopyFileProgressCallback;
8158537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)
8268043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)  typedef FileSystemOperation::CopyOrMoveOption CopyOrMoveOption;
8368043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)
84d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)  // Creates an AsyncFileUtil instance which performs file operations on
85d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)  // local native file system. The created instance assumes
86d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)  // FileSystemURL::path() has the target platform path.
871320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  STORAGE_EXPORT static AsyncFileUtil*
88d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)      CreateForLocalFileSystem();
89d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)
902a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  AsyncFileUtil() {}
912a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  virtual ~AsyncFileUtil() {}
922a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
932a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // Creates or opens a file with the given flags.
94cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  // If File::FLAG_CREATE is set in |file_flags| it always tries to create
952a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // a new file at the given |url| and calls back with
96cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  // File::FILE_ERROR_FILE_EXISTS if the |url| already exists.
972a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  //
98a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)  // FileSystemOperationImpl::OpenFile calls this.
990f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)  // This is used only by Pepper/NaCl File API.
1002a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  //
1017dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch  virtual void CreateOrOpen(
102868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)      scoped_ptr<FileSystemOperationContext> context,
1032a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      const FileSystemURL& url,
1042a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      int file_flags,
1052a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      const CreateOrOpenCallback& callback) = 0;
1062a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
1072a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // Ensures that the given |url| exist.  This creates a empty new file
1082a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // at |url| if the |url| does not exist.
1092a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  //
110a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)  // FileSystemOperationImpl::CreateFile calls this.
1112a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  //
1122a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // This reports following error code via |callback|:
113cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  // - File::FILE_OK and created==true if a file has not existed and
1142a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  //   is created at |url|.
115cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  // - File::FILE_OK and created==false if the file already exists.
1162a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // - Other error code (with created=false) if a file hasn't existed yet
1172a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  //   and there was an error while creating a new file.
1182a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  //
1197dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch  virtual void EnsureFileExists(
120868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)      scoped_ptr<FileSystemOperationContext> context,
1212a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      const FileSystemURL& url,
1222a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      const EnsureFileExistsCallback& callback) = 0;
1232a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
1242a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // Creates directory at given url.
1252a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  //
126a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)  // FileSystemOperationImpl::CreateDirectory calls this.
1272a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  //
1282a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // This reports following error code via |callback|:
129cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  // - File::FILE_ERROR_NOT_FOUND if the |url|'s parent directory
1302a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  //   does not exist and |recursive| is false.
131cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  // - File::FILE_ERROR_EXISTS if a directory already exists at |url|
1322a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  //   and |exclusive| is true.
133cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  // - File::FILE_ERROR_EXISTS if a file already exists at |url|
1342a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  //   (regardless of |exclusive| value).
1352a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // - Other error code if it failed to create a directory.
1362a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  //
1377dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch  virtual void CreateDirectory(
138868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)      scoped_ptr<FileSystemOperationContext> context,
1392a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      const FileSystemURL& url,
1402a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      bool exclusive,
1412a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      bool recursive,
1422a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      const StatusCallback& callback) = 0;
1432a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
1442a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // Retrieves the information about a file.
1452a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  //
146a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)  // FileSystemOperationImpl::GetMetadata calls this.
1472a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  //
1482a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // This reports following error code via |callback|:
149cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  // - File::FILE_ERROR_NOT_FOUND if the file doesn't exist.
1502a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // - Other error code if there was an error while retrieving the file info.
1512a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  //
1527dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch  virtual void GetFileInfo(
153868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)      scoped_ptr<FileSystemOperationContext> context,
1542a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      const FileSystemURL& url,
1552a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      const GetFileInfoCallback& callback) = 0;
1562a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
1572a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // Reads contents of a directory at |path|.
1582a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  //
159a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)  // FileSystemOperationImpl::ReadDirectory calls this.
1602a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  //
161c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  // Note that the |name| field of each entry in |file_list|
162c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  // returned by |callback| should have a base file name
163c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  // of the entry relative to the directory, but not an absolute path.
164c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  //
165c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  // (E.g. if ReadDirectory is called for a directory
166c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  // 'path/to/dir' and the directory has entries 'a' and 'b',
167c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  // the returned |file_list| should include entries whose names
168c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  // are 'a' and 'b', but not '/path/to/dir/a' and '/path/to/dir/b'.)
169c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  //
1702a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // This reports following error code via |callback|:
171cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  // - File::FILE_ERROR_NOT_FOUND if the target directory doesn't exist.
172cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  // - File::FILE_ERROR_NOT_A_DIRECTORY if an entry exists at |url| but
1732a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  //   is a file (not a directory).
1742a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  //
1757dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch  virtual void ReadDirectory(
176868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)      scoped_ptr<FileSystemOperationContext> context,
1772a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      const FileSystemURL& url,
1782a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      const ReadDirectoryCallback& callback) = 0;
1792a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
1802a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // Modifies timestamps of a file or directory at |url| with
1812a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // |last_access_time| and |last_modified_time|. The function DOES NOT
1822a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // create a file unlike 'touch' command on Linux.
1832a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  //
184a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)  // FileSystemOperationImpl::TouchFile calls this.
1850f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)  // This is used only by Pepper/NaCl File API.
1862a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  //
1877dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch  virtual void Touch(
188868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)      scoped_ptr<FileSystemOperationContext> context,
1892a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      const FileSystemURL& url,
1902a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      const base::Time& last_access_time,
1912a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      const base::Time& last_modified_time,
1922a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      const StatusCallback& callback) = 0;
1932a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
1942a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // Truncates a file at |path| to |length|. If |length| is larger than
1952a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // the original file size, the file will be extended, and the extended
1962a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // part is filled with null bytes.
1972a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  //
198a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)  // FileSystemOperationImpl::Truncate calls this.
1992a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  //
2002a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // This reports following error code via |callback|:
201cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  // - File::FILE_ERROR_NOT_FOUND if the file doesn't exist.
2022a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  //
2037dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch  virtual void Truncate(
204868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)      scoped_ptr<FileSystemOperationContext> context,
2052a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      const FileSystemURL& url,
2062a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      int64 length,
2072a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      const StatusCallback& callback) = 0;
2082a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
2092a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // Copies a file from |src_url| to |dest_url|.
2102a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // This must be called for files that belong to the same filesystem
2112a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // (i.e. type() and origin() of the |src_url| and |dest_url| must match).
21258537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  // |progress_callback| is a callback to report the progress update.
21358537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  // See file_system_operations.h for details. This should be called on the
21458537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  // same thread as where the method's called (IO thread). Calling this
21558537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  // is optional. It is recommended to use this callback for heavier operations
21658537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  // (such as file network downloading), so that, e.g., clients (UIs) can
21758537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  // update its state to show progress to users. This may be a null callback.
2182a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  //
219a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)  // FileSystemOperationImpl::Copy calls this for same-filesystem copy case.
2202a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  //
2212a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // This reports following error code via |callback|:
222cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  // - File::FILE_ERROR_NOT_FOUND if |src_url|
2232a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  //   or the parent directory of |dest_url| does not exist.
224cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  // - File::FILE_ERROR_NOT_A_FILE if |src_url| exists but is not a file.
225cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  // - File::FILE_ERROR_INVALID_OPERATION if |dest_url| exists and
2262a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  //   is not a file.
227cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  // - File::FILE_ERROR_FAILED if |dest_url| does not exist and
2282a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  //   its parent path is a file.
2292a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  //
2307dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch  virtual void CopyFileLocal(
231868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)      scoped_ptr<FileSystemOperationContext> context,
2322a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      const FileSystemURL& src_url,
2332a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      const FileSystemURL& dest_url,
23468043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)      CopyOrMoveOption option,
23558537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)      const CopyFileProgressCallback& progress_callback,
2362a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      const StatusCallback& callback) = 0;
2372a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
2382a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // Moves a local file from |src_url| to |dest_url|.
2392a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // This must be called for files that belong to the same filesystem
2402a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // (i.e. type() and origin() of the |src_url| and |dest_url| must match).
2412a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  //
242a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)  // FileSystemOperationImpl::Move calls this for same-filesystem move case.
2432a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  //
2442a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // This reports following error code via |callback|:
245cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  // - File::FILE_ERROR_NOT_FOUND if |src_url|
2462a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  //   or the parent directory of |dest_url| does not exist.
247cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  // - File::FILE_ERROR_NOT_A_FILE if |src_url| exists but is not a file.
248cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  // - File::FILE_ERROR_INVALID_OPERATION if |dest_url| exists and
2492a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  //   is not a file.
250cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  // - File::FILE_ERROR_FAILED if |dest_url| does not exist and
2512a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  //   its parent path is a file.
2522a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  //
2537dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch  virtual void MoveFileLocal(
254868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)      scoped_ptr<FileSystemOperationContext> context,
2552a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      const FileSystemURL& src_url,
2562a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      const FileSystemURL& dest_url,
25768043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)      CopyOrMoveOption option,
2582a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      const StatusCallback& callback) = 0;
2592a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
2602a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // Copies in a single file from a different filesystem.
2612a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  //
262a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)  // FileSystemOperationImpl::Copy or Move calls this for cross-filesystem
2632a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // cases.
2642a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  //
2652a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // This reports following error code via |callback|:
266cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  // - File::FILE_ERROR_NOT_FOUND if |src_file_path|
2672a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  //   or the parent directory of |dest_url| does not exist.
268cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  // - File::FILE_ERROR_INVALID_OPERATION if |dest_url| exists and
2692a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  //   is not a file.
270cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  // - File::FILE_ERROR_FAILED if |dest_url| does not exist and
2712a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  //   its parent path is a file.
2722a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  //
2737dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch  virtual void CopyInForeignFile(
274868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)        scoped_ptr<FileSystemOperationContext> context,
2752a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)        const base::FilePath& src_file_path,
2762a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)        const FileSystemURL& dest_url,
2772a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)        const StatusCallback& callback) = 0;
2782a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
2792a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // Deletes a single file.
2802a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  //
281a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)  // FileSystemOperationImpl::RemoveFile calls this.
2822a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  //
2832a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // This reports following error code via |callback|:
284cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  // - File::FILE_ERROR_NOT_FOUND if |url| does not exist.
285cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  // - File::FILE_ERROR_NOT_A_FILE if |url| is not a file.
2862a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  //
2877dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch  virtual void DeleteFile(
288868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)      scoped_ptr<FileSystemOperationContext> context,
2892a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      const FileSystemURL& url,
2902a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      const StatusCallback& callback) = 0;
2912a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
2922a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // Removes a single empty directory.
2932a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  //
294a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)  // FileSystemOperationImpl::RemoveDirectory calls this.
2952a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  //
2962a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // This reports following error code via |callback|:
297cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  // - File::FILE_ERROR_NOT_FOUND if |url| does not exist.
298cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  // - File::FILE_ERROR_NOT_A_DIRECTORY if |url| is not a directory.
299cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  // - File::FILE_ERROR_NOT_EMPTY if |url| is not empty.
3002a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  //
3017dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch  virtual void DeleteDirectory(
302868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)      scoped_ptr<FileSystemOperationContext> context,
3032a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      const FileSystemURL& url,
3042a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      const StatusCallback& callback) = 0;
3052a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
306eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch  // Removes a single file or a single directory with its contents
307eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch  // (i.e. files/subdirectories under the directory).
308eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch  //
309a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)  // FileSystemOperationImpl::Remove calls this.
310eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch  // On some platforms, such as Chrome OS Drive File System, recursive file
311eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch  // deletion can be implemented more efficiently than calling DeleteFile() and
312eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch  // DeleteDirectory() for each files/directories.
313eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch  // This method is optional, so if not supported,
314cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  // File::FILE_ERROR_INVALID_OPERATION should be returned via |callback|.
315eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch  //
316eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch  // This reports following error code via |callback|:
317cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  // - File::FILE_ERROR_NOT_FOUND if |url| does not exist.
318cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  // - File::FILE_ERROR_INVALID_OPERATION if this operation is not supported.
3197dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch  virtual void DeleteRecursively(
320eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch      scoped_ptr<FileSystemOperationContext> context,
321eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch      const FileSystemURL& url,
322eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch      const StatusCallback& callback) = 0;
323eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch
3242a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // Creates a local snapshot file for a given |url| and returns the
3252a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // metadata and platform path of the snapshot file via |callback|.
3262a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // In regular filesystem cases the implementation may simply return
3272a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // the metadata of the file itself (as well as GetMetadata does),
3282a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // while in non-regular filesystem case the backend may create a
3292a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // temporary snapshot file which holds the file data and return
3302a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // the metadata of the temporary file.
3312a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  //
3322a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // In the callback, it returns:
3332a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // |file_info| is the metadata of the snapshot file created.
334c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  // |platform_path| is the full absolute platform path to the snapshot
335c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  // file created.  If a file is not backed by a real local file in
336c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  // the implementor's FileSystem, the implementor must create a
337c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  // local snapshot file and return the path of the created file.
338c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  //
339c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  // If implementors creates a temporary file for snapshotting and wants
340c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  // FileAPI backend to take care of the lifetime of the file (so that
341c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  // it won't get deleted while JS layer has any references to the created
342c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  // File/Blob object), it should return non-empty |file_ref|.
343c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  // Via the |file_ref| implementors can schedule a file deletion
344c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  // or arbitrary callbacks when the last reference of File/Blob is dropped.
3452a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  //
346a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)  // FileSystemOperationImpl::CreateSnapshotFile calls this.
3472a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  //
3482a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // This reports following error code via |callback|:
349cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  // - File::FILE_ERROR_NOT_FOUND if |url| does not exist.
350cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  // - File::FILE_ERROR_NOT_A_FILE if |url| exists but is a directory.
3512a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  //
3522a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // The field values of |file_info| are undefined (implementation
3532a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // dependent) in error cases, and the caller should always
3542a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // check the return code.
3557dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch  virtual void CreateSnapshotFile(
356868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)      scoped_ptr<FileSystemOperationContext> context,
3572a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      const FileSystemURL& url,
3582a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      const CreateSnapshotFileCallback& callback) = 0;
3592a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
3602a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) private:
3612a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  DISALLOW_COPY_AND_ASSIGN(AsyncFileUtil);
3622a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)};
3632a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
36403b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)}  // namespace storage
3652a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
3661320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci#endif  // STORAGE_BROWSER_FILEAPI_ASYNC_FILE_UTIL_H_
367