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 CONTENT_CHILD_FILEAPI_FILE_SYSTEM_DISPATCHER_H_ 6 #define CONTENT_CHILD_FILEAPI_FILE_SYSTEM_DISPATCHER_H_ 7 8 #include <string> 9 #include <vector> 10 11 #include "base/basictypes.h" 12 #include "base/callback_forward.h" 13 #include "base/id_map.h" 14 #include "base/process/process.h" 15 #include "ipc/ipc_listener.h" 16 #include "ipc/ipc_platform_file.h" 17 #include "webkit/common/fileapi/file_system_types.h" 18 #include "webkit/common/quota/quota_types.h" 19 20 namespace base { 21 class FilePath; 22 } 23 24 namespace fileapi { 25 struct DirectoryEntry; 26 struct FileSystemInfo; 27 } 28 29 class GURL; 30 31 namespace content { 32 33 // Dispatches and sends file system related messages sent to/from a child 34 // process from/to the main browser process. There is one instance 35 // per child process. Messages are dispatched on the main child thread. 36 class FileSystemDispatcher : public IPC::Listener { 37 public: 38 typedef base::Callback<void(base::File::Error error)> StatusCallback; 39 typedef base::Callback<void( 40 const base::File::Info& file_info)> MetadataCallback; 41 typedef base::Callback<void( 42 const base::File::Info& file_info, 43 const base::FilePath& platform_path, 44 int request_id)> CreateSnapshotFileCallback; 45 typedef base::Callback<void( 46 const std::vector<fileapi::DirectoryEntry>& entries, 47 bool has_more)> ReadDirectoryCallback; 48 typedef base::Callback<void( 49 const std::string& name, 50 const GURL& root)> OpenFileSystemCallback; 51 typedef base::Callback<void( 52 const fileapi::FileSystemInfo& info, 53 const base::FilePath& file_path, 54 bool is_directory)> ResolveURLCallback; 55 typedef base::Callback<void( 56 int64 bytes, 57 bool complete)> WriteCallback; 58 typedef base::Callback<void( 59 base::PlatformFile file, 60 int file_open_id, 61 quota::QuotaLimitType quota_policy)> OpenFileCallback; 62 63 FileSystemDispatcher(); 64 virtual ~FileSystemDispatcher(); 65 66 // IPC::Listener implementation. 67 virtual bool OnMessageReceived(const IPC::Message& msg) OVERRIDE; 68 69 void OpenFileSystem(const GURL& origin_url, 70 fileapi::FileSystemType type, 71 const OpenFileSystemCallback& success_callback, 72 const StatusCallback& error_callback); 73 void ResolveURL(const GURL& filesystem_url, 74 const ResolveURLCallback& success_callback, 75 const StatusCallback& error_callback); 76 void DeleteFileSystem(const GURL& origin_url, 77 fileapi::FileSystemType type, 78 const StatusCallback& callback); 79 void Move(const GURL& src_path, 80 const GURL& dest_path, 81 const StatusCallback& callback); 82 void Copy(const GURL& src_path, 83 const GURL& dest_path, 84 const StatusCallback& callback); 85 void Remove(const GURL& path, 86 bool recursive, 87 const StatusCallback& callback); 88 void ReadMetadata(const GURL& path, 89 const MetadataCallback& success_callback, 90 const StatusCallback& error_callback); 91 void CreateFile(const GURL& path, 92 bool exclusive, 93 const StatusCallback& callback); 94 void CreateDirectory(const GURL& path, 95 bool exclusive, 96 bool recursive, 97 const StatusCallback& callback); 98 void Exists(const GURL& path, 99 bool for_directory, 100 const StatusCallback& callback); 101 void ReadDirectory(const GURL& path, 102 const ReadDirectoryCallback& success_callback, 103 const StatusCallback& error_callback); 104 void Truncate(const GURL& path, 105 int64 offset, 106 int* request_id_out, 107 const StatusCallback& callback); 108 void Write(const GURL& path, 109 const std::string& blob_id, 110 int64 offset, 111 int* request_id_out, 112 const WriteCallback& success_callback, 113 const StatusCallback& error_callback); 114 void Cancel(int request_id_to_cancel, 115 const StatusCallback& callback); 116 void TouchFile(const GURL& file_path, 117 const base::Time& last_access_time, 118 const base::Time& last_modified_time, 119 const StatusCallback& callback); 120 121 // The caller must send FileSystemHostMsg_DidReceiveSnapshot message 122 // with |request_id| passed to |success_callback| after the snapshot file 123 // is successfully received. 124 void CreateSnapshotFile(const GURL& file_path, 125 const CreateSnapshotFileCallback& success_callback, 126 const StatusCallback& error_callback); 127 128 private: 129 class CallbackDispatcher; 130 131 // Message handlers. 132 void OnDidOpenFileSystem(int request_id, 133 const std::string& name, 134 const GURL& root); 135 void OnDidResolveURL(int request_id, 136 const fileapi::FileSystemInfo& info, 137 const base::FilePath& file_path, 138 bool is_directory); 139 void OnDidSucceed(int request_id); 140 void OnDidReadMetadata(int request_id, 141 const base::File::Info& file_info); 142 void OnDidCreateSnapshotFile(int request_id, 143 const base::File::Info& file_info, 144 const base::FilePath& platform_path); 145 void OnDidReadDirectory(int request_id, 146 const std::vector<fileapi::DirectoryEntry>& entries, 147 bool has_more); 148 void OnDidFail(int request_id, base::File::Error error_code); 149 void OnDidWrite(int request_id, int64 bytes, bool complete); 150 void OnDidOpenFile( 151 int request_id, 152 IPC::PlatformFileForTransit file, 153 int file_open_id, 154 quota::QuotaLimitType quota_policy); 155 156 IDMap<CallbackDispatcher, IDMapOwnPointer> dispatchers_; 157 158 DISALLOW_COPY_AND_ASSIGN(FileSystemDispatcher); 159 }; 160 161 } // namespace content 162 163 #endif // CONTENT_CHILD_FILEAPI_FILE_SYSTEM_DISPATCHER_H_ 164