- add sources.
[platform/framework/web/crosswalk.git] / src / content / child / fileapi / file_system_dispatcher.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_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 struct PlatformFileInfo;
23 }
24
25 namespace fileapi {
26 struct DirectoryEntry;
27 struct FileSystemInfo;
28 }
29
30 class GURL;
31
32 namespace content {
33
34 // Dispatches and sends file system related messages sent to/from a child
35 // process from/to the main browser process.  There is one instance
36 // per child process.  Messages are dispatched on the main child thread.
37 class FileSystemDispatcher : public IPC::Listener {
38  public:
39   typedef base::Callback<void(base::PlatformFileError error)> StatusCallback;
40   typedef base::Callback<void(
41       const base::PlatformFileInfo& file_info)> MetadataCallback;
42   typedef base::Callback<void(
43       const base::PlatformFileInfo& file_info,
44       const base::FilePath& platform_path,
45       int request_id)> CreateSnapshotFileCallback;
46   typedef base::Callback<void(
47       const std::vector<fileapi::DirectoryEntry>& entries,
48       bool has_more)> ReadDirectoryCallback;
49   typedef base::Callback<void(
50       const std::string& name,
51       const GURL& root)> OpenFileSystemCallback;
52   typedef base::Callback<void(
53       const fileapi::FileSystemInfo& info,
54       const base::FilePath& file_path,
55       bool is_directory)> ResolveURLCallback;
56   typedef base::Callback<void(
57       int64 bytes,
58       bool complete)> WriteCallback;
59   typedef base::Callback<void(
60       base::PlatformFile file,
61       int file_open_id,
62       quota::QuotaLimitType quota_policy)> OpenFileCallback;
63
64   FileSystemDispatcher();
65   virtual ~FileSystemDispatcher();
66
67   // IPC::Listener implementation.
68   virtual bool OnMessageReceived(const IPC::Message& msg) OVERRIDE;
69
70   void OpenFileSystem(const GURL& origin_url,
71                       fileapi::FileSystemType type,
72                       const OpenFileSystemCallback& success_callback,
73                       const StatusCallback& error_callback);
74   void ResolveURL(const GURL& filesystem_url,
75                   const ResolveURLCallback& success_callback,
76                   const StatusCallback& error_callback);
77   void DeleteFileSystem(const GURL& origin_url,
78                         fileapi::FileSystemType type,
79                         const StatusCallback& callback);
80   void Move(const GURL& src_path,
81             const GURL& dest_path,
82             const StatusCallback& callback);
83   void Copy(const GURL& src_path,
84             const GURL& dest_path,
85             const StatusCallback& callback);
86   void Remove(const GURL& path,
87               bool recursive,
88               const StatusCallback& callback);
89   void ReadMetadata(const GURL& path,
90                     const MetadataCallback& success_callback,
91                     const StatusCallback& error_callback);
92   void CreateFile(const GURL& path,
93                   bool exclusive,
94                   const StatusCallback& callback);
95   void CreateDirectory(const GURL& path,
96                        bool exclusive,
97                        bool recursive,
98                        const StatusCallback& callback);
99   void Exists(const GURL& path,
100               bool for_directory,
101               const StatusCallback& callback);
102   void ReadDirectory(const GURL& path,
103                      const ReadDirectoryCallback& success_callback,
104                      const StatusCallback& error_callback);
105   void Truncate(const GURL& path,
106                 int64 offset,
107                 int* request_id_out,
108                 const StatusCallback& callback);
109   void WriteDeprecated(
110       const GURL& path,
111       const GURL& blob_url,
112       int64 offset,
113       int* request_id_out,
114       const WriteCallback& success_callback,
115       const StatusCallback& error_callback);
116   void Write(const GURL& path,
117              const std::string& blob_id,
118              int64 offset,
119              int* request_id_out,
120              const WriteCallback& success_callback,
121              const StatusCallback& error_callback);
122   void Cancel(int request_id_to_cancel,
123               const StatusCallback& callback);
124   void TouchFile(const GURL& file_path,
125                  const base::Time& last_access_time,
126                  const base::Time& last_modified_time,
127                  const StatusCallback& callback);
128
129   // This returns a raw open PlatformFile, unlike the above, which are
130   // self-contained operations.
131   void OpenPepperFile(const GURL& file_path,
132                       int pp_open_flags,
133                       const OpenFileCallback& success_callback,
134                       const StatusCallback& error_callback);
135   // This must be paired with OpenFile, and called after finished using the
136   // raw PlatformFile returned from OpenFile.
137   void NotifyCloseFile(int file_open_id);
138
139   // The caller must send FileSystemHostMsg_DidReceiveSnapshot message
140   // with |request_id| passed to |success_callback| after the snapshot file
141   // is successfully received.
142   void CreateSnapshotFile(const GURL& file_path,
143                           const CreateSnapshotFileCallback& success_callback,
144                           const StatusCallback& error_callback);
145
146  private:
147   class CallbackDispatcher;
148
149   // Message handlers.
150   void OnDidOpenFileSystem(int request_id,
151                            const std::string& name,
152                            const GURL& root);
153   void OnDidResolveURL(int request_id,
154                        const fileapi::FileSystemInfo& info,
155                        const base::FilePath& file_path,
156                        bool is_directory);
157   void OnDidSucceed(int request_id);
158   void OnDidReadMetadata(int request_id,
159                          const base::PlatformFileInfo& file_info);
160   void OnDidCreateSnapshotFile(int request_id,
161                                const base::PlatformFileInfo& file_info,
162                                const base::FilePath& platform_path);
163   void OnDidReadDirectory(int request_id,
164                           const std::vector<fileapi::DirectoryEntry>& entries,
165                           bool has_more);
166   void OnDidFail(int request_id, base::PlatformFileError error_code);
167   void OnDidWrite(int request_id, int64 bytes, bool complete);
168   void OnDidOpenFile(
169       int request_id,
170       IPC::PlatformFileForTransit file,
171       int file_open_id,
172       quota::QuotaLimitType quota_policy);
173
174   IDMap<CallbackDispatcher, IDMapOwnPointer> dispatchers_;
175
176   DISALLOW_COPY_AND_ASSIGN(FileSystemDispatcher);
177 };
178
179 }  // namespace content
180
181 #endif  // CONTENT_CHILD_FILEAPI_FILE_SYSTEM_DISPATCHER_H_