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