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_BROWSER_FILEAPI_FILEAPI_MESSAGE_FILTER_H_ 6#define CONTENT_BROWSER_FILEAPI_FILEAPI_MESSAGE_FILTER_H_ 7 8#include <map> 9#include <set> 10#include <string> 11 12#include "base/callback.h" 13#include "base/containers/hash_tables.h" 14#include "base/files/file_util_proxy.h" 15#include "base/memory/ref_counted.h" 16#include "base/memory/shared_memory.h" 17#include "content/browser/streams/stream.h" 18#include "content/browser/streams/stream_context.h" 19#include "content/common/content_export.h" 20#include "content/public/browser/browser_message_filter.h" 21#include "storage/browser/fileapi/file_system_context.h" 22#include "storage/browser/fileapi/file_system_operation_runner.h" 23#include "storage/common/blob/blob_data.h" 24#include "storage/common/fileapi/file_system_types.h" 25#include "storage/common/quota/quota_types.h" 26 27class GURL; 28 29namespace base { 30class FilePath; 31class Time; 32} 33 34namespace storage { 35class FileSystemURL; 36class FileSystemOperationRunner; 37struct DirectoryEntry; 38struct FileSystemInfo; 39} 40 41namespace net { 42class URLRequestContext; 43class URLRequestContextGetter; 44} // namespace net 45 46namespace content { 47class BlobStorageHost; 48} 49 50namespace storage { 51class ShareableFileReference; 52} 53 54namespace content { 55class ChildProcessSecurityPolicyImpl; 56class ChromeBlobStorageContext; 57 58// TODO(tyoshino): Factor out code except for IPC gluing from 59// FileAPIMessageFilter into separate classes. See crbug.com/263741. 60class CONTENT_EXPORT FileAPIMessageFilter : public BrowserMessageFilter { 61 public: 62 // Used by the renderer process host on the UI thread. 63 FileAPIMessageFilter(int process_id, 64 net::URLRequestContextGetter* request_context_getter, 65 storage::FileSystemContext* file_system_context, 66 ChromeBlobStorageContext* blob_storage_context, 67 StreamContext* stream_context); 68 // Used by the worker process host on the IO thread. 69 FileAPIMessageFilter(int process_id, 70 net::URLRequestContext* request_context, 71 storage::FileSystemContext* file_system_context, 72 ChromeBlobStorageContext* blob_storage_context, 73 StreamContext* stream_context); 74 75 // BrowserMessageFilter implementation. 76 virtual void OnChannelConnected(int32 peer_pid) OVERRIDE; 77 virtual void OnChannelClosing() OVERRIDE; 78 virtual base::TaskRunner* OverrideTaskRunnerForMessage( 79 const IPC::Message& message) OVERRIDE; 80 virtual bool OnMessageReceived(const IPC::Message& message) OVERRIDE; 81 82 protected: 83 virtual ~FileAPIMessageFilter(); 84 85 virtual void BadMessageReceived() OVERRIDE; 86 87 private: 88 typedef storage::FileSystemOperationRunner::OperationID OperationID; 89 90 void OnOpenFileSystem(int request_id, 91 const GURL& origin_url, 92 storage::FileSystemType type); 93 void OnResolveURL(int request_id, 94 const GURL& filesystem_url); 95 void OnDeleteFileSystem(int request_id, 96 const GURL& origin_url, 97 storage::FileSystemType type); 98 void OnMove(int request_id, 99 const GURL& src_path, 100 const GURL& dest_path); 101 void OnCopy(int request_id, 102 const GURL& src_path, 103 const GURL& dest_path); 104 void OnRemove(int request_id, const GURL& path, bool recursive); 105 void OnReadMetadata(int request_id, const GURL& path); 106 void OnCreate(int request_id, 107 const GURL& path, 108 bool exclusive, 109 bool is_directory, 110 bool recursive); 111 void OnExists(int request_id, const GURL& path, bool is_directory); 112 void OnReadDirectory(int request_id, const GURL& path); 113 void OnWrite(int request_id, 114 const GURL& path, 115 const std::string& blob_uuid, 116 int64 offset); 117 void OnTruncate(int request_id, const GURL& path, int64 length); 118 void OnTouchFile(int request_id, 119 const GURL& path, 120 const base::Time& last_access_time, 121 const base::Time& last_modified_time); 122 void OnCancel(int request_id, int request_to_cancel); 123 void OnSyncGetPlatformPath(const GURL& path, 124 base::FilePath* platform_path); 125 void OnCreateSnapshotFile(int request_id, 126 const GURL& path); 127 void OnDidReceiveSnapshotFile(int request_id); 128 129 // Handlers for BlobHostMsg_ family messages. 130 131 void OnStartBuildingBlob(const std::string& uuid); 132 void OnAppendBlobDataItemToBlob(const std::string& uuid, 133 const storage::BlobData::Item& item); 134 void OnAppendSharedMemoryToBlob(const std::string& uuid, 135 base::SharedMemoryHandle handle, 136 size_t buffer_size); 137 void OnFinishBuildingBlob(const std::string& uuid, 138 const std::string& content_type); 139 void OnIncrementBlobRefCount(const std::string& uuid); 140 void OnDecrementBlobRefCount(const std::string& uuid); 141 void OnRegisterPublicBlobURL(const GURL& public_url, const std::string& uuid); 142 void OnRevokePublicBlobURL(const GURL& public_url); 143 144 // Handlers for StreamHostMsg_ family messages. 145 // 146 // TODO(tyoshino): Consider renaming BlobData to more generic one as it's now 147 // used for Stream. 148 149 // Currently |content_type| is ignored. 150 // 151 // TODO(tyoshino): Set |content_type| to the stream. 152 void OnStartBuildingStream(const GURL& url, const std::string& content_type); 153 void OnAppendBlobDataItemToStream(const GURL& url, 154 const storage::BlobData::Item& item); 155 void OnAppendSharedMemoryToStream( 156 const GURL& url, base::SharedMemoryHandle handle, size_t buffer_size); 157 void OnFinishBuildingStream(const GURL& url); 158 void OnAbortBuildingStream(const GURL& url); 159 void OnCloneStream(const GURL& url, const GURL& src_url); 160 void OnRemoveStream(const GURL& url); 161 162 // Callback functions to be used when each file operation is finished. 163 void DidFinish(int request_id, base::File::Error result); 164 void DidGetMetadata(int request_id, 165 base::File::Error result, 166 const base::File::Info& info); 167 void DidGetMetadataForStreaming(int request_id, 168 base::File::Error result, 169 const base::File::Info& info); 170 void DidReadDirectory(int request_id, 171 base::File::Error result, 172 const std::vector<storage::DirectoryEntry>& entries, 173 bool has_more); 174 void DidWrite(int request_id, 175 base::File::Error result, 176 int64 bytes, 177 bool complete); 178 void DidOpenFileSystem(int request_id, 179 const GURL& root, 180 const std::string& filesystem_name, 181 base::File::Error result); 182 void DidResolveURL(int request_id, 183 base::File::Error result, 184 const storage::FileSystemInfo& info, 185 const base::FilePath& file_path, 186 storage::FileSystemContext::ResolvedEntryType type); 187 void DidDeleteFileSystem(int request_id, 188 base::File::Error result); 189 void DidCreateSnapshot( 190 int request_id, 191 const storage::FileSystemURL& url, 192 base::File::Error result, 193 const base::File::Info& info, 194 const base::FilePath& platform_path, 195 const scoped_refptr<storage::ShareableFileReference>& file_ref); 196 197 // Sends a FileSystemMsg_DidFail and returns false if |url| is invalid. 198 bool ValidateFileSystemURL(int request_id, const storage::FileSystemURL& url); 199 200 // Retrieves the Stream object for |url| from |stream_context_|. Returns unset 201 // scoped_refptr when there's no Stream instance for the given |url| 202 // registered with stream_context_->registry(). 203 scoped_refptr<Stream> GetStreamForURL(const GURL& url); 204 205 storage::FileSystemOperationRunner* operation_runner() { 206 return operation_runner_.get(); 207 } 208 209 int process_id_; 210 211 storage::FileSystemContext* context_; 212 ChildProcessSecurityPolicyImpl* security_policy_; 213 214 // Keeps map from request_id to OperationID for ongoing operations. 215 // (Primarily for Cancel operation) 216 typedef std::map<int, OperationID> OperationsMap; 217 OperationsMap operations_; 218 219 // The getter holds the context until OnChannelConnected() can be called from 220 // the IO thread, which will extract the net::URLRequestContext from it. 221 scoped_refptr<net::URLRequestContextGetter> request_context_getter_; 222 net::URLRequestContext* request_context_; 223 224 scoped_refptr<ChromeBlobStorageContext> blob_storage_context_; 225 scoped_refptr<StreamContext> stream_context_; 226 227 scoped_ptr<storage::FileSystemOperationRunner> operation_runner_; 228 229 // Keeps track of blobs used in this process and cleans up 230 // when the renderer process dies. 231 scoped_ptr<BlobStorageHost> blob_storage_host_; 232 233 // Keep track of stream URLs registered in this process. Need to unregister 234 // all of them when the renderer process dies. 235 base::hash_set<std::string> stream_urls_; 236 237 // Used to keep snapshot files alive while a DidCreateSnapshot 238 // is being sent to the renderer. 239 std::map<int, scoped_refptr<storage::ShareableFileReference> > 240 in_transit_snapshot_files_; 241 242 DISALLOW_COPY_AND_ASSIGN(FileAPIMessageFilter); 243}; 244 245} // namespace content 246 247#endif // CONTENT_BROWSER_FILEAPI_FILEAPI_MESSAGE_FILTER_H_ 248