1 // Copyright (c) 2012 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_NATIVE_MEDIA_FILE_UTIL_H_
6 #define CHROME_BROWSER_MEDIA_GALLERIES_FILEAPI_NATIVE_MEDIA_FILE_UTIL_H_
7 
8 #include <stddef.h>
9 #include <stdint.h>
10 
11 #include <memory>
12 
13 #include "base/macros.h"
14 #include "base/sequenced_task_runner.h"
15 #include "storage/browser/file_system/async_file_util.h"
16 
17 namespace net {
18 class IOBuffer;
19 }
20 
21 // Implements the AsyncFileUtil interface to perform native file system
22 // operations, restricted to operating only on media files.
23 // Instances must be used, and torn-down, only on the browser IO-thread.
24 class NativeMediaFileUtil : public storage::AsyncFileUtil {
25  public:
26   // |media_task_runner| specifies the TaskRunner on which to perform all
27   // native file system operations, and media file filtering. This must
28   // be the same TaskRunner passed in each FileSystemOperationContext.
29   explicit NativeMediaFileUtil(
30       scoped_refptr<base::SequencedTaskRunner> media_task_runner);
31   ~NativeMediaFileUtil() override;
32 
33   // Uses the MIME sniffer code, which actually looks into the file,
34   // to determine if it is really a media file (to avoid exposing
35   // non-media files with a media file extension.)
36   static base::File::Error IsMediaFile(const base::FilePath& path);
37   static base::File::Error BufferIsMediaHeader(net::IOBuffer* buf,
38                                                      size_t length);
39 
40   // Methods to support CreateOrOpen. Public so they can be shared with
41   // DeviceMediaAsyncFileUtil.
42   static void CreatedSnapshotFileForCreateOrOpen(
43       base::SequencedTaskRunner* media_task_runner,
44       int file_flags,
45       storage::AsyncFileUtil::CreateOrOpenCallback callback,
46       base::File::Error result,
47       const base::File::Info& file_info,
48       const base::FilePath& platform_path,
49       scoped_refptr<storage::ShareableFileReference> file_ref);
50 
51   // AsyncFileUtil overrides.
52   void CreateOrOpen(
53       std::unique_ptr<storage::FileSystemOperationContext> context,
54       const storage::FileSystemURL& url,
55       int file_flags,
56       CreateOrOpenCallback callback) override;
57   void EnsureFileExists(
58       std::unique_ptr<storage::FileSystemOperationContext> context,
59       const storage::FileSystemURL& url,
60       EnsureFileExistsCallback callback) override;
61   void CreateDirectory(
62       std::unique_ptr<storage::FileSystemOperationContext> context,
63       const storage::FileSystemURL& url,
64       bool exclusive,
65       bool recursive,
66       StatusCallback callback) override;
67   void GetFileInfo(std::unique_ptr<storage::FileSystemOperationContext> context,
68                    const storage::FileSystemURL& url,
69                    int /* fields */,
70                    GetFileInfoCallback callback) override;
71   void ReadDirectory(
72       std::unique_ptr<storage::FileSystemOperationContext> context,
73       const storage::FileSystemURL& url,
74       ReadDirectoryCallback callback) override;
75   void Touch(std::unique_ptr<storage::FileSystemOperationContext> context,
76              const storage::FileSystemURL& url,
77              const base::Time& last_access_time,
78              const base::Time& last_modified_time,
79              StatusCallback callback) override;
80   void Truncate(std::unique_ptr<storage::FileSystemOperationContext> context,
81                 const storage::FileSystemURL& url,
82                 int64_t length,
83                 StatusCallback callback) override;
84   void CopyFileLocal(
85       std::unique_ptr<storage::FileSystemOperationContext> context,
86       const storage::FileSystemURL& src_url,
87       const storage::FileSystemURL& dest_url,
88       CopyOrMoveOption option,
89       CopyFileProgressCallback progress_callback,
90       StatusCallback callback) override;
91   void MoveFileLocal(
92       std::unique_ptr<storage::FileSystemOperationContext> context,
93       const storage::FileSystemURL& src_url,
94       const storage::FileSystemURL& dest_url,
95       CopyOrMoveOption option,
96       StatusCallback callback) override;
97   void CopyInForeignFile(
98       std::unique_ptr<storage::FileSystemOperationContext> context,
99       const base::FilePath& src_file_path,
100       const storage::FileSystemURL& dest_url,
101       StatusCallback callback) override;
102   void DeleteFile(std::unique_ptr<storage::FileSystemOperationContext> context,
103                   const storage::FileSystemURL& url,
104                   StatusCallback callback) override;
105   void DeleteDirectory(
106       std::unique_ptr<storage::FileSystemOperationContext> context,
107       const storage::FileSystemURL& url,
108       StatusCallback callback) override;
109   void DeleteRecursively(
110       std::unique_ptr<storage::FileSystemOperationContext> context,
111       const storage::FileSystemURL& url,
112       StatusCallback callback) override;
113   void CreateSnapshotFile(
114       std::unique_ptr<storage::FileSystemOperationContext> context,
115       const storage::FileSystemURL& url,
116       CreateSnapshotFileCallback callback) override;
117 
118  private:
119   // |core_| holds state which must be used and torn-down on the
120   // |media_task_runner_|, rather than on the IO-thread.
121   class Core;
122 
123   scoped_refptr<base::SequencedTaskRunner> media_task_runner_;
124   std::unique_ptr<Core> core_;
125 
126   DISALLOW_COPY_AND_ASSIGN(NativeMediaFileUtil);
127 };
128 
129 #endif  // CHROME_BROWSER_MEDIA_GALLERIES_FILEAPI_NATIVE_MEDIA_FILE_UTIL_H_
130