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