1// Copyright 2014 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#include "mojo/examples/pepper_container_app/plugin_module.h"
6
7#include <string>
8
9#include "base/files/file_path.h"
10#include "base/logging.h"
11#include "build/build_config.h"
12#include "mojo/examples/pepper_container_app/interface_list.h"
13#include "mojo/examples/pepper_container_app/plugin_instance.h"
14#include "ppapi/c/pp_errors.h"
15#include "ppapi/shared_impl/callback_tracker.h"
16
17namespace mojo {
18namespace examples {
19
20namespace {
21
22const void* GetInterface(const char* name) {
23  const void* interface =
24      InterfaceList::GetInstance()->GetBrowserInterface(name);
25
26  if (!interface)
27    LOG(WARNING) << "Interface requested " << name;
28
29  return interface;
30}
31
32}  // namespace
33
34PluginModule::EntryPoints::EntryPoints() : get_interface(NULL),
35                                           initialize_module(NULL),
36                                           shutdown_module(NULL) {}
37
38PluginModule::PluginModule() : callback_tracker_(new ppapi::CallbackTracker) {
39  Initialize();
40}
41
42PluginModule::~PluginModule() {
43  callback_tracker_->AbortAll();
44
45  if (entry_points_.shutdown_module)
46    entry_points_.shutdown_module();
47}
48
49scoped_ptr<PluginInstance> PluginModule::CreateInstance() {
50  return make_scoped_ptr(new PluginInstance(this));
51}
52
53const void* PluginModule::GetPluginInterface(const char* name) const {
54  if (entry_points_.get_interface)
55    return entry_points_.get_interface(name);
56  return NULL;
57}
58
59void PluginModule::Initialize() {
60  // Platform-specific filename.
61  // TODO(yzshen): Don't hard-code it.
62#if defined(OS_WIN)
63  static const wchar_t plugin_name[] = L"ppapi_example_gles2_spinning_cube.dll";
64#elif defined(OS_MACOSX)
65  static const char plugin_name[] = "ppapi_example_gles2_spinning_cube.plugin";
66#elif defined(OS_POSIX)
67  static const char plugin_name[] = "libppapi_example_gles2_spinning_cube.so";
68#endif
69
70  base::FilePath plugin_path(plugin_name);
71
72  base::NativeLibraryLoadError error;
73  plugin_module_.Reset(base::LoadNativeLibrary(plugin_path, &error));
74
75  if (!plugin_module_.is_valid()) {
76    LOG(WARNING) << "Cannot load " << plugin_path.AsUTF8Unsafe()
77                 << ". Error: " << error.ToString();
78    return;
79  }
80
81  entry_points_.get_interface =
82      reinterpret_cast<PP_GetInterface_Func>(
83          plugin_module_.GetFunctionPointer("PPP_GetInterface"));
84  if (!entry_points_.get_interface) {
85    LOG(WARNING) << "No PPP_GetInterface in plugin library";
86    return;
87  }
88
89  entry_points_.initialize_module =
90      reinterpret_cast<PP_InitializeModule_Func>(
91          plugin_module_.GetFunctionPointer("PPP_InitializeModule"));
92  if (!entry_points_.initialize_module) {
93    LOG(WARNING) << "No PPP_InitializeModule in plugin library";
94    return;
95  }
96
97  // It's okay for PPP_ShutdownModule to not be defined and |shutdown_module| to
98  // be NULL.
99  entry_points_.shutdown_module =
100      reinterpret_cast<PP_ShutdownModule_Func>(
101          plugin_module_.GetFunctionPointer("PPP_ShutdownModule"));
102
103  int32_t result = entry_points_.initialize_module(pp_module(),
104                                                   &GetInterface);
105  if (result != PP_OK)
106    LOG(WARNING) << "Initializing module failed with error " << result;
107}
108
109}  // namespace examples
110}  // namespace mojo
111