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