OLD | NEW |
1 /* | 1 /* |
2 * Copyright 2010 The Native Client Authors. All rights reserved. | 2 * Copyright 2010 The Native Client Authors. All rights reserved. |
3 * Use of this source code is governed by a BSD-style license that can | 3 * Use of this source code is governed by a BSD-style license that can |
4 * be found in the LICENSE file. | 4 * be found in the LICENSE file. |
5 */ | 5 */ |
6 | 6 |
7 #include <string.h> | 7 #include <string.h> |
8 #include <vector> | 8 #include <vector> |
9 | 9 |
10 #include "native_client/src/include/portability.h" | 10 #include "native_client/src/include/portability.h" |
11 #include "native_client/src/shared/platform/nacl_check.h" | 11 #include "native_client/src/shared/platform/nacl_check.h" |
12 #include "native_client/src/shared/ppapi_proxy/utility.h" | 12 #include "native_client/src/shared/ppapi_proxy/utility.h" |
13 #include "native_client/src/shared/ppapi_proxy/plugin_var.h" | 13 #include "native_client/src/shared/ppapi_proxy/plugin_var.h" |
14 #include "native_client/tests/fake_browser_ppapi/fake_core.h" | 14 #include "native_client/tests/fake_browser_ppapi/fake_core.h" |
15 #include "native_client/tests/fake_browser_ppapi/fake_host.h" | 15 #include "native_client/tests/fake_browser_ppapi/fake_host.h" |
16 #include "native_client/tests/fake_browser_ppapi/fake_instance.h" | 16 #include "native_client/tests/fake_browser_ppapi/fake_instance.h" |
17 #include "native_client/tests/fake_browser_ppapi/fake_window.h" | 17 #include "native_client/tests/fake_browser_ppapi/fake_window.h" |
18 #include "native_client/tests/fake_browser_ppapi/test_scriptable.h" | 18 #include "native_client/tests/fake_browser_ppapi/test_scriptable.h" |
19 #include "ppapi/c/ppb_core.h" | 19 #include "ppapi/c/ppb_core.h" |
20 #include "ppapi/c/ppb_instance.h" | 20 #include "ppapi/c/ppb_instance.h" |
21 #include "ppapi/c/ppb_var.h" | 21 #include "ppapi/c/ppb_var.h" |
22 #include "ppapi/c/ppp_instance.h" | 22 #include "ppapi/c/ppp_instance.h" |
23 #include "ppapi/c/pp_errors.h" | 23 #include "ppapi/c/pp_errors.h" |
24 | 24 |
25 using ppapi_proxy::DebugPrintf; | 25 using ppapi_proxy::DebugPrintf; |
26 using fake_browser_ppapi::Host; | 26 using fake_browser_ppapi::Host; |
27 using fake_browser_ppapi::FakeWindow; | 27 using fake_browser_ppapi::FakeWindow; |
28 | 28 |
29 static fake_browser_ppapi::Host* host = NULL; | 29 namespace { |
| 30 |
| 31 Host* host = NULL; |
30 | 32 |
31 const void* FakeGetInterface(const char* interface_name) { | 33 const void* FakeGetInterface(const char* interface_name) { |
32 DebugPrintf("Getting interface for name '%s'\n", interface_name); | 34 DebugPrintf("Getting interface for name '%s'\n", interface_name); |
33 if (strcmp(interface_name, PPB_CORE_INTERFACE) == 0) { | 35 if (strcmp(interface_name, PPB_CORE_INTERFACE) == 0) { |
34 return host->core_interface(); | 36 return host->core_interface(); |
35 } else if (strcmp(interface_name, PPB_INSTANCE_INTERFACE) == 0) { | 37 } else if (strcmp(interface_name, PPB_INSTANCE_INTERFACE) == 0) { |
36 return host->instance_interface(); | 38 return host->instance_interface(); |
37 } else if (strcmp(interface_name, PPB_VAR_INTERFACE) == 0) { | 39 } else if (strcmp(interface_name, PPB_VAR_INTERFACE) == 0) { |
38 return host->var_interface(); | 40 return host->var_interface(); |
39 } | 41 } |
40 return NULL; | 42 return NULL; |
41 } | 43 } |
42 | 44 |
43 PP_Module FakeGenModuleId() { | 45 // Module ids are needed for some call APIs, but the fake browser does |
| 46 // not implement the storage tracking APIs that would use a real value. |
| 47 // TODO(sehr): implement storage tracking. |
| 48 // The storage allocated by the browser for the window object, etc., are |
| 49 // attributed to the browser's module id. |
| 50 PP_Module BrowserModuleId() { |
| 51 static void* id; |
| 52 return reinterpret_cast<PP_Module>(&id); |
| 53 } |
| 54 |
| 55 // The storage allocated by the plugin for its scriptable objects are |
| 56 // attributed to the its module id. |
| 57 PP_Module PluginModuleId() { |
44 static void* id; | 58 static void* id; |
45 return reinterpret_cast<PP_Module>(&id); | 59 return reinterpret_cast<PP_Module>(&id); |
46 } | 60 } |
47 | 61 |
48 bool ParseArgs(const char* str, | 62 bool ParseArgs(const char* str, |
49 uint32_t* argc, | 63 uint32_t* argc, |
50 const char*** argn, | 64 const char*** argn, |
51 const char*** argv) { | 65 const char*** argv) { |
52 std::vector<std::string> argn_vector; | 66 std::vector<std::string> argn_vector; |
53 std::vector<std::string> argv_vector; | 67 std::vector<std::string> argv_vector; |
(...skipping 15 matching lines...) Expand all Loading... |
69 *argv = reinterpret_cast<const char**>(malloc(*argc * sizeof(*argv))); | 83 *argv = reinterpret_cast<const char**>(malloc(*argc * sizeof(*argv))); |
70 for (uint32_t i = 0; i < *argc; ++i) { | 84 for (uint32_t i = 0; i < *argc; ++i) { |
71 (*argn)[i] = strdup(argn_vector[i].c_str()); | 85 (*argn)[i] = strdup(argn_vector[i].c_str()); |
72 (*argv)[i] = strdup(argv_vector[i].c_str()); | 86 (*argv)[i] = strdup(argv_vector[i].c_str()); |
73 printf("arg[%u]: '%s' = '%s'\n", i, (*argn)[i], (*argv)[i]); | 87 printf("arg[%u]: '%s' = '%s'\n", i, (*argn)[i], (*argv)[i]); |
74 } | 88 } |
75 return true; | 89 return true; |
76 } | 90 } |
77 | 91 |
78 // Test instance execution. | 92 // Test instance execution. |
79 void TestInstance(const PPP_Instance* instance_interface, | 93 void TestInstance(PP_Module browser_module_id, |
| 94 const PPP_Instance* instance_interface, |
80 const char* page_url, | 95 const char* page_url, |
81 uint32_t argc, | 96 uint32_t argc, |
82 const char** argn, | 97 const char** argn, |
83 const char** argv) { | 98 const char** argv) { |
84 printf("page url %s\n", page_url); | 99 printf("page url %s\n", page_url); |
85 // Create a fake window object. | 100 // Create a fake window object. |
86 FakeWindow window(host, page_url); | 101 FakeWindow window(browser_module_id, host, page_url); |
87 // Create an instance and the corresponding id. | 102 // Create an instance and the corresponding id. |
88 fake_browser_ppapi::Instance browser_instance(&window); | 103 fake_browser_ppapi::Instance browser_instance(&window); |
89 PP_Instance instance_id = reinterpret_cast<PP_Instance>(&browser_instance); | 104 PP_Instance instance_id = reinterpret_cast<PP_Instance>(&browser_instance); |
90 // Create a plugin instance. | 105 // Create a plugin instance. |
91 CHECK(instance_interface->New(instance_id)); | 106 CHECK(instance_interface->New(instance_id)); |
92 // Initialize the instance. | 107 // Initialize the instance. |
93 CHECK(instance_interface->Initialize(instance_id, argc, argn, argv)); | 108 CHECK(instance_interface->Initialize(instance_id, argc, argn, argv)); |
94 // Test the scriptable object for the instance. | 109 // Test the scriptable object for the instance. |
95 PP_Var instance_object = instance_interface->GetInstanceObject(instance_id); | 110 PP_Var instance_object = instance_interface->GetInstanceObject(instance_id); |
96 const PPB_Var* var_interface = | 111 const PPB_Var* var_interface = |
97 reinterpret_cast<const PPB_Var*>(FakeGetInterface(PPB_VAR_INTERFACE)); | 112 reinterpret_cast<const PPB_Var*>(FakeGetInterface(PPB_VAR_INTERFACE)); |
98 TestScriptableObject(instance_object, | 113 TestScriptableObject(instance_object, |
99 browser_instance.GetInterface(), | 114 browser_instance.GetInterface(), |
100 var_interface, | 115 var_interface, |
101 instance_id); | 116 instance_id, |
| 117 browser_module_id); |
102 } | 118 } |
103 | 119 |
| 120 } // namespace |
| 121 |
104 int main(int argc, char** argv) { | 122 int main(int argc, char** argv) { |
105 // Turn off stdout buffering to aid debugging in case of a crash. | 123 // Turn off stdout buffering to aid debugging in case of a crash. |
106 setvbuf(stdout, NULL, _IONBF, 0); | 124 setvbuf(stdout, NULL, _IONBF, 0); |
107 | 125 |
108 NaClLogModuleInit(); | 126 NaClLogModuleInit(); |
109 | 127 |
110 if (argc < 5) { | 128 if (argc < 5) { |
111 fprintf(stderr, | 129 fprintf(stderr, |
112 "Usage: fake_browser_ppapi plugin page_url \"embed args\"" | 130 "Usage: fake_browser_ppapi plugin page_url \"embed args\"" |
113 " root_path\n"); | 131 " root_path\n"); |
114 return 1; | 132 return 1; |
115 } | 133 } |
116 | 134 |
117 const char* plugin_name = argv[1]; | 135 const char* plugin_name = argv[1]; |
118 host = | 136 host = |
119 new fake_browser_ppapi::Host(plugin_name, | 137 new fake_browser_ppapi::Host(plugin_name, |
120 fake_browser_ppapi::Core::GetInterface(), | 138 fake_browser_ppapi::Core::GetInterface(), |
121 ppapi_proxy::PluginInstance::GetInterface(), | 139 ppapi_proxy::PluginInstance::GetInterface(), |
122 ppapi_proxy::PluginVar::GetInterface()); | 140 ppapi_proxy::PluginVar::GetInterface()); |
123 CHECK(host != NULL); | 141 CHECK(host != NULL); |
124 | 142 |
125 // Test startup. | 143 // Test startup. |
126 CHECK(host->InitializeModule(FakeGenModuleId(), FakeGetInterface) == PP_OK); | 144 CHECK(host->InitializeModule(PluginModuleId(), FakeGetInterface) == PP_OK); |
127 | 145 |
128 // Get an instance of the plugin. | 146 // Get an instance of the plugin. |
129 const PPP_Instance* instance_interface = | 147 const PPP_Instance* instance_interface = |
130 reinterpret_cast<const PPP_Instance*>( | 148 reinterpret_cast<const PPP_Instance*>( |
131 host->GetInterface(PPP_INSTANCE_INTERFACE)); | 149 host->GetInterface(PPP_INSTANCE_INTERFACE)); |
132 CHECK(instance_interface != NULL); | 150 CHECK(instance_interface != NULL); |
133 const char* page_url = argv[2]; | 151 const char* page_url = argv[2]; |
134 | 152 |
135 // Get the embed argc/argn/argv. | 153 // Get the embed argc/argn/argv. |
136 const char* embed_args = argv[3]; | 154 const char* embed_args = argv[3]; |
137 uint32_t embed_argc; | 155 uint32_t embed_argc; |
138 const char** embed_argn; | 156 const char** embed_argn; |
139 const char** embed_argv; | 157 const char** embed_argv; |
140 CHECK(ParseArgs(embed_args, &embed_argc, &embed_argn, &embed_argv)); | 158 CHECK(ParseArgs(embed_args, &embed_argc, &embed_argn, &embed_argv)); |
141 | 159 |
142 // Temporary support for reading files from disk rather than HTML. | 160 // Temporary support for reading files from disk rather than HTML. |
143 const char* root_path = argv[4]; | 161 const char* root_path = argv[4]; |
144 setenv("NACL_PPAPI_LOCAL_ORIGIN", root_path, 1); | 162 setenv("NACL_PPAPI_LOCAL_ORIGIN", root_path, 1); |
145 | 163 |
146 // Test an instance. | 164 // Test an instance. |
147 TestInstance(instance_interface, | 165 TestInstance(BrowserModuleId(), |
| 166 instance_interface, |
148 page_url, | 167 page_url, |
149 embed_argc, | 168 embed_argc, |
150 embed_argn, | 169 embed_argn, |
151 embed_argv); | 170 embed_argv); |
152 | 171 |
153 // Shutdown. | 172 // Shutdown. |
154 host->ShutdownModule(); | 173 host->ShutdownModule(); |
155 | 174 |
156 // Close the plugin .so. | 175 // Close the plugin .so. |
157 delete host; | 176 delete host; |
158 | 177 |
159 return 0; | 178 return 0; |
160 } | 179 } |
OLD | NEW |