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