| OLD | NEW |
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "ppapi/proxy/ppb_file_system_proxy.h" | 5 #include "ppapi/proxy/ppb_file_system_proxy.h" |
| 6 | 6 |
| 7 #include "base/message_loop.h" | 7 #include "base/message_loop.h" |
| 8 #include "base/task.h" | 8 #include "base/task.h" |
| 9 #include "ppapi/c/dev/ppb_file_system_dev.h" | 9 #include "ppapi/c/dev/ppb_file_system_dev.h" |
| 10 #include "ppapi/c/pp_errors.h" | 10 #include "ppapi/c/pp_errors.h" |
| 11 #include "ppapi/proxy/enter_proxy.h" | |
| 12 #include "ppapi/proxy/host_dispatcher.h" | 11 #include "ppapi/proxy/host_dispatcher.h" |
| 13 #include "ppapi/proxy/plugin_dispatcher.h" | 12 #include "ppapi/proxy/plugin_dispatcher.h" |
| 14 #include "ppapi/proxy/plugin_resource.h" | 13 #include "ppapi/proxy/plugin_resource.h" |
| 15 #include "ppapi/proxy/ppapi_messages.h" | 14 #include "ppapi/proxy/ppapi_messages.h" |
| 16 #include "ppapi/proxy/serialized_var.h" | 15 #include "ppapi/proxy/serialized_var.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::EnterFunctionNoLock; | |
| 23 using ppapi::thunk::PPB_FileSystem_API; | |
| 24 using ppapi::thunk::ResourceCreationAPI; | |
| 25 | 16 |
| 26 namespace pp { | 17 namespace pp { |
| 27 namespace proxy { | 18 namespace proxy { |
| 28 | 19 |
| 29 namespace { | |
| 30 | |
| 31 InterfaceProxy* CreateFileSystemProxy(Dispatcher* dispatcher, | |
| 32 const void* target_interface) { | |
| 33 return new PPB_FileSystem_Proxy(dispatcher, target_interface); | |
| 34 } | |
| 35 | |
| 36 } // namespace | |
| 37 | |
| 38 // This object maintains most of the state of the ref in the plugin for fast | 20 // This object maintains most of the state of the ref in the plugin for fast |
| 39 // querying. It's all set in the constructor from the "create info" sent from | 21 // querying. It's all set in the constructor from the "create info" sent from |
| 40 // the host. | 22 // the host. |
| 41 class FileSystem : public PluginResource, public PPB_FileSystem_API { | 23 class FileSystem : public PluginResource { |
| 42 public: | 24 public: |
| 43 FileSystem(const HostResource& host_resource, PP_FileSystemType_Dev type); | 25 FileSystem(const HostResource& host_resource, PP_FileSystemType_Dev type); |
| 44 virtual ~FileSystem(); | 26 virtual ~FileSystem(); |
| 45 | 27 |
| 46 // ResourceObjectBase override. | 28 virtual FileSystem* AsFileSystem(); |
| 47 virtual PPB_FileSystem_API* AsPPB_FileSystem_API() OVERRIDE; | |
| 48 | 29 |
| 49 // PPB_FileSystem_APi implementation. | |
| 50 virtual int32_t Open(int64_t expected_size, | |
| 51 PP_CompletionCallback callback) OVERRIDE; | |
| 52 virtual PP_FileSystemType_Dev GetType() OVERRIDE; | |
| 53 | |
| 54 // Called when the host has responded to our open request. | |
| 55 void OpenComplete(int32_t result); | |
| 56 | |
| 57 private: | |
| 58 PP_FileSystemType_Dev type_; | 30 PP_FileSystemType_Dev type_; |
| 59 bool called_open_; | 31 bool called_open_; |
| 60 PP_CompletionCallback current_open_callback_; | 32 PP_CompletionCallback current_open_callback_; |
| 61 | 33 |
| 34 private: |
| 62 DISALLOW_COPY_AND_ASSIGN(FileSystem); | 35 DISALLOW_COPY_AND_ASSIGN(FileSystem); |
| 63 }; | 36 }; |
| 64 | 37 |
| 65 FileSystem::FileSystem(const HostResource& host_resource, | 38 FileSystem::FileSystem(const HostResource& host_resource, |
| 66 PP_FileSystemType_Dev type) | 39 PP_FileSystemType_Dev type) |
| 67 : PluginResource(host_resource), | 40 : PluginResource(host_resource), |
| 68 type_(type), | 41 type_(type), |
| 69 called_open_(false), | 42 called_open_(false), |
| 70 current_open_callback_(PP_MakeCompletionCallback(NULL, NULL)) { | 43 current_open_callback_(PP_MakeCompletionCallback(NULL, NULL)) { |
| 71 } | 44 } |
| 72 | 45 |
| 73 // TODO(brettw) this logic is duplicated with some other resource objects | 46 // TODO(brettw) this logic is duplicated with some other resource objects |
| 74 // like FileChooser. It would be nice to look at all of the different resources | 47 // like FileChooser. It would be nice to look at all of the different resources |
| 75 // that need callback tracking and design something that they can all re-use. | 48 // that need callback tracking and design something that they can all re-use. |
| 76 FileSystem::~FileSystem() { | 49 FileSystem::~FileSystem() { |
| 77 // Ensure the callback is always fired. | 50 // Ensure the callback is always fired. |
| 78 if (current_open_callback_.func) { | 51 if (current_open_callback_.func) { |
| 79 // TODO(brettw) the callbacks at this level should be refactored with a | 52 // TODO(brettw) the callbacks at this level should be refactored with a |
| 80 // more automatic tracking system like we have in the renderer. | 53 // more automatic tracking system like we have in the renderer. |
| 81 MessageLoop::current()->PostTask(FROM_HERE, NewRunnableFunction( | 54 MessageLoop::current()->PostTask(FROM_HERE, NewRunnableFunction( |
| 82 current_open_callback_.func, current_open_callback_.user_data, | 55 current_open_callback_.func, current_open_callback_.user_data, |
| 83 static_cast<int32_t>(PP_ERROR_ABORTED))); | 56 static_cast<int32_t>(PP_ERROR_ABORTED))); |
| 84 } | 57 } |
| 85 } | 58 } |
| 86 | 59 |
| 87 PPB_FileSystem_API* FileSystem::AsPPB_FileSystem_API() { | 60 FileSystem* FileSystem::AsFileSystem() { |
| 88 return this; | 61 return this; |
| 89 } | 62 } |
| 90 | 63 |
| 91 int32_t FileSystem::Open(int64_t expected_size, | 64 namespace { |
| 92 PP_CompletionCallback callback) { | 65 |
| 93 if (current_open_callback_.func) | 66 PP_Resource Create(PP_Instance instance, PP_FileSystemType_Dev type) { |
| 67 PluginDispatcher* dispatcher = PluginDispatcher::GetForInstance(instance); |
| 68 if (!dispatcher) |
| 69 return PP_ERROR_BADARGUMENT; |
| 70 |
| 71 HostResource result; |
| 72 dispatcher->Send(new PpapiHostMsg_PPBFileSystem_Create( |
| 73 INTERFACE_ID_PPB_FILE_SYSTEM, instance, type, &result)); |
| 74 if (result.is_null()) |
| 75 return 0; |
| 76 |
| 77 linked_ptr<FileSystem> object(new FileSystem(result, type)); |
| 78 return PluginResourceTracker::GetInstance()->AddResource(object); |
| 79 } |
| 80 |
| 81 PP_Bool IsFileSystem(PP_Resource resource) { |
| 82 FileSystem* object = PluginResource::GetAs<FileSystem>(resource); |
| 83 return BoolToPPBool(!!object); |
| 84 } |
| 85 |
| 86 int32_t Open(PP_Resource file_system, |
| 87 int64_t expected_size, |
| 88 struct PP_CompletionCallback callback) { |
| 89 FileSystem* object = PluginResource::GetAs<FileSystem>(file_system); |
| 90 if (!object) |
| 91 return PP_ERROR_BADRESOURCE; |
| 92 |
| 93 Dispatcher* dispatcher = PluginDispatcher::GetForInstance(object->instance()); |
| 94 if (!dispatcher) |
| 95 return PP_ERROR_BADARGUMENT; |
| 96 |
| 97 if (object->current_open_callback_.func) |
| 94 return PP_ERROR_INPROGRESS; | 98 return PP_ERROR_INPROGRESS; |
| 95 if (called_open_) | 99 else if (object->called_open_) |
| 96 return PP_ERROR_FAILED; | 100 return PP_ERROR_FAILED; |
| 97 | 101 |
| 98 current_open_callback_ = callback; | 102 object->current_open_callback_ = callback; |
| 99 called_open_ = true; | 103 object->called_open_ = true; |
| 100 GetDispatcher()->Send(new PpapiHostMsg_PPBFileSystem_Open( | 104 |
| 101 INTERFACE_ID_PPB_FILE_SYSTEM, host_resource(), expected_size)); | 105 dispatcher->Send(new PpapiHostMsg_PPBFileSystem_Open( |
| 106 INTERFACE_ID_PPB_FILE_SYSTEM, object->host_resource(), expected_size)); |
| 102 return PP_OK_COMPLETIONPENDING; | 107 return PP_OK_COMPLETIONPENDING; |
| 103 } | 108 } |
| 104 | 109 |
| 105 PP_FileSystemType_Dev FileSystem::GetType() { | 110 PP_FileSystemType_Dev GetType(PP_Resource resource) { |
| 106 return type_; | 111 FileSystem* object = PluginResource::GetAs<FileSystem>(resource); |
| 112 if (!object) |
| 113 return PP_FILESYSTEMTYPE_INVALID; |
| 114 return object->type_; |
| 107 } | 115 } |
| 108 | 116 |
| 109 void FileSystem::OpenComplete(int32_t result) { | 117 const PPB_FileSystem_Dev file_system_interface = { |
| 110 PP_RunAndClearCompletionCallback(¤t_open_callback_, result); | 118 &Create, |
| 119 &IsFileSystem, |
| 120 &Open, |
| 121 &GetType |
| 122 }; |
| 123 |
| 124 InterfaceProxy* CreateFileSystemProxy(Dispatcher* dispatcher, |
| 125 const void* target_interface) { |
| 126 return new PPB_FileSystem_Proxy(dispatcher, target_interface); |
| 111 } | 127 } |
| 112 | 128 |
| 129 } // namespace |
| 130 |
| 113 PPB_FileSystem_Proxy::PPB_FileSystem_Proxy(Dispatcher* dispatcher, | 131 PPB_FileSystem_Proxy::PPB_FileSystem_Proxy(Dispatcher* dispatcher, |
| 114 const void* target_interface) | 132 const void* target_interface) |
| 115 : InterfaceProxy(dispatcher, target_interface), | 133 : InterfaceProxy(dispatcher, target_interface), |
| 116 callback_factory_(ALLOW_THIS_IN_INITIALIZER_LIST(this)) { | 134 callback_factory_(ALLOW_THIS_IN_INITIALIZER_LIST(this)) { |
| 117 } | 135 } |
| 118 | 136 |
| 119 PPB_FileSystem_Proxy::~PPB_FileSystem_Proxy() { | 137 PPB_FileSystem_Proxy::~PPB_FileSystem_Proxy() { |
| 120 } | 138 } |
| 121 | 139 |
| 122 const InterfaceProxy::Info* PPB_FileSystem_Proxy::GetInfo() { | 140 const InterfaceProxy::Info* PPB_FileSystem_Proxy::GetInfo() { |
| 123 static const Info info = { | 141 static const Info info = { |
| 124 ::ppapi::thunk::GetPPB_FileSystem_Thunk(), | 142 &file_system_interface, |
| 125 PPB_FILESYSTEM_DEV_INTERFACE, | 143 PPB_FILESYSTEM_DEV_INTERFACE, |
| 126 INTERFACE_ID_PPB_FILE_SYSTEM, | 144 INTERFACE_ID_PPB_FILE_SYSTEM, |
| 127 false, | 145 false, |
| 128 &CreateFileSystemProxy, | 146 &CreateFileSystemProxy, |
| 129 }; | 147 }; |
| 130 return &info; | 148 return &info; |
| 131 } | 149 } |
| 132 | 150 |
| 133 // static | |
| 134 PP_Resource PPB_FileSystem_Proxy::CreateProxyResource( | |
| 135 PP_Instance instance, | |
| 136 PP_FileSystemType_Dev type) { | |
| 137 PluginDispatcher* dispatcher = PluginDispatcher::GetForInstance(instance); | |
| 138 if (!dispatcher) | |
| 139 return PP_ERROR_BADARGUMENT; | |
| 140 | |
| 141 HostResource result; | |
| 142 dispatcher->Send(new PpapiHostMsg_PPBFileSystem_Create( | |
| 143 INTERFACE_ID_PPB_FILE_SYSTEM, instance, type, &result)); | |
| 144 if (result.is_null()) | |
| 145 return 0; | |
| 146 | |
| 147 linked_ptr<FileSystem> object(new FileSystem(result, type)); | |
| 148 return PluginResourceTracker::GetInstance()->AddResource(object); | |
| 149 } | |
| 150 | |
| 151 bool PPB_FileSystem_Proxy::OnMessageReceived(const IPC::Message& msg) { | 151 bool PPB_FileSystem_Proxy::OnMessageReceived(const IPC::Message& msg) { |
| 152 bool handled = true; | 152 bool handled = true; |
| 153 IPC_BEGIN_MESSAGE_MAP(PPB_FileSystem_Proxy, msg) | 153 IPC_BEGIN_MESSAGE_MAP(PPB_FileSystem_Proxy, msg) |
| 154 IPC_MESSAGE_HANDLER(PpapiHostMsg_PPBFileSystem_Create, OnMsgCreate) | 154 IPC_MESSAGE_HANDLER(PpapiHostMsg_PPBFileSystem_Create, OnMsgCreate) |
| 155 IPC_MESSAGE_HANDLER(PpapiHostMsg_PPBFileSystem_Open, OnMsgOpen) | 155 IPC_MESSAGE_HANDLER(PpapiHostMsg_PPBFileSystem_Open, OnMsgOpen) |
| 156 IPC_MESSAGE_HANDLER(PpapiMsg_PPBFileSystem_OpenComplete, OnMsgOpenComplete) | 156 IPC_MESSAGE_HANDLER(PpapiMsg_PPBFileSystem_OpenComplete, OnMsgOpenComplete) |
| 157 IPC_MESSAGE_UNHANDLED(handled = false) | 157 IPC_MESSAGE_UNHANDLED(handled = false) |
| 158 IPC_END_MESSAGE_MAP() | 158 IPC_END_MESSAGE_MAP() |
| 159 return handled; | 159 return handled; |
| 160 } | 160 } |
| 161 | 161 |
| 162 void PPB_FileSystem_Proxy::OnMsgCreate(PP_Instance instance, | 162 void PPB_FileSystem_Proxy::OnMsgCreate(PP_Instance instance, |
| 163 int type, | 163 int type, |
| 164 HostResource* result) { | 164 HostResource* result) { |
| 165 EnterFunctionNoLock<ResourceCreationAPI> enter(instance, true); | 165 PP_Resource resource = ppb_file_system_target()->Create( |
| 166 if (enter.failed()) | |
| 167 return; | |
| 168 PP_Resource resource = enter.functions()->CreateFileSystem( | |
| 169 instance, static_cast<PP_FileSystemType_Dev>(type)); | 166 instance, static_cast<PP_FileSystemType_Dev>(type)); |
| 170 if (!resource) | 167 if (!resource) |
| 171 return; // CreateInfo default constructor initializes to 0. | 168 return; // CreateInfo default constructor initializes to 0. |
| 172 result->SetHostResource(instance, resource); | 169 result->SetHostResource(instance, resource); |
| 173 } | 170 } |
| 174 | 171 |
| 175 void PPB_FileSystem_Proxy::OnMsgOpen(const HostResource& host_resource, | 172 void PPB_FileSystem_Proxy::OnMsgOpen(const HostResource& host_resource, |
| 176 int64_t expected_size) { | 173 int64_t expected_size) { |
| 177 EnterHostFromHostResource<PPB_FileSystem_API> enter(host_resource); | |
| 178 if (enter.failed()) | |
| 179 return; | |
| 180 | |
| 181 CompletionCallback callback = callback_factory_.NewCallback( | 174 CompletionCallback callback = callback_factory_.NewCallback( |
| 182 &PPB_FileSystem_Proxy::OpenCompleteInHost, host_resource); | 175 &PPB_FileSystem_Proxy::OpenCompleteInHost, host_resource); |
| 183 int32_t result = enter.object()->Open(expected_size, | 176 |
| 184 callback.pp_completion_callback()); | 177 int32_t result = ppb_file_system_target()->Open( |
| 178 host_resource.host_resource(), expected_size, |
| 179 callback.pp_completion_callback()); |
| 185 if (result != PP_OK_COMPLETIONPENDING) | 180 if (result != PP_OK_COMPLETIONPENDING) |
| 186 callback.Run(result); | 181 callback.Run(result); |
| 187 } | 182 } |
| 188 | 183 |
| 189 // Called in the plugin to handle the open callback. | 184 // Called in the plugin to handle the open callback. |
| 190 void PPB_FileSystem_Proxy::OnMsgOpenComplete(const HostResource& host_resource, | 185 void PPB_FileSystem_Proxy::OnMsgOpenComplete(const HostResource& filesystem, |
| 191 int32_t result) { | 186 int32_t result) { |
| 192 EnterPluginFromHostResource<PPB_FileSystem_API> enter(host_resource); | 187 FileSystem* object = PluginResource::GetAs<FileSystem>( |
| 193 if (enter.succeeded()) | 188 PluginResourceTracker::GetInstance()->PluginResourceForHostResource( |
| 194 static_cast<FileSystem*>(enter.object())->OpenComplete(result); | 189 filesystem)); |
| 190 if (!object || !object->current_open_callback_.func) |
| 191 return; |
| 192 PP_RunAndClearCompletionCallback(&object->current_open_callback_, result); |
| 195 } | 193 } |
| 196 | 194 |
| 197 void PPB_FileSystem_Proxy::OpenCompleteInHost( | 195 void PPB_FileSystem_Proxy::OpenCompleteInHost( |
| 198 int32_t result, | 196 int32_t result, |
| 199 const HostResource& host_resource) { | 197 const HostResource& host_resource) { |
| 200 dispatcher()->Send(new PpapiMsg_PPBFileSystem_OpenComplete( | 198 dispatcher()->Send(new PpapiMsg_PPBFileSystem_OpenComplete( |
| 201 INTERFACE_ID_PPB_FILE_SYSTEM, host_resource, result)); | 199 INTERFACE_ID_PPB_FILE_SYSTEM, host_resource, result)); |
| 202 } | 200 } |
| 203 | 201 |
| 204 } // namespace proxy | 202 } // namespace proxy |
| 205 } // namespace pp | 203 } // namespace pp |
| OLD | NEW |