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_RENDERER_PEPPER_PEPPER_FILE_IO_HOST_H_ 6 #define CONTENT_RENDERER_PEPPER_PEPPER_FILE_IO_HOST_H_ 7 8 #include <set> 9 #include <string> 10 11 #include "base/basictypes.h" 12 #include "base/callback_forward.h" 13 #include "base/id_map.h" 14 #include "base/memory/weak_ptr.h" 15 #include "base/platform_file.h" 16 #include "content/public/renderer/renderer_ppapi_host.h" 17 #include "ipc/ipc_listener.h" 18 #include "ipc/ipc_platform_file.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" 22 #include "ppapi/thunk/ppb_file_ref_api.h" 23 #include "url/gurl.h" 24 #include "webkit/common/quota/quota_types.h" 25 26 using ppapi::host::ReplyMessageContext; 27 28 namespace content { 29 class QuotaFileIO; 30 31 class PepperFileIOHost : public ppapi::host::ResourceHost, 32 public base::SupportsWeakPtr<PepperFileIOHost>, 33 public IPC::Listener { 34 public: 35 typedef base::Callback<void (base::PlatformFileError)> 36 NotifyCloseFileCallback; 37 38 PepperFileIOHost(RendererPpapiHost* host, 39 PP_Instance instance, 40 PP_Resource resource); 41 virtual ~PepperFileIOHost(); 42 43 // ppapi::host::ResourceHost override. 44 virtual int32_t OnResourceMessageReceived( 45 const IPC::Message& msg, 46 ppapi::host::HostMessageContext* context) OVERRIDE; 47 48 private: 49 // IPC::Listener implementation. 50 virtual bool OnMessageReceived(const IPC::Message& message) OVERRIDE; 51 52 void OnAsyncFileOpened( 53 base::PlatformFileError error_code, 54 IPC::PlatformFileForTransit file_for_transit, 55 int message_id); 56 57 int32_t OnHostMsgOpen(ppapi::host::HostMessageContext* context, 58 PP_Resource file_ref_resource, 59 int32_t open_flags); 60 int32_t OnHostMsgTouch(ppapi::host::HostMessageContext* context, 61 PP_Time last_access_time, 62 PP_Time last_modified_time); 63 int32_t OnHostMsgWrite(ppapi::host::HostMessageContext* context, 64 int64_t offset, 65 const std::string& buffer); 66 int32_t OnHostMsgSetLength(ppapi::host::HostMessageContext* context, 67 int64_t length); 68 int32_t OnHostMsgClose(ppapi::host::HostMessageContext* context); 69 int32_t OnHostMsgFlush(ppapi::host::HostMessageContext* context); 70 // Private API. 71 int32_t OnHostMsgRequestOSFileHandle( 72 ppapi::host::HostMessageContext* context); 73 // Trusted API. 74 int32_t OnHostMsgGetOSFileDescriptor( 75 ppapi::host::HostMessageContext* context); 76 int32_t OnHostMsgWillWrite(ppapi::host::HostMessageContext* context, 77 int64_t offset, 78 int32_t bytes_to_write); 79 int32_t OnHostMsgWillSetLength(ppapi::host::HostMessageContext* context, 80 int64_t length); 81 82 // Callback handlers. These mostly convert the PlatformFileError to the 83 // PP_Error code and send back the reply. Note that the argument 84 // ReplyMessageContext is copied so that we have a closure containing all 85 // necessary information to reply. 86 void ExecutePlatformGeneralCallback(ReplyMessageContext reply_context, 87 base::PlatformFileError error_code); 88 void ExecutePlatformOpenFileCallback(ReplyMessageContext reply_context, 89 base::PlatformFileError error_code, 90 base::PassPlatformFile file); 91 void ExecutePlatformOpenFileSystemURLCallback( 92 ReplyMessageContext reply_context, 93 base::PlatformFileError error_code, 94 base::PassPlatformFile file, 95 quota::QuotaLimitType quota_policy, 96 const NotifyCloseFileCallback& callback); 97 void ExecutePlatformQueryCallback(ReplyMessageContext reply_context, 98 base::PlatformFileError error_code, 99 const base::PlatformFileInfo& file_info); 100 void ExecutePlatformReadCallback(ReplyMessageContext reply_context, 101 base::PlatformFileError error_code, 102 const char* data, int bytes_read); 103 void ExecutePlatformWriteCallback(ReplyMessageContext reply_context, 104 base::PlatformFileError error_code, 105 int bytes_written); 106 107 RendererPpapiHost* renderer_ppapi_host_; 108 109 base::PlatformFile file_; 110 111 // The file system type specified in the Open() call. This will be 112 // PP_FILESYSTEMTYPE_INVALID before open was called. This value does not 113 // indicate that the open command actually succeeded. 114 PP_FileSystemType file_system_type_; 115 116 // Valid only for PP_FILESYSTEMTYPE_LOCAL{PERSISTENT,TEMPORARY}. 117 GURL file_system_url_; 118 119 // Used to check if we can pass file handle to plugins. 120 quota::QuotaLimitType quota_policy_; 121 122 // Callback function for notifying when the file handle is closed. 123 NotifyCloseFileCallback notify_close_file_callback_; 124 125 // Pointer to a QuotaFileIO instance, which is valid only while a file 126 // of type PP_FILESYSTEMTYPE_LOCAL{PERSISTENT,TEMPORARY} is opened. 127 scoped_ptr<QuotaFileIO> quota_file_io_; 128 129 bool is_running_in_process_; 130 131 int32_t open_flags_; 132 133 base::WeakPtrFactory<PepperFileIOHost> weak_factory_; 134 135 ppapi::FileIOStateManager state_manager_; 136 137 int routing_id_; 138 139 typedef base::Callback<void (base::PlatformFileError, base::PassPlatformFile)> 140 AsyncOpenFileCallback; 141 142 IDMap<AsyncOpenFileCallback> pending_async_open_files_; 143 144 DISALLOW_COPY_AND_ASSIGN(PepperFileIOHost); 145 }; 146 147 } // namespace content 148 149 #endif // CONTENT_RENDERER_PEPPER_PEPPER_FILE_IO_HOST_H_ 150