OLD | NEW |
| (Empty) |
1 /* | |
2 * Copyright (c) 2011 The Native Client Authors. All rights reserved. | |
3 * Use of this source code is governed by a BSD-style license that can be | |
4 * found in the LICENSE file. | |
5 */ | |
6 | |
7 #include <stdio.h> | |
8 #include <string.h> | |
9 #include <unistd.h> | |
10 | |
11 #include "native_client/src/include/portability.h" | |
12 #include "native_client/src/shared/platform/nacl_check.h" | |
13 #include "ppapi/c/dev/ppb_var_deprecated.h" | |
14 #include "ppapi/c/dev/ppb_file_system_dev.h" | |
15 #include "ppapi/c/dev/ppp_class_deprecated.h" | |
16 #include "ppapi/c/pp_completion_callback.h" | |
17 #include "ppapi/c/pp_errors.h" | |
18 #include "ppapi/c/pp_instance.h" | |
19 #include "ppapi/c/pp_module.h" | |
20 #include "ppapi/c/pp_var.h" | |
21 #include "ppapi/c/ppb.h" | |
22 #include "ppapi/c/ppb_core.h" | |
23 #include "ppapi/c/ppb_instance.h" | |
24 #include "ppapi/c/ppb_url_request_info.h" | |
25 | |
26 #define EXPECT_TRUE(expr) do { \ | |
27 if (!(expr)) { \ | |
28 return PP_MakeBool(PP_FALSE); \ | |
29 } \ | |
30 } while (0) | |
31 | |
32 /* Function prototypes */ | |
33 static struct PPB_Var_Deprecated* GetPPB_Var(); | |
34 static struct PPB_Core* GetPPB_Core(); | |
35 static struct PP_Var TestCreate(struct PPB_FileSystem_Dev* ppb_file_system); | |
36 static struct PP_Var TestIsFileSystem( | |
37 struct PPB_FileSystem_Dev* ppb_file_system); | |
38 static struct PP_Var TestOpen(struct PPB_FileSystem_Dev* ppb_file_system); | |
39 static struct PP_Var TestGetType(struct PPB_FileSystem_Dev* ppb_file_system); | |
40 | |
41 /* Global variables */ | |
42 PPB_GetInterface get_browser_interface_func = NULL; | |
43 PP_Instance instance = 0; | |
44 PP_Module module = 0; | |
45 const PP_Instance kInvalidInstance = 0; | |
46 const PP_Resource kInvalidResource = 0; | |
47 const PP_FileSystemType_Dev kFileSystemTypes[] = { | |
48 PP_FILESYSTEMTYPE_EXTERNAL, | |
49 PP_FILESYSTEMTYPE_LOCALPERSISTENT, | |
50 PP_FILESYSTEMTYPE_LOCALTEMPORARY | |
51 }; | |
52 const size_t kNumFileSystemTypes = | |
53 sizeof(kFileSystemTypes) / sizeof(kFileSystemTypes[0]); | |
54 | |
55 static bool HasProperty(void* object, | |
56 struct PP_Var name, | |
57 struct PP_Var* exception) { | |
58 UNREFERENCED_PARAMETER(object); | |
59 UNREFERENCED_PARAMETER(name); | |
60 UNREFERENCED_PARAMETER(exception); | |
61 | |
62 return false; | |
63 } | |
64 | |
65 static bool HasMethod(void* object, | |
66 struct PP_Var name, | |
67 struct PP_Var* exception) { | |
68 uint32_t len = 0; | |
69 struct PPB_Var_Deprecated* ppb_var_interface = GetPPB_Var(); | |
70 const char* method_name = ppb_var_interface->VarToUtf8(name, &len); | |
71 bool has_method = false; | |
72 | |
73 UNREFERENCED_PARAMETER(object); | |
74 UNREFERENCED_PARAMETER(exception); | |
75 | |
76 if (0 == strncmp(method_name, "testCreate", len) || | |
77 0 == strncmp(method_name, "testIsFileSystem", len) || | |
78 0 == strncmp(method_name, "testOpen", len) || | |
79 0 == strncmp(method_name, "testGetType", len)) { | |
80 has_method = true; | |
81 } | |
82 return has_method; | |
83 } | |
84 | |
85 static struct PP_Var GetProperty(void* object, | |
86 struct PP_Var name, | |
87 struct PP_Var* exception) { | |
88 UNREFERENCED_PARAMETER(object); | |
89 UNREFERENCED_PARAMETER(name); | |
90 UNREFERENCED_PARAMETER(exception); | |
91 | |
92 return PP_MakeUndefined(); | |
93 } | |
94 | |
95 static void GetAllPropertyNames(void* object, | |
96 uint32_t* property_count, | |
97 struct PP_Var** properties, | |
98 struct PP_Var* exception) { | |
99 UNREFERENCED_PARAMETER(object); | |
100 UNREFERENCED_PARAMETER(properties); | |
101 UNREFERENCED_PARAMETER(exception); | |
102 | |
103 *property_count = 0; | |
104 } | |
105 | |
106 static void SetProperty(void* object, | |
107 struct PP_Var name, | |
108 struct PP_Var value, | |
109 struct PP_Var* exception) { | |
110 UNREFERENCED_PARAMETER(object); | |
111 UNREFERENCED_PARAMETER(name); | |
112 UNREFERENCED_PARAMETER(value); | |
113 UNREFERENCED_PARAMETER(exception); | |
114 } | |
115 | |
116 static void RemoveProperty(void* object, | |
117 struct PP_Var name, | |
118 struct PP_Var* exception) { | |
119 UNREFERENCED_PARAMETER(object); | |
120 UNREFERENCED_PARAMETER(name); | |
121 UNREFERENCED_PARAMETER(exception); | |
122 } | |
123 | |
124 static struct PP_Var Call(void* object, | |
125 struct PP_Var method_name, | |
126 uint32_t argc, | |
127 struct PP_Var* argv, | |
128 struct PP_Var* exception) { | |
129 struct PPB_Var_Deprecated* ppb_var_interface = GetPPB_Var(); | |
130 uint32_t len; | |
131 const char* method = ppb_var_interface->VarToUtf8(method_name, &len); | |
132 static struct PPB_FileSystem_Dev* ppb_file_system = NULL; | |
133 | |
134 UNREFERENCED_PARAMETER(object); | |
135 UNREFERENCED_PARAMETER(argc); | |
136 UNREFERENCED_PARAMETER(argv); | |
137 UNREFERENCED_PARAMETER(exception); | |
138 | |
139 if (NULL == ppb_file_system) { | |
140 ppb_file_system = (struct PPB_FileSystem_Dev*)(*get_browser_interface_func)( | |
141 PPB_FILESYSTEM_DEV_INTERFACE); | |
142 } | |
143 if (NULL == ppb_file_system) { | |
144 printf("%s ppb_file_system is NULL.\n", __FUNCTION__); | |
145 printf("\tPPB_FILESYSTEM_DEV_INTERFACE: %s.\n", | |
146 PPB_FILESYSTEM_DEV_INTERFACE); | |
147 return PP_MakeNull(); | |
148 } | |
149 if (0 == strncmp(method, "testCreate", len)) { | |
150 return TestCreate(ppb_file_system); | |
151 } else if (0 == strncmp(method, "testIsFileSystem", len)) { | |
152 return TestIsFileSystem(ppb_file_system); | |
153 } else if (0 == strncmp(method, "testOpen", len)) { | |
154 return TestOpen(ppb_file_system); | |
155 } else if (0 == strncmp(method, "testGetType", len)) { | |
156 return TestGetType(ppb_file_system); | |
157 } | |
158 return PP_MakeNull(); | |
159 } | |
160 | |
161 static struct PP_Var Construct(void* object, | |
162 uint32_t argc, | |
163 struct PP_Var* argv, | |
164 struct PP_Var* exception) { | |
165 UNREFERENCED_PARAMETER(object); | |
166 UNREFERENCED_PARAMETER(argc); | |
167 UNREFERENCED_PARAMETER(argv); | |
168 UNREFERENCED_PARAMETER(exception); | |
169 return PP_MakeUndefined(); | |
170 } | |
171 | |
172 static void Deallocate(void* object) { | |
173 UNREFERENCED_PARAMETER(object); | |
174 } | |
175 | |
176 struct PP_Var GetScriptableObject(PP_Instance plugin_instance) { | |
177 if (NULL != get_browser_interface_func) { | |
178 struct PPB_Var_Deprecated* ppb_var_interface = GetPPB_Var(); | |
179 static struct PPP_Class_Deprecated ppp_class = { | |
180 HasProperty, | |
181 HasMethod, | |
182 GetProperty, | |
183 GetAllPropertyNames, | |
184 SetProperty, | |
185 RemoveProperty, | |
186 Call, | |
187 Construct, | |
188 Deallocate | |
189 }; | |
190 instance = plugin_instance; | |
191 | |
192 return ppb_var_interface->CreateObject(instance, &ppp_class, NULL); | |
193 } | |
194 return PP_MakeNull(); | |
195 } | |
196 | |
197 static struct PPB_Core* GetPPB_Core() { | |
198 return (struct PPB_Core*)(*get_browser_interface_func)( | |
199 PPB_CORE_INTERFACE); | |
200 } | |
201 | |
202 static struct PPB_Var_Deprecated* GetPPB_Var() { | |
203 return (struct PPB_Var_Deprecated*)(*get_browser_interface_func)( | |
204 PPB_VAR_DEPRECATED_INTERFACE); | |
205 } | |
206 | |
207 static struct PP_Var TestCreate(struct PPB_FileSystem_Dev* ppb_file_system) { | |
208 PP_Resource file_system = 0; | |
209 struct PPB_Core* const ppb_core = GetPPB_Core(); | |
210 /* | |
211 * Test to see if PPB_FileSystem_Dev::Create returns PP_Resource value of 0 | |
212 * if the instance parameter is invalid. | |
213 */ | |
214 file_system = ppb_file_system->Create(kInvalidInstance, | |
215 PP_FILESYSTEMTYPE_EXTERNAL); | |
216 EXPECT_TRUE(file_system == kInvalidResource); | |
217 file_system = ppb_file_system->Create(kInvalidInstance, | |
218 PP_FILESYSTEMTYPE_LOCALPERSISTENT); | |
219 EXPECT_TRUE(file_system == kInvalidResource); | |
220 file_system = ppb_file_system->Create(kInvalidInstance, | |
221 PP_FILESYSTEMTYPE_LOCALTEMPORARY); | |
222 EXPECT_TRUE(file_system == kInvalidResource); | |
223 | |
224 /* Test for failure when an invalid file system type is requested. */ | |
225 file_system = ppb_file_system->Create(instance, PP_FILESYSTEMTYPE_INVALID); | |
226 EXPECT_TRUE(file_system == kInvalidResource); | |
227 | |
228 /* | |
229 * Test to see if PPB_FileSystem_Dev::Create returns a valid PP_Resource | |
230 * value when given a valid PP_Instance value parameter. Test for all | |
231 * three file system types PPB_FileSystem_Dev supports. | |
232 */ | |
233 for (size_t j = 0; j < kNumFileSystemTypes; ++j) { | |
234 file_system = | |
235 ppb_file_system->Create(instance, kFileSystemTypes[j]); | |
236 EXPECT_TRUE(file_system != kInvalidResource); | |
237 ppb_core->ReleaseResource(file_system); | |
238 } | |
239 return PP_MakeBool(PP_TRUE); | |
240 } | |
241 | |
242 void OpenCallback(void* data, int32_t result) { | |
243 /* | |
244 * We don't check for the result value since we only care the callback was | |
245 * fired. | |
246 */ | |
247 struct PPB_Var_Deprecated* ppb_var = GetPPB_Var(); | |
248 struct PP_Var window; | |
249 struct PP_Var open_callback = ppb_var->VarFromUtf8( | |
250 module, | |
251 "OpenCallback", | |
252 strlen("OpenCallback")); | |
253 struct PP_Var exception = PP_MakeUndefined(); | |
254 struct PPB_Instance* instance_interface = | |
255 (struct PPB_Instance*)(*get_browser_interface_func)( | |
256 PPB_INSTANCE_INTERFACE); | |
257 | |
258 UNREFERENCED_PARAMETER(data); | |
259 UNREFERENCED_PARAMETER(result); | |
260 | |
261 if (NULL == instance_interface) | |
262 printf("%s instance_interface is NULL\n", __FUNCTION__); | |
263 window = instance_interface->GetWindowObject(instance); | |
264 if (PP_VARTYPE_OBJECT != window.type) { | |
265 printf("%s window.type: %i\n", __FUNCTION__, window.type); | |
266 } | |
267 ppb_var->Call(window, open_callback, 0, NULL, &exception); | |
268 ppb_var->Release(open_callback); | |
269 ppb_var->Release(window); | |
270 } | |
271 | |
272 static struct PP_Var TestIsFileSystem( | |
273 struct PPB_FileSystem_Dev* ppb_file_system) { | |
274 struct PPB_Core* const ppb_core = GetPPB_Core(); | |
275 struct PPB_URLRequestInfo* const ppb_url_request_info = | |
276 (struct PPB_URLRequestInfo*)(*get_browser_interface_func)( | |
277 PPB_URLREQUESTINFO_INTERFACE); | |
278 PP_Resource url_request_info = 0; | |
279 PP_Resource file_system = 0; | |
280 size_t j = 0; | |
281 PP_Bool is_file_system = PP_FALSE; | |
282 | |
283 /* Test fail for invalid resource. */ | |
284 EXPECT_TRUE(ppb_file_system->IsFileSystem(kInvalidResource) != PP_TRUE); | |
285 | |
286 /* | |
287 * Test pass for the different valid system types, and test fail against a | |
288 * resource that has been released. | |
289 */ | |
290 for (j = 0; j < kNumFileSystemTypes; ++j) { | |
291 file_system = ppb_file_system->Create(instance, | |
292 kFileSystemTypes[j]); | |
293 CHECK(file_system != 0); | |
294 | |
295 is_file_system = ppb_file_system->IsFileSystem(file_system); | |
296 ppb_core->ReleaseResource(file_system); | |
297 | |
298 EXPECT_TRUE(is_file_system == PP_TRUE); | |
299 | |
300 is_file_system = ppb_file_system->IsFileSystem(file_system); | |
301 EXPECT_TRUE(is_file_system == PP_FALSE); | |
302 } | |
303 | |
304 /* Test fail against a non-filesystem resource */ | |
305 url_request_info = ppb_url_request_info->Create(instance); | |
306 CHECK(url_request_info != 0); | |
307 is_file_system = ppb_file_system->IsFileSystem(url_request_info); | |
308 ppb_core->ReleaseResource(url_request_info); | |
309 EXPECT_TRUE(is_file_system == PP_FALSE); | |
310 | |
311 return PP_MakeBool(PP_TRUE); | |
312 } | |
313 | |
314 static struct PP_Var TestOpen(struct PPB_FileSystem_Dev* ppb_file_system) { | |
315 size_t j = 0; | |
316 PP_Resource file_system = 0; | |
317 struct PP_CompletionCallback open_callback = PP_MakeCompletionCallback( | |
318 OpenCallback, | |
319 NULL); | |
320 struct PPB_Core* const ppb_core = GetPPB_Core(); | |
321 | |
322 /* Test to make sure opening an invalid file system fails. */ | |
323 int32_t pp_error = ppb_file_system->Open(kInvalidResource, | |
324 1024, /* Dummy value */ | |
325 open_callback); | |
326 EXPECT_TRUE(pp_error == PP_ERROR_BADRESOURCE); | |
327 | |
328 /* | |
329 * Test to make sure external file system is not supported. | |
330 * TODO(sanga): Once Chrome supports external file systems, change this test | |
331 * to reflect the change. | |
332 */ | |
333 file_system = ppb_file_system->Create(instance, | |
334 PP_FILESYSTEMTYPE_EXTERNAL); | |
335 pp_error = ppb_file_system->Open(file_system, | |
336 1024, /* Dummy value */ | |
337 open_callback); | |
338 ppb_core->ReleaseResource(file_system); | |
339 EXPECT_TRUE(pp_error != PP_OK); | |
340 EXPECT_TRUE(pp_error != PP_OK_COMPLETIONPENDING); | |
341 | |
342 /* Test local temporary and local persistant file systems */ | |
343 for (j = 1; j < kNumFileSystemTypes; ++j) { | |
344 #ifdef __native_client__ | |
345 /* Test fail for blocking open */ | |
346 /* | |
347 * Only conduct this test with nexe. Trusted ppapi plugin does not work | |
348 * with synchronous Open call. | |
349 * See http://code.google.com/p/chromium/issues/detail?id=78449 | |
350 */ | |
351 file_system = ppb_file_system->Create(instance, | |
352 kFileSystemTypes[j]); | |
353 pp_error = ppb_file_system->Open(file_system, | |
354 1024, /* Dummy value */ | |
355 PP_BlockUntilComplete()); | |
356 ppb_core->ReleaseResource(file_system); | |
357 EXPECT_TRUE(pp_error == PP_ERROR_BADARGUMENT); | |
358 #endif | |
359 | |
360 /* Test success for asynchronous open */ | |
361 file_system = ppb_file_system->Create(instance, | |
362 kFileSystemTypes[j]); | |
363 pp_error = ppb_file_system->Open(file_system, | |
364 1024, /* Dummy value */ | |
365 open_callback); | |
366 ppb_core->ReleaseResource(file_system); | |
367 EXPECT_TRUE(pp_error == PP_OK_COMPLETIONPENDING); | |
368 | |
369 /* Test fail for multiple opens */ | |
370 file_system = ppb_file_system->Create(instance, | |
371 kFileSystemTypes[j]); | |
372 pp_error = ppb_file_system->Open(file_system, | |
373 1024, /* Dummy value */ | |
374 open_callback); | |
375 CHECK(pp_error == PP_OK_COMPLETIONPENDING); /* Previously tested */ | |
376 pp_error = ppb_file_system->Open(file_system, | |
377 1024, /* Dummy value */ | |
378 open_callback); | |
379 ppb_core->ReleaseResource(file_system); | |
380 EXPECT_TRUE(pp_error == PP_ERROR_FAILED); | |
381 } | |
382 | |
383 return PP_MakeBool(PP_TRUE); | |
384 } | |
385 | |
386 static struct PP_Var TestGetType(struct PPB_FileSystem_Dev* ppb_file_system) { | |
387 struct PPB_Core* const ppb_core = GetPPB_Core(); | |
388 struct PPB_URLRequestInfo* const ppb_url_request_info = | |
389 (struct PPB_URLRequestInfo*)(*get_browser_interface_func)( | |
390 PPB_URLREQUESTINFO_INTERFACE); | |
391 PP_Resource url_request_info = 0; | |
392 PP_Resource file_system = 0; | |
393 size_t j = 0; | |
394 PP_FileSystemType_Dev type = PP_FILESYSTEMTYPE_INVALID; | |
395 | |
396 /* Test for invalid resource. */ | |
397 if (PP_FILESYSTEMTYPE_INVALID != ppb_file_system->GetType(0)) | |
398 return PP_MakeBool(PP_FALSE); | |
399 | |
400 /* Test pass for the different valid system types */ | |
401 for (j = 0; j < kNumFileSystemTypes; ++j) { | |
402 file_system = ppb_file_system->Create(instance, | |
403 kFileSystemTypes[j]); | |
404 CHECK(file_system != 0); | |
405 | |
406 type = ppb_file_system->GetType(file_system); | |
407 ppb_core->ReleaseResource(file_system); | |
408 | |
409 EXPECT_TRUE(type == kFileSystemTypes[j]); | |
410 } | |
411 | |
412 /* Test fail against a non-filesystem resource */ | |
413 url_request_info = ppb_url_request_info->Create(instance); | |
414 CHECK(url_request_info != 0); | |
415 | |
416 type = ppb_file_system->GetType(url_request_info); | |
417 ppb_core->ReleaseResource(url_request_info); | |
418 EXPECT_TRUE(type == PP_FILESYSTEMTYPE_INVALID); | |
419 | |
420 return PP_MakeBool(PP_TRUE); | |
421 } | |
OLD | NEW |