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 "ppapi/c/pp_file_info.h" | |
12 #include "ppapi/c/ppb_file_io.h" | |
13 #include "ppapi/c/private/ppb_flash_file.h" | |
14 #include "ppapi/shared_impl/file_type_conversion.h" | |
15 #include "ppapi/shared_impl/time_conversion.h" | |
16 #include "ppapi/thunk/enter.h" | |
17 #include "webkit/plugins/ppapi/common.h" | |
18 #include "webkit/plugins/ppapi/file_path.h" | |
19 #include "webkit/plugins/ppapi/host_globals.h" | |
20 #include "webkit/plugins/ppapi/plugin_delegate.h" | |
21 #include "webkit/plugins/ppapi/plugin_module.h" | |
22 #include "webkit/plugins/ppapi/ppapi_plugin_instance.h" | |
23 #include "webkit/plugins/ppapi/ppb_file_ref_impl.h" | |
24 #include "webkit/plugins/ppapi/resource_helper.h" | |
25 | |
26 #if defined(OS_WIN) | |
27 #include "base/utf_string_conversions.h" | |
28 #endif | |
29 | |
30 using ppapi::thunk::EnterResource; | |
31 using ppapi::thunk::PPB_FileRef_API; | |
32 using ppapi::TimeToPPTime; | |
33 | |
34 namespace webkit { | |
35 namespace ppapi { | |
36 | |
37 namespace { | |
38 | |
39 void FreeDirContents(PP_Instance instance, PP_DirContents_Dev* contents) { | |
40 DCHECK(contents); | |
41 for (int32_t i = 0; i < contents->count; ++i) { | |
42 delete [] contents->entries[i].name; | |
43 } | |
44 delete [] contents->entries; | |
45 delete contents; | |
46 } | |
47 | |
48 } // namespace | |
49 | |
50 // PPB_Flash_File_ModuleLocal_Impl --------------------------------------------- | |
51 | |
52 namespace { | |
53 | |
54 bool CreateThreadAdapterForInstance(PP_Instance instance) { | |
55 return false; // No multithreaded access allowed. | |
56 } | |
57 | |
58 void ClearThreadAdapterForInstance(PP_Instance instance) { | |
59 } | |
60 | |
61 int32_t OpenModuleLocalFile(PP_Instance pp_instance, | |
62 const char* path, | |
63 int32_t mode, | |
64 PP_FileHandle* file) { | |
65 int flags = 0; | |
66 if (!path || | |
67 !::ppapi::PepperFileOpenFlagsToPlatformFileFlags(mode, &flags) || | |
68 !file) | |
69 return PP_ERROR_BADARGUMENT; | |
70 | |
71 PluginInstance* instance = HostGlobals::Get()->GetInstance(pp_instance); | |
72 if (!instance) | |
73 return PP_ERROR_FAILED; | |
74 | |
75 base::PlatformFile base_file; | |
76 base::PlatformFileError result = instance->delegate()->OpenFile( | |
77 PepperFilePath::MakeModuleLocal(instance->module(), path), | |
78 flags, | |
79 &base_file); | |
80 *file = base_file; | |
81 return ::ppapi::PlatformFileErrorToPepperError(result); | |
82 } | |
83 | |
84 int32_t RenameModuleLocalFile(PP_Instance pp_instance, | |
85 const char* from_path, | |
86 const char* to_path) { | |
87 if (!from_path || !to_path) | |
88 return PP_ERROR_BADARGUMENT; | |
89 | |
90 PluginInstance* instance = HostGlobals::Get()->GetInstance(pp_instance); | |
91 if (!instance) | |
92 return PP_ERROR_FAILED; | |
93 | |
94 base::PlatformFileError result = instance->delegate()->RenameFile( | |
95 PepperFilePath::MakeModuleLocal(instance->module(), from_path), | |
96 PepperFilePath::MakeModuleLocal(instance->module(), to_path)); | |
97 return ::ppapi::PlatformFileErrorToPepperError(result); | |
98 } | |
99 | |
100 int32_t DeleteModuleLocalFileOrDir(PP_Instance pp_instance, | |
101 const char* path, | |
102 PP_Bool recursive) { | |
103 if (!path) | |
104 return PP_ERROR_BADARGUMENT; | |
105 | |
106 PluginInstance* instance = HostGlobals::Get()->GetInstance(pp_instance); | |
107 if (!instance) | |
108 return PP_ERROR_FAILED; | |
109 | |
110 base::PlatformFileError result = instance->delegate()->DeleteFileOrDir( | |
111 PepperFilePath::MakeModuleLocal(instance->module(), path), | |
112 PPBoolToBool(recursive)); | |
113 return ::ppapi::PlatformFileErrorToPepperError(result); | |
114 } | |
115 | |
116 int32_t CreateModuleLocalDir(PP_Instance pp_instance, const char* path) { | |
117 if (!path) | |
118 return PP_ERROR_BADARGUMENT; | |
119 | |
120 PluginInstance* instance = HostGlobals::Get()->GetInstance(pp_instance); | |
121 if (!instance) | |
122 return PP_ERROR_FAILED; | |
123 | |
124 base::PlatformFileError result = instance->delegate()->CreateDir( | |
125 PepperFilePath::MakeModuleLocal(instance->module(), path)); | |
126 return ::ppapi::PlatformFileErrorToPepperError(result); | |
127 } | |
128 | |
129 int32_t QueryModuleLocalFile(PP_Instance pp_instance, | |
130 const char* path, | |
131 PP_FileInfo* info) { | |
132 if (!path || !info) | |
133 return PP_ERROR_BADARGUMENT; | |
134 | |
135 PluginInstance* instance = HostGlobals::Get()->GetInstance(pp_instance); | |
136 if (!instance) | |
137 return PP_ERROR_FAILED; | |
138 | |
139 base::PlatformFileInfo file_info; | |
140 base::PlatformFileError result = instance->delegate()->QueryFile( | |
141 PepperFilePath::MakeModuleLocal(instance->module(), path), | |
142 &file_info); | |
143 if (result == base::PLATFORM_FILE_OK) { | |
144 info->size = file_info.size; | |
145 info->creation_time = TimeToPPTime(file_info.creation_time); | |
146 info->last_access_time = TimeToPPTime(file_info.last_accessed); | |
147 info->last_modified_time = TimeToPPTime(file_info.last_modified); | |
148 info->system_type = PP_FILESYSTEMTYPE_EXTERNAL; | |
149 if (file_info.is_directory) | |
150 info->type = PP_FILETYPE_DIRECTORY; | |
151 else | |
152 info->type = PP_FILETYPE_REGULAR; | |
153 } | |
154 return ::ppapi::PlatformFileErrorToPepperError(result); | |
155 } | |
156 | |
157 int32_t GetModuleLocalDirContents(PP_Instance pp_instance, | |
158 const char* path, | |
159 PP_DirContents_Dev** contents) { | |
160 if (!path || !contents) | |
161 return PP_ERROR_BADARGUMENT; | |
162 PluginInstance* instance = HostGlobals::Get()->GetInstance(pp_instance); | |
163 if (!instance) | |
164 return PP_ERROR_FAILED; | |
165 | |
166 *contents = NULL; | |
167 DirContents pepper_contents; | |
168 base::PlatformFileError result = instance->delegate()->GetDirContents( | |
169 PepperFilePath::MakeModuleLocal(instance->module(), path), | |
170 &pepper_contents); | |
171 | |
172 if (result != base::PLATFORM_FILE_OK) | |
173 return ::ppapi::PlatformFileErrorToPepperError(result); | |
174 | |
175 *contents = new PP_DirContents_Dev; | |
176 size_t count = pepper_contents.size(); | |
177 (*contents)->count = count; | |
178 (*contents)->entries = new PP_DirEntry_Dev[count]; | |
179 for (size_t i = 0; i < count; ++i) { | |
180 PP_DirEntry_Dev& entry = (*contents)->entries[i]; | |
181 #if defined(OS_WIN) | |
182 const std::string& name = UTF16ToUTF8(pepper_contents[i].name.value()); | |
183 #else | |
184 const std::string& name = pepper_contents[i].name.value(); | |
185 #endif | |
186 size_t size = name.size() + 1; | |
187 char* name_copy = new char[size]; | |
188 memcpy(name_copy, name.c_str(), size); | |
189 entry.name = name_copy; | |
190 entry.is_dir = BoolToPPBool(pepper_contents[i].is_dir); | |
191 } | |
192 return PP_OK; | |
193 } | |
194 | |
195 const PPB_Flash_File_ModuleLocal ppb_flash_file_modulelocal = { | |
196 &CreateThreadAdapterForInstance, | |
197 &ClearThreadAdapterForInstance, | |
198 &OpenModuleLocalFile, | |
199 &RenameModuleLocalFile, | |
200 &DeleteModuleLocalFileOrDir, | |
201 &CreateModuleLocalDir, | |
202 &QueryModuleLocalFile, | |
203 &GetModuleLocalDirContents, | |
204 &FreeDirContents, | |
205 }; | |
206 | |
207 } // namespace | |
208 | |
209 // static | |
210 const PPB_Flash_File_ModuleLocal* | |
211 PPB_Flash_File_ModuleLocal_Impl::GetInterface() { | |
212 return &ppb_flash_file_modulelocal; | |
213 } | |
214 | |
215 // PPB_Flash_File_FileRef_Impl ------------------------------------------------- | |
216 | |
217 namespace { | |
218 | |
219 int32_t OpenFileRefFile(PP_Resource file_ref_id, | |
220 int32_t mode, | |
221 PP_FileHandle* file) { | |
222 int flags = 0; | |
223 if (!::ppapi::PepperFileOpenFlagsToPlatformFileFlags(mode, &flags) || !file) | |
224 return PP_ERROR_BADARGUMENT; | |
225 | |
226 EnterResource<PPB_FileRef_API> enter(file_ref_id, true); | |
227 if (enter.failed()) | |
228 return PP_ERROR_BADRESOURCE; | |
229 PPB_FileRef_Impl* file_ref = static_cast<PPB_FileRef_Impl*>(enter.object()); | |
230 | |
231 PluginInstance* instance = ResourceHelper::GetPluginInstance(file_ref); | |
232 if (!instance) | |
233 return PP_ERROR_FAILED; | |
234 | |
235 base::PlatformFile base_file; | |
236 base::PlatformFileError result = instance->delegate()->OpenFile( | |
237 PepperFilePath::MakeAbsolute(file_ref->GetSystemPath()), | |
238 flags, | |
239 &base_file); | |
240 *file = base_file; | |
241 return ::ppapi::PlatformFileErrorToPepperError(result); | |
242 } | |
243 | |
244 int32_t QueryFileRefFile(PP_Resource file_ref_id, | |
245 PP_FileInfo* info) { | |
246 EnterResource<PPB_FileRef_API> enter(file_ref_id, true); | |
247 if (enter.failed()) | |
248 return PP_ERROR_BADRESOURCE; | |
249 PPB_FileRef_Impl* file_ref = static_cast<PPB_FileRef_Impl*>(enter.object()); | |
250 | |
251 PluginInstance* instance = ResourceHelper::GetPluginInstance(file_ref); | |
252 if (!instance) | |
253 return PP_ERROR_FAILED; | |
254 | |
255 base::PlatformFileInfo file_info; | |
256 base::PlatformFileError result = instance->delegate()->QueryFile( | |
257 PepperFilePath::MakeAbsolute(file_ref->GetSystemPath()), | |
258 &file_info); | |
259 if (result == base::PLATFORM_FILE_OK) { | |
260 info->size = file_info.size; | |
261 info->creation_time = TimeToPPTime(file_info.creation_time); | |
262 info->last_access_time = TimeToPPTime(file_info.last_accessed); | |
263 info->last_modified_time = TimeToPPTime(file_info.last_modified); | |
264 info->system_type = PP_FILESYSTEMTYPE_EXTERNAL; | |
265 if (file_info.is_directory) | |
266 info->type = PP_FILETYPE_DIRECTORY; | |
267 else | |
268 info->type = PP_FILETYPE_REGULAR; | |
269 } | |
270 return ::ppapi::PlatformFileErrorToPepperError(result); | |
271 } | |
272 | |
273 const PPB_Flash_File_FileRef ppb_flash_file_fileref = { | |
274 &OpenFileRefFile, | |
275 &QueryFileRefFile, | |
276 }; | |
277 | |
278 } // namespace | |
279 | |
280 // static | |
281 const PPB_Flash_File_FileRef* PPB_Flash_File_FileRef_Impl::GetInterface() { | |
282 return &ppb_flash_file_fileref; | |
283 } | |
284 | |
285 } // namespace ppapi | |
286 } // namespace webkit | |
OLD | NEW |