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