Upstream version 7.36.149.0
[platform/framework/web/crosswalk.git] / src / chrome / browser / media_galleries / fileapi / device_media_async_file_util.h
1 // 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.h"
9 #include "base/files/file_path.h"
10 #include "base/memory/scoped_ptr.h"
11 #include "base/memory/weak_ptr.h"
12 #include "webkit/browser/fileapi/async_file_util.h"
13 #include "webkit/common/blob/shareable_file_reference.h"
14
15 namespace base {
16 class SequencedTaskRunner;
17 class Time;
18 }
19
20 namespace fileapi {
21 class FileSystemOperationContext;
22 class FileSystemURL;
23 }
24
25 namespace net {
26 class IOBuffer;
27 }
28
29 namespace webkit_blob {
30 class FileStreamReader;
31 }
32
33 enum MediaFileValidationType {
34   NO_MEDIA_FILE_VALIDATION,
35   APPLY_MEDIA_FILE_VALIDATION,
36 };
37
38 class DeviceMediaAsyncFileUtil : public fileapi::AsyncFileUtil {
39  public:
40   virtual ~DeviceMediaAsyncFileUtil();
41
42   // Returns an instance of DeviceMediaAsyncFileUtil. Returns NULL if
43   // asynchronous operation is not supported. Callers own the returned
44   // object.
45   static DeviceMediaAsyncFileUtil* Create(
46       const base::FilePath& profile_path,
47       MediaFileValidationType validation_type);
48
49   bool SupportsStreaming(const fileapi::FileSystemURL& url);
50
51   // AsyncFileUtil overrides.
52   virtual void CreateOrOpen(
53       scoped_ptr<fileapi::FileSystemOperationContext> context,
54       const fileapi::FileSystemURL& url,
55       int file_flags,
56       const CreateOrOpenCallback& callback) OVERRIDE;
57   virtual void EnsureFileExists(
58       scoped_ptr<fileapi::FileSystemOperationContext> context,
59       const fileapi::FileSystemURL& url,
60       const EnsureFileExistsCallback& callback) OVERRIDE;
61   virtual void CreateDirectory(
62       scoped_ptr<fileapi::FileSystemOperationContext> context,
63       const fileapi::FileSystemURL& url,
64       bool exclusive,
65       bool recursive,
66       const StatusCallback& callback) OVERRIDE;
67   virtual void GetFileInfo(
68       scoped_ptr<fileapi::FileSystemOperationContext> context,
69       const fileapi::FileSystemURL& url,
70       const GetFileInfoCallback& callback) OVERRIDE;
71   virtual void ReadDirectory(
72       scoped_ptr<fileapi::FileSystemOperationContext> context,
73       const fileapi::FileSystemURL& url,
74       const ReadDirectoryCallback& callback) OVERRIDE;
75   virtual void Touch(
76       scoped_ptr<fileapi::FileSystemOperationContext> context,
77       const fileapi::FileSystemURL& url,
78       const base::Time& last_access_time,
79       const base::Time& last_modified_time,
80       const StatusCallback& callback) OVERRIDE;
81   virtual void Truncate(
82       scoped_ptr<fileapi::FileSystemOperationContext> context,
83       const fileapi::FileSystemURL& url,
84       int64 length,
85       const StatusCallback& callback) OVERRIDE;
86   virtual void CopyFileLocal(
87       scoped_ptr<fileapi::FileSystemOperationContext> context,
88       const fileapi::FileSystemURL& src_url,
89       const fileapi::FileSystemURL& dest_url,
90       CopyOrMoveOption option,
91       const CopyFileProgressCallback& progress_callback,
92       const StatusCallback& callback) OVERRIDE;
93   virtual void MoveFileLocal(
94       scoped_ptr<fileapi::FileSystemOperationContext> context,
95       const fileapi::FileSystemURL& src_url,
96       const fileapi::FileSystemURL& dest_url,
97       CopyOrMoveOption option,
98       const StatusCallback& callback) OVERRIDE;
99   virtual void CopyInForeignFile(
100       scoped_ptr<fileapi::FileSystemOperationContext> context,
101       const base::FilePath& src_file_path,
102       const fileapi::FileSystemURL& dest_url,
103       const StatusCallback& callback) OVERRIDE;
104   virtual void DeleteFile(
105       scoped_ptr<fileapi::FileSystemOperationContext> context,
106       const fileapi::FileSystemURL& url,
107       const StatusCallback& callback) OVERRIDE;
108   virtual void DeleteDirectory(
109       scoped_ptr<fileapi::FileSystemOperationContext> context,
110       const fileapi::FileSystemURL& url,
111       const StatusCallback& callback) OVERRIDE;
112   virtual void DeleteRecursively(
113       scoped_ptr<fileapi::FileSystemOperationContext> context,
114       const fileapi::FileSystemURL& url,
115       const StatusCallback& callback) OVERRIDE;
116   virtual void CreateSnapshotFile(
117       scoped_ptr<fileapi::FileSystemOperationContext> context,
118       const fileapi::FileSystemURL& url,
119       const CreateSnapshotFileCallback& callback) OVERRIDE;
120
121   // This method is called when existing Blobs are read.
122   // |expected_modification_time| indicates the expected snapshot state of the
123   // underlying storage. The returned FileStreamReader must return an error
124   // when the state of the underlying storage changes. Any errors associated
125   // with reading this file are returned by the FileStreamReader itself.
126   virtual scoped_ptr<webkit_blob::FileStreamReader> GetFileStreamReader(
127       const fileapi::FileSystemURL& url,
128       int64 offset,
129       const base::Time& expected_modification_time,
130       fileapi::FileSystemContext* context);
131
132  private:
133   // Use Create() to get an instance of DeviceMediaAsyncFileUtil.
134   DeviceMediaAsyncFileUtil(const base::FilePath& profile_path,
135                            MediaFileValidationType validation_type);
136
137   // Called when GetFileInfo method call succeeds. |file_info| contains the
138   // file details of the requested url. |callback| is invoked to complete the
139   // GetFileInfo request.
140   void OnDidGetFileInfo(
141       const AsyncFileUtil::GetFileInfoCallback& callback,
142       const base::File::Info& file_info);
143
144   // Called when GetFileInfo method call failed to get the details of file
145   // specified by the requested url. |callback| is invoked to notify the
146   // caller about the platform file |error|.
147   void OnGetFileInfoError(
148       const AsyncFileUtil::GetFileInfoCallback& callback,
149       base::File::Error error);
150
151   // Called when ReadDirectory method call succeeds. |callback| is invoked to
152   // complete the ReadDirectory request.
153   //
154   // If the contents of the given directory are reported in one batch, then
155   // |file_list| will have the list of all files/directories in the given
156   // directory and |has_more| will be false.
157   //
158   // If the contents of the given directory are reported in multiple chunks,
159   // |file_list| will have only a subset of all contents (the subsets reported
160   // in any two calls are disjoint), and |has_more| will be true, except for
161   // the last chunk.
162   void OnDidReadDirectory(
163       const AsyncFileUtil::ReadDirectoryCallback& callback,
164       const AsyncFileUtil::EntryList& file_list,
165       bool has_more);
166
167   // Called when ReadDirectory method call failed to enumerate the directory
168   // objects. |callback| is invoked to notify the caller about the |error|
169   // that occured while reading the directory objects.
170   void OnReadDirectoryError(
171       const AsyncFileUtil::ReadDirectoryCallback& callback,
172       base::File::Error error);
173
174   // Called when the snapshot file specified by the |platform_path| is
175   // successfully created. |file_info| contains the device media file details
176   // for which the snapshot file is created.
177   void OnDidCreateSnapshotFile(
178       const AsyncFileUtil::CreateSnapshotFileCallback& callback,
179       base::SequencedTaskRunner* media_task_runner,
180       const base::File::Info& file_info,
181       const base::FilePath& platform_path);
182
183   // Called after OnDidCreateSnapshotFile finishes media check.
184   // |callback| is invoked to complete the CreateSnapshotFile request.
185   void OnDidCheckMedia(
186       const AsyncFileUtil::CreateSnapshotFileCallback& callback,
187       const base::File::Info& file_info,
188       scoped_refptr<webkit_blob::ShareableFileReference> platform_file,
189       base::File::Error error);
190
191   // Called when CreateSnapshotFile method call fails. |callback| is invoked to
192   // notify the caller about the |error|.
193   void OnCreateSnapshotFileError(
194       const AsyncFileUtil::CreateSnapshotFileCallback& callback,
195       base::File::Error error);
196
197   // Called when the snapshot file specified by the |snapshot_file_path| is
198   // created to hold the contents of the url.path(). If the snapshot
199   // file is successfully created, |snapshot_file_path| will be an non-empty
200   // file path. In case of failure, |snapshot_file_path| will be an empty file
201   // path. Forwards the CreateSnapshot request to the delegate to copy the
202   // contents of url.path() to |snapshot_file_path|.
203   void OnSnapshotFileCreatedRunTask(
204       scoped_ptr<fileapi::FileSystemOperationContext> context,
205       const AsyncFileUtil::CreateSnapshotFileCallback& callback,
206       const fileapi::FileSystemURL& url,
207       const base::FilePath& snapshot_file_path);
208
209   // Profile path.
210   const base::FilePath profile_path_;
211
212   const MediaFileValidationType validation_type_;
213
214   // For callbacks that may run after destruction.
215   base::WeakPtrFactory<DeviceMediaAsyncFileUtil> weak_ptr_factory_;
216
217   DISALLOW_COPY_AND_ASSIGN(DeviceMediaAsyncFileUtil);
218 };
219
220 #endif  // CHROME_BROWSER_MEDIA_GALLERIES_FILEAPI_DEVICE_MEDIA_ASYNC_FILE_UTIL_H_