| 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 "native_client/tests/fake_browser_ppapi/fake_window.h" | 7 #include "native_client/tests/fake_browser_ppapi/fake_window.h" |
| 8 #include <string.h> | 8 #include <string.h> |
| 9 #include <map> | 9 #include <map> |
| 10 #include <string> | 10 #include <string> |
| 11 | 11 |
| 12 #include "native_client/src/include/portability.h" | 12 #include "native_client/src/include/portability.h" |
| 13 #include "native_client/src/shared/ppapi_proxy/utility.h" | 13 #include "native_client/src/shared/ppapi_proxy/utility.h" |
| 14 #include "native_client/src/shared/ppapi_proxy/plugin_var.h" | 14 #include "native_client/src/shared/ppapi_proxy/plugin_var.h" |
| 15 #include "native_client/tests/fake_browser_ppapi/fake_object.h" | 15 #include "native_client/tests/fake_browser_ppapi/fake_object.h" |
| 16 #include "ppapi/c/pp_var.h" | 16 #include "ppapi/c/pp_var.h" |
| 17 #include "ppapi/c/ppb_var.h" | 17 #include "ppapi/c/ppb_var.h" |
| 18 | 18 |
| 19 using fake_browser_ppapi::Object; | 19 using fake_browser_ppapi::Object; |
| 20 using ppapi_proxy::PluginVar; | 20 using ppapi_proxy::PluginVar; |
| 21 | 21 |
| 22 namespace { | 22 namespace { |
| 23 | 23 |
| 24 PP_Var* NewStringVar(const char* str) { | 24 PP_Var* NewStringVar(PP_Module browser_module, const char* str) { |
| 25 static const PPB_Var* ppb_var = NULL; | 25 static const PPB_Var* ppb_var = NULL; |
| 26 if (ppb_var == NULL) { | 26 if (ppb_var == NULL) { |
| 27 ppb_var = reinterpret_cast<const PPB_Var*>(PluginVar::GetInterface()); | 27 ppb_var = reinterpret_cast<const PPB_Var*>(PluginVar::GetInterface()); |
| 28 if (ppb_var == NULL) { | 28 if (ppb_var == NULL) { |
| 29 return NULL; | 29 return NULL; |
| 30 } | 30 } |
| 31 } | 31 } |
| 32 | 32 |
| 33 PP_Var* var = reinterpret_cast<PP_Var*>(malloc(sizeof(*var))); | 33 PP_Var* var = reinterpret_cast<PP_Var*>(malloc(sizeof(*var))); |
| 34 *var = ppb_var->VarFromUtf8(str, strlen(str)); | 34 *var = ppb_var->VarFromUtf8(browser_module, str, strlen(str)); |
| 35 return var; | 35 return var; |
| 36 } | 36 } |
| 37 | 37 |
| 38 // Returns a PP_Var that mocks the window.location object. | 38 // Returns a PP_Var that mocks the window.location object. |
| 39 PP_Var* LocationObject(const char* page_url) { | 39 PP_Var* LocationObject(PP_Module browser_module, const char* page_url) { |
| 40 // Populate the properties map. | 40 // Populate the properties map. |
| 41 PP_Var* href = NewStringVar(page_url); | 41 PP_Var* href = NewStringVar(browser_module, page_url); |
| 42 Object::PropertyMap properties; | 42 Object::PropertyMap properties; |
| 43 properties["href"] = href; | 43 properties["href"] = href; |
| 44 | 44 |
| 45 // Populate the methods map. | 45 // Populate the methods map. |
| 46 Object::MethodMap methods; | 46 Object::MethodMap methods; |
| 47 | 47 |
| 48 // Create and return a PP_Var for location. | 48 // Create and return a PP_Var for location. |
| 49 PP_Var* location = reinterpret_cast<PP_Var*>(malloc(sizeof(*location))); | 49 PP_Var* location = reinterpret_cast<PP_Var*>(malloc(sizeof(*location))); |
| 50 *location = Object::New(properties, methods); | 50 *location = Object::New(browser_module, properties, methods); |
| 51 return location; | 51 return location; |
| 52 } | 52 } |
| 53 | 53 |
| 54 // Emulates the window.console.log method. | 54 // Emulates the window.console.log method. |
| 55 PP_Var ConsoleLog(Object* object, | 55 PP_Var ConsoleLog(Object* object, |
| 56 uint32_t argc, | 56 uint32_t argc, |
| 57 PP_Var* argv, | 57 PP_Var* argv, |
| 58 PP_Var* exception) { | 58 PP_Var* exception) { |
| 59 UNREFERENCED_PARAMETER(object); | 59 UNREFERENCED_PARAMETER(object); |
| 60 UNREFERENCED_PARAMETER(exception); | 60 UNREFERENCED_PARAMETER(exception); |
| 61 printf("console.log("); | 61 printf("console.log("); |
| 62 for (uint32_t i = 0; i < argc; ++i) { | 62 for (uint32_t i = 0; i < argc; ++i) { |
| 63 // NB: currently we are not doing the printf-style formatting. | 63 // NB: currently we are not doing the printf-style formatting. |
| 64 // TODO(sehr): implement the formatting. | 64 // TODO(sehr): implement the formatting. |
| 65 printf("'%s'", PluginVar::VarToString(argv[i]).c_str()); | 65 printf("'%s'", PluginVar::VarToString(argv[i]).c_str()); |
| 66 if (i < argc - 1) { | 66 if (i < argc - 1) { |
| 67 printf(", "); | 67 printf(", "); |
| 68 } | 68 } |
| 69 } | 69 } |
| 70 printf(")\n"); | 70 printf(")\n"); |
| 71 return PP_MakeVoid(); | 71 return PP_MakeVoid(); |
| 72 } | 72 } |
| 73 | 73 |
| 74 // Returns a PP_Var that mocks the window.console object. | 74 // Returns a PP_Var that mocks the window.console object. |
| 75 PP_Var* ConsoleObject() { | 75 PP_Var* ConsoleObject(PP_Module browser_module) { |
| 76 // Populate the properties map. | 76 // Populate the properties map. |
| 77 Object::PropertyMap properties; | 77 Object::PropertyMap properties; |
| 78 | 78 |
| 79 // Populate the methods map. | 79 // Populate the methods map. |
| 80 Object::MethodMap methods; | 80 Object::MethodMap methods; |
| 81 methods["log"] = ConsoleLog; | 81 methods["log"] = ConsoleLog; |
| 82 | 82 |
| 83 PP_Var* console = reinterpret_cast<PP_Var*>(malloc(sizeof(*console))); | 83 PP_Var* console = reinterpret_cast<PP_Var*>(malloc(sizeof(*console))); |
| 84 *console = Object::New(properties, methods); | 84 *console = Object::New(browser_module, properties, methods); |
| 85 return console; | 85 return console; |
| 86 } | 86 } |
| 87 | 87 |
| 88 // Emulates the window.alert method. | 88 // Emulates the window.alert method. |
| 89 PP_Var Alert(Object* object, | 89 PP_Var Alert(Object* object, |
| 90 uint32_t argc, | 90 uint32_t argc, |
| 91 PP_Var* argv, | 91 PP_Var* argv, |
| 92 PP_Var* exception) { | 92 PP_Var* exception) { |
| 93 UNREFERENCED_PARAMETER(object); | 93 UNREFERENCED_PARAMETER(object); |
| 94 UNREFERENCED_PARAMETER(exception); | 94 UNREFERENCED_PARAMETER(exception); |
| 95 printf("window.alert("); | 95 printf("window.alert("); |
| 96 if (argc == 1) { | 96 if (argc == 1) { |
| 97 printf("'%s'", PluginVar::VarToString(argv[0]).c_str()); | 97 printf("'%s'", PluginVar::VarToString(argv[0]).c_str()); |
| 98 } else { | 98 } else { |
| 99 printf("<BAD PARAMETER COUNT: %d>", argc); | 99 printf("<BAD PARAMETER COUNT: %d>", argc); |
| 100 } | 100 } |
| 101 printf(")\n"); | 101 printf(")\n"); |
| 102 return PP_MakeVoid(); | 102 return PP_MakeVoid(); |
| 103 } | 103 } |
| 104 | 104 |
| 105 } // namespace | 105 } // namespace |
| 106 | 106 |
| 107 namespace fake_browser_ppapi { | 107 namespace fake_browser_ppapi { |
| 108 | 108 |
| 109 FakeWindow::FakeWindow(Host* host, const char* page_url) : host_(host) { | 109 FakeWindow::FakeWindow(PP_Module browser_module, |
| 110 Host* host, |
| 111 const char* page_url) : host_(host) { |
| 110 // Populate the properties map. | 112 // Populate the properties map. |
| 111 Object::PropertyMap properties; | 113 Object::PropertyMap properties; |
| 112 properties["console"] = ConsoleObject(); | 114 properties["console"] = ConsoleObject(browser_module); |
| 113 properties["location"] = LocationObject(page_url); | 115 properties["location"] = LocationObject(browser_module, page_url); |
| 114 // Populate the methods map. | 116 // Populate the methods map. |
| 115 Object::MethodMap methods; | 117 Object::MethodMap methods; |
| 116 methods["alert"] = Alert; | 118 methods["alert"] = Alert; |
| 117 Object* window_object = new Object(properties, methods); | 119 Object* window_object = new Object(browser_module, properties, methods); |
| 118 window_var_ = | 120 window_var_ = |
| 119 host_->var_interface()->CreateObject(&ppapi_proxy::Object::object_class, | 121 host_->var_interface()->CreateObject(browser_module, |
| 122 &ppapi_proxy::Object::object_class, |
| 120 window_object); | 123 window_object); |
| 121 } | 124 } |
| 122 | 125 |
| 123 FakeWindow::~FakeWindow() { | 126 FakeWindow::~FakeWindow() { |
| 124 host_->var_interface()->Release(window_var_); | 127 host_->var_interface()->Release(window_var_); |
| 125 } | 128 } |
| 126 | 129 |
| 127 PP_Var FakeWindow::FakeWindowObject() { | 130 PP_Var FakeWindow::FakeWindowObject() { |
| 128 host_->var_interface()->AddRef(window_var_); | 131 host_->var_interface()->AddRef(window_var_); |
| 129 return window_var_; | 132 return window_var_; |
| 130 } | 133 } |
| 131 | 134 |
| 132 } // namespace fake_browser_ppapi | 135 } // namespace fake_browser_ppapi |
| OLD | NEW |