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.
5 #ifndef CHROME_BROWSER_MEDIA_GALLERIES_FILEAPI_NATIVE_MEDIA_FILE_UTIL_H_
6 #define CHROME_BROWSER_MEDIA_GALLERIES_FILEAPI_NATIVE_MEDIA_FILE_UTIL_H_
8 #include "base/memory/scoped_ptr.h"
9 #include "base/memory/weak_ptr.h"
10 #include "webkit/browser/fileapi/async_file_util.h"
16 class MediaPathFilter
;
18 // This class handles native file system operations with media type filtering.
19 // To support virtual file systems it implements the AsyncFileUtil interface
20 // from scratch and provides synchronous override points.
21 class NativeMediaFileUtil
: public fileapi::AsyncFileUtil
{
23 explicit NativeMediaFileUtil(MediaPathFilter
* media_path_filter
);
24 virtual ~NativeMediaFileUtil();
26 // Uses the MIME sniffer code, which actually looks into the file,
27 // to determine if it is really a media file (to avoid exposing
28 // non-media files with a media file extension.)
29 static base::File::Error
IsMediaFile(const base::FilePath
& path
);
30 static base::File::Error
BufferIsMediaHeader(net::IOBuffer
* buf
,
33 // AsyncFileUtil overrides.
34 virtual void CreateOrOpen(
35 scoped_ptr
<fileapi::FileSystemOperationContext
> context
,
36 const fileapi::FileSystemURL
& url
,
38 const CreateOrOpenCallback
& callback
) OVERRIDE
;
39 virtual void EnsureFileExists(
40 scoped_ptr
<fileapi::FileSystemOperationContext
> context
,
41 const fileapi::FileSystemURL
& url
,
42 const EnsureFileExistsCallback
& callback
) OVERRIDE
;
43 virtual void CreateDirectory(
44 scoped_ptr
<fileapi::FileSystemOperationContext
> context
,
45 const fileapi::FileSystemURL
& url
,
48 const StatusCallback
& callback
) OVERRIDE
;
49 virtual void GetFileInfo(
50 scoped_ptr
<fileapi::FileSystemOperationContext
> context
,
51 const fileapi::FileSystemURL
& url
,
52 const GetFileInfoCallback
& callback
) OVERRIDE
;
53 virtual void ReadDirectory(
54 scoped_ptr
<fileapi::FileSystemOperationContext
> context
,
55 const fileapi::FileSystemURL
& url
,
56 const ReadDirectoryCallback
& callback
) OVERRIDE
;
58 scoped_ptr
<fileapi::FileSystemOperationContext
> context
,
59 const fileapi::FileSystemURL
& url
,
60 const base::Time
& last_access_time
,
61 const base::Time
& last_modified_time
,
62 const StatusCallback
& callback
) OVERRIDE
;
63 virtual void Truncate(
64 scoped_ptr
<fileapi::FileSystemOperationContext
> context
,
65 const fileapi::FileSystemURL
& url
,
67 const StatusCallback
& callback
) OVERRIDE
;
68 virtual void CopyFileLocal(
69 scoped_ptr
<fileapi::FileSystemOperationContext
> context
,
70 const fileapi::FileSystemURL
& src_url
,
71 const fileapi::FileSystemURL
& dest_url
,
72 CopyOrMoveOption option
,
73 const CopyFileProgressCallback
& progress_callback
,
74 const StatusCallback
& callback
) OVERRIDE
;
75 virtual void MoveFileLocal(
76 scoped_ptr
<fileapi::FileSystemOperationContext
> context
,
77 const fileapi::FileSystemURL
& src_url
,
78 const fileapi::FileSystemURL
& dest_url
,
79 CopyOrMoveOption option
,
80 const StatusCallback
& callback
) OVERRIDE
;
81 virtual void CopyInForeignFile(
82 scoped_ptr
<fileapi::FileSystemOperationContext
> context
,
83 const base::FilePath
& src_file_path
,
84 const fileapi::FileSystemURL
& dest_url
,
85 const StatusCallback
& callback
) OVERRIDE
;
86 virtual void DeleteFile(
87 scoped_ptr
<fileapi::FileSystemOperationContext
> context
,
88 const fileapi::FileSystemURL
& url
,
89 const StatusCallback
& callback
) OVERRIDE
;
90 virtual void DeleteDirectory(
91 scoped_ptr
<fileapi::FileSystemOperationContext
> context
,
92 const fileapi::FileSystemURL
& url
,
93 const StatusCallback
& callback
) OVERRIDE
;
94 virtual void DeleteRecursively(
95 scoped_ptr
<fileapi::FileSystemOperationContext
> context
,
96 const fileapi::FileSystemURL
& url
,
97 const StatusCallback
& callback
) OVERRIDE
;
98 virtual void CreateSnapshotFile(
99 scoped_ptr
<fileapi::FileSystemOperationContext
> context
,
100 const fileapi::FileSystemURL
& url
,
101 const CreateSnapshotFileCallback
& callback
) OVERRIDE
;
104 virtual void CreateDirectoryOnTaskRunnerThread(
105 scoped_ptr
<fileapi::FileSystemOperationContext
> context
,
106 const fileapi::FileSystemURL
& url
,
109 const StatusCallback
& callback
);
110 virtual void GetFileInfoOnTaskRunnerThread(
111 scoped_ptr
<fileapi::FileSystemOperationContext
> context
,
112 const fileapi::FileSystemURL
& url
,
113 const GetFileInfoCallback
& callback
);
114 virtual void ReadDirectoryOnTaskRunnerThread(
115 scoped_ptr
<fileapi::FileSystemOperationContext
> context
,
116 const fileapi::FileSystemURL
& url
,
117 const ReadDirectoryCallback
& callback
);
118 virtual void CopyOrMoveFileLocalOnTaskRunnerThread(
119 scoped_ptr
<fileapi::FileSystemOperationContext
> context
,
120 const fileapi::FileSystemURL
& src_url
,
121 const fileapi::FileSystemURL
& dest_url
,
122 CopyOrMoveOption option
,
124 const StatusCallback
& callback
);
125 virtual void CopyInForeignFileOnTaskRunnerThread(
126 scoped_ptr
<fileapi::FileSystemOperationContext
> context
,
127 const base::FilePath
& src_file_path
,
128 const fileapi::FileSystemURL
& dest_url
,
129 const StatusCallback
& callback
);
130 virtual void DeleteFileOnTaskRunnerThread(
131 scoped_ptr
<fileapi::FileSystemOperationContext
> context
,
132 const fileapi::FileSystemURL
& url
,
133 const StatusCallback
& callback
);
134 virtual void DeleteDirectoryOnTaskRunnerThread(
135 scoped_ptr
<fileapi::FileSystemOperationContext
> context
,
136 const fileapi::FileSystemURL
& url
,
137 const StatusCallback
& callback
);
138 virtual void CreateSnapshotFileOnTaskRunnerThread(
139 scoped_ptr
<fileapi::FileSystemOperationContext
> context
,
140 const fileapi::FileSystemURL
& url
,
141 const CreateSnapshotFileCallback
& callback
);
143 // The following methods should only be called on the task runner thread.
145 // Necessary for copy/move to succeed.
146 virtual base::File::Error
CreateDirectorySync(
147 fileapi::FileSystemOperationContext
* context
,
148 const fileapi::FileSystemURL
& url
,
151 virtual base::File::Error
CopyOrMoveFileSync(
152 fileapi::FileSystemOperationContext
* context
,
153 const fileapi::FileSystemURL
& src_url
,
154 const fileapi::FileSystemURL
& dest_url
,
155 CopyOrMoveOption option
,
157 virtual base::File::Error
CopyInForeignFileSync(
158 fileapi::FileSystemOperationContext
* context
,
159 const base::FilePath
& src_file_path
,
160 const fileapi::FileSystemURL
& dest_url
);
161 virtual base::File::Error
GetFileInfoSync(
162 fileapi::FileSystemOperationContext
* context
,
163 const fileapi::FileSystemURL
& url
,
164 base::File::Info
* file_info
,
165 base::FilePath
* platform_path
);
166 // Called by GetFileInfoSync. Meant to be overridden by subclasses that
167 // have special mappings from URLs to platform paths (virtual filesystems).
168 virtual base::File::Error
GetLocalFilePath(
169 fileapi::FileSystemOperationContext
* context
,
170 const fileapi::FileSystemURL
& file_system_url
,
171 base::FilePath
* local_file_path
);
172 virtual base::File::Error
ReadDirectorySync(
173 fileapi::FileSystemOperationContext
* context
,
174 const fileapi::FileSystemURL
& url
,
175 EntryList
* file_list
);
176 virtual base::File::Error
DeleteFileSync(
177 fileapi::FileSystemOperationContext
* context
,
178 const fileapi::FileSystemURL
& url
);
179 // Necessary for move to succeed.
180 virtual base::File::Error
DeleteDirectorySync(
181 fileapi::FileSystemOperationContext
* context
,
182 const fileapi::FileSystemURL
& url
);
183 virtual base::File::Error
CreateSnapshotFileSync(
184 fileapi::FileSystemOperationContext
* context
,
185 const fileapi::FileSystemURL
& url
,
186 base::File::Info
* file_info
,
187 base::FilePath
* platform_path
,
188 scoped_refptr
<webkit_blob::ShareableFileReference
>* file_ref
);
191 MediaPathFilter
* media_path_filter() {
192 return media_path_filter_
;
196 // Like GetLocalFilePath(), but always take media_path_filter() into
197 // consideration. If the media_path_filter() check fails, return
198 // PLATFORM_FILE_ERROR_SECURITY. |local_file_path| does not have to exist.
199 base::File::Error
GetFilteredLocalFilePath(
200 fileapi::FileSystemOperationContext
* context
,
201 const fileapi::FileSystemURL
& file_system_url
,
202 base::FilePath
* local_file_path
);
204 // Like GetLocalFilePath(), but if the file does not exist, then return
206 // If |local_file_path| is a file, then take media_path_filter() into
208 // If the media_path_filter() check fails, return |failure_error|.
209 // If |local_file_path| is a directory, return PLATFORM_FILE_OK.
210 base::File::Error
GetFilteredLocalFilePathForExistingFileOrDirectory(
211 fileapi::FileSystemOperationContext
* context
,
212 const fileapi::FileSystemURL
& file_system_url
,
213 base::File::Error failure_error
,
214 base::FilePath
* local_file_path
);
217 // Not owned, owned by the backend which owns this.
218 MediaPathFilter
* media_path_filter_
;
220 base::WeakPtrFactory
<NativeMediaFileUtil
> weak_factory_
;
222 DISALLOW_COPY_AND_ASSIGN(NativeMediaFileUtil
);
225 #endif // CHROME_BROWSER_MEDIA_GALLERIES_FILEAPI_NATIVE_MEDIA_FILE_UTIL_H_