OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2011 The Native Client Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 // |
| 5 // This implements the required interfaces for representing a plugin module |
| 6 // instance in browser interactions and provides a way to register custom |
| 7 // plugin interfaces. |
| 8 // |
| 9 |
| 10 #include <stdio.h> |
| 11 #include <string.h> |
| 12 |
| 13 #include <map> |
| 14 |
| 15 #include "native_client/src/include/nacl_macros.h" |
| 16 #include "native_client/src/shared/platform/nacl_check.h" |
| 17 #include "native_client/tests/ppapi_test_lib/get_browser_interface.h" |
| 18 #include "native_client/tests/ppapi_test_lib/internal_utils.h" |
| 19 #include "native_client/tests/ppapi_test_lib/test_interface.h" |
| 20 |
| 21 #include "ppapi/c/dev/ppb_var_deprecated.h" |
| 22 #include "ppapi/c/pp_errors.h" |
| 23 #include "ppapi/c/pp_instance.h" |
| 24 #include "ppapi/c/pp_module.h" |
| 25 #include "ppapi/c/pp_var.h" |
| 26 #include "ppapi/c/ppb.h" |
| 27 #include "ppapi/c/ppb_var.h" |
| 28 #include "ppapi/c/ppp.h" |
| 29 #include "ppapi/c/ppp_instance.h" |
| 30 #include "ppapi/c/ppp_messaging.h" |
| 31 |
| 32 /////////////////////////////////////////////////////////////////////////////// |
| 33 // Plugin interface registration |
| 34 /////////////////////////////////////////////////////////////////////////////// |
| 35 |
| 36 namespace { |
| 37 |
| 38 class PluginInterfaceTable { |
| 39 public: |
| 40 // Return singleton intsance. |
| 41 static PluginInterfaceTable* Get() { |
| 42 static PluginInterfaceTable table; |
| 43 return &table; |
| 44 } |
| 45 |
| 46 void AddInterface(const char* interface_name, const void* ppp_interface) { |
| 47 interface_map_[nacl::string(interface_name)] = ppp_interface; |
| 48 } |
| 49 const void* GetInterface(const char* interface_name) { |
| 50 // This will add a NULL element for missing interfaces. |
| 51 return interface_map_[nacl::string(interface_name)]; |
| 52 } |
| 53 |
| 54 private: |
| 55 NACL_DISALLOW_COPY_AND_ASSIGN(PluginInterfaceTable); |
| 56 |
| 57 PluginInterfaceTable() {} |
| 58 |
| 59 typedef std::map<nacl::string, const void*> InterfaceMap; |
| 60 InterfaceMap interface_map_; |
| 61 }; |
| 62 |
| 63 } // namespace |
| 64 |
| 65 void RegisterPluginInterface(const char* interface_name, |
| 66 const void* ppp_interface) { |
| 67 PluginInterfaceTable::Get()->AddInterface(interface_name, ppp_interface); |
| 68 } |
| 69 |
| 70 |
| 71 /////////////////////////////////////////////////////////////////////////////// |
| 72 // PPP_Instance implementation |
| 73 /////////////////////////////////////////////////////////////////////////////// |
| 74 |
| 75 PP_Bool DidCreateDefault(PP_Instance instance, |
| 76 uint32_t /*argc*/, |
| 77 const char* /*argn*/[], |
| 78 const char* /*argv*/[]) { |
| 79 CHECK(ppb_get_interface() != NULL); |
| 80 CHECK(PPBCore() != NULL); |
| 81 CHECK(PPBGraphics2D() != NULL); |
| 82 CHECK(PPBImageData() != NULL); |
| 83 CHECK(PPBInstance() != NULL); |
| 84 CHECK(PPBMessaging() != NULL); |
| 85 CHECK(PPBURLLoader() != NULL); |
| 86 CHECK(PPBURLRequestInfo() != NULL); |
| 87 CHECK(PPBURLResponseInfo() != NULL); |
| 88 CHECK(PPBVar() != NULL); |
| 89 |
| 90 set_pp_instance(instance); |
| 91 SetupTests(); |
| 92 |
| 93 return PP_TRUE; |
| 94 } |
| 95 |
| 96 namespace { |
| 97 |
| 98 void DidDestroy(PP_Instance /*instance*/) { |
| 99 } |
| 100 |
| 101 void DidChangeView(PP_Instance /*instance*/, |
| 102 const struct PP_Rect* /*position*/, |
| 103 const struct PP_Rect* /*clip*/) { |
| 104 } |
| 105 |
| 106 void DidChangeFocus(PP_Instance /*instance*/, |
| 107 PP_Bool /*has_focus*/) { |
| 108 } |
| 109 |
| 110 PP_Bool HandleDocumentLoad(PP_Instance instance, |
| 111 PP_Resource url_loader) { |
| 112 return PP_TRUE; |
| 113 } |
| 114 |
| 115 const PPP_Instance ppp_instance_interface = { |
| 116 DidCreateDefault, |
| 117 DidDestroy, |
| 118 DidChangeView, |
| 119 DidChangeFocus, |
| 120 HandleDocumentLoad |
| 121 }; |
| 122 |
| 123 /////////////////////////////////////////////////////////////////////////////// |
| 124 // PPP_Messaging implementation |
| 125 /////////////////////////////////////////////////////////////////////////////// |
| 126 |
| 127 void HandleMessage(PP_Instance instance, PP_Var message) { |
| 128 if (message.type != PP_VARTYPE_STRING) |
| 129 return; |
| 130 uint32_t len = 0; |
| 131 const char* test_name = PPBVar()->VarToUtf8(message, &len); |
| 132 RunTest(test_name); |
| 133 } |
| 134 |
| 135 const PPP_Messaging ppp_messaging_interface = { |
| 136 HandleMessage |
| 137 }; |
| 138 |
| 139 } // namespace |
| 140 |
| 141 /////////////////////////////////////////////////////////////////////////////// |
| 142 // PPP implementation |
| 143 /////////////////////////////////////////////////////////////////////////////// |
| 144 |
| 145 int32_t PPP_InitializeModule(PP_Module module, |
| 146 PPB_GetInterface get_browser_interface) { |
| 147 set_pp_module(module); |
| 148 set_ppb_get_interface(get_browser_interface); |
| 149 SetupPluginInterfaces(); |
| 150 return PP_OK; |
| 151 } |
| 152 |
| 153 void PPP_ShutdownModule() { |
| 154 } |
| 155 |
| 156 const void* PPP_GetInterface(const char* interface_name) { |
| 157 const void* ppp = PluginInterfaceTable::Get()->GetInterface(interface_name); |
| 158 |
| 159 // The PPP_Instance interface is required for every plugin, |
| 160 // so supply one if the tester has not. |
| 161 if (ppp == NULL && 0 == strncmp(PPP_INSTANCE_INTERFACE, interface_name, |
| 162 strlen(PPP_INSTANCE_INTERFACE))) { |
| 163 return &ppp_instance_interface; |
| 164 } |
| 165 // The PPP_Messaging interface is required for the test set-up, |
| 166 // so we supply our own. |
| 167 if (0 == strncmp(PPP_MESSAGING_INTERFACE, interface_name, |
| 168 strlen(PPP_MESSAGING_INTERFACE))) { |
| 169 CHECK(ppp == NULL); |
| 170 return &ppp_messaging_interface; |
| 171 } |
| 172 // All other interfaces are to be optionally supplied by the tester, |
| 173 // so we return whatever was added in SetupPluginInterfaces() (if anything). |
| 174 return ppp; |
| 175 } |
OLD | NEW |