| OLD | NEW |
| (Empty) |
| 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 #include "ppapi/proxy/ppb_file_system_proxy.h" | |
| 6 | |
| 7 #include "base/bind.h" | |
| 8 #include "base/message_loop.h" | |
| 9 #include "ppapi/c/pp_errors.h" | |
| 10 #include "ppapi/c/ppb_file_system.h" | |
| 11 #include "ppapi/proxy/enter_proxy.h" | |
| 12 #include "ppapi/proxy/host_dispatcher.h" | |
| 13 #include "ppapi/proxy/plugin_dispatcher.h" | |
| 14 #include "ppapi/proxy/ppapi_messages.h" | |
| 15 #include "ppapi/proxy/serialized_var.h" | |
| 16 #include "ppapi/shared_impl/tracked_callback.h" | |
| 17 #include "ppapi/thunk/enter.h" | |
| 18 #include "ppapi/thunk/ppb_file_system_api.h" | |
| 19 #include "ppapi/thunk/resource_creation_api.h" | |
| 20 #include "ppapi/thunk/thunk.h" | |
| 21 | |
| 22 using ppapi::thunk::PPB_FileSystem_API; | |
| 23 using ppapi::thunk::ResourceCreationAPI; | |
| 24 | |
| 25 namespace ppapi { | |
| 26 namespace proxy { | |
| 27 | |
| 28 namespace { | |
| 29 | |
| 30 InterfaceProxy* CreateFileSystemProxy(Dispatcher* dispatcher) { | |
| 31 return new PPB_FileSystem_Proxy(dispatcher); | |
| 32 } | |
| 33 | |
| 34 } // namespace | |
| 35 | |
| 36 // This object maintains most of the state of the ref in the plugin for fast | |
| 37 // querying. It's all set in the constructor from the "create info" sent from | |
| 38 // the host. | |
| 39 class FileSystem : public Resource, public PPB_FileSystem_API { | |
| 40 public: | |
| 41 FileSystem(const HostResource& host_resource, PP_FileSystemType type); | |
| 42 virtual ~FileSystem(); | |
| 43 | |
| 44 // Resource override. | |
| 45 virtual PPB_FileSystem_API* AsPPB_FileSystem_API() OVERRIDE; | |
| 46 | |
| 47 // PPB_FileSystem_APi implementation. | |
| 48 virtual int32_t Open(int64_t expected_size, | |
| 49 scoped_refptr<TrackedCallback> callback) OVERRIDE; | |
| 50 virtual PP_FileSystemType GetType() OVERRIDE; | |
| 51 | |
| 52 // Called when the host has responded to our open request. | |
| 53 void OpenComplete(int32_t result); | |
| 54 | |
| 55 private: | |
| 56 PP_FileSystemType type_; | |
| 57 bool called_open_; | |
| 58 scoped_refptr<TrackedCallback> current_open_callback_; | |
| 59 | |
| 60 DISALLOW_COPY_AND_ASSIGN(FileSystem); | |
| 61 }; | |
| 62 | |
| 63 FileSystem::FileSystem(const HostResource& host_resource, | |
| 64 PP_FileSystemType type) | |
| 65 : Resource(OBJECT_IS_PROXY, host_resource), | |
| 66 type_(type), | |
| 67 called_open_(false) { | |
| 68 } | |
| 69 | |
| 70 FileSystem::~FileSystem() { | |
| 71 } | |
| 72 | |
| 73 PPB_FileSystem_API* FileSystem::AsPPB_FileSystem_API() { | |
| 74 return this; | |
| 75 } | |
| 76 | |
| 77 int32_t FileSystem::Open(int64_t expected_size, | |
| 78 scoped_refptr<TrackedCallback> callback) { | |
| 79 if (TrackedCallback::IsPending(current_open_callback_)) | |
| 80 return PP_ERROR_INPROGRESS; | |
| 81 if (called_open_) | |
| 82 return PP_ERROR_FAILED; | |
| 83 | |
| 84 current_open_callback_ = callback; | |
| 85 called_open_ = true; | |
| 86 PluginDispatcher::GetForResource(this)->Send( | |
| 87 new PpapiHostMsg_PPBFileSystem_Open( | |
| 88 API_ID_PPB_FILE_SYSTEM, host_resource(), expected_size)); | |
| 89 return PP_OK_COMPLETIONPENDING; | |
| 90 } | |
| 91 | |
| 92 PP_FileSystemType FileSystem::GetType() { | |
| 93 return type_; | |
| 94 } | |
| 95 | |
| 96 void FileSystem::OpenComplete(int32_t result) { | |
| 97 current_open_callback_->Run(result); | |
| 98 } | |
| 99 | |
| 100 PPB_FileSystem_Proxy::PPB_FileSystem_Proxy(Dispatcher* dispatcher) | |
| 101 : InterfaceProxy(dispatcher), | |
| 102 callback_factory_(ALLOW_THIS_IN_INITIALIZER_LIST(this)) { | |
| 103 } | |
| 104 | |
| 105 PPB_FileSystem_Proxy::~PPB_FileSystem_Proxy() { | |
| 106 } | |
| 107 | |
| 108 const InterfaceProxy::Info* PPB_FileSystem_Proxy::GetInfo() { | |
| 109 static const Info info = { | |
| 110 thunk::GetPPB_FileSystem_1_0_Thunk(), | |
| 111 PPB_FILESYSTEM_INTERFACE_1_0, | |
| 112 API_ID_PPB_FILE_SYSTEM, | |
| 113 false, | |
| 114 &CreateFileSystemProxy, | |
| 115 }; | |
| 116 return &info; | |
| 117 } | |
| 118 | |
| 119 // static | |
| 120 PP_Resource PPB_FileSystem_Proxy::CreateProxyResource( | |
| 121 PP_Instance instance, | |
| 122 PP_FileSystemType type) { | |
| 123 PluginDispatcher* dispatcher = PluginDispatcher::GetForInstance(instance); | |
| 124 if (!dispatcher) | |
| 125 return PP_ERROR_BADARGUMENT; | |
| 126 | |
| 127 HostResource result; | |
| 128 dispatcher->Send(new PpapiHostMsg_PPBFileSystem_Create( | |
| 129 API_ID_PPB_FILE_SYSTEM, instance, type, &result)); | |
| 130 if (result.is_null()) | |
| 131 return 0; | |
| 132 return (new FileSystem(result, type))->GetReference(); | |
| 133 } | |
| 134 | |
| 135 bool PPB_FileSystem_Proxy::OnMessageReceived(const IPC::Message& msg) { | |
| 136 bool handled = true; | |
| 137 IPC_BEGIN_MESSAGE_MAP(PPB_FileSystem_Proxy, msg) | |
| 138 #if !defined(OS_NACL) | |
| 139 IPC_MESSAGE_HANDLER(PpapiHostMsg_PPBFileSystem_Create, OnMsgCreate) | |
| 140 IPC_MESSAGE_HANDLER(PpapiHostMsg_PPBFileSystem_Open, OnMsgOpen) | |
| 141 #endif | |
| 142 IPC_MESSAGE_HANDLER(PpapiMsg_PPBFileSystem_OpenComplete, OnMsgOpenComplete) | |
| 143 IPC_MESSAGE_UNHANDLED(handled = false) | |
| 144 IPC_END_MESSAGE_MAP() | |
| 145 return handled; | |
| 146 } | |
| 147 | |
| 148 #if !defined(OS_NACL) | |
| 149 void PPB_FileSystem_Proxy::OnMsgCreate(PP_Instance instance, | |
| 150 int type, | |
| 151 HostResource* result) { | |
| 152 thunk::EnterResourceCreation enter(instance); | |
| 153 if (enter.failed()) | |
| 154 return; | |
| 155 PP_Resource resource = enter.functions()->CreateFileSystem( | |
| 156 instance, static_cast<PP_FileSystemType>(type)); | |
| 157 if (!resource) | |
| 158 return; // CreateInfo default constructor initializes to 0. | |
| 159 result->SetHostResource(instance, resource); | |
| 160 } | |
| 161 | |
| 162 void PPB_FileSystem_Proxy::OnMsgOpen(const HostResource& host_resource, | |
| 163 int64_t expected_size) { | |
| 164 EnterHostFromHostResourceForceCallback<PPB_FileSystem_API> enter( | |
| 165 host_resource, callback_factory_, | |
| 166 &PPB_FileSystem_Proxy::OpenCompleteInHost, host_resource); | |
| 167 if (enter.succeeded()) | |
| 168 enter.SetResult(enter.object()->Open(expected_size, enter.callback())); | |
| 169 } | |
| 170 #endif // !defined(OS_NACL) | |
| 171 | |
| 172 // Called in the plugin to handle the open callback. | |
| 173 void PPB_FileSystem_Proxy::OnMsgOpenComplete(const HostResource& host_resource, | |
| 174 int32_t result) { | |
| 175 EnterPluginFromHostResource<PPB_FileSystem_API> enter(host_resource); | |
| 176 if (enter.succeeded()) | |
| 177 static_cast<FileSystem*>(enter.object())->OpenComplete(result); | |
| 178 } | |
| 179 | |
| 180 #if !defined(OS_NACL) | |
| 181 void PPB_FileSystem_Proxy::OpenCompleteInHost( | |
| 182 int32_t result, | |
| 183 const HostResource& host_resource) { | |
| 184 dispatcher()->Send(new PpapiMsg_PPBFileSystem_OpenComplete( | |
| 185 API_ID_PPB_FILE_SYSTEM, host_resource, result)); | |
| 186 } | |
| 187 #endif // !defined(OS_NACL) | |
| 188 | |
| 189 } // namespace proxy | |
| 190 } // namespace ppapi | |
| OLD | NEW |