1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
|
// Copyright 2008, Google Inc.
// All rights reserved.
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are
// met:
//
// * Redistributions of source code must retain the above copyright
// notice, this list of conditions and the following disclaimer.
// * Redistributions in binary form must reproduce the above
// copyright notice, this list of conditions and the following disclaimer
// in the documentation and/or other materials provided with the
// distribution.
// * Neither the name of Google Inc. nor the names of its
// contributors may be used to endorse or promote products derived from
// this software without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#ifndef WEBKIT_ACTIVEX_SHIM_ACTIVEX_UTIL_H__
#define WEBKIT_ACTIVEX_SHIM_ACTIVEX_UTIL_H__
#include <windows.h>
#include <comdef.h>
#include <string>
#include "base/logging.h"
#include "webkit/glue/plugins/nphostapi.h"
namespace activex_shim {
class DispatchObject;
// Logging
#ifdef TRACK_INTERFACE
#define TRACK_METHOD() LOG(INFO) << "Called: " << __FUNCTION__
#define TRACK_QUERY_INTERFACE(iid, succeeded) \
TrackQueryInterface(iid, succeeded, __FUNCTION__)
#else
#define TRACK_METHOD()
#define TRACK_QUERY_INTERFACE(iid, succeeded)
#endif
// Unfortunately this value is not defined in any Windows header.
const int kHimetricPerInch = 2540;
// Used in macro to log which interface is queried and if it is successful.
void TrackQueryInterface(REFIID iid, bool succeeded, const char* from_function);
// NP types conversions
bool NPIdentifierToWString(NPIdentifier name, std::wstring* ret);
bool VariantToNPVariant(DispatchObject* obj, const VARIANT* vt, NPVariant* npv);
bool NPVariantToVariant(const NPVariant* npv, VARIANT* vt);
// Dispatch interface helpers
bool DispGetID(IDispatch* disp, const wchar_t* name, DISPID* dispid);
bool DispIsMethodOrProperty(IDispatch* disp, const wchar_t* name,
bool checkmethod);
// This is a general invoke function. Use this function to call methods or
// get properties.
// DO NOT use this function to set properties. Use DispSetProperty instead.
bool DispInvoke(IDispatch* disp, const wchar_t* name, VARIANT* args,
int arg_count, VARIANT* result);
// A special version for PROPERTYSET.
bool DispSetProperty(IDispatch* disp, const wchar_t* name,
const VARIANT& rvalue);
// ActiveX object security
enum ActiveXSafety {
SAFE_FOR_SCRIPTING = 0x1,
SAFE_FOR_INITIALIZING = 0x2,
};
// Gets the IObjectSafety interface of the control and set its safe options.
unsigned long GetAndSetObjectSafetyOptions(IUnknown* control);
// Uses the StdComponentCategoriesMgr to determine the safety options the object
// registered.
unsigned long GetRegisteredObjectSafetyOptions(const CLSID& clsid);
// Coord transformation
// Screen coord to Himetric coord.
long ScreenToHimetricX(long x);
long ScreenToHimetricY(long y);
void ScreenToHimetric(long cx, long cy, SIZE* size);
// Create a copy of the string with memory allocated by CoTaskMemAlloc
wchar_t* CoTaskMemAllocString(const std::wstring& s);
// Reference counted IUnknown implementation.
template <class Base> class IUnknownImpl : public Base {
public:
IUnknownImpl() : ref_(1) {
}
// IUnknown
virtual ULONG STDMETHODCALLTYPE AddRef() { return ++ref_; }
virtual ULONG STDMETHODCALLTYPE Release() {
--ref_;
if (ref_ == 0) {
delete this;
}
}
// We don't add QueryInterface here cause normally the subclass should
// have its own implementation.
private:
ULONG ref_;
};
// The original CComObject does reference counting and delete object when
// reference count reaches 0. This is not desirable for us. If an ActiveX
// control incorrectly decrease our reference, then we will crash. Thus
// let's manage our own life!
template <class Base> class NoRefIUnknownImpl : public Base {
public:
~NoRefIUnknownImpl() {
// Let the base class clean up before destruction. It's dangerous for base
// class to do cleanup in destructor, because we usually create the
// object as: NoRefIUnknownImpl<WebActiveXSite>. So when it
// destructs, the outer NoRefIUnknownImpl destructs first, then virtual
// table pointer of IUnknown interface is modified. At this time if we call
// control's code like IOleInPlaceObject::InPlaceDeactivate, and it calls
// back to IUnknown of myself, it will cause "pure function call" exception.
//
// Using a FinalRelease is what ATL does. I found the reason after getting
// the crashes in base class' destructor.
FinalRelease();
}
// IUnknown
virtual ULONG STDMETHODCALLTYPE AddRef() { return 1; }
virtual ULONG STDMETHODCALLTYPE Release() { return 0; }
// We don't add QueryInterface here cause normally the subclass should
// have its own implementation.
};
// A Minimum IDispatch implementation. Used for other classes who need
// the interface but lazy to implement all the typeinfo etc.
class MinimumIDispatchImpl : public IDispatch {
public:
virtual HRESULT STDMETHODCALLTYPE GetTypeInfoCount(UINT* ctinfo) {
*ctinfo = 0;
return S_OK;
}
virtual HRESULT STDMETHODCALLTYPE GetTypeInfo(UINT itinfo, LCID lcid,
ITypeInfo** tinfo) {
return E_NOTIMPL;
}
virtual HRESULT STDMETHODCALLTYPE GetIDsOfNames(
REFIID riid,
LPOLESTR* names,
UINT cnames,
LCID lcid,
DISPID* dispids) {
return E_NOTIMPL;
}
virtual HRESULT STDMETHODCALLTYPE Invoke(
DISPID dispid,
REFIID riid,
LCID lcid,
WORD flags,
DISPPARAMS* params,
VARIANT* result,
EXCEPINFO* except_info,
UINT* arg_error) {
return E_NOTIMPL;
}
};
// This struct is a simple wrap of VARIANT type, so that it could automatically
// initialize when constructed and clear when destructed.
// DO NOT add any virtual function or variable members to this struct, because
// it could be used in arrays.
struct ScopedVariant : public VARIANT {
ScopedVariant() {
VariantInit(this);
}
~ScopedVariant() {
VariantClear(this);
}
};
} // namespace activex_shim
#endif // #ifndef WEBKIT_ACTIVEX_SHIM_ACTIVEX_UTIL_H__
|