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 "webkit/plugins/ppapi/ppb_flash_file_impl.h" |
| 6 |
| 7 #include <string.h> |
| 8 |
| 9 #include <string> |
| 10 |
| 11 #include "base/file_path.h" |
| 12 #include "base/utf_string_conversions.h" |
| 13 #include "ppapi/c/dev/pp_file_info_dev.h" |
| 14 #include "ppapi/c/dev/ppb_file_io_dev.h" |
| 15 #include "ppapi/c/private/ppb_flash_file.h" |
| 16 #include "webkit/plugins/ppapi/common.h" |
| 17 #include "webkit/plugins/ppapi/error_util.h" |
| 18 #include "webkit/plugins/ppapi/plugin_delegate.h" |
| 19 #include "webkit/plugins/ppapi/plugin_module.h" |
| 20 #include "webkit/plugins/ppapi/ppapi_plugin_instance.h" |
| 21 #include "webkit/plugins/ppapi/resource_tracker.h" |
| 22 |
| 23 namespace webkit { |
| 24 namespace ppapi { |
| 25 |
| 26 namespace { |
| 27 |
| 28 FilePath GetFilePathFromUTF8(const char* path) { |
| 29 #if defined(OS_WIN) |
| 30 return FilePath(UTF8ToUTF16(path)); |
| 31 #else |
| 32 return FilePath(path); |
| 33 #endif |
| 34 } |
| 35 |
| 36 int32_t OpenModuleLocalFile(PP_Instance pp_instance, |
| 37 const char* path, |
| 38 int32_t mode, |
| 39 PP_FileHandle* file) { |
| 40 PluginInstance* instance = ResourceTracker::Get()->GetInstance(pp_instance); |
| 41 if (!instance) |
| 42 return PP_ERROR_FAILED; |
| 43 |
| 44 int flags = 0; |
| 45 if (mode & PP_FILEOPENFLAG_READ) |
| 46 flags |= base::PLATFORM_FILE_READ; |
| 47 if (mode & PP_FILEOPENFLAG_WRITE) { |
| 48 flags |= base::PLATFORM_FILE_WRITE; |
| 49 flags |= base::PLATFORM_FILE_WRITE_ATTRIBUTES; |
| 50 } |
| 51 if (mode & PP_FILEOPENFLAG_TRUNCATE) { |
| 52 DCHECK(mode & PP_FILEOPENFLAG_WRITE); |
| 53 flags |= base::PLATFORM_FILE_TRUNCATE; |
| 54 } |
| 55 |
| 56 if (mode & PP_FILEOPENFLAG_CREATE) { |
| 57 if (mode & PP_FILEOPENFLAG_EXCLUSIVE) |
| 58 flags |= base::PLATFORM_FILE_CREATE; |
| 59 else |
| 60 flags |= base::PLATFORM_FILE_OPEN_ALWAYS; |
| 61 } else { |
| 62 flags |= base::PLATFORM_FILE_OPEN; |
| 63 } |
| 64 |
| 65 base::PlatformFile base_file; |
| 66 base::PlatformFileError result = instance->delegate()->OpenModuleLocalFile( |
| 67 instance->module()->name(), |
| 68 GetFilePathFromUTF8(path), |
| 69 flags, |
| 70 &base_file); |
| 71 *file = base_file; |
| 72 return PlatformFileErrorToPepperError(result); |
| 73 } |
| 74 |
| 75 |
| 76 int32_t RenameModuleLocalFile(PP_Instance pp_instance, |
| 77 const char* path_from, |
| 78 const char* path_to) { |
| 79 PluginInstance* instance = ResourceTracker::Get()->GetInstance(pp_instance); |
| 80 if (!instance) |
| 81 return PP_ERROR_FAILED; |
| 82 |
| 83 base::PlatformFileError result = instance->delegate()->RenameModuleLocalFile( |
| 84 instance->module()->name(), |
| 85 GetFilePathFromUTF8(path_from), |
| 86 GetFilePathFromUTF8(path_to)); |
| 87 return PlatformFileErrorToPepperError(result); |
| 88 } |
| 89 |
| 90 int32_t DeleteModuleLocalFileOrDir(PP_Instance pp_instance, |
| 91 const char* path, |
| 92 PP_Bool recursive) { |
| 93 PluginInstance* instance = ResourceTracker::Get()->GetInstance(pp_instance); |
| 94 if (!instance) |
| 95 return PP_ERROR_FAILED; |
| 96 |
| 97 base::PlatformFileError result = |
| 98 instance->delegate()->DeleteModuleLocalFileOrDir( |
| 99 instance->module()->name(), GetFilePathFromUTF8(path), |
| 100 PPBoolToBool(recursive)); |
| 101 return PlatformFileErrorToPepperError(result); |
| 102 } |
| 103 |
| 104 int32_t CreateModuleLocalDir(PP_Instance pp_instance, const char* path) { |
| 105 PluginInstance* instance = ResourceTracker::Get()->GetInstance(pp_instance); |
| 106 if (!instance) |
| 107 return PP_ERROR_FAILED; |
| 108 |
| 109 base::PlatformFileError result = instance->delegate()->CreateModuleLocalDir( |
| 110 instance->module()->name(), GetFilePathFromUTF8(path)); |
| 111 return PlatformFileErrorToPepperError(result); |
| 112 } |
| 113 |
| 114 int32_t QueryModuleLocalFile(PP_Instance pp_instance, |
| 115 const char* path, |
| 116 PP_FileInfo_Dev* info) { |
| 117 PluginInstance* instance = ResourceTracker::Get()->GetInstance(pp_instance); |
| 118 if (!instance) |
| 119 return PP_ERROR_FAILED; |
| 120 |
| 121 base::PlatformFileInfo file_info; |
| 122 base::PlatformFileError result = instance->delegate()->QueryModuleLocalFile( |
| 123 instance->module()->name(), GetFilePathFromUTF8(path), &file_info); |
| 124 if (result == base::PLATFORM_FILE_OK) { |
| 125 info->size = file_info.size; |
| 126 info->creation_time = file_info.creation_time.ToDoubleT(); |
| 127 info->last_access_time = file_info.last_accessed.ToDoubleT(); |
| 128 info->last_modified_time = file_info.last_modified.ToDoubleT(); |
| 129 info->system_type = PP_FILESYSTEMTYPE_EXTERNAL; |
| 130 if (file_info.is_directory) |
| 131 info->type = PP_FILETYPE_DIRECTORY; |
| 132 else |
| 133 info->type = PP_FILETYPE_REGULAR; |
| 134 } |
| 135 return PlatformFileErrorToPepperError(result); |
| 136 } |
| 137 |
| 138 int32_t GetModuleLocalDirContents(PP_Instance pp_instance, |
| 139 const char* path, |
| 140 PP_DirContents_Dev** contents) { |
| 141 PluginInstance* instance = ResourceTracker::Get()->GetInstance(pp_instance); |
| 142 if (!instance) |
| 143 return PP_ERROR_FAILED; |
| 144 |
| 145 *contents = NULL; |
| 146 DirContents pepper_contents; |
| 147 base::PlatformFileError result = |
| 148 instance->delegate()->GetModuleLocalDirContents( |
| 149 instance->module()->name(), |
| 150 GetFilePathFromUTF8(path), |
| 151 &pepper_contents); |
| 152 |
| 153 if (result != base::PLATFORM_FILE_OK) |
| 154 return PlatformFileErrorToPepperError(result); |
| 155 |
| 156 *contents = new PP_DirContents_Dev; |
| 157 size_t count = pepper_contents.size(); |
| 158 (*contents)->count = count; |
| 159 (*contents)->entries = new PP_DirEntry_Dev[count]; |
| 160 for (size_t i = 0; i < count; ++i) { |
| 161 PP_DirEntry_Dev& entry = (*contents)->entries[i]; |
| 162 #if defined(OS_WIN) |
| 163 const std::string& name = UTF16ToUTF8(pepper_contents[i].name.value()); |
| 164 #else |
| 165 const std::string& name = pepper_contents[i].name.value(); |
| 166 #endif |
| 167 size_t size = name.size() + 1; |
| 168 char* name_copy = new char[size]; |
| 169 memcpy(name_copy, name.c_str(), size); |
| 170 entry.name = name_copy; |
| 171 entry.is_dir = BoolToPPBool(pepper_contents[i].is_dir); |
| 172 } |
| 173 return PP_OK; |
| 174 } |
| 175 |
| 176 void FreeModuleLocalDirContents(PP_Instance instance, |
| 177 PP_DirContents_Dev* contents) { |
| 178 DCHECK(contents); |
| 179 for (int32_t i = 0; i < contents->count; ++i) { |
| 180 delete [] contents->entries[i].name; |
| 181 } |
| 182 delete [] contents->entries; |
| 183 delete contents; |
| 184 } |
| 185 |
| 186 const PPB_Flash_File_ModuleLocal ppb_flash_file_module_local = { |
| 187 &OpenModuleLocalFile, |
| 188 &RenameModuleLocalFile, |
| 189 &DeleteModuleLocalFileOrDir, |
| 190 &CreateModuleLocalDir, |
| 191 &QueryModuleLocalFile, |
| 192 &GetModuleLocalDirContents, |
| 193 &FreeModuleLocalDirContents, |
| 194 }; |
| 195 |
| 196 } // namespace |
| 197 |
| 198 // static |
| 199 const PPB_Flash_File_ModuleLocal* |
| 200 PPB_Flash_File_ModuleLocal_Impl::GetInterface() { |
| 201 return &ppb_flash_file_module_local; |
| 202 } |
| 203 |
| 204 } // namespace ppapi |
| 205 } // namespace webkit |
OLD | NEW |