OLD | NEW |
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "webkit/plugins/ppapi/ppb_flash_impl.h" | 5 #include "webkit/plugins/ppapi/ppb_flash_impl.h" |
6 | 6 |
7 #include <string.h> | |
8 | |
9 #include <string> | 7 #include <string> |
10 | 8 |
11 #include "base/file_path.h" | |
12 #include "base/message_loop.h" | 9 #include "base/message_loop.h" |
13 #include "base/utf_string_conversions.h" | |
14 #include "googleurl/src/gurl.h" | 10 #include "googleurl/src/gurl.h" |
15 #include "ppapi/c/dev/pp_file_info_dev.h" | |
16 #include "ppapi/c/dev/ppb_file_io_dev.h" | |
17 #include "ppapi/c/pp_completion_callback.h" | |
18 #include "ppapi/c/private/ppb_flash.h" | 11 #include "ppapi/c/private/ppb_flash.h" |
19 #include "webkit/plugins/ppapi/common.h" | 12 #include "webkit/plugins/ppapi/common.h" |
20 #include "webkit/plugins/ppapi/error_util.h" | |
21 #include "webkit/plugins/ppapi/plugin_delegate.h" | 13 #include "webkit/plugins/ppapi/plugin_delegate.h" |
22 #include "webkit/plugins/ppapi/plugin_module.h" | |
23 #include "webkit/plugins/ppapi/ppapi_plugin_instance.h" | 14 #include "webkit/plugins/ppapi/ppapi_plugin_instance.h" |
24 #include "webkit/plugins/ppapi/resource_tracker.h" | 15 #include "webkit/plugins/ppapi/resource_tracker.h" |
25 #include "webkit/plugins/ppapi/var.h" | 16 #include "webkit/plugins/ppapi/var.h" |
26 | 17 |
27 namespace webkit { | 18 namespace webkit { |
28 namespace ppapi { | 19 namespace ppapi { |
29 | 20 |
30 namespace { | 21 namespace { |
31 | 22 |
32 void SetInstanceAlwaysOnTop(PP_Instance pp_instance, PP_Bool on_top) { | 23 void SetInstanceAlwaysOnTop(PP_Instance pp_instance, PP_Bool on_top) { |
(...skipping 11 matching lines...) Expand all Loading... |
44 GURL gurl(url); | 35 GURL gurl(url); |
45 if (!gurl.is_valid()) | 36 if (!gurl.is_valid()) |
46 return PP_MakeUndefined(); | 37 return PP_MakeUndefined(); |
47 | 38 |
48 std::string proxy_host = instance->delegate()->ResolveProxy(gurl); | 39 std::string proxy_host = instance->delegate()->ResolveProxy(gurl); |
49 if (proxy_host.empty()) | 40 if (proxy_host.empty()) |
50 return PP_MakeUndefined(); // No proxy. | 41 return PP_MakeUndefined(); // No proxy. |
51 return StringVar::StringToPPVar(instance->module(), proxy_host); | 42 return StringVar::StringToPPVar(instance->module(), proxy_host); |
52 } | 43 } |
53 | 44 |
54 FilePath GetFilePathFromUTF8(const char* path) { | |
55 #if defined(OS_WIN) | |
56 return FilePath(UTF8ToUTF16(path)); | |
57 #else | |
58 return FilePath(path); | |
59 #endif | |
60 } | |
61 | |
62 int32_t OpenModuleLocalFile(PP_Instance pp_instance, | |
63 const char* path, | |
64 int32_t mode, | |
65 PP_FileHandle* file) { | |
66 PluginInstance* instance = ResourceTracker::Get()->GetInstance(pp_instance); | |
67 if (!instance) | |
68 return PP_ERROR_FAILED; | |
69 | |
70 int flags = 0; | |
71 if (mode & PP_FILEOPENFLAG_READ) | |
72 flags |= base::PLATFORM_FILE_READ; | |
73 if (mode & PP_FILEOPENFLAG_WRITE) { | |
74 flags |= base::PLATFORM_FILE_WRITE; | |
75 flags |= base::PLATFORM_FILE_WRITE_ATTRIBUTES; | |
76 } | |
77 if (mode & PP_FILEOPENFLAG_TRUNCATE) { | |
78 DCHECK(mode & PP_FILEOPENFLAG_WRITE); | |
79 flags |= base::PLATFORM_FILE_TRUNCATE; | |
80 } | |
81 | |
82 if (mode & PP_FILEOPENFLAG_CREATE) { | |
83 if (mode & PP_FILEOPENFLAG_EXCLUSIVE) | |
84 flags |= base::PLATFORM_FILE_CREATE; | |
85 else | |
86 flags |= base::PLATFORM_FILE_OPEN_ALWAYS; | |
87 } else { | |
88 flags |= base::PLATFORM_FILE_OPEN; | |
89 } | |
90 | |
91 base::PlatformFile base_file; | |
92 base::PlatformFileError result = instance->delegate()->OpenModuleLocalFile( | |
93 instance->module()->name(), | |
94 GetFilePathFromUTF8(path), | |
95 flags, | |
96 &base_file); | |
97 *file = base_file; | |
98 return PlatformFileErrorToPepperError(result); | |
99 } | |
100 | |
101 | |
102 int32_t RenameModuleLocalFile(PP_Instance pp_instance, | |
103 const char* path_from, | |
104 const char* path_to) { | |
105 PluginInstance* instance = ResourceTracker::Get()->GetInstance(pp_instance); | |
106 if (!instance) | |
107 return PP_ERROR_FAILED; | |
108 | |
109 base::PlatformFileError result = instance->delegate()->RenameModuleLocalFile( | |
110 instance->module()->name(), | |
111 GetFilePathFromUTF8(path_from), | |
112 GetFilePathFromUTF8(path_to)); | |
113 return PlatformFileErrorToPepperError(result); | |
114 } | |
115 | |
116 int32_t DeleteModuleLocalFileOrDir(PP_Instance pp_instance, | |
117 const char* path, | |
118 PP_Bool recursive) { | |
119 PluginInstance* instance = ResourceTracker::Get()->GetInstance(pp_instance); | |
120 if (!instance) | |
121 return PP_ERROR_FAILED; | |
122 | |
123 base::PlatformFileError result = | |
124 instance->delegate()->DeleteModuleLocalFileOrDir( | |
125 instance->module()->name(), GetFilePathFromUTF8(path), | |
126 PPBoolToBool(recursive)); | |
127 return PlatformFileErrorToPepperError(result); | |
128 } | |
129 | |
130 int32_t CreateModuleLocalDir(PP_Instance pp_instance, const char* path) { | |
131 PluginInstance* instance = ResourceTracker::Get()->GetInstance(pp_instance); | |
132 if (!instance) | |
133 return PP_ERROR_FAILED; | |
134 | |
135 base::PlatformFileError result = instance->delegate()->CreateModuleLocalDir( | |
136 instance->module()->name(), GetFilePathFromUTF8(path)); | |
137 return PlatformFileErrorToPepperError(result); | |
138 } | |
139 | |
140 int32_t QueryModuleLocalFile(PP_Instance pp_instance, | |
141 const char* path, | |
142 PP_FileInfo_Dev* info) { | |
143 PluginInstance* instance = ResourceTracker::Get()->GetInstance(pp_instance); | |
144 if (!instance) | |
145 return PP_ERROR_FAILED; | |
146 | |
147 base::PlatformFileInfo file_info; | |
148 base::PlatformFileError result = instance->delegate()->QueryModuleLocalFile( | |
149 instance->module()->name(), GetFilePathFromUTF8(path), &file_info); | |
150 if (result == base::PLATFORM_FILE_OK) { | |
151 info->size = file_info.size; | |
152 info->creation_time = file_info.creation_time.ToDoubleT(); | |
153 info->last_access_time = file_info.last_accessed.ToDoubleT(); | |
154 info->last_modified_time = file_info.last_modified.ToDoubleT(); | |
155 info->system_type = PP_FILESYSTEMTYPE_EXTERNAL; | |
156 if (file_info.is_directory) | |
157 info->type = PP_FILETYPE_DIRECTORY; | |
158 else | |
159 info->type = PP_FILETYPE_REGULAR; | |
160 } | |
161 return PlatformFileErrorToPepperError(result); | |
162 } | |
163 | |
164 int32_t GetModuleLocalDirContents(PP_Instance pp_instance, | |
165 const char* path, | |
166 PP_DirContents_Dev** contents) { | |
167 PluginInstance* instance = ResourceTracker::Get()->GetInstance(pp_instance); | |
168 if (!instance) | |
169 return PP_ERROR_FAILED; | |
170 | |
171 *contents = NULL; | |
172 DirContents pepper_contents; | |
173 base::PlatformFileError result = | |
174 instance->delegate()->GetModuleLocalDirContents( | |
175 instance->module()->name(), | |
176 GetFilePathFromUTF8(path), | |
177 &pepper_contents); | |
178 | |
179 if (result != base::PLATFORM_FILE_OK) | |
180 return PlatformFileErrorToPepperError(result); | |
181 | |
182 *contents = new PP_DirContents_Dev; | |
183 size_t count = pepper_contents.size(); | |
184 (*contents)->count = count; | |
185 (*contents)->entries = new PP_DirEntry_Dev[count]; | |
186 for (size_t i = 0; i < count; ++i) { | |
187 PP_DirEntry_Dev& entry = (*contents)->entries[i]; | |
188 #if defined(OS_WIN) | |
189 const std::string& name = UTF16ToUTF8(pepper_contents[i].name.value()); | |
190 #else | |
191 const std::string& name = pepper_contents[i].name.value(); | |
192 #endif | |
193 size_t size = name.size() + 1; | |
194 char* name_copy = new char[size]; | |
195 memcpy(name_copy, name.c_str(), size); | |
196 entry.name = name_copy; | |
197 entry.is_dir = BoolToPPBool(pepper_contents[i].is_dir); | |
198 } | |
199 return PP_OK; | |
200 } | |
201 | |
202 void FreeModuleLocalDirContents(PP_Instance instance, | |
203 PP_DirContents_Dev* contents) { | |
204 DCHECK(contents); | |
205 for (int32_t i = 0; i < contents->count; ++i) { | |
206 delete [] contents->entries[i].name; | |
207 } | |
208 delete [] contents->entries; | |
209 delete contents; | |
210 } | |
211 | |
212 PP_Bool NavigateToURL(PP_Instance pp_instance, | 45 PP_Bool NavigateToURL(PP_Instance pp_instance, |
213 const char* url, | 46 const char* url, |
214 const char* target) { | 47 const char* target) { |
215 PluginInstance* instance = ResourceTracker::Get()->GetInstance(pp_instance); | 48 PluginInstance* instance = ResourceTracker::Get()->GetInstance(pp_instance); |
216 if (!instance) | 49 if (!instance) |
217 return PP_FALSE; | 50 return PP_FALSE; |
218 return BoolToPPBool(instance->NavigateToURL(url, target)); | 51 return BoolToPPBool(instance->NavigateToURL(url, target)); |
219 } | 52 } |
220 | 53 |
221 void RunMessageLoop(PP_Instance instance) { | 54 void RunMessageLoop(PP_Instance instance) { |
222 bool old_state = MessageLoop::current()->NestableTasksAllowed(); | 55 bool old_state = MessageLoop::current()->NestableTasksAllowed(); |
223 MessageLoop::current()->SetNestableTasksAllowed(true); | 56 MessageLoop::current()->SetNestableTasksAllowed(true); |
224 MessageLoop::current()->Run(); | 57 MessageLoop::current()->Run(); |
225 MessageLoop::current()->SetNestableTasksAllowed(old_state); | 58 MessageLoop::current()->SetNestableTasksAllowed(old_state); |
226 } | 59 } |
227 | 60 |
228 void QuitMessageLoop(PP_Instance instance) { | 61 void QuitMessageLoop(PP_Instance instance) { |
229 MessageLoop::current()->QuitNow(); | 62 MessageLoop::current()->QuitNow(); |
230 } | 63 } |
231 | 64 |
232 const PPB_Flash ppb_flash = { | 65 const PPB_Flash ppb_flash = { |
233 &SetInstanceAlwaysOnTop, | 66 &SetInstanceAlwaysOnTop, |
234 &PPB_Flash_Impl::DrawGlyphs, | 67 &PPB_Flash_Impl::DrawGlyphs, |
235 &GetProxyForURL, | 68 &GetProxyForURL, |
236 &OpenModuleLocalFile, | |
237 &RenameModuleLocalFile, | |
238 &DeleteModuleLocalFileOrDir, | |
239 &CreateModuleLocalDir, | |
240 &QueryModuleLocalFile, | |
241 &GetModuleLocalDirContents, | |
242 &FreeModuleLocalDirContents, | |
243 &NavigateToURL, | 69 &NavigateToURL, |
244 &RunMessageLoop, | 70 &RunMessageLoop, |
245 &QuitMessageLoop, | 71 &QuitMessageLoop, |
246 }; | 72 }; |
247 | 73 |
248 } // namespace | 74 } // namespace |
249 | 75 |
250 // static | 76 // static |
251 const PPB_Flash* PPB_Flash_Impl::GetInterface() { | 77 const PPB_Flash* PPB_Flash_Impl::GetInterface() { |
252 return &ppb_flash; | 78 return &ppb_flash; |
253 } | 79 } |
254 | 80 |
255 } // namespace ppapi | 81 } // namespace ppapi |
256 } // namespace webkit | 82 } // namespace webkit |
OLD | NEW |