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