Upstream version 10.39.225.0
[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 "storage/common/fileapi/file_system_types.h"
18 #include "storage/common/quota/quota_types.h"
19
20 namespace base {
21 class FilePath;
22 }
23
24 namespace storage {
25 struct DirectoryEntry;
26 struct FileSystemInfo;
27 }
28
29 class GURL;
30
31 namespace content {
32
33 // Dispatches and sends file system related messages sent to/from a child
34 // process from/to the main browser process.  There is one instance
35 // per child process.  Messages are dispatched on the main child thread.
36 class FileSystemDispatcher : public IPC::Listener {
37  public:
38   typedef base::Callback<void(base::File::Error error)> StatusCallback;
39   typedef base::Callback<void(
40       const base::File::Info& file_info)> MetadataCallback;
41   typedef base::Callback<void(
42       const base::File::Info& file_info,
43       const base::FilePath& platform_path,
44       int request_id)> CreateSnapshotFileCallback;
45   typedef base::Callback<
46       void(const std::vector<storage::DirectoryEntry>& entries, bool has_more)>
47       ReadDirectoryCallback;
48   typedef base::Callback<void(
49       const std::string& name,
50       const GURL& root)> OpenFileSystemCallback;
51   typedef base::Callback<void(const storage::FileSystemInfo& info,
52                               const base::FilePath& file_path,
53                               bool is_directory)> ResolveURLCallback;
54   typedef base::Callback<void(
55       int64 bytes,
56       bool complete)> WriteCallback;
57   typedef base::Callback<void(base::PlatformFile file,
58                               int file_open_id,
59                               storage::QuotaLimitType quota_policy)>
60       OpenFileCallback;
61
62   FileSystemDispatcher();
63   virtual ~FileSystemDispatcher();
64
65   // IPC::Listener implementation.
66   virtual bool OnMessageReceived(const IPC::Message& msg) OVERRIDE;
67
68   void OpenFileSystem(const GURL& origin_url,
69                       storage::FileSystemType type,
70                       const OpenFileSystemCallback& success_callback,
71                       const StatusCallback& error_callback);
72   void ResolveURL(const GURL& filesystem_url,
73                   const ResolveURLCallback& success_callback,
74                   const StatusCallback& error_callback);
75   void DeleteFileSystem(const GURL& origin_url,
76                         storage::FileSystemType type,
77                         const StatusCallback& callback);
78   void Move(const GURL& src_path,
79             const GURL& dest_path,
80             const StatusCallback& callback);
81   void Copy(const GURL& src_path,
82             const GURL& dest_path,
83             const StatusCallback& callback);
84   void Remove(const GURL& path,
85               bool recursive,
86               const StatusCallback& callback);
87   void ReadMetadata(const GURL& path,
88                     const MetadataCallback& success_callback,
89                     const StatusCallback& error_callback);
90   void CreateFile(const GURL& path,
91                   bool exclusive,
92                   const StatusCallback& callback);
93   void CreateDirectory(const GURL& path,
94                        bool exclusive,
95                        bool recursive,
96                        const StatusCallback& callback);
97   void Exists(const GURL& path,
98               bool for_directory,
99               const StatusCallback& callback);
100   void ReadDirectory(const GURL& path,
101                      const ReadDirectoryCallback& success_callback,
102                      const StatusCallback& error_callback);
103   void Truncate(const GURL& path,
104                 int64 offset,
105                 int* request_id_out,
106                 const StatusCallback& callback);
107   void Write(const GURL& path,
108              const std::string& blob_id,
109              int64 offset,
110              int* request_id_out,
111              const WriteCallback& success_callback,
112              const StatusCallback& error_callback);
113   void Cancel(int request_id_to_cancel,
114               const StatusCallback& callback);
115   void TouchFile(const GURL& file_path,
116                  const base::Time& last_access_time,
117                  const base::Time& last_modified_time,
118                  const StatusCallback& callback);
119
120   // The caller must send FileSystemHostMsg_DidReceiveSnapshot message
121   // with |request_id| passed to |success_callback| after the snapshot file
122   // is successfully received.
123   void CreateSnapshotFile(const GURL& file_path,
124                           const CreateSnapshotFileCallback& success_callback,
125                           const StatusCallback& error_callback);
126
127  private:
128   class CallbackDispatcher;
129
130   // Message handlers.
131   void OnDidOpenFileSystem(int request_id,
132                            const std::string& name,
133                            const GURL& root);
134   void OnDidResolveURL(int request_id,
135                        const storage::FileSystemInfo& info,
136                        const base::FilePath& file_path,
137                        bool is_directory);
138   void OnDidSucceed(int request_id);
139   void OnDidReadMetadata(int request_id,
140                          const base::File::Info& file_info);
141   void OnDidCreateSnapshotFile(int request_id,
142                                const base::File::Info& file_info,
143                                const base::FilePath& platform_path);
144   void OnDidReadDirectory(int request_id,
145                           const std::vector<storage::DirectoryEntry>& entries,
146                           bool has_more);
147   void OnDidFail(int request_id, base::File::Error error_code);
148   void OnDidWrite(int request_id, int64 bytes, bool complete);
149
150   IDMap<CallbackDispatcher, IDMapOwnPointer> dispatchers_;
151
152   DISALLOW_COPY_AND_ASSIGN(FileSystemDispatcher);
153 };
154
155 }  // namespace content
156
157 #endif  // CONTENT_CHILD_FILEAPI_FILE_SYSTEM_DISPATCHER_H_