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_flash_file_proxy.h" |
| 6 |
| 7 #include "base/logging.h" |
| 8 #include "build/build_config.h" |
| 9 #include "ppapi/c/dev/pp_file_info_dev.h" |
| 10 #include "ppapi/c/pp_errors.h" |
| 11 #include "ppapi/c/private/ppb_flash_file.h" |
| 12 #include "ppapi/proxy/plugin_dispatcher.h" |
| 13 #include "ppapi/proxy/plugin_resource.h" |
| 14 #include "ppapi/proxy/ppapi_messages.h" |
| 15 |
| 16 namespace pp { |
| 17 namespace proxy { |
| 18 |
| 19 namespace { |
| 20 |
| 21 // Given an error code and a handle result from a Pepper API call, converts |
| 22 // to a PlatformFileForTransit, possibly also updating the error value if |
| 23 // an error occurred. |
| 24 IPC::PlatformFileForTransit PlatformFileToPlatformFileForTransit( |
| 25 int32_t* error, |
| 26 base::PlatformFile file) { |
| 27 if (*error != PP_OK) |
| 28 return IPC::InvalidPlatformFileForTransit(); |
| 29 #if defined(OS_WIN) |
| 30 /* TODO(brettw): figure out how to get the target process handle. |
| 31 HANDLE result; |
| 32 if (!::DuplicateHandle(::GetCurrentProcess(), file, |
| 33 target_process, &result, 0, false, |
| 34 DUPLICATE_CLOSE_SOURCE | DUPLICATE_SAME_ACCESS)) { |
| 35 *error = PP_ERROR_NOACCESS; |
| 36 return INVALID_HANDLE_VALUE; |
| 37 } |
| 38 return result; |
| 39 */ |
| 40 NOTIMPLEMENTED(); |
| 41 *error = PP_ERROR_NOACCESS; |
| 42 return INVALID_HANDLE_VALUE; |
| 43 #elif defined(OS_POSIX) |
| 44 return base::FileDescriptor(file, true); |
| 45 #endif |
| 46 } |
| 47 |
| 48 void FreeDirContents(PP_Instance /* instance */, |
| 49 PP_DirContents_Dev* contents) { |
| 50 for (int32_t i = 0; i < contents->count; ++i) |
| 51 delete[] contents->entries[i].name; |
| 52 delete[] contents->entries; |
| 53 delete contents; |
| 54 } |
| 55 |
| 56 int32_t OpenModuleLocalFile(PP_Instance instance, |
| 57 const char* path, |
| 58 int32_t mode, |
| 59 PP_FileHandle* file) { |
| 60 PluginDispatcher* dispatcher = PluginDispatcher::GetForInstance(instance); |
| 61 if (!dispatcher) |
| 62 return PP_ERROR_BADARGUMENT; |
| 63 |
| 64 int32_t result = PP_ERROR_FAILED; |
| 65 IPC::PlatformFileForTransit transit; |
| 66 dispatcher->Send(new PpapiHostMsg_PPBFlashFile_ModuleLocal_OpenFile( |
| 67 INTERFACE_ID_PPB_FLASH_FILE_MODULELOCAL, |
| 68 instance, path, mode, &transit, &result)); |
| 69 *file = IPC::PlatformFileForTransitToPlatformFile(transit); |
| 70 return result; |
| 71 } |
| 72 |
| 73 int32_t RenameModuleLocalFile(PP_Instance instance, |
| 74 const char* path_from, |
| 75 const char* path_to) { |
| 76 PluginDispatcher* dispatcher = PluginDispatcher::GetForInstance(instance); |
| 77 if (!dispatcher) |
| 78 return PP_ERROR_BADARGUMENT; |
| 79 |
| 80 int32_t result = PP_ERROR_FAILED; |
| 81 dispatcher->Send(new PpapiHostMsg_PPBFlashFile_ModuleLocal_RenameFile( |
| 82 INTERFACE_ID_PPB_FLASH_FILE_MODULELOCAL, |
| 83 instance, path_from, path_to, &result)); |
| 84 return result; |
| 85 } |
| 86 |
| 87 int32_t DeleteModuleLocalFileOrDir(PP_Instance instance, |
| 88 const char* path, |
| 89 PP_Bool recursive) { |
| 90 PluginDispatcher* dispatcher = PluginDispatcher::GetForInstance(instance); |
| 91 if (!dispatcher) |
| 92 return PP_ERROR_BADARGUMENT; |
| 93 |
| 94 int32_t result = PP_ERROR_FAILED; |
| 95 dispatcher->Send(new PpapiHostMsg_PPBFlashFile_ModuleLocal_DeleteFileOrDir( |
| 96 INTERFACE_ID_PPB_FLASH_FILE_MODULELOCAL, |
| 97 instance, path, recursive, &result)); |
| 98 return result; |
| 99 } |
| 100 |
| 101 int32_t CreateModuleLocalDir(PP_Instance instance, const char* path) { |
| 102 PluginDispatcher* dispatcher = PluginDispatcher::GetForInstance(instance); |
| 103 if (!dispatcher) |
| 104 return PP_ERROR_BADARGUMENT; |
| 105 |
| 106 int32_t result = PP_ERROR_FAILED; |
| 107 dispatcher->Send(new PpapiHostMsg_PPBFlashFile_ModuleLocal_CreateDir( |
| 108 INTERFACE_ID_PPB_FLASH_FILE_MODULELOCAL, instance, path, &result)); |
| 109 return result; |
| 110 } |
| 111 |
| 112 int32_t QueryModuleLocalFile(PP_Instance instance, |
| 113 const char* path, |
| 114 PP_FileInfo_Dev* info) { |
| 115 PluginDispatcher* dispatcher = PluginDispatcher::GetForInstance(instance); |
| 116 if (!dispatcher) |
| 117 return PP_ERROR_BADARGUMENT; |
| 118 |
| 119 int32_t result = PP_ERROR_FAILED; |
| 120 dispatcher->Send(new PpapiHostMsg_PPBFlashFile_ModuleLocal_QueryFile( |
| 121 INTERFACE_ID_PPB_FLASH_FILE_MODULELOCAL, instance, path, info, &result)); |
| 122 return result; |
| 123 } |
| 124 |
| 125 int32_t GetModuleLocalDirContents(PP_Instance instance, |
| 126 const char* path, |
| 127 PP_DirContents_Dev** contents) { |
| 128 PluginDispatcher* dispatcher = PluginDispatcher::GetForInstance(instance); |
| 129 if (!dispatcher) |
| 130 return PP_ERROR_BADARGUMENT; |
| 131 |
| 132 int32_t result = PP_ERROR_FAILED; |
| 133 std::vector<SerializedDirEntry> entries; |
| 134 dispatcher->Send(new PpapiHostMsg_PPBFlashFile_ModuleLocal_GetDirContents( |
| 135 INTERFACE_ID_PPB_FLASH_FILE_MODULELOCAL, |
| 136 instance, path, &entries, &result)); |
| 137 |
| 138 if (result != PP_OK) |
| 139 return result; |
| 140 |
| 141 // Copy the serialized dir entries to the output struct. |
| 142 *contents = new PP_DirContents_Dev; |
| 143 (*contents)->count = static_cast<int32_t>(entries.size()); |
| 144 (*contents)->entries = new PP_DirEntry_Dev[entries.size()]; |
| 145 for (size_t i = 0; i < entries.size(); i++) { |
| 146 const SerializedDirEntry& source = entries[i]; |
| 147 PP_DirEntry_Dev* dest = &(*contents)->entries[i]; |
| 148 |
| 149 char* name_copy = new char[source.name.size() + 1]; |
| 150 memcpy(name_copy, source.name.c_str(), source.name.size() + 1); |
| 151 dest->name = name_copy; |
| 152 dest->is_dir = BoolToPPBool(source.is_dir); |
| 153 } |
| 154 |
| 155 return result; |
| 156 } |
| 157 |
| 158 const PPB_Flash_File_ModuleLocal flash_file_module_local_interface = { |
| 159 &OpenModuleLocalFile, |
| 160 &RenameModuleLocalFile, |
| 161 &DeleteModuleLocalFileOrDir, |
| 162 &CreateModuleLocalDir, |
| 163 &QueryModuleLocalFile, |
| 164 &GetModuleLocalDirContents, |
| 165 &FreeDirContents, |
| 166 }; |
| 167 |
| 168 InterfaceProxy* CreateFlashFileModuleLocalProxy(Dispatcher* dispatcher, |
| 169 const void* target_interface) { |
| 170 return new PPB_Flash_File_ModuleLocal_Proxy(dispatcher, target_interface); |
| 171 } |
| 172 |
| 173 } // namespace |
| 174 |
| 175 PPB_Flash_File_ModuleLocal_Proxy::PPB_Flash_File_ModuleLocal_Proxy( |
| 176 Dispatcher* dispatcher, |
| 177 const void* target_interface) |
| 178 : InterfaceProxy(dispatcher, target_interface) { |
| 179 } |
| 180 |
| 181 PPB_Flash_File_ModuleLocal_Proxy::~PPB_Flash_File_ModuleLocal_Proxy() { |
| 182 } |
| 183 |
| 184 // static |
| 185 const InterfaceProxy::Info* PPB_Flash_File_ModuleLocal_Proxy::GetInfo() { |
| 186 static const Info info = { |
| 187 &flash_file_module_local_interface, |
| 188 PPB_FLASH_FILE_MODULELOCAL_INTERFACE, |
| 189 INTERFACE_ID_PPB_FLASH_FILE_MODULELOCAL, |
| 190 true, |
| 191 &CreateFlashFileModuleLocalProxy, |
| 192 }; |
| 193 return &info; |
| 194 } |
| 195 |
| 196 bool PPB_Flash_File_ModuleLocal_Proxy::OnMessageReceived( |
| 197 const IPC::Message& msg) { |
| 198 bool handled = true; |
| 199 IPC_BEGIN_MESSAGE_MAP(PPB_Flash_File_ModuleLocal_Proxy, msg) |
| 200 IPC_MESSAGE_HANDLER(PpapiHostMsg_PPBFlashFile_ModuleLocal_OpenFile, |
| 201 OnMsgOpenFile) |
| 202 IPC_MESSAGE_HANDLER(PpapiHostMsg_PPBFlashFile_ModuleLocal_RenameFile, |
| 203 OnMsgRenameFile) |
| 204 IPC_MESSAGE_HANDLER(PpapiHostMsg_PPBFlashFile_ModuleLocal_DeleteFileOrDir, |
| 205 OnMsgDeleteFileOrDir) |
| 206 IPC_MESSAGE_HANDLER(PpapiHostMsg_PPBFlashFile_ModuleLocal_CreateDir, |
| 207 OnMsgCreateDir) |
| 208 IPC_MESSAGE_HANDLER(PpapiHostMsg_PPBFlashFile_ModuleLocal_QueryFile, |
| 209 OnMsgQueryFile) |
| 210 IPC_MESSAGE_HANDLER(PpapiHostMsg_PPBFlashFile_ModuleLocal_GetDirContents, |
| 211 OnMsgGetDirContents) |
| 212 IPC_MESSAGE_UNHANDLED(handled = false) |
| 213 IPC_END_MESSAGE_MAP() |
| 214 // TODO(brettw) handle bad messages! |
| 215 return handled; |
| 216 } |
| 217 |
| 218 void PPB_Flash_File_ModuleLocal_Proxy::OnMsgOpenFile( |
| 219 PP_Instance instance, |
| 220 const std::string& path, |
| 221 int32_t mode, |
| 222 IPC::PlatformFileForTransit* file_handle, |
| 223 int32_t* result) { |
| 224 base::PlatformFile file; |
| 225 *result = ppb_flash_file_module_local_target()-> |
| 226 OpenFile(instance, path.c_str(), mode, &file); |
| 227 *file_handle = PlatformFileToPlatformFileForTransit(result, file); |
| 228 } |
| 229 |
| 230 void PPB_Flash_File_ModuleLocal_Proxy::OnMsgRenameFile( |
| 231 PP_Instance instance, |
| 232 const std::string& path_from, |
| 233 const std::string& path_to, |
| 234 int32_t* result) { |
| 235 *result = ppb_flash_file_module_local_target()-> |
| 236 RenameFile(instance, path_from.c_str(), path_to.c_str()); |
| 237 } |
| 238 |
| 239 void PPB_Flash_File_ModuleLocal_Proxy::OnMsgDeleteFileOrDir( |
| 240 PP_Instance instance, |
| 241 const std::string& path, |
| 242 PP_Bool recursive, |
| 243 int32_t* result) { |
| 244 *result = ppb_flash_file_module_local_target()-> |
| 245 DeleteFileOrDir(instance, path.c_str(), recursive); |
| 246 } |
| 247 |
| 248 void PPB_Flash_File_ModuleLocal_Proxy::OnMsgCreateDir(PP_Instance instance, |
| 249 const std::string& path, |
| 250 int32_t* result) { |
| 251 *result = ppb_flash_file_module_local_target()-> |
| 252 CreateDir(instance, path.c_str()); |
| 253 } |
| 254 |
| 255 void PPB_Flash_File_ModuleLocal_Proxy::OnMsgQueryFile(PP_Instance instance, |
| 256 const std::string& path, |
| 257 PP_FileInfo_Dev* info, |
| 258 int32_t* result) { |
| 259 *result = ppb_flash_file_module_local_target()-> |
| 260 QueryFile(instance, path.c_str(), info); |
| 261 } |
| 262 |
| 263 void PPB_Flash_File_ModuleLocal_Proxy::OnMsgGetDirContents( |
| 264 PP_Instance instance, |
| 265 const std::string& path, |
| 266 std::vector<pp::proxy::SerializedDirEntry>* entries, |
| 267 int32_t* result) { |
| 268 PP_DirContents_Dev* contents = NULL; |
| 269 *result = ppb_flash_file_module_local_target()-> |
| 270 GetDirContents(instance, path.c_str(), &contents); |
| 271 if (*result != PP_OK) |
| 272 return; |
| 273 |
| 274 // Convert the list of entries to the serialized version. |
| 275 entries->resize(contents->count); |
| 276 for (int32_t i = 0; i < contents->count; i++) { |
| 277 (*entries)[i].name.assign(contents->entries[i].name); |
| 278 (*entries)[i].is_dir = PPBoolToBool(contents->entries[i].is_dir); |
| 279 } |
| 280 ppb_flash_file_module_local_target()->FreeDirContents(instance, contents); |
| 281 } |
| 282 |
| 283 } // namespace proxy |
| 284 } // namespace pp |
OLD | NEW |