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