blob: 3cea78a1e3a486b4765785ab1955342c2140ac1a [file] [log] [blame]
Torne (Richard Coles)c2e0dbd2013-05-09 18:35:53 +01001// Copyright (c) 2013 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 CHROME_BROWSER_MEDIA_GALLERIES_FILEAPI_DEVICE_MEDIA_ASYNC_FILE_UTIL_H_
6#define CHROME_BROWSER_MEDIA_GALLERIES_FILEAPI_DEVICE_MEDIA_ASYNC_FILE_UTIL_H_
7
8#include "base/files/file_path.h"
Torne (Richard Coles)868fa2f2013-06-11 10:57:03 +01009#include "base/memory/scoped_ptr.h"
Torne (Richard Coles)c2e0dbd2013-05-09 18:35:53 +010010#include "base/memory/weak_ptr.h"
11#include "base/platform_file.h"
Torne (Richard Coles)90dce4d2013-05-29 14:40:03 +010012#include "webkit/browser/fileapi/async_file_util.h"
13#include "webkit/common/blob/shareable_file_reference.h"
Torne (Richard Coles)c2e0dbd2013-05-09 18:35:53 +010014
15namespace base {
16class SequencedTaskRunner;
17class Time;
18}
19
20namespace fileapi {
21class FileSystemOperationContext;
22class FileSystemURL;
23}
24
25namespace chrome {
26
27class DeviceMediaAsyncFileUtil : public fileapi::AsyncFileUtil {
28 public:
29 virtual ~DeviceMediaAsyncFileUtil();
30
31 // Returns an instance of DeviceMediaAsyncFileUtil. Returns NULL if
32 // asynchronous operation is not supported. Callers own the returned
33 // object.
34 static DeviceMediaAsyncFileUtil* Create(const base::FilePath& profile_path);
35
36 // AsyncFileUtil overrides.
Ben Murdoch7dbb3d52013-07-17 14:55:54 +010037 virtual void CreateOrOpen(
Torne (Richard Coles)868fa2f2013-06-11 10:57:03 +010038 scoped_ptr<fileapi::FileSystemOperationContext> context,
Torne (Richard Coles)c2e0dbd2013-05-09 18:35:53 +010039 const fileapi::FileSystemURL& url,
40 int file_flags,
41 const CreateOrOpenCallback& callback) OVERRIDE;
Ben Murdoch7dbb3d52013-07-17 14:55:54 +010042 virtual void EnsureFileExists(
Torne (Richard Coles)868fa2f2013-06-11 10:57:03 +010043 scoped_ptr<fileapi::FileSystemOperationContext> context,
Torne (Richard Coles)c2e0dbd2013-05-09 18:35:53 +010044 const fileapi::FileSystemURL& url,
45 const EnsureFileExistsCallback& callback) OVERRIDE;
Ben Murdoch7dbb3d52013-07-17 14:55:54 +010046 virtual void CreateDirectory(
Torne (Richard Coles)868fa2f2013-06-11 10:57:03 +010047 scoped_ptr<fileapi::FileSystemOperationContext> context,
Torne (Richard Coles)c2e0dbd2013-05-09 18:35:53 +010048 const fileapi::FileSystemURL& url,
49 bool exclusive,
50 bool recursive,
51 const StatusCallback& callback) OVERRIDE;
Ben Murdoch7dbb3d52013-07-17 14:55:54 +010052 virtual void GetFileInfo(
Torne (Richard Coles)868fa2f2013-06-11 10:57:03 +010053 scoped_ptr<fileapi::FileSystemOperationContext> context,
Torne (Richard Coles)c2e0dbd2013-05-09 18:35:53 +010054 const fileapi::FileSystemURL& url,
55 const GetFileInfoCallback& callback) OVERRIDE;
Ben Murdoch7dbb3d52013-07-17 14:55:54 +010056 virtual void ReadDirectory(
Torne (Richard Coles)868fa2f2013-06-11 10:57:03 +010057 scoped_ptr<fileapi::FileSystemOperationContext> context,
Torne (Richard Coles)c2e0dbd2013-05-09 18:35:53 +010058 const fileapi::FileSystemURL& url,
59 const ReadDirectoryCallback& callback) OVERRIDE;
Ben Murdoch7dbb3d52013-07-17 14:55:54 +010060 virtual void Touch(
Torne (Richard Coles)868fa2f2013-06-11 10:57:03 +010061 scoped_ptr<fileapi::FileSystemOperationContext> context,
Torne (Richard Coles)c2e0dbd2013-05-09 18:35:53 +010062 const fileapi::FileSystemURL& url,
63 const base::Time& last_access_time,
64 const base::Time& last_modified_time,
65 const StatusCallback& callback) OVERRIDE;
Ben Murdoch7dbb3d52013-07-17 14:55:54 +010066 virtual void Truncate(
Torne (Richard Coles)868fa2f2013-06-11 10:57:03 +010067 scoped_ptr<fileapi::FileSystemOperationContext> context,
Torne (Richard Coles)c2e0dbd2013-05-09 18:35:53 +010068 const fileapi::FileSystemURL& url,
69 int64 length,
70 const StatusCallback& callback) OVERRIDE;
Ben Murdoch7dbb3d52013-07-17 14:55:54 +010071 virtual void CopyFileLocal(
Torne (Richard Coles)868fa2f2013-06-11 10:57:03 +010072 scoped_ptr<fileapi::FileSystemOperationContext> context,
Torne (Richard Coles)c2e0dbd2013-05-09 18:35:53 +010073 const fileapi::FileSystemURL& src_url,
74 const fileapi::FileSystemURL& dest_url,
75 const StatusCallback& callback) OVERRIDE;
Ben Murdoch7dbb3d52013-07-17 14:55:54 +010076 virtual void MoveFileLocal(
Torne (Richard Coles)868fa2f2013-06-11 10:57:03 +010077 scoped_ptr<fileapi::FileSystemOperationContext> context,
Torne (Richard Coles)c2e0dbd2013-05-09 18:35:53 +010078 const fileapi::FileSystemURL& src_url,
79 const fileapi::FileSystemURL& dest_url,
80 const StatusCallback& callback) OVERRIDE;
Ben Murdoch7dbb3d52013-07-17 14:55:54 +010081 virtual void CopyInForeignFile(
Torne (Richard Coles)868fa2f2013-06-11 10:57:03 +010082 scoped_ptr<fileapi::FileSystemOperationContext> context,
Torne (Richard Coles)c2e0dbd2013-05-09 18:35:53 +010083 const base::FilePath& src_file_path,
84 const fileapi::FileSystemURL& dest_url,
85 const StatusCallback& callback) OVERRIDE;
Ben Murdoch7dbb3d52013-07-17 14:55:54 +010086 virtual void DeleteFile(
Torne (Richard Coles)868fa2f2013-06-11 10:57:03 +010087 scoped_ptr<fileapi::FileSystemOperationContext> context,
Torne (Richard Coles)c2e0dbd2013-05-09 18:35:53 +010088 const fileapi::FileSystemURL& url,
89 const StatusCallback& callback) OVERRIDE;
Ben Murdoch7dbb3d52013-07-17 14:55:54 +010090 virtual void DeleteDirectory(
Torne (Richard Coles)868fa2f2013-06-11 10:57:03 +010091 scoped_ptr<fileapi::FileSystemOperationContext> context,
Torne (Richard Coles)c2e0dbd2013-05-09 18:35:53 +010092 const fileapi::FileSystemURL& url,
93 const StatusCallback& callback) OVERRIDE;
Ben Murdoch7dbb3d52013-07-17 14:55:54 +010094 virtual void DeleteRecursively(
Ben Murdocheb525c52013-07-10 11:40:50 +010095 scoped_ptr<fileapi::FileSystemOperationContext> context,
96 const fileapi::FileSystemURL& url,
97 const StatusCallback& callback) OVERRIDE;
Ben Murdoch7dbb3d52013-07-17 14:55:54 +010098 virtual void CreateSnapshotFile(
Torne (Richard Coles)868fa2f2013-06-11 10:57:03 +010099 scoped_ptr<fileapi::FileSystemOperationContext> context,
Torne (Richard Coles)c2e0dbd2013-05-09 18:35:53 +0100100 const fileapi::FileSystemURL& url,
101 const CreateSnapshotFileCallback& callback) OVERRIDE;
102
103 private:
104 // Use Create() to get an instance of DeviceMediaAsyncFileUtil.
105 explicit DeviceMediaAsyncFileUtil(const base::FilePath& profile_path);
106
Ben Murdochbbcdd452013-07-25 10:06:34 +0100107 // Called when GetFileInfo method call succeeds. |file_info| contains the
108 // file details of the requested url. |callback| is invoked to complete the
109 // GetFileInfo request.
Torne (Richard Coles)c2e0dbd2013-05-09 18:35:53 +0100110 void OnDidGetFileInfo(
111 const AsyncFileUtil::GetFileInfoCallback& callback,
Torne (Richard Coles)c2e0dbd2013-05-09 18:35:53 +0100112 const base::PlatformFileInfo& file_info);
113
114 // Called when GetFileInfo method call failed to get the details of file
Ben Murdochbbcdd452013-07-25 10:06:34 +0100115 // specified by the requested url. |callback| is invoked to notify the
Torne (Richard Coles)c2e0dbd2013-05-09 18:35:53 +0100116 // caller about the platform file |error|.
117 void OnGetFileInfoError(
118 const AsyncFileUtil::GetFileInfoCallback& callback,
Torne (Richard Coles)c2e0dbd2013-05-09 18:35:53 +0100119 base::PlatformFileError error);
120
121 // Called when ReadDirectory method call succeeds. |callback| is invoked to
122 // complete the ReadDirectory request.
123 //
124 // If the contents of the given directory are reported in one batch, then
125 // |file_list| will have the list of all files/directories in the given
126 // directory and |has_more| will be false.
127 //
128 // If the contents of the given directory are reported in multiple chunks,
129 // |file_list| will have only a subset of all contents (the subsets reported
130 // in any two calls are disjoint), and |has_more| will be true, except for
131 // the last chunk.
132 void OnDidReadDirectory(
133 const AsyncFileUtil::ReadDirectoryCallback& callback,
134 const AsyncFileUtil::EntryList& file_list,
135 bool has_more);
136
137 // Called when ReadDirectory method call failed to enumerate the directory
138 // objects. |callback| is invoked to notify the caller about the |error|
139 // that occured while reading the directory objects.
140 void OnReadDirectoryError(
141 const AsyncFileUtil::ReadDirectoryCallback& callback,
142 base::PlatformFileError error);
143
144 // Called when the snapshot file specified by the |platform_path| is
145 // successfully created. |file_info| contains the device media file details
Torne (Richard Coles)a93a17c2013-05-15 11:34:50 +0100146 // for which the snapshot file is created.
Torne (Richard Coles)c2e0dbd2013-05-09 18:35:53 +0100147 void OnDidCreateSnapshotFile(
148 const AsyncFileUtil::CreateSnapshotFileCallback& callback,
149 base::SequencedTaskRunner* media_task_runner,
150 const base::PlatformFileInfo& file_info,
151 const base::FilePath& platform_path);
152
Torne (Richard Coles)a93a17c2013-05-15 11:34:50 +0100153 // Called after OnDidCreateSnapshotFile finishes media check.
154 // |callback| is invoked to complete the CreateSnapshotFile request.
Torne (Richard Coles)a93a17c2013-05-15 11:34:50 +0100155 void OnDidCheckMedia(
156 const AsyncFileUtil::CreateSnapshotFileCallback& callback,
Torne (Richard Coles)a93a17c2013-05-15 11:34:50 +0100157 const base::PlatformFileInfo& file_info,
158 scoped_refptr<webkit_blob::ShareableFileReference> platform_file,
Torne (Richard Coles)90dce4d2013-05-29 14:40:03 +0100159 base::PlatformFileError error);
Torne (Richard Coles)a93a17c2013-05-15 11:34:50 +0100160
Torne (Richard Coles)c2e0dbd2013-05-09 18:35:53 +0100161 // Called when CreateSnapshotFile method call fails. |callback| is invoked to
162 // notify the caller about the |error|.
163 void OnCreateSnapshotFileError(
164 const AsyncFileUtil::CreateSnapshotFileCallback& callback,
165 base::PlatformFileError error);
166
167 // Called when the snapshot file specified by the |snapshot_file_path| is
Ben Murdochbb1529c2013-08-08 10:24:53 +0100168 // created to hold the contents of the url.path(). If the snapshot
Torne (Richard Coles)c2e0dbd2013-05-09 18:35:53 +0100169 // file is successfully created, |snapshot_file_path| will be an non-empty
170 // file path. In case of failure, |snapshot_file_path| will be an empty file
171 // path. Forwards the CreateSnapshot request to the delegate to copy the
Ben Murdochbb1529c2013-08-08 10:24:53 +0100172 // contents of url.path() to |snapshot_file_path|.
Torne (Richard Coles)c2e0dbd2013-05-09 18:35:53 +0100173 void OnSnapshotFileCreatedRunTask(
Torne (Richard Coles)868fa2f2013-06-11 10:57:03 +0100174 scoped_ptr<fileapi::FileSystemOperationContext> context,
Torne (Richard Coles)c2e0dbd2013-05-09 18:35:53 +0100175 const AsyncFileUtil::CreateSnapshotFileCallback& callback,
Ben Murdochbb1529c2013-08-08 10:24:53 +0100176 const fileapi::FileSystemURL& url,
Torne (Richard Coles)c2e0dbd2013-05-09 18:35:53 +0100177 base::FilePath* snapshot_file_path);
178
179 // Profile path.
180 const base::FilePath profile_path_;
181
182 // For callbacks that may run after destruction.
183 base::WeakPtrFactory<DeviceMediaAsyncFileUtil> weak_ptr_factory_;
184
185 DISALLOW_COPY_AND_ASSIGN(DeviceMediaAsyncFileUtil);
186};
187
188} // namespace chrome
189
190#endif // CHROME_BROWSER_MEDIA_GALLERIES_FILEAPI_DEVICE_MEDIA_ASYNC_FILE_UTIL_H_