FireBreath  1.4.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Pages
COMJavascriptObject.h
1 /**********************************************************\
2 Original Author: Richard Bateman (taxilian)
3 
4 Created: Nov 6, 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 Richard Bateman, Firebreath development team
13 \**********************************************************/
14 
15 #pragma once
16 #ifndef H_COMJAVASCRIPTOBJECT
17 #define H_COMJAVASCRIPTOBJECT
18 
19 #include "JSAPI_IDispatchEx.h"
20 #include "ShareableReference.h"
21 #include <boost/make_shared.hpp>
22 
23 namespace FB {
24  namespace ActiveX {
25  class ActiveXBrowserHost;
26 
27  typedef boost::shared_ptr<FB::ShareableReference<IDispatchEx> > SharedIDispatchRef;
28  typedef boost::weak_ptr<FB::ShareableReference<IDispatchEx> > WeakIDispatchRef;
29  template <const GUID* pclsid, class ICurObjInterface, const IID* piid, const GUID* plibid>
30  class ATL_NO_VTABLE COMJavascriptObject :
31  public CComObjectRootEx<CComMultiThreadModel>,
32  public CComCoClass<COMJavascriptObject<pclsid, ICurObjInterface, piid, plibid>, pclsid>,
33 
34  public JSAPI_IDispatchEx<COMJavascriptObject<pclsid, ICurObjInterface, piid, plibid>, ICurObjInterface, piid>,
35  public IObjectSafetyImpl<COMJavascriptObject<pclsid, ICurObjInterface, piid, plibid>, INTERFACESAFE_FOR_UNTRUSTED_CALLER | INTERFACESAFE_FOR_UNTRUSTED_DATA/* | INTERFACE_USES_DISPEX*/ >,
36 
37  // Required for standard events
38  public IProvideClassInfo2Impl<pclsid, NULL, plibid>
39  {
40  public:
41  typedef COMJavascriptObject<pclsid, ICurObjInterface, piid, plibid> CurObjType;
42  COMJavascriptObject(void)
43  : JSAPI_IDispatchEx<CurObjType, ICurObjInterface, piid>("SUBOBJECT")
44  {
45  m_sharedRef = boost::make_shared<FB::ShareableReference<IDispatchEx> >(this);
46  }
47  virtual ~COMJavascriptObject(void)
48  {
49  }
50 
51  static IDispatchEx *NewObject(ActiveXBrowserHostPtr host, FB::JSAPIWeakPtr api, bool auto_release = false)
52  {
53  CComObject<CurObjType> *obj;
54  HRESULT hr = CComObject<CurObjType>::CreateInstance(&obj);
55 
56  obj->setAPI(api, host);
57  obj->m_autoRelease = auto_release;
58  if (auto_release) {
59  FB::JSAPIPtr tmp(api.lock());
60  // If we are auto-releasing it then we need to retain it as well
61  if (tmp) {
62  host->retainJSAPIPtr(tmp);
63  }
64  }
65  IDispatchEx *retval;
66  hr = obj->QueryInterface(IID_IDispatchEx, (void **)&retval);
67 
68  return retval;
69  }
70 
71  const WeakIDispatchRef getWeakReference() { return m_sharedRef; }
72 
73  DECLARE_NOT_AGGREGATABLE(CurObjType)
74 
75  BEGIN_COM_MAP(CurObjType)
76  COM_INTERFACE_ENTRY_IID((*piid), ICurObjInterface)
77  COM_INTERFACE_ENTRY(IDispatch)
78  COM_INTERFACE_ENTRY(IDispatchEx)
79  COM_INTERFACE_ENTRY(IFireBreathObject)
80  COM_INTERFACE_ENTRY(IObjectSafety)
81  COM_INTERFACE_ENTRY(IConnectionPointContainer)
82  COM_INTERFACE_ENTRY(IConnectionPoint)
83  COM_INTERFACE_ENTRY_IID(IID_IObjectSafety, IObjectSafety)
84  COM_INTERFACE_ENTRY(IProvideClassInfo)
85  COM_INTERFACE_ENTRY(IProvideClassInfo2)
86  END_COM_MAP()
87 
88  DECLARE_PROTECT_FINAL_CONSTRUCT()
89 
90  HRESULT FinalConstruct()
91  {
92  return S_OK;
93  }
94 
95  void FinalRelease()
96  {
97  if (m_autoRelease) {
98  FB::JSAPIPtr api(m_api.lock());
99  // If the JSAPI object is still around and we're set to autorelease, tell the BrowserHost
100  // that we're done with it. Otherwise it's either gone or we don't control its lifecycle
101  if (api) {
102  ActiveXBrowserHostPtr host = getHost();
103  if (host)
104  host->releaseJSAPIPtr(api);
105  }
106  }
107  }
108 
109  private:
110  bool m_autoRelease;
111  SharedIDispatchRef m_sharedRef;
112  };
113  };
114 };
115 #endif
116 
boost::weak_ptr< FB::JSAPI > JSAPIWeakPtr
Defines an alias for a JSAPI weak_ptr (you should never use a JSAPI* directly)
Definition: APITypes.h:88
boost::shared_ptr< FB::JSAPI > JSAPIPtr
Defines an alias for a JSAPI shared_ptr (you should never use a JSAPI* directly)
Definition: APITypes.h:94