blob: dec06e392c7051b1360f34da37a00b4c58895deb [file] [log] [blame]
dumi@chromium.orgc980e402010-08-21 07:42:50 +09001// Copyright (c) 2010 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
kkanetkar@chromium.org3a3bdea2010-09-02 12:43:36 +09005#ifndef BASE_FILE_UTIL_PROXY_H_
6#define BASE_FILE_UTIL_PROXY_H_
7
8#include <vector>
dumi@chromium.orgc980e402010-08-21 07:42:50 +09009
10#include "base/callback.h"
kkanetkar@chromium.org3a3bdea2010-09-02 12:43:36 +090011#include "base/file_path.h"
12#include "base/file_util.h"
dumi@chromium.orgc980e402010-08-21 07:42:50 +090013#include "base/platform_file.h"
14#include "base/ref_counted.h"
15#include "base/tracked_objects.h"
16
jianli@chromium.org9ed1f9d2010-08-31 11:42:36 +090017namespace file_util {
18struct FileInfo;
19}
20
dumi@chromium.orgc980e402010-08-21 07:42:50 +090021namespace base {
22
kkanetkar@chromium.org3a3bdea2010-09-02 12:43:36 +090023namespace file_util_proxy {
24 // Holds metadata for file or directory entry.
25struct Entry {
26 FilePath::StringType name;
27 bool isDirectory;
28};
29} // namespace file_util_proxy
30
dumi@chromium.orgc980e402010-08-21 07:42:50 +090031class MessageLoopProxy;
32
33// This class provides asynchronous access to common file routines.
34class FileUtilProxy {
35 public:
dumi@chromium.org6dedd6d2010-08-25 05:26:23 +090036 // This callback is used by methods that report only an error code. It is
dumi@chromium.orgc980e402010-08-21 07:42:50 +090037 // valid to pass NULL as the callback parameter to any function that takes a
38 // StatusCallback, in which case the operation will complete silently.
dumi@chromium.org50f197d2010-09-01 04:30:27 +090039 typedef Callback1<base::PlatformFileError /* error code */
40 >::Type StatusCallback;
dumi@chromium.orgc980e402010-08-21 07:42:50 +090041
42 // Creates or opens a file with the given flags. It is invalid to pass NULL
43 // for the callback.
dumi@chromium.org50f197d2010-09-01 04:30:27 +090044 typedef Callback3<base::PlatformFileError /* error code */,
45 base::PassPlatformFile,
dumi@chromium.org6dedd6d2010-08-25 05:26:23 +090046 bool /* created */>::Type CreateOrOpenCallback;
47 static bool CreateOrOpen(scoped_refptr<MessageLoopProxy> message_loop_proxy,
dumi@chromium.orgc980e402010-08-21 07:42:50 +090048 const FilePath& file_path,
49 int file_flags,
50 CreateOrOpenCallback* callback);
51
52 // Creates a temporary file for writing. The path and an open file handle
53 // are returned. It is invalid to pass NULL for the callback.
dumi@chromium.org50f197d2010-09-01 04:30:27 +090054 typedef Callback3<base::PlatformFileError /* error code */,
55 base::PassPlatformFile,
dumi@chromium.org6dedd6d2010-08-25 05:26:23 +090056 FilePath>::Type CreateTemporaryCallback;
57 static bool CreateTemporary(
dumi@chromium.orgc980e402010-08-21 07:42:50 +090058 scoped_refptr<MessageLoopProxy> message_loop_proxy,
59 CreateTemporaryCallback* callback);
60
61 // Close the given file handle.
dumi@chromium.org6dedd6d2010-08-25 05:26:23 +090062 static bool Close(scoped_refptr<MessageLoopProxy> message_loop_proxy,
dumi@chromium.orgc980e402010-08-21 07:42:50 +090063 base::PlatformFile,
64 StatusCallback* callback);
65
jianli@chromium.org9ed1f9d2010-08-31 11:42:36 +090066 // Retrieves the information about a file. It is invalid to pass NULL for the
67 // callback.
dumi@chromium.org50f197d2010-09-01 04:30:27 +090068 typedef Callback2<base::PlatformFileError /* error code */,
jianli@chromium.org9ed1f9d2010-08-31 11:42:36 +090069 const file_util::FileInfo& /*file_info*/
70 >::Type GetFileInfoCallback;
71 static bool GetFileInfo(
72 scoped_refptr<MessageLoopProxy> message_loop_proxy,
73 const FilePath& file_path,
74 GetFileInfoCallback* callback);
75
kkanetkar@chromium.org3a3bdea2010-09-02 12:43:36 +090076 typedef Callback2<base::PlatformFileError /* error code */,
77 const std::vector<base::file_util_proxy::Entry>&
78 >::Type ReadDirectoryCallback;
79 static bool ReadDirectory(scoped_refptr<MessageLoopProxy> message_loop_proxy,
80 const FilePath& file_path,
81 ReadDirectoryCallback* callback);
82
83 // Copies a file or a directory from |src_file_path| to |dest_file_path|
84 // Error cases:
85 // If destination file doesn't exist or destination's parent
86 // doesn't exists.
87 // If source dir exists but destination path is an existing file.
88 // If source is a parent of destination.
89 // If source doesn't exists.
90 static bool Copy(scoped_refptr<MessageLoopProxy> message_loop_proxy,
91 const FilePath& src_file_path,
92 const FilePath& dest_file_path,
93 StatusCallback* callback);
94
95 // Creates directory at given path. It's an error to create
96 // if |exclusive| is true and dir already exists.
97 static bool CreateDirectory(
98 scoped_refptr<MessageLoopProxy> message_loop_proxy,
99 const FilePath& file_path,
100 bool exclusive,
101 StatusCallback* callback);
102
103 // Deletes a file or empty directory.
104 static bool Delete(scoped_refptr<MessageLoopProxy> message_loop_proxy,
105 const FilePath& file_path,
106 StatusCallback* callback);
107
108 // Moves a file or a directory from src_file_path to dest_file_path.
109 // Error cases are similar to Copy method's error cases.
110 static bool Move(
111 scoped_refptr<MessageLoopProxy> message_loop_proxy,
112 const FilePath& src_file_path,
113 const FilePath& dest_file_path,
114 StatusCallback* callback);
115
116 // Deletes a directory and all of its contents.
117 static bool RecursiveDelete(
118 scoped_refptr<MessageLoopProxy> message_loop_proxy,
119 const FilePath& file_path,
120 StatusCallback* callback);
121
dumi@chromium.orgc980e402010-08-21 07:42:50 +0900122 private:
123 DISALLOW_IMPLICIT_CONSTRUCTORS(FileUtilProxy);
124};
125
kkanetkar@chromium.org3a3bdea2010-09-02 12:43:36 +0900126} // namespace base
dumi@chromium.orgc980e402010-08-21 07:42:50 +0900127
kkanetkar@chromium.org3a3bdea2010-09-02 12:43:36 +0900128#endif // BASE_FILE_UTIL_PROXY_H_