Upstream version 9.38.198.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/ref_counted.h"
11 #include "base/memory/scoped_ptr.h"
12 #include "base/memory/weak_ptr.h"
13 #include "webkit/browser/fileapi/async_file_util.h"
14 #include "webkit/common/blob/shareable_file_reference.h"
15
16 namespace fileapi {
17 class FileSystemOperationContext;
18 class FileSystemURL;
19 }
20
21 namespace webkit_blob {
22 class FileStreamReader;
23 }
24
25 enum MediaFileValidationType {
26   NO_MEDIA_FILE_VALIDATION,
27   APPLY_MEDIA_FILE_VALIDATION,
28 };
29
30 class DeviceMediaAsyncFileUtil : public fileapi::AsyncFileUtil {
31  public:
32   virtual ~DeviceMediaAsyncFileUtil();
33
34   // Returns an instance of DeviceMediaAsyncFileUtil.
35   static scoped_ptr<DeviceMediaAsyncFileUtil> Create(
36       const base::FilePath& profile_path,
37       MediaFileValidationType validation_type);
38
39   bool SupportsStreaming(const fileapi::FileSystemURL& url);
40
41   // AsyncFileUtil overrides.
42   virtual void CreateOrOpen(
43       scoped_ptr<fileapi::FileSystemOperationContext> context,
44       const fileapi::FileSystemURL& url,
45       int file_flags,
46       const CreateOrOpenCallback& callback) OVERRIDE;
47   virtual void EnsureFileExists(
48       scoped_ptr<fileapi::FileSystemOperationContext> context,
49       const fileapi::FileSystemURL& url,
50       const EnsureFileExistsCallback& callback) OVERRIDE;
51   virtual void CreateDirectory(
52       scoped_ptr<fileapi::FileSystemOperationContext> context,
53       const fileapi::FileSystemURL& url,
54       bool exclusive,
55       bool recursive,
56       const StatusCallback& callback) OVERRIDE;
57   virtual void GetFileInfo(
58       scoped_ptr<fileapi::FileSystemOperationContext> context,
59       const fileapi::FileSystemURL& url,
60       const GetFileInfoCallback& callback) OVERRIDE;
61   virtual void ReadDirectory(
62       scoped_ptr<fileapi::FileSystemOperationContext> context,
63       const fileapi::FileSystemURL& url,
64       const ReadDirectoryCallback& callback) OVERRIDE;
65   virtual void Touch(
66       scoped_ptr<fileapi::FileSystemOperationContext> context,
67       const fileapi::FileSystemURL& url,
68       const base::Time& last_access_time,
69       const base::Time& last_modified_time,
70       const StatusCallback& callback) OVERRIDE;
71   virtual void Truncate(
72       scoped_ptr<fileapi::FileSystemOperationContext> context,
73       const fileapi::FileSystemURL& url,
74       int64 length,
75       const StatusCallback& callback) OVERRIDE;
76   virtual void CopyFileLocal(
77       scoped_ptr<fileapi::FileSystemOperationContext> context,
78       const fileapi::FileSystemURL& src_url,
79       const fileapi::FileSystemURL& dest_url,
80       CopyOrMoveOption option,
81       const CopyFileProgressCallback& progress_callback,
82       const StatusCallback& callback) OVERRIDE;
83   virtual void MoveFileLocal(
84       scoped_ptr<fileapi::FileSystemOperationContext> context,
85       const fileapi::FileSystemURL& src_url,
86       const fileapi::FileSystemURL& dest_url,
87       CopyOrMoveOption option,
88       const StatusCallback& callback) OVERRIDE;
89   virtual void CopyInForeignFile(
90       scoped_ptr<fileapi::FileSystemOperationContext> context,
91       const base::FilePath& src_file_path,
92       const fileapi::FileSystemURL& dest_url,
93       const StatusCallback& callback) OVERRIDE;
94   virtual void DeleteFile(
95       scoped_ptr<fileapi::FileSystemOperationContext> context,
96       const fileapi::FileSystemURL& url,
97       const StatusCallback& callback) OVERRIDE;
98   virtual void DeleteDirectory(
99       scoped_ptr<fileapi::FileSystemOperationContext> context,
100       const fileapi::FileSystemURL& url,
101       const StatusCallback& callback) OVERRIDE;
102   virtual void DeleteRecursively(
103       scoped_ptr<fileapi::FileSystemOperationContext> context,
104       const fileapi::FileSystemURL& url,
105       const StatusCallback& callback) OVERRIDE;
106   virtual void CreateSnapshotFile(
107       scoped_ptr<fileapi::FileSystemOperationContext> context,
108       const fileapi::FileSystemURL& url,
109       const CreateSnapshotFileCallback& callback) OVERRIDE;
110
111   // This method is called when existing Blobs are read.
112   // |expected_modification_time| indicates the expected snapshot state of the
113   // underlying storage. The returned FileStreamReader must return an error
114   // when the state of the underlying storage changes. Any errors associated
115   // with reading this file are returned by the FileStreamReader itself.
116   virtual scoped_ptr<webkit_blob::FileStreamReader> GetFileStreamReader(
117       const fileapi::FileSystemURL& url,
118       int64 offset,
119       const base::Time& expected_modification_time,
120       fileapi::FileSystemContext* context);
121
122  private:
123   class MediaPathFilterWrapper;
124
125   // Use Create() to get an instance of DeviceMediaAsyncFileUtil.
126   DeviceMediaAsyncFileUtil(const base::FilePath& profile_path,
127                            MediaFileValidationType validation_type);
128
129   // Called when GetFileInfo method call succeeds. |file_info| contains the
130   // file details of the requested url. |callback| is invoked to complete the
131   // GetFileInfo request.
132   void OnDidGetFileInfo(
133       base::SequencedTaskRunner* task_runner,
134       const base::FilePath& path,
135       const GetFileInfoCallback& callback,
136       const base::File::Info& file_info);
137
138   // Called when ReadDirectory method call succeeds. |callback| is invoked to
139   // complete the ReadDirectory request.
140   //
141   // If the contents of the given directory are reported in one batch, then
142   // |file_list| will have the list of all files/directories in the given
143   // directory and |has_more| will be false.
144   //
145   // If the contents of the given directory are reported in multiple chunks,
146   // |file_list| will have only a subset of all contents (the subsets reported
147   // in any two calls are disjoint), and |has_more| will be true, except for
148   // the last chunk.
149   void OnDidReadDirectory(
150       base::SequencedTaskRunner* task_runner,
151       const ReadDirectoryCallback& callback,
152       const EntryList& file_list,
153       bool has_more);
154
155   bool validate_media_files() const;
156
157   // Profile path.
158   const base::FilePath profile_path_;
159
160   scoped_refptr<MediaPathFilterWrapper> media_path_filter_wrapper_;
161
162   // For callbacks that may run after destruction.
163   base::WeakPtrFactory<DeviceMediaAsyncFileUtil> weak_ptr_factory_;
164
165   DISALLOW_COPY_AND_ASSIGN(DeviceMediaAsyncFileUtil);
166 };
167
168 #endif  // CHROME_BROWSER_MEDIA_GALLERIES_FILEAPI_DEVICE_MEDIA_ASYNC_FILE_UTIL_H_