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
3// found in the LICENSE file.
4
5#ifndef STORAGE_BROWSER_FILEAPI_FILE_SYSTEM_FILE_UTIL_H_
6#define STORAGE_BROWSER_FILEAPI_FILE_SYSTEM_FILE_UTIL_H_
7
8#include "base/files/file.h"
9#include "base/files/file_path.h"
10#include "base/memory/scoped_ptr.h"
11#include "storage/browser/fileapi/file_system_operation.h"
12#include "storage/browser/storage_browser_export.h"
13#include "storage/common/blob/scoped_file.h"
14
15namespace base {
16class Time;
17}
18
19namespace storage {
20
21class FileSystemOperationContext;
22class FileSystemURL;
23
24// A file utility interface that provides basic file utility methods for
25// FileSystem API.
26//
27// Layering structure of the FileSystemFileUtil was split out.
28// See http://crbug.com/128136 if you need it.
29class STORAGE_EXPORT FileSystemFileUtil {
30 public:
31  typedef FileSystemOperation::CopyOrMoveOption CopyOrMoveOption;
32
33  // It will be implemented by each subclass such as FileSystemFileEnumerator.
34  class STORAGE_EXPORT AbstractFileEnumerator {
35   public:
36    virtual ~AbstractFileEnumerator() {}
37
38    // Returns an empty string if there are no more results.
39    virtual base::FilePath Next() = 0;
40
41    // These methods return metadata for the file most recently returned by
42    // Next(). If Next() has never been called, or if Next() most recently
43    // returned an empty string, then return the default values of 0,
44    // "null time", and false, respectively.
45    virtual int64 Size() = 0;
46    virtual base::Time LastModifiedTime() = 0;
47    virtual bool IsDirectory() = 0;
48  };
49
50  class STORAGE_EXPORT EmptyFileEnumerator
51      : public AbstractFileEnumerator {
52    virtual base::FilePath Next() OVERRIDE;
53    virtual int64 Size() OVERRIDE;
54    virtual base::Time LastModifiedTime() OVERRIDE;
55    virtual bool IsDirectory() OVERRIDE;
56  };
57
58  virtual ~FileSystemFileUtil() {}
59
60  // Creates or opens a file with the given flags.
61  // See header comments for AsyncFileUtil::CreateOrOpen() for more details.
62  // This is used only by Pepper/NaCl File API.
63  virtual base::File CreateOrOpen(
64      FileSystemOperationContext* context,
65      const FileSystemURL& url,
66      int file_flags) = 0;
67
68  // Ensures that the given |url| exist.  This creates a empty new file
69  // at |url| if the |url| does not exist.
70  // See header comments for AsyncFileUtil::EnsureFileExists() for more details.
71  virtual base::File::Error EnsureFileExists(
72      FileSystemOperationContext* context,
73      const FileSystemURL& url, bool* created) = 0;
74
75  // Creates directory at given url.
76  // See header comments for AsyncFileUtil::CreateDirectory() for more details.
77  virtual base::File::Error CreateDirectory(
78      FileSystemOperationContext* context,
79      const FileSystemURL& url,
80      bool exclusive,
81      bool recursive) = 0;
82
83  // Retrieves the information about a file.
84  // See header comments for AsyncFileUtil::GetFileInfo() for more details.
85  virtual base::File::Error GetFileInfo(
86      FileSystemOperationContext* context,
87      const FileSystemURL& url,
88      base::File::Info* file_info,
89      base::FilePath* platform_path) = 0;
90
91  // Returns a pointer to a new instance of AbstractFileEnumerator which is
92  // implemented for each FileSystemFileUtil subclass. The instance needs to be
93  // freed by the caller, and its lifetime should not extend past when the
94  // current call returns to the main FILE message loop.
95  //
96  // The supplied context must remain valid at least lifetime of the enumerator
97  // instance.
98  virtual scoped_ptr<AbstractFileEnumerator> CreateFileEnumerator(
99      FileSystemOperationContext* context,
100      const FileSystemURL& root_url) = 0;
101
102  // Maps |file_system_url| given |context| into |local_file_path|
103  // which represents physical file location on the host OS.
104  // This may not always make sense for all subclasses.
105  virtual base::File::Error GetLocalFilePath(
106      FileSystemOperationContext* context,
107      const FileSystemURL& file_system_url,
108      base::FilePath* local_file_path) = 0;
109
110  // Updates the file metadata information.
111  // See header comments for AsyncFileUtil::Touch() for more details.
112  virtual base::File::Error Touch(
113      FileSystemOperationContext* context,
114      const FileSystemURL& url,
115      const base::Time& last_access_time,
116      const base::Time& last_modified_time) = 0;
117
118  // Truncates a file to the given length.
119  // See header comments for AsyncFileUtil::Truncate() for more details.
120  virtual base::File::Error Truncate(
121      FileSystemOperationContext* context,
122      const FileSystemURL& url,
123      int64 length) = 0;
124
125  // Copies or moves a single file from |src_url| to |dest_url|.
126  // The filesystem type of |src_url| and |dest_url| MUST be same.
127  // For |option|, please see file_system_operation.h
128  //
129  // This returns:
130  // - File::FILE_ERROR_NOT_FOUND if |src_url|
131  //   or the parent directory of |dest_url| does not exist.
132  // - File::FILE_ERROR_NOT_A_FILE if |src_url| exists but is not a file.
133  // - File::FILE_ERROR_INVALID_OPERATION if |dest_url| exists and
134  //   is not a file.
135  // - File::FILE_ERROR_FAILED if |dest_url| does not exist and
136  //   its parent path is a file.
137  //
138  virtual base::File::Error CopyOrMoveFile(
139      FileSystemOperationContext* context,
140      const FileSystemURL& src_url,
141      const FileSystemURL& dest_url,
142      CopyOrMoveOption option,
143      bool copy) = 0;
144
145  // Copies in a single file from a different filesystem.
146  // See header comments for AsyncFileUtil::CopyInForeignFile() for
147  // more details.
148  virtual base::File::Error CopyInForeignFile(
149        FileSystemOperationContext* context,
150        const base::FilePath& src_file_path,
151        const FileSystemURL& dest_url) = 0;
152
153  // Deletes a single file.
154  // See header comments for AsyncFileUtil::DeleteFile() for more details.
155  virtual base::File::Error DeleteFile(
156      FileSystemOperationContext* context,
157      const FileSystemURL& url) = 0;
158
159  // Deletes a single empty directory.
160  // See header comments for AsyncFileUtil::DeleteDirectory() for more details.
161  virtual base::File::Error DeleteDirectory(
162      FileSystemOperationContext* context,
163      const FileSystemURL& url) = 0;
164
165  // Creates a local snapshot file for a given |url| and returns the
166  // metadata and platform path of the snapshot file via |callback|.
167  //
168  // See header comments for AsyncFileUtil::CreateSnapshotFile() for
169  // more details.
170  virtual storage::ScopedFile CreateSnapshotFile(
171      FileSystemOperationContext* context,
172      const FileSystemURL& url,
173      base::File::Error* error,
174      base::File::Info* file_info,
175      base::FilePath* platform_path) = 0;
176
177 protected:
178  FileSystemFileUtil() {}
179
180 private:
181  DISALLOW_COPY_AND_ASSIGN(FileSystemFileUtil);
182};
183
184}  // namespace storage
185
186#endif  // STORAGE_BROWSER_FILEAPI_FILE_SYSTEM_FILE_UTIL_H_
187