OLD | NEW |
| (Empty) |
1 // Copyright (c) 2010 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/glue/plugins/pepper_private2.h" | |
6 | |
7 #include <string.h> | |
8 | |
9 #include "base/file_path.h" | |
10 #include "base/stringprintf.h" | |
11 #include "base/utf_string_conversions.h" | |
12 #include "googleurl/src/gurl.h" | |
13 #include "ppapi/c/dev/pp_file_info_dev.h" | |
14 #include "ppapi/c/dev/ppb_file_io_dev.h" | |
15 #include "webkit/glue/plugins/pepper_error_util.h" | |
16 #include "webkit/glue/plugins/pepper_plugin_delegate.h" | |
17 #include "webkit/glue/plugins/pepper_plugin_instance.h" | |
18 #include "webkit/glue/plugins/pepper_plugin_module.h" | |
19 #include "webkit/glue/plugins/pepper_var.h" | |
20 #include "webkit/glue/plugins/ppb_private2.h" | |
21 | |
22 namespace pepper { | |
23 | |
24 namespace { | |
25 | |
26 PluginInstance* GetSomeInstance(PP_Module pp_module) { | |
27 PluginModule* module = ResourceTracker::Get()->GetModule(pp_module); | |
28 if (!module) | |
29 return NULL; | |
30 | |
31 return module->GetSomeInstance(); | |
32 } | |
33 | |
34 void SetInstanceAlwaysOnTop(PP_Instance pp_instance, bool on_top) { | |
35 PluginInstance* instance = ResourceTracker::Get()->GetInstance(pp_instance); | |
36 if (!instance) | |
37 return; | |
38 instance->set_always_on_top(on_top); | |
39 } | |
40 | |
41 PP_Var GetProxyForURL(PP_Module pp_module, const char* url) { | |
42 PluginInstance* instance = GetSomeInstance(pp_module); | |
43 if (!instance) | |
44 return PP_MakeUndefined(); | |
45 | |
46 GURL gurl(url); | |
47 if (!gurl.is_valid()) | |
48 return PP_MakeUndefined(); | |
49 | |
50 std::string proxy_host = instance->delegate()->ResolveProxy(gurl); | |
51 if (proxy_host.empty()) | |
52 return PP_MakeUndefined(); // No proxy. | |
53 return StringVar::StringToPPVar(instance->module(), proxy_host); | |
54 } | |
55 | |
56 FilePath GetFilePathFromUTF8(const char* path) { | |
57 #if defined(OS_WIN) | |
58 return FilePath(UTF8ToUTF16(path)); | |
59 #else | |
60 return FilePath(path); | |
61 #endif | |
62 } | |
63 | |
64 int32_t OpenModuleLocalFile(PP_Module module, | |
65 const char* path, | |
66 int32_t mode, | |
67 PP_FileHandle* file) { | |
68 PluginInstance* instance = GetSomeInstance(module); | |
69 if (!instance) | |
70 return PP_ERROR_FAILED; | |
71 | |
72 int flags = 0; | |
73 if (mode & PP_FILEOPENFLAG_READ) | |
74 flags |= base::PLATFORM_FILE_READ; | |
75 if (mode & PP_FILEOPENFLAG_WRITE) { | |
76 flags |= base::PLATFORM_FILE_WRITE; | |
77 flags |= base::PLATFORM_FILE_WRITE_ATTRIBUTES; | |
78 } | |
79 if (mode & PP_FILEOPENFLAG_TRUNCATE) { | |
80 DCHECK(mode & PP_FILEOPENFLAG_WRITE); | |
81 flags |= base::PLATFORM_FILE_TRUNCATE; | |
82 } | |
83 | |
84 if (mode & PP_FILEOPENFLAG_CREATE) { | |
85 if (mode & PP_FILEOPENFLAG_EXCLUSIVE) | |
86 flags |= base::PLATFORM_FILE_CREATE; | |
87 else | |
88 flags |= base::PLATFORM_FILE_OPEN_ALWAYS; | |
89 } else { | |
90 flags |= base::PLATFORM_FILE_OPEN; | |
91 } | |
92 | |
93 base::PlatformFile base_file; | |
94 base::PlatformFileError result = instance->delegate()->OpenModuleLocalFile( | |
95 instance->module()->name(), | |
96 GetFilePathFromUTF8(path), | |
97 flags, | |
98 &base_file); | |
99 *file = base_file; | |
100 return PlatformFileErrorToPepperError(result); | |
101 } | |
102 | |
103 | |
104 int32_t RenameModuleLocalFile(PP_Module module, | |
105 const char* path_from, | |
106 const char* path_to) { | |
107 PluginInstance* instance = GetSomeInstance(module); | |
108 if (!instance) | |
109 return PP_ERROR_FAILED; | |
110 | |
111 base::PlatformFileError result = instance->delegate()->RenameModuleLocalFile( | |
112 instance->module()->name(), | |
113 GetFilePathFromUTF8(path_from), | |
114 GetFilePathFromUTF8(path_to)); | |
115 return PlatformFileErrorToPepperError(result); | |
116 } | |
117 | |
118 int32_t DeleteModuleLocalFileOrDir(PP_Module module, | |
119 const char* path, | |
120 bool recursive) { | |
121 PluginInstance* instance = GetSomeInstance(module); | |
122 if (!instance) | |
123 return PP_ERROR_FAILED; | |
124 | |
125 base::PlatformFileError result = | |
126 instance->delegate()->DeleteModuleLocalFileOrDir( | |
127 instance->module()->name(), GetFilePathFromUTF8(path), recursive); | |
128 return PlatformFileErrorToPepperError(result); | |
129 } | |
130 | |
131 int32_t CreateModuleLocalDir(PP_Module module, const char* path) { | |
132 PluginInstance* instance = GetSomeInstance(module); | |
133 if (!instance) | |
134 return PP_ERROR_FAILED; | |
135 | |
136 base::PlatformFileError result = instance->delegate()->CreateModuleLocalDir( | |
137 instance->module()->name(), GetFilePathFromUTF8(path)); | |
138 return PlatformFileErrorToPepperError(result); | |
139 } | |
140 | |
141 int32_t QueryModuleLocalFile(PP_Module module, | |
142 const char* path, | |
143 PP_FileInfo_Dev* info) { | |
144 PluginInstance* instance = GetSomeInstance(module); | |
145 if (!instance) | |
146 return PP_ERROR_FAILED; | |
147 | |
148 base::PlatformFileInfo file_info; | |
149 base::PlatformFileError result = instance->delegate()->QueryModuleLocalFile( | |
150 instance->module()->name(), GetFilePathFromUTF8(path), &file_info); | |
151 if (result == base::PLATFORM_FILE_OK) { | |
152 info->size = file_info.size; | |
153 info->creation_time = file_info.creation_time.ToDoubleT(); | |
154 info->last_access_time = file_info.last_accessed.ToDoubleT(); | |
155 info->last_modified_time = file_info.last_modified.ToDoubleT(); | |
156 info->system_type = PP_FILESYSTEMTYPE_EXTERNAL; | |
157 if (file_info.is_directory) | |
158 info->type = PP_FILETYPE_DIRECTORY; | |
159 else | |
160 info->type = PP_FILETYPE_REGULAR; | |
161 } | |
162 return PlatformFileErrorToPepperError(result); | |
163 } | |
164 | |
165 int32_t GetModuleLocalDirContents(PP_Module module, | |
166 const char* path, | |
167 PP_DirContents_Dev** contents) { | |
168 PluginInstance* instance = GetSomeInstance(module); | |
169 if (!instance) | |
170 return PP_ERROR_FAILED; | |
171 | |
172 *contents = NULL; | |
173 PepperDirContents pepper_contents; | |
174 base::PlatformFileError result = | |
175 instance->delegate()->GetModuleLocalDirContents( | |
176 instance->module()->name(), | |
177 GetFilePathFromUTF8(path), | |
178 &pepper_contents); | |
179 | |
180 if (result != base::PLATFORM_FILE_OK) | |
181 return PlatformFileErrorToPepperError(result); | |
182 | |
183 *contents = new PP_DirContents_Dev; | |
184 size_t count = pepper_contents.size(); | |
185 (*contents)->count = count; | |
186 (*contents)->entries = new PP_DirEntry_Dev[count]; | |
187 for (size_t i = 0; i < count; ++i) { | |
188 PP_DirEntry_Dev& entry = (*contents)->entries[i]; | |
189 #if defined(OS_WIN) | |
190 const std::string& name = UTF16ToUTF8(pepper_contents[i].name.value()); | |
191 #else | |
192 const std::string& name = pepper_contents[i].name.value(); | |
193 #endif | |
194 size_t size = name.size() + 1; | |
195 char* name_copy = new char[size]; | |
196 memcpy(name_copy, name.c_str(), size); | |
197 entry.name = name_copy; | |
198 entry.is_dir = pepper_contents[i].is_dir; | |
199 } | |
200 return PP_OK; | |
201 } | |
202 | |
203 void FreeModuleLocalDirContents(PP_Module module, | |
204 PP_DirContents_Dev* contents) { | |
205 DCHECK(contents); | |
206 for (int32_t i = 0; i < contents->count; ++i) { | |
207 delete [] contents->entries[i].name; | |
208 } | |
209 delete [] contents->entries; | |
210 delete contents; | |
211 } | |
212 | |
213 bool NavigateToURL(PP_Instance pp_instance, | |
214 const char* url, | |
215 const char* target) { | |
216 PluginInstance* instance = ResourceTracker::Get()->GetInstance(pp_instance); | |
217 if (!instance) | |
218 return false; | |
219 return instance->NavigateToURL(url, target); | |
220 } | |
221 | |
222 const PPB_Private2 ppb_private2 = { | |
223 &SetInstanceAlwaysOnTop, | |
224 &Private2::DrawGlyphs, | |
225 &GetProxyForURL, | |
226 &OpenModuleLocalFile, | |
227 &RenameModuleLocalFile, | |
228 &DeleteModuleLocalFileOrDir, | |
229 &CreateModuleLocalDir, | |
230 &QueryModuleLocalFile, | |
231 &GetModuleLocalDirContents, | |
232 &FreeModuleLocalDirContents, | |
233 &NavigateToURL, | |
234 }; | |
235 | |
236 } // namespace | |
237 | |
238 // static | |
239 const PPB_Private2* Private2::GetInterface() { | |
240 return &ppb_private2; | |
241 } | |
242 | |
243 } // namespace pepper | |
OLD | NEW |