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 "base/memory/scoped_ptr.h" 9#include "base/memory/weak_ptr.h" 10#include "storage/browser/fileapi/async_file_util.h" 11 12namespace net { 13class IOBuffer; 14} 15 16class MediaPathFilter; 17 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. 21class NativeMediaFileUtil : public storage::AsyncFileUtil { 22 public: 23 explicit NativeMediaFileUtil(MediaPathFilter* media_path_filter); 24 virtual ~NativeMediaFileUtil(); 25 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, 31 size_t length); 32 33 // Methods to support CreateOrOpen. Public so they can be shared with 34 // DeviceMediaAsyncFileUtil. 35 static void CreatedSnapshotFileForCreateOrOpen( 36 base::SequencedTaskRunner* media_task_runner, 37 int file_flags, 38 const storage::AsyncFileUtil::CreateOrOpenCallback& callback, 39 base::File::Error result, 40 const base::File::Info& file_info, 41 const base::FilePath& platform_path, 42 const scoped_refptr<storage::ShareableFileReference>& file_ref); 43 44 // AsyncFileUtil overrides. 45 virtual void CreateOrOpen( 46 scoped_ptr<storage::FileSystemOperationContext> context, 47 const storage::FileSystemURL& url, 48 int file_flags, 49 const CreateOrOpenCallback& callback) OVERRIDE; 50 virtual void EnsureFileExists( 51 scoped_ptr<storage::FileSystemOperationContext> context, 52 const storage::FileSystemURL& url, 53 const EnsureFileExistsCallback& callback) OVERRIDE; 54 virtual void CreateDirectory( 55 scoped_ptr<storage::FileSystemOperationContext> context, 56 const storage::FileSystemURL& url, 57 bool exclusive, 58 bool recursive, 59 const StatusCallback& callback) OVERRIDE; 60 virtual void GetFileInfo( 61 scoped_ptr<storage::FileSystemOperationContext> context, 62 const storage::FileSystemURL& url, 63 const GetFileInfoCallback& callback) OVERRIDE; 64 virtual void ReadDirectory( 65 scoped_ptr<storage::FileSystemOperationContext> context, 66 const storage::FileSystemURL& url, 67 const ReadDirectoryCallback& callback) OVERRIDE; 68 virtual void Touch(scoped_ptr<storage::FileSystemOperationContext> context, 69 const storage::FileSystemURL& url, 70 const base::Time& last_access_time, 71 const base::Time& last_modified_time, 72 const StatusCallback& callback) OVERRIDE; 73 virtual void Truncate(scoped_ptr<storage::FileSystemOperationContext> context, 74 const storage::FileSystemURL& url, 75 int64 length, 76 const StatusCallback& callback) OVERRIDE; 77 virtual void CopyFileLocal( 78 scoped_ptr<storage::FileSystemOperationContext> context, 79 const storage::FileSystemURL& src_url, 80 const storage::FileSystemURL& dest_url, 81 CopyOrMoveOption option, 82 const CopyFileProgressCallback& progress_callback, 83 const StatusCallback& callback) OVERRIDE; 84 virtual void MoveFileLocal( 85 scoped_ptr<storage::FileSystemOperationContext> context, 86 const storage::FileSystemURL& src_url, 87 const storage::FileSystemURL& dest_url, 88 CopyOrMoveOption option, 89 const StatusCallback& callback) OVERRIDE; 90 virtual void CopyInForeignFile( 91 scoped_ptr<storage::FileSystemOperationContext> context, 92 const base::FilePath& src_file_path, 93 const storage::FileSystemURL& dest_url, 94 const StatusCallback& callback) OVERRIDE; 95 virtual void DeleteFile( 96 scoped_ptr<storage::FileSystemOperationContext> context, 97 const storage::FileSystemURL& url, 98 const StatusCallback& callback) OVERRIDE; 99 virtual void DeleteDirectory( 100 scoped_ptr<storage::FileSystemOperationContext> context, 101 const storage::FileSystemURL& url, 102 const StatusCallback& callback) OVERRIDE; 103 virtual void DeleteRecursively( 104 scoped_ptr<storage::FileSystemOperationContext> context, 105 const storage::FileSystemURL& url, 106 const StatusCallback& callback) OVERRIDE; 107 virtual void CreateSnapshotFile( 108 scoped_ptr<storage::FileSystemOperationContext> context, 109 const storage::FileSystemURL& url, 110 const CreateSnapshotFileCallback& callback) OVERRIDE; 111 112 protected: 113 virtual void CreateDirectoryOnTaskRunnerThread( 114 scoped_ptr<storage::FileSystemOperationContext> context, 115 const storage::FileSystemURL& url, 116 bool exclusive, 117 bool recursive, 118 const StatusCallback& callback); 119 virtual void GetFileInfoOnTaskRunnerThread( 120 scoped_ptr<storage::FileSystemOperationContext> context, 121 const storage::FileSystemURL& url, 122 const GetFileInfoCallback& callback); 123 virtual void ReadDirectoryOnTaskRunnerThread( 124 scoped_ptr<storage::FileSystemOperationContext> context, 125 const storage::FileSystemURL& url, 126 const ReadDirectoryCallback& callback); 127 virtual void CopyOrMoveFileLocalOnTaskRunnerThread( 128 scoped_ptr<storage::FileSystemOperationContext> context, 129 const storage::FileSystemURL& src_url, 130 const storage::FileSystemURL& dest_url, 131 CopyOrMoveOption option, 132 bool copy, 133 const StatusCallback& callback); 134 virtual void CopyInForeignFileOnTaskRunnerThread( 135 scoped_ptr<storage::FileSystemOperationContext> context, 136 const base::FilePath& src_file_path, 137 const storage::FileSystemURL& dest_url, 138 const StatusCallback& callback); 139 virtual void DeleteFileOnTaskRunnerThread( 140 scoped_ptr<storage::FileSystemOperationContext> context, 141 const storage::FileSystemURL& url, 142 const StatusCallback& callback); 143 virtual void DeleteDirectoryOnTaskRunnerThread( 144 scoped_ptr<storage::FileSystemOperationContext> context, 145 const storage::FileSystemURL& url, 146 const StatusCallback& callback); 147 virtual void CreateSnapshotFileOnTaskRunnerThread( 148 scoped_ptr<storage::FileSystemOperationContext> context, 149 const storage::FileSystemURL& url, 150 const CreateSnapshotFileCallback& callback); 151 152 // The following methods should only be called on the task runner thread. 153 154 // Necessary for copy/move to succeed. 155 virtual base::File::Error CreateDirectorySync( 156 storage::FileSystemOperationContext* context, 157 const storage::FileSystemURL& url, 158 bool exclusive, 159 bool recursive); 160 virtual base::File::Error CopyOrMoveFileSync( 161 storage::FileSystemOperationContext* context, 162 const storage::FileSystemURL& src_url, 163 const storage::FileSystemURL& dest_url, 164 CopyOrMoveOption option, 165 bool copy); 166 virtual base::File::Error CopyInForeignFileSync( 167 storage::FileSystemOperationContext* context, 168 const base::FilePath& src_file_path, 169 const storage::FileSystemURL& dest_url); 170 virtual base::File::Error GetFileInfoSync( 171 storage::FileSystemOperationContext* context, 172 const storage::FileSystemURL& url, 173 base::File::Info* file_info, 174 base::FilePath* platform_path); 175 // Called by GetFileInfoSync. Meant to be overridden by subclasses that 176 // have special mappings from URLs to platform paths (virtual filesystems). 177 virtual base::File::Error GetLocalFilePath( 178 storage::FileSystemOperationContext* context, 179 const storage::FileSystemURL& file_system_url, 180 base::FilePath* local_file_path); 181 virtual base::File::Error ReadDirectorySync( 182 storage::FileSystemOperationContext* context, 183 const storage::FileSystemURL& url, 184 EntryList* file_list); 185 virtual base::File::Error DeleteFileSync( 186 storage::FileSystemOperationContext* context, 187 const storage::FileSystemURL& url); 188 // Necessary for move to succeed. 189 virtual base::File::Error DeleteDirectorySync( 190 storage::FileSystemOperationContext* context, 191 const storage::FileSystemURL& url); 192 virtual base::File::Error CreateSnapshotFileSync( 193 storage::FileSystemOperationContext* context, 194 const storage::FileSystemURL& url, 195 base::File::Info* file_info, 196 base::FilePath* platform_path, 197 scoped_refptr<storage::ShareableFileReference>* file_ref); 198 199 MediaPathFilter* media_path_filter() { 200 return media_path_filter_; 201 } 202 203 private: 204 // Like GetLocalFilePath(), but always take media_path_filter() into 205 // consideration. If the media_path_filter() check fails, return 206 // Fila::FILE_ERROR_SECURITY. |local_file_path| does not have to exist. 207 base::File::Error GetFilteredLocalFilePath( 208 storage::FileSystemOperationContext* context, 209 const storage::FileSystemURL& file_system_url, 210 base::FilePath* local_file_path); 211 212 // Like GetLocalFilePath(), but if the file does not exist, then return 213 // |failure_error|. 214 // If |local_file_path| is a file, then take media_path_filter() into 215 // consideration. 216 // If the media_path_filter() check fails, return |failure_error|. 217 // If |local_file_path| is a directory, return File::FILE_OK. 218 base::File::Error GetFilteredLocalFilePathForExistingFileOrDirectory( 219 storage::FileSystemOperationContext* context, 220 const storage::FileSystemURL& file_system_url, 221 base::File::Error failure_error, 222 base::FilePath* local_file_path); 223 224 // Not owned, owned by the backend which owns this. 225 MediaPathFilter* const media_path_filter_; 226 227 base::WeakPtrFactory<NativeMediaFileUtil> weak_factory_; 228 229 DISALLOW_COPY_AND_ASSIGN(NativeMediaFileUtil); 230}; 231 232#endif // CHROME_BROWSER_MEDIA_GALLERIES_FILEAPI_NATIVE_MEDIA_FILE_UTIL_H_ 233