FireBreath  1.4.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Pages
NpapiPluginMac.mm
1 /**********************************************************\
2 Original Author: Richard Bateman (taxilian)
3 
4 Created: Dec 3, 2009
5 License: Dual license model; choose one of two:
6  New BSD License
7  http://www.opensource.org/licenses/bsd-license.php
8  - or -
9  GNU Lesser General Public License, version 2.1
10  http://www.gnu.org/licenses/lgpl-2.1.html
11 
12 Copyright 2009 PacketPass, Inc and the Firebreath development team
13 \**********************************************************/
14 
15 #include <stdio.h>
16 #include <stdlib.h>
17 
18 #include <dlfcn.h>
19 
20 #include "NpapiBrowserHost.h"
21 #include "PluginCore.h"
22 #include "global/config.h"
23 
24 #include "FactoryBase.h"
25 
26 #include "NpapiPluginFactory.h"
27 #include <boost/make_shared.hpp>
28 #include <string>
29 
30 #include "Mac/NpapiPluginMac.h"
31 
32 #include "PluginEvents/DrawingEvents.h"
33 
34 using namespace FB::Npapi;
35 
36 FB::Npapi::NpapiPluginPtr FB::Npapi::createNpapiPlugin(const FB::Npapi::NpapiBrowserHostPtr& host, const std::string& mimetype)
37 {
38  return boost::make_shared<NpapiPluginMac>(host, mimetype);
39 }
40 
41 namespace
42 {
43  std::string getPluginPath()
44  {
45  ::Dl_info dlinfo;
46  if (::dladdr((void*)::NP_Initialize, &dlinfo) != 0) {
47  return dlinfo.dli_fname;
48  } else {
49  return "";
50  }
51  }
52 }
53 
54 NpapiPluginMac::NpapiPluginMac(const FB::Npapi::NpapiBrowserHostPtr &host, const std::string& mimetype)
55  : NpapiPlugin(host, mimetype), m_eventModel((NPEventModel)-1), m_drawingModel((NPDrawingModel)-1)
56 {
57  // If you receive a BAD_ACCESS error here you probably have something
58  // wrong in your FactoryMain.cpp in your plugin project's folder
59 
60  PluginCore::setPlatform("Mac", "NPAPI");
61 
62  // Get the path to the bundle
63  static const std::string pluginPath = getPluginPath();
64  setFSPath(pluginPath);
65 }
66 
67 NpapiPluginMac::~NpapiPluginMac()
68 {
69 }
70 
71 void NpapiPluginMac::init(NPMIMEType pluginType, int16_t argc, char* argn[], char *argv[]) {
72  NpapiPlugin::init(pluginType, argc, argn, argv);
73 
74  // These HAVE to be called here during the NPP_New call to set the drawing and event models.
75  boost::optional<std::string> drawingModel = pluginMain->getParam("drawingmodel");
76  if (drawingModel)
77  m_drawingModel = PluginWindowMac::initPluginWindowMac(m_npHost, drawingModel->c_str());
78  else
79  m_drawingModel = PluginWindowMac::initPluginWindowMac(m_npHost);
80  m_eventModel = PluginEventMac::initPluginEventMac(m_npHost, m_drawingModel);
81 
82  // We can create our event model handler now.
83  pluginEvt = PluginEventMacPtr(PluginEventMac::createPluginEventMac(m_eventModel));
84 
85  // We can create our drawing model handler now. It will be made for the chosen drawing model.
86  pluginWin = PluginWindowMacPtr(PluginWindowMac::createPluginWindowMac(m_drawingModel));
87  if (pluginWin)
88  {
89  pluginWin->setNpHost(m_npHost);
90  // Tell the event model which window to use for events.
91  if (pluginEvt)
92  pluginEvt->setPluginWindow(pluginWin);
93  pluginWin->setPluginEvent(pluginEvt);
94 
95  boost::optional<std::string> showdrawingmodel = pluginMain->getParam("showdrawingmodel");
96  if (showdrawingmodel && (0 != strcmp(showdrawingmodel->c_str(), "0")))
97  pluginWin->setShowDrawingModel(true);
98  }
99 }
100 NPError NpapiPluginMac::SetValue(NPNVariable variable, void *value)
101 {
102  if (variable == NPNVcontentsScaleFactor) {
103  double contentsScaleFactor = *(double *)value;
104  ContentsScaleFactorChangedEvent ev(contentsScaleFactor);
105  // Ignore the return value (whether the event was handled) since we don't have
106  // any fallback handlers anyway.
107  pluginWin->SendEvent(&ev);
108  }
109 
110  return NPERR_NO_ERROR;
111 }
112 
113 
114 NPError NpapiPluginMac::SetWindow(NPWindow* window) {
115  NPError res = NPERR_NO_ERROR;
116  if (window)
117  {
118  if (pluginWin)
119  {
120  // Set the Window.
121  res = pluginWin->SetWindow(window);
122 
123  // Notify the PluginCore about our new window. This will post an AttachedEvent.
124  if (!pluginMain->GetWindow())
125  pluginMain->SetWindow(pluginWin.get());
126  }
127  } else {
128  // Notify the PluginCore about our missing window. This will post an DetachedEvent.
129  pluginMain->ClearWindow();
130  pluginWin.reset();
131  }
132  return res;
133 }
134 
135 int16_t NpapiPluginMac::HandleEvent(void* event)
136 {
137  if (pluginEvt)
138  return pluginEvt->HandleEvent(event);
139  return 0;
140 }
141 
142 int16_t NpapiPluginMac::GetValue(NPPVariable variable, void *value) {
143  int16_t res = NPERR_GENERIC_ERROR;
144  switch (variable) {
145  case NPPVpluginCoreAnimationLayer:
146  {
147  if (pluginWin) {
148  // The PluginWindow owns the CALayer.
149  res = pluginWin->GetValue(variable, value);
150 
151  // Notify the PluginCore about our new window. This will post an AttachedEvent.
152  if (!pluginMain->GetWindow())
153  pluginMain->SetWindow(pluginWin.get());
154  }
155  FBLOG_INFO("PluginCore", "GetValue(NPPVpluginCoreAnimationLayer)");
156  } break;
157  default:
158  res = NpapiPlugin::GetValue(variable, value);
159  break;
160  }
161  return res;
162 }
163 
164 void NpapiPluginMac::HandleTimerEvent() {
165  if (pluginWin)
166  return pluginWin->handleTimerEvent();
167 }
static void setPlatform(const std::string &os, const std::string &browser)
Called by the browser when the Operating System and Browser are known.
Definition: PluginCore.cpp:36