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.
5 #ifndef CONTENT_RENDERER_PEPPER_PEPPER_FILE_IO_HOST_H_
6 #define CONTENT_RENDERER_PEPPER_PEPPER_FILE_IO_HOST_H_
10 #include "base/basictypes.h"
11 #include "base/callback_forward.h"
12 #include "base/memory/weak_ptr.h"
13 #include "base/platform_file.h"
14 #include "content/public/renderer/renderer_ppapi_host.h"
15 #include "ipc/ipc_listener.h"
16 #include "ipc/ipc_platform_file.h"
17 #include "ppapi/c/pp_file_info.h"
18 #include "ppapi/c/pp_time.h"
19 #include "ppapi/host/host_message_context.h"
20 #include "ppapi/host/resource_host.h"
21 #include "ppapi/shared_impl/file_io_state_manager.h"
23 #include "webkit/common/quota/quota_types.h"
25 using ppapi::host::ReplyMessageContext;
30 class PepperFileIOHost : public ppapi::host::ResourceHost,
31 public base::SupportsWeakPtr<PepperFileIOHost>,
32 public IPC::Listener {
34 typedef base::Callback<void (base::PlatformFileError)>
35 NotifyCloseFileCallback;
37 PepperFileIOHost(RendererPpapiHost* host,
39 PP_Resource resource);
40 virtual ~PepperFileIOHost();
42 // ppapi::host::ResourceHost override.
43 virtual int32_t OnResourceMessageReceived(
44 const IPC::Message& msg,
45 ppapi::host::HostMessageContext* context) OVERRIDE;
48 // IPC::Listener implementation.
49 virtual bool OnMessageReceived(const IPC::Message& message) OVERRIDE;
51 void OnAsyncFileOpened(
52 base::PlatformFileError error_code,
53 IPC::PlatformFileForTransit file_for_transit);
55 int32_t OnHostMsgOpen(ppapi::host::HostMessageContext* context,
56 PP_Resource file_ref_resource,
58 int32_t OnHostMsgTouch(ppapi::host::HostMessageContext* context,
59 PP_Time last_access_time,
60 PP_Time last_modified_time);
61 int32_t OnHostMsgWrite(ppapi::host::HostMessageContext* context,
63 const std::string& buffer);
64 int32_t OnHostMsgSetLength(ppapi::host::HostMessageContext* context,
66 int32_t OnHostMsgClose(ppapi::host::HostMessageContext* context);
67 int32_t OnHostMsgFlush(ppapi::host::HostMessageContext* context);
69 int32_t OnHostMsgRequestOSFileHandle(
70 ppapi::host::HostMessageContext* context);
72 // Callback handlers. These mostly convert the PlatformFileError to the
73 // PP_Error code and send back the reply. Note that the argument
74 // ReplyMessageContext is copied so that we have a closure containing all
75 // necessary information to reply.
76 void ExecutePlatformGeneralCallback(ReplyMessageContext reply_context,
77 base::PlatformFileError error_code);
78 void ExecutePlatformOpenFileCallback(ReplyMessageContext reply_context,
79 base::PlatformFileError error_code,
80 base::PassPlatformFile file);
81 void ExecutePlatformOpenFileSystemURLCallback(
82 ReplyMessageContext reply_context,
83 base::PlatformFileError error_code,
84 base::PassPlatformFile file,
85 quota::QuotaLimitType quota_policy,
86 const NotifyCloseFileCallback& callback);
87 void ExecutePlatformQueryCallback(ReplyMessageContext reply_context,
88 base::PlatformFileError error_code,
89 const base::PlatformFileInfo& file_info);
90 void ExecutePlatformReadCallback(ReplyMessageContext reply_context,
91 base::PlatformFileError error_code,
92 const char* data, int bytes_read);
93 void ExecutePlatformWriteCallback(ReplyMessageContext reply_context,
94 base::PlatformFileError error_code,
97 RendererPpapiHost* renderer_ppapi_host_;
98 base::PlatformFile file_;
100 // The file system type specified in the Open() call. This will be
101 // PP_FILESYSTEMTYPE_INVALID before open was called. This value does not
102 // indicate that the open command actually succeeded.
103 PP_FileSystemType file_system_type_;
105 // Valid only for PP_FILESYSTEMTYPE_LOCAL{PERSISTENT,TEMPORARY}.
106 GURL file_system_url_;
108 // Used to check if we can pass file handle to plugins.
109 quota::QuotaLimitType quota_policy_;
111 // Callback function for notifying when the file handle is closed.
112 NotifyCloseFileCallback notify_close_file_callback_;
114 // Pointer to a QuotaFileIO instance, which is valid only while a file
115 // of type PP_FILESYSTEMTYPE_LOCAL{PERSISTENT,TEMPORARY} is opened.
116 scoped_ptr<QuotaFileIO> quota_file_io_;
120 ppapi::FileIOStateManager state_manager_;
124 base::Callback<void(base::PlatformFileError, base::PassPlatformFile)>
125 pending_open_callback_;
127 base::WeakPtrFactory<PepperFileIOHost> weak_factory_;
129 DISALLOW_COPY_AND_ASSIGN(PepperFileIOHost);
132 } // namespace content
134 #endif // CONTENT_RENDERER_PEPPER_PEPPER_FILE_IO_HOST_H_