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 "native_client/tests/fake_browser_ppapi/fake_window.h" | |
8 | |
9 #include <stdio.h> | |
10 #include <string.h> | |
11 | |
12 #include <map> | |
13 #include <string> | |
14 | |
15 #include "native_client/src/include/checked_cast.h" | |
16 #include "native_client/src/include/portability.h" | |
17 #include "native_client/src/shared/ppapi_proxy/plugin_ppb_var_deprecated.h" | |
18 #include "native_client/tests/fake_browser_ppapi/fake_object.h" | |
19 #include "native_client/tests/fake_browser_ppapi/utility.h" | |
20 #include "ppapi/c/dev/ppb_var_deprecated.h" | |
21 #include "ppapi/c/pp_var.h" | |
22 | |
23 using fake_browser_ppapi::Object; | |
24 using ppapi_proxy::PluginVarDeprecated; | |
25 | |
26 namespace { | |
27 | |
28 PP_Module g_browser_module; | |
29 PP_Instance g_browser_instance; | |
30 | |
31 PP_Var* NewStringVar(PP_Module browser_module, const char* str) { | |
32 static const PPB_Var_Deprecated* ppb_var = NULL; | |
33 if (ppb_var == NULL) { | |
34 ppb_var = reinterpret_cast<const PPB_Var_Deprecated*>( | |
35 PluginVarDeprecated::GetInterface()); | |
36 if (ppb_var == NULL) { | |
37 return NULL; | |
38 } | |
39 } | |
40 | |
41 PP_Var* var = reinterpret_cast<PP_Var*>(malloc(sizeof(*var))); | |
42 *var = ppb_var->VarFromUtf8(browser_module, | |
43 str, | |
44 nacl::assert_cast<uint32_t>(strlen(str))); | |
45 return var; | |
46 } | |
47 | |
48 // Returns a PP_Var that mocks the window.location object. | |
49 PP_Var* LocationObject(PP_Module browser_module, | |
50 PP_Instance browser_instance, | |
51 const char* page_url) { | |
52 // Populate the properties map. | |
53 PP_Var* href = NewStringVar(browser_module, page_url); | |
54 Object::PropertyMap properties; | |
55 properties["href"] = href; | |
56 | |
57 // Populate the methods map. | |
58 Object::MethodMap methods; | |
59 | |
60 // Create and return a PP_Var for location. | |
61 PP_Var* location = reinterpret_cast<PP_Var*>(malloc(sizeof(*location))); | |
62 *location = Object::New(browser_module, browser_instance, | |
63 properties, methods); | |
64 return location; | |
65 } | |
66 | |
67 // Emulates the window.console.log method. | |
68 PP_Var ConsoleLog(Object* object, | |
69 uint32_t argc, | |
70 PP_Var* argv, | |
71 PP_Var* exception) { | |
72 UNREFERENCED_PARAMETER(object); | |
73 UNREFERENCED_PARAMETER(exception); | |
74 printf("console.log("); | |
75 for (uint32_t i = 0; i < argc; ++i) { | |
76 // NB: currently we are not doing the printf-style formatting. | |
77 // TODO(sehr): implement the formatting. | |
78 printf("'%s'", PluginVarDeprecated::DebugString(argv[i]).c_str()); | |
79 if (i < argc - 1) { | |
80 printf(", "); | |
81 } | |
82 } | |
83 printf(")\n"); | |
84 return PP_MakeUndefined(); | |
85 } | |
86 | |
87 // Returns a PP_Var that mocks the window.console object. | |
88 PP_Var* ConsoleObject(PP_Module browser_module, PP_Instance browser_instance) { | |
89 // Populate the properties map. | |
90 Object::PropertyMap properties; | |
91 | |
92 // Populate the methods map. | |
93 Object::MethodMap methods; | |
94 methods["log"] = ConsoleLog; | |
95 | |
96 PP_Var* console = reinterpret_cast<PP_Var*>(malloc(sizeof(*console))); | |
97 *console = Object::New(browser_module, browser_instance, properties, methods); | |
98 return console; | |
99 } | |
100 | |
101 // Emulates the window.alert method. | |
102 PP_Var Alert(Object* object, | |
103 uint32_t argc, | |
104 PP_Var* argv, | |
105 PP_Var* exception) { | |
106 UNREFERENCED_PARAMETER(object); | |
107 UNREFERENCED_PARAMETER(exception); | |
108 printf("window.alert("); | |
109 if (argc == 1) { | |
110 printf("'%s'", PluginVarDeprecated::DebugString(argv[0]).c_str()); | |
111 } else { | |
112 printf("<BAD PARAMETER COUNT: %d>", argc); | |
113 } | |
114 printf(")\n"); | |
115 return PP_MakeUndefined(); | |
116 } | |
117 | |
118 std::string GetNexeURL(const char* nexes, const char* isa) { | |
119 const char* match = strstr(nexes, isa); | |
120 if (match == NULL) { | |
121 return std::string(""); | |
122 } | |
123 const char* start = match + strlen(isa); | |
124 const char* end = strchr(start, '"'); | |
125 return std::string(start, end - start); | |
126 } | |
127 | |
128 // Returns a PP_Var that mocks the nexes dictionary. | |
129 PP_Var* NexesObject(PP_Module browser_module, | |
130 PP_Instance browser_instance, | |
131 const char* nexes) { | |
132 // Populate the properties map. | |
133 Object::PropertyMap properties; | |
134 properties["x86-32"] = | |
135 NewStringVar(browser_module, GetNexeURL(nexes, "\"x86-32\": \"").c_str()); | |
136 properties["x86-64"] = | |
137 NewStringVar(browser_module, GetNexeURL(nexes, "\"x86-64\": \"").c_str()); | |
138 properties["arm"] = | |
139 NewStringVar(browser_module, GetNexeURL(nexes, "\"arm\": \"").c_str()); | |
140 | |
141 | |
142 // Populate the methods map. | |
143 Object::MethodMap methods; | |
144 | |
145 PP_Var* nexes_object = | |
146 reinterpret_cast<PP_Var*>(malloc(sizeof(*nexes_object))); | |
147 *nexes_object = | |
148 Object::New(browser_module, browser_instance, properties, methods); | |
149 return nexes_object; | |
150 } | |
151 | |
152 // Emulates the window.JSON.parse method. | |
153 PP_Var Parse(Object* object, | |
154 uint32_t argc, | |
155 PP_Var* argv, | |
156 PP_Var* exception) { | |
157 UNREFERENCED_PARAMETER(object); | |
158 UNREFERENCED_PARAMETER(exception); | |
159 printf("window.JSON.parse("); | |
160 if (argc == 1) { | |
161 printf("'%s')\n", PluginVarDeprecated::DebugString(argv[0]).c_str()); | |
162 } else { | |
163 printf("<BAD PARAMETER COUNT: %d>)\n", argc); | |
164 return PP_MakeUndefined(); | |
165 } | |
166 // Build the nexes object from the json string. | |
167 // Populate the properties map. | |
168 Object::PropertyMap properties; | |
169 properties["nexes"] = | |
170 NexesObject(g_browser_module, | |
171 g_browser_instance, | |
172 PluginVarDeprecated::DebugString(argv[0]).c_str()); | |
173 | |
174 // Populate the methods map. | |
175 Object::MethodMap methods; | |
176 | |
177 return Object::New(g_browser_module, g_browser_instance, properties, methods); | |
178 } | |
179 | |
180 // Returns a PP_Var that mocks the window.JSON object. | |
181 PP_Var* JsonParserObject(PP_Module browser_module, | |
182 PP_Instance browser_instance) { | |
183 // Populate the properties map. | |
184 Object::PropertyMap properties; | |
185 | |
186 // Populate the methods map. | |
187 Object::MethodMap methods; | |
188 methods["parse"] = Parse; | |
189 | |
190 PP_Var* console = reinterpret_cast<PP_Var*>(malloc(sizeof(*console))); | |
191 *console = Object::New(browser_module, browser_instance, properties, methods); | |
192 return console; | |
193 } | |
194 | |
195 } // namespace | |
196 | |
197 namespace fake_browser_ppapi { | |
198 | |
199 FakeWindow::FakeWindow(PP_Module browser_module, | |
200 PP_Instance browser_instance, | |
201 Host* host, | |
202 const char* page_url) : host_(host) { | |
203 // Populate the properties map. | |
204 Object::PropertyMap properties; | |
205 properties["console"] = ConsoleObject(browser_module, browser_instance); | |
206 properties["location"] = LocationObject(browser_module, browser_instance, | |
207 page_url); | |
208 properties["JSON"] = JsonParserObject(browser_module, browser_instance); | |
209 // Populate the methods map. | |
210 Object::MethodMap methods; | |
211 methods["alert"] = Alert; | |
212 Object* window_object = new Object(browser_module, browser_instance, | |
213 properties, methods); | |
214 window_var_ = | |
215 host_->var_deprecated_interface()->CreateObject( | |
216 browser_instance, | |
217 &ppapi_proxy::Object::object_class, | |
218 window_object); | |
219 g_browser_module = browser_module; | |
220 g_browser_instance = browser_instance; | |
221 } | |
222 | |
223 FakeWindow::~FakeWindow() { | |
224 host_->var_deprecated_interface()->Release(window_var_); | |
225 } | |
226 | |
227 PP_Var FakeWindow::FakeWindowObject() { | |
228 host_->var_deprecated_interface()->AddRef(window_var_); | |
229 return window_var_; | |
230 } | |
231 | |
232 } // namespace fake_browser_ppapi | |
OLD | NEW |