Upstream version 5.34.104.0
[platform/framework/web/crosswalk.git] / src / content / browser / renderer_host / pepper / pepper_file_io_host.h
1 // Copyright 2013 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_RENDERER_PEPPER_PEPPER_FILE_IO_HOST_H_
6 #define CONTENT_RENDERER_PEPPER_PEPPER_FILE_IO_HOST_H_
7
8 #include <string>
9
10 #include "base/basictypes.h"
11 #include "base/callback_forward.h"
12 #include "base/files/file.h"
13 #include "base/memory/weak_ptr.h"
14 #include "base/platform_file.h"
15 #include "content/browser/renderer_host/pepper/browser_ppapi_host_impl.h"
16 #include "ipc/ipc_listener.h"
17 #include "ipc/ipc_platform_file.h"
18 #include "ppapi/c/pp_file_info.h"
19 #include "ppapi/c/pp_time.h"
20 #include "ppapi/host/host_message_context.h"
21 #include "ppapi/host/resource_host.h"
22 #include "ppapi/shared_impl/file_io_state_manager.h"
23 #include "url/gurl.h"
24 #include "webkit/browser/fileapi/file_system_context.h"
25
26 namespace ppapi {
27 struct FileGrowth;
28 }
29
30 namespace content {
31 class PepperFileSystemBrowserHost;
32
33 class PepperFileIOHost : public ppapi::host::ResourceHost,
34                          public base::SupportsWeakPtr<PepperFileIOHost> {
35  public:
36   typedef base::Callback<void (base::File::Error)>
37       NotifyCloseFileCallback;
38
39   PepperFileIOHost(BrowserPpapiHostImpl* host,
40                    PP_Instance instance,
41                    PP_Resource resource);
42   virtual ~PepperFileIOHost();
43
44   // ppapi::host::ResourceHost override.
45   virtual int32_t OnResourceMessageReceived(
46       const IPC::Message& msg,
47       ppapi::host::HostMessageContext* context) OVERRIDE;
48
49   struct UIThreadStuff {
50     UIThreadStuff();
51     ~UIThreadStuff();
52     base::ProcessId resolved_render_process_id;
53     scoped_refptr<fileapi::FileSystemContext> file_system_context;
54   };
55  private:
56   int32_t OnHostMsgOpen(ppapi::host::HostMessageContext* context,
57                         PP_Resource file_ref_resource,
58                         int32_t open_flags);
59   int32_t OnHostMsgTouch(ppapi::host::HostMessageContext* context,
60                          PP_Time last_access_time,
61                          PP_Time last_modified_time);
62   int32_t OnHostMsgSetLength(ppapi::host::HostMessageContext* context,
63                              int64_t length);
64   int32_t OnHostMsgClose(ppapi::host::HostMessageContext* context,
65                          const ppapi::FileGrowth& file_growth);
66   int32_t OnHostMsgFlush(ppapi::host::HostMessageContext* context);
67   int32_t OnHostMsgRequestOSFileHandle(
68       ppapi::host::HostMessageContext* context);
69
70   void GotPluginAllowedToCallRequestOSFileHandle(
71       ppapi::host::ReplyMessageContext reply_context,
72       bool plugin_allowed);
73
74   // Callback handlers. These mostly convert the File::Error to the
75   // PP_Error code and send back the reply. Note that the argument
76   // ReplyMessageContext is copied so that we have a closure containing all
77   // necessary information to reply.
78   void ExecutePlatformGeneralCallback(
79       ppapi::host::ReplyMessageContext reply_context,
80       base::File::Error error_code);
81   void ExecutePlatformOpenFileCallback(
82       ppapi::host::ReplyMessageContext reply_context,
83       base::File::Error error_code,
84       base::PassPlatformFile file,
85       bool unused_created);
86
87   void GotUIThreadStuffForInternalFileSystems(
88       ppapi::host::ReplyMessageContext reply_context,
89       int platform_file_flags,
90       UIThreadStuff ui_thread_stuff);
91   void DidOpenInternalFile(
92       ppapi::host::ReplyMessageContext reply_context,
93       base::File::Error result,
94       base::PlatformFile file,
95       const base::Closure& on_close_callback);
96   void GotResolvedRenderProcessId(
97       ppapi::host::ReplyMessageContext reply_context,
98       base::FilePath path,
99       int platform_file_flags,
100       base::ProcessId resolved_render_process_id);
101
102   void DidOpenQuotaFile(ppapi::host::ReplyMessageContext reply_context,
103                         base::PlatformFile file,
104                         int64_t max_written_offset);
105   bool CallSetLength(ppapi::host::ReplyMessageContext reply_context,
106                      int64_t length);
107
108   void DidCloseFile(base::File::Error error);
109
110   void SendOpenErrorReply(ppapi::host::ReplyMessageContext reply_context);
111
112   // Adds file_ to |reply_context| with the specified |open_flags|.
113   bool AddFileToReplyContext(
114       int32_t open_flags,
115       ppapi::host::ReplyMessageContext* reply_context) const;
116
117   BrowserPpapiHostImpl* browser_ppapi_host_;
118
119   RenderProcessHost* render_process_host_;
120   int render_process_id_;
121   base::ProcessId resolved_render_process_id_;
122
123   base::PlatformFile file_;
124   int32_t open_flags_;
125
126   // The file system type specified in the Open() call. This will be
127   // PP_FILESYSTEMTYPE_INVALID before open was called. This value does not
128   // indicate that the open command actually succeeded.
129   PP_FileSystemType file_system_type_;
130   base::WeakPtr<PepperFileSystemBrowserHost> file_system_host_;
131
132   // Valid only for PP_FILESYSTEMTYPE_LOCAL{PERSISTENT,TEMPORARY}.
133   scoped_refptr<fileapi::FileSystemContext> file_system_context_;
134   fileapi::FileSystemURL file_system_url_;
135   base::Closure on_close_callback_;
136   int64_t max_written_offset_;
137   bool check_quota_;
138
139   ppapi::FileIOStateManager state_manager_;
140
141   scoped_refptr<base::MessageLoopProxy> file_message_loop_;
142
143   base::WeakPtrFactory<PepperFileIOHost> weak_factory_;
144
145   DISALLOW_COPY_AND_ASSIGN(PepperFileIOHost);
146 };
147
148 }  // namespace content
149
150 #endif  // CONTENT_RENDERER_PEPPER_PEPPER_FILE_IO_HOST_H_