module_instance.cc revision 5821806d5e7f356e8fa4b058a389a808ea183019
1// Copyright (c) 2011 The Chromium 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
36namespace {
37
38class 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
65void 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
75PP_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
96void DidDestroyDefault(PP_Instance /*instance*/) {
97}
98
99void DidChangeViewDefault(PP_Instance /*instance*/, PP_Resource /*view*/) {
100}
101
102void DidChangeFocusDefault(PP_Instance /*instance*/,
103                           PP_Bool /*has_focus*/) {
104}
105
106PP_Bool HandleDocumentLoadDefault(PP_Instance instance,
107                                  PP_Resource url_loader) {
108  return PP_TRUE;
109}
110
111namespace {
112
113const PPP_Instance ppp_instance_interface = {
114  DidCreateDefault,
115  DidDestroyDefault,
116  DidChangeViewDefault,
117  DidChangeFocusDefault,
118  HandleDocumentLoadDefault
119};
120
121///////////////////////////////////////////////////////////////////////////////
122// PPP_Messaging implementation
123///////////////////////////////////////////////////////////////////////////////
124
125void HandleMessage(PP_Instance instance, PP_Var message) {
126  if (message.type != PP_VARTYPE_STRING)
127    return;
128  uint32_t len = 0;
129  const char* test_name = PPBVar()->VarToUtf8(message, &len);
130  RunTest(test_name);
131}
132
133const PPP_Messaging ppp_messaging_interface = {
134  HandleMessage
135};
136
137}  // namespace
138
139///////////////////////////////////////////////////////////////////////////////
140// PPP implementation
141///////////////////////////////////////////////////////////////////////////////
142
143int32_t PPP_InitializeModule(PP_Module module,
144                             PPB_GetInterface get_browser_interface) {
145  set_pp_module(module);
146  set_ppb_get_interface(get_browser_interface);
147  SetupPluginInterfaces();
148  return PP_OK;
149}
150
151void PPP_ShutdownModule() {
152}
153
154const void* PPP_GetInterface(const char* interface_name) {
155  const void* ppp = PluginInterfaceTable::Get()->GetInterface(interface_name);
156
157  // The PPP_Instance interface is required for every plugin,
158  // so supply one if the tester has not.
159  if (ppp == NULL && 0 == strncmp(PPP_INSTANCE_INTERFACE, interface_name,
160                                  strlen(PPP_INSTANCE_INTERFACE))) {
161    return &ppp_instance_interface;
162  }
163  // The PPP_Messaging interface is required for the test set-up,
164  // so we supply our own.
165  if (0 == strncmp(PPP_MESSAGING_INTERFACE, interface_name,
166                   strlen(PPP_MESSAGING_INTERFACE))) {
167    CHECK(ppp == NULL);
168    return &ppp_messaging_interface;
169  }
170  // All other interfaces are to be optionally supplied by the tester,
171  // so we return whatever was added in SetupPluginInterfaces() (if anything).
172  return ppp;
173}
174