[Author: aa]
[google-gears.git] / gears / base / ie / module_wrapper.h
blob10feff2e689037ff20977f7c257e272a80699172
1 // Copyright 2007, Google Inc.
2 //
3 // Redistribution and use in source and binary forms, with or without
4 // modification, are permitted provided that the following conditions are met:
5 //
6 // 1. Redistributions of source code must retain the above copyright notice,
7 // this list of conditions and the following disclaimer.
8 // 2. Redistributions in binary form must reproduce the above copyright notice,
9 // this list of conditions and the following disclaimer in the documentation
10 // and/or other materials provided with the distribution.
11 // 3. Neither the name of Google Inc. nor the names of its contributors may be
12 // used to endorse or promote products derived from this software without
13 // specific prior written permission.
15 // THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
16 // WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
17 // MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
18 // EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
19 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
20 // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
21 // OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
22 // WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
23 // OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
24 // ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 #ifndef GEARS_BASE_IE_MODULE_WRAPPER_H__
27 #define GEARS_BASE_IE_MODULE_WRAPPER_H__
29 #include <assert.h>
30 #include "gears/base/common/base_class.h"
31 #include "gears/base/common/common.h"
32 #include "gears/base/common/dispatcher.h"
33 #include "gears/base/common/js_types.h"
34 #include "gears/base/ie/atl_headers.h"
35 #include "gears/third_party/scoped_ptr/scoped_ptr.h"
37 // Represents the bridge between the JavaScript engine and a Gears module. A
38 // ModuleWrapper wraps each Gears module instance and exposes its methods to
39 // JavaScript. The wrapper is ref-counted and has ownership of the module so
40 // that when all references are released, the module is destroyed.
41 class ATL_NO_VTABLE ModuleWrapper
42 : public ModuleWrapperBaseClass,
43 public IDispatch,
44 public CComObjectRootEx<CComMultiThreadModel>,
45 public CComCoClass<ModuleWrapper> {
46 public:
47 BEGIN_COM_MAP(ModuleWrapper)
48 COM_INTERFACE_ENTRY(IDispatch)
49 END_COM_MAP()
51 DECLARE_NOT_AGGREGATABLE(ModuleWrapper)
52 DECLARE_PROTECT_FINAL_CONSTRUCT()
54 ModuleWrapper() {
55 VariantInit(&token_);
56 token_.vt = VT_DISPATCH;
57 token_.pdispVal = const_cast<ModuleWrapper *>(this);
60 ~ModuleWrapper() {}
62 static JsCallContext *PeekJsCallContext();
64 // IDispatch
65 STDMETHOD(GetTypeInfoCount)(unsigned int FAR* retval) {
66 // JavaScript does not call this
67 assert(false);
68 return E_NOTIMPL;
71 STDMETHOD(GetTypeInfo)(unsigned int index, LCID lcid,
72 ITypeInfo FAR* FAR* retval) {
73 // JavaScript does not call this
74 assert(false);
75 return E_NOTIMPL;
78 STDMETHOD(GetIDsOfNames)(REFIID iid, OLECHAR FAR* FAR* names,
79 unsigned int num_names, LCID lcid,
80 DISPID FAR* retval);
81 STDMETHOD(Invoke)(DISPID member_id, REFIID iid, LCID lcid, WORD flags,
82 DISPPARAMS FAR* params, VARIANT FAR* retval,
83 EXCEPINFO FAR* exception,
84 unsigned int FAR* arg_error_index);
86 // ModuleWrapperBaseClass
87 // Returns a token for this wrapper class that can be returned via the
88 // JsRunnerInterface.
89 virtual JsToken GetWrapperToken() const {
90 return token_;
93 // Gets the Dispatcher for this module.
94 virtual DispatcherInterface *GetDispatcher() const {
95 assert(dispatcher_.get());
96 return dispatcher_.get();
99 virtual void AddReference() {
100 AddRef();
103 virtual void RemoveReference() {
104 Release();
107 void Init(ModuleImplBaseClassVirtual *impl, DispatcherInterface *dispatcher) {
108 assert(!impl_.get());
109 assert(impl);
110 impl_.reset(impl);
112 assert(!dispatcher_.get());
113 assert(dispatcher);
114 dispatcher_.reset(dispatcher);
117 private:
118 scoped_ptr<ModuleImplBaseClassVirtual> impl_;
119 scoped_ptr<DispatcherInterface> dispatcher_;
120 VARIANT token_;
122 DISALLOW_EVIL_CONSTRUCTORS(ModuleWrapper);
127 // Creates an instance of the class and its wrapper.
128 template<class GearsClass>
129 GearsClass *CreateModule(JsRunnerInterface *js_runner) {
130 CComObject<ModuleWrapper> *module_wrapper;
131 HRESULT hr = CComObject<ModuleWrapper>::CreateInstance(&module_wrapper);
132 if (FAILED(hr)) return NULL;
134 GearsClass *impl = new GearsClass();
135 Dispatcher<GearsClass> *dispatcher = new Dispatcher<GearsClass>(impl);
137 module_wrapper->Init(impl, dispatcher);
138 impl->SetJsWrapper(module_wrapper);
139 impl->AddReference();
141 return impl;
144 #endif // GEARS_BASE_IE_MODULE_WRAPPER_H__