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