blob: 3fe7d580b34e7f68daa72d65fee589227e888283 [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 {
kinuko@chromium.org83604d12010-09-03 07:28:49 +090024
25// Holds metadata for file or directory entry.
kkanetkar@chromium.org3a3bdea2010-09-02 12:43:36 +090026struct Entry {
27 FilePath::StringType name;
kinuko@chromium.org83604d12010-09-03 07:28:49 +090028 bool is_directory;
kkanetkar@chromium.org3a3bdea2010-09-02 12:43:36 +090029};
kinuko@chromium.org83604d12010-09-03 07:28:49 +090030
kkanetkar@chromium.org3a3bdea2010-09-02 12:43:36 +090031} // namespace file_util_proxy
32
dumi@chromium.orgc980e402010-08-21 07:42:50 +090033class MessageLoopProxy;
34
35// This class provides asynchronous access to common file routines.
36class FileUtilProxy {
37 public:
dumi@chromium.org6dedd6d2010-08-25 05:26:23 +090038 // This callback is used by methods that report only an error code. It is
dumi@chromium.orgc980e402010-08-21 07:42:50 +090039 // valid to pass NULL as the callback parameter to any function that takes a
40 // StatusCallback, in which case the operation will complete silently.
dumi@chromium.org50f197d2010-09-01 04:30:27 +090041 typedef Callback1<base::PlatformFileError /* error code */
42 >::Type StatusCallback;
dumi@chromium.orgc980e402010-08-21 07:42:50 +090043
44 // Creates or opens a file with the given flags. It is invalid to pass NULL
45 // for the callback.
dumi@chromium.org50f197d2010-09-01 04:30:27 +090046 typedef Callback3<base::PlatformFileError /* error code */,
47 base::PassPlatformFile,
dumi@chromium.org6dedd6d2010-08-25 05:26:23 +090048 bool /* created */>::Type CreateOrOpenCallback;
49 static bool CreateOrOpen(scoped_refptr<MessageLoopProxy> message_loop_proxy,
dumi@chromium.orgc980e402010-08-21 07:42:50 +090050 const FilePath& file_path,
51 int file_flags,
52 CreateOrOpenCallback* callback);
53
54 // Creates a temporary file for writing. The path and an open file handle
55 // are returned. It is invalid to pass NULL for the callback.
dumi@chromium.org50f197d2010-09-01 04:30:27 +090056 typedef Callback3<base::PlatformFileError /* error code */,
57 base::PassPlatformFile,
dumi@chromium.org6dedd6d2010-08-25 05:26:23 +090058 FilePath>::Type CreateTemporaryCallback;
59 static bool CreateTemporary(
dumi@chromium.orgc980e402010-08-21 07:42:50 +090060 scoped_refptr<MessageLoopProxy> message_loop_proxy,
61 CreateTemporaryCallback* callback);
62
63 // Close the given file handle.
dumi@chromium.org6dedd6d2010-08-25 05:26:23 +090064 static bool Close(scoped_refptr<MessageLoopProxy> message_loop_proxy,
dumi@chromium.orgc980e402010-08-21 07:42:50 +090065 base::PlatformFile,
66 StatusCallback* callback);
67
jianli@chromium.org9ed1f9d2010-08-31 11:42:36 +090068 // Retrieves the information about a file. It is invalid to pass NULL for the
69 // callback.
dumi@chromium.org50f197d2010-09-01 04:30:27 +090070 typedef Callback2<base::PlatformFileError /* error code */,
jianli@chromium.org9ed1f9d2010-08-31 11:42:36 +090071 const file_util::FileInfo& /*file_info*/
72 >::Type GetFileInfoCallback;
73 static bool GetFileInfo(
74 scoped_refptr<MessageLoopProxy> message_loop_proxy,
75 const FilePath& file_path,
76 GetFileInfoCallback* callback);
77
kkanetkar@chromium.org3a3bdea2010-09-02 12:43:36 +090078 typedef Callback2<base::PlatformFileError /* error code */,
79 const std::vector<base::file_util_proxy::Entry>&
80 >::Type ReadDirectoryCallback;
81 static bool ReadDirectory(scoped_refptr<MessageLoopProxy> message_loop_proxy,
82 const FilePath& file_path,
83 ReadDirectoryCallback* callback);
84
85 // Copies a file or a directory from |src_file_path| to |dest_file_path|
86 // Error cases:
87 // If destination file doesn't exist or destination's parent
88 // doesn't exists.
89 // If source dir exists but destination path is an existing file.
90 // If source is a parent of destination.
91 // If source doesn't exists.
92 static bool Copy(scoped_refptr<MessageLoopProxy> message_loop_proxy,
93 const FilePath& src_file_path,
94 const FilePath& dest_file_path,
95 StatusCallback* callback);
96
97 // Creates directory at given path. It's an error to create
98 // if |exclusive| is true and dir already exists.
99 static bool CreateDirectory(
100 scoped_refptr<MessageLoopProxy> message_loop_proxy,
101 const FilePath& file_path,
102 bool exclusive,
103 StatusCallback* callback);
104
105 // Deletes a file or empty directory.
106 static bool Delete(scoped_refptr<MessageLoopProxy> message_loop_proxy,
107 const FilePath& file_path,
108 StatusCallback* callback);
109
110 // Moves a file or a directory from src_file_path to dest_file_path.
111 // Error cases are similar to Copy method's error cases.
112 static bool Move(
113 scoped_refptr<MessageLoopProxy> message_loop_proxy,
114 const FilePath& src_file_path,
115 const FilePath& dest_file_path,
116 StatusCallback* callback);
117
118 // Deletes a directory and all of its contents.
119 static bool RecursiveDelete(
120 scoped_refptr<MessageLoopProxy> message_loop_proxy,
121 const FilePath& file_path,
122 StatusCallback* callback);
123
dumi@chromium.orgc980e402010-08-21 07:42:50 +0900124 private:
125 DISALLOW_IMPLICIT_CONSTRUCTORS(FileUtilProxy);
126};
127
kkanetkar@chromium.org3a3bdea2010-09-02 12:43:36 +0900128} // namespace base
dumi@chromium.orgc980e402010-08-21 07:42:50 +0900129
kkanetkar@chromium.org3a3bdea2010-09-02 12:43:36 +0900130#endif // BASE_FILE_UTIL_PROXY_H_