| OLD | NEW |
| (Empty) |
| 1 // Copyright (c) 2006-2008 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 #ifndef WEBKIT_ACTIVEX_SHIM_ACTIVEX_UTIL_H__ | |
| 6 #define WEBKIT_ACTIVEX_SHIM_ACTIVEX_UTIL_H__ | |
| 7 | |
| 8 #include <windows.h> | |
| 9 #include <comdef.h> | |
| 10 #include <string> | |
| 11 #include "base/logging.h" | |
| 12 #include "webkit/glue/plugins/nphostapi.h" | |
| 13 | |
| 14 namespace activex_shim { | |
| 15 | |
| 16 class DispatchObject; | |
| 17 | |
| 18 // Logging | |
| 19 #ifdef TRACK_INTERFACE | |
| 20 #define TRACK_METHOD() LOG(INFO) << "Called: " << __FUNCTION__ | |
| 21 #define TRACK_QUERY_INTERFACE(iid, succeeded) \ | |
| 22 TrackQueryInterface(iid, succeeded, __FUNCTION__) | |
| 23 #else | |
| 24 #define TRACK_METHOD() | |
| 25 #define TRACK_QUERY_INTERFACE(iid, succeeded) | |
| 26 #endif | |
| 27 | |
| 28 // Unfortunately this value is not defined in any Windows header. | |
| 29 const int kHimetricPerInch = 2540; | |
| 30 | |
| 31 // Used in macro to log which interface is queried and if it is successful. | |
| 32 void TrackQueryInterface(REFIID iid, bool succeeded, const char* from_function); | |
| 33 | |
| 34 // NP types conversions | |
| 35 bool NPIdentifierToWString(NPIdentifier name, std::wstring* ret); | |
| 36 bool VariantToNPVariant(DispatchObject* obj, const VARIANT* vt, NPVariant* npv); | |
| 37 bool NPVariantToVariant(const NPVariant* npv, VARIANT* vt); | |
| 38 | |
| 39 // Dispatch interface helpers | |
| 40 bool DispGetID(IDispatch* disp, const wchar_t* name, DISPID* dispid); | |
| 41 bool DispIsMethodOrProperty(IDispatch* disp, const wchar_t* name, | |
| 42 bool checkmethod); | |
| 43 // This is a general invoke function. Use this function to call methods or | |
| 44 // get properties. | |
| 45 // DO NOT use this function to set properties. Use DispSetProperty instead. | |
| 46 bool DispInvoke(IDispatch* disp, const wchar_t* name, VARIANT* args, | |
| 47 int arg_count, VARIANT* result); | |
| 48 // A special version for PROPERTYSET. | |
| 49 bool DispSetProperty(IDispatch* disp, const wchar_t* name, | |
| 50 const VARIANT& rvalue); | |
| 51 | |
| 52 // ActiveX object security | |
| 53 enum ActiveXSafety { | |
| 54 SAFE_FOR_SCRIPTING = 0x1, | |
| 55 SAFE_FOR_INITIALIZING = 0x2, | |
| 56 }; | |
| 57 | |
| 58 // Gets the IObjectSafety interface of the control and set its safe options. | |
| 59 unsigned long GetAndSetObjectSafetyOptions(IUnknown* control); | |
| 60 // Uses the StdComponentCategoriesMgr to determine the safety options the object | |
| 61 // registered. | |
| 62 unsigned long GetRegisteredObjectSafetyOptions(const CLSID& clsid); | |
| 63 | |
| 64 // Coord transformation | |
| 65 // Screen coord to Himetric coord. | |
| 66 long ScreenToHimetricX(long x); | |
| 67 long ScreenToHimetricY(long y); | |
| 68 void ScreenToHimetric(long cx, long cy, SIZE* size); | |
| 69 | |
| 70 // Create a copy of the string with memory allocated by CoTaskMemAlloc | |
| 71 wchar_t* CoTaskMemAllocString(const std::wstring& s); | |
| 72 | |
| 73 // Reference counted IUnknown implementation. | |
| 74 template <class Base> class IUnknownImpl : public Base { | |
| 75 public: | |
| 76 IUnknownImpl() : ref_(1) { | |
| 77 } | |
| 78 // IUnknown | |
| 79 virtual ULONG STDMETHODCALLTYPE AddRef() { return ++ref_; } | |
| 80 virtual ULONG STDMETHODCALLTYPE Release() { | |
| 81 --ref_; | |
| 82 if (ref_ == 0) { | |
| 83 delete this; | |
| 84 } | |
| 85 } | |
| 86 // We don't add QueryInterface here cause normally the subclass should | |
| 87 // have its own implementation. | |
| 88 | |
| 89 private: | |
| 90 ULONG ref_; | |
| 91 }; | |
| 92 | |
| 93 // The original CComObject does reference counting and delete object when | |
| 94 // reference count reaches 0. This is not desirable for us. If an ActiveX | |
| 95 // control incorrectly decrease our reference, then we will crash. Thus | |
| 96 // let's manage our own life! | |
| 97 template <class Base> class NoRefIUnknownImpl : public Base { | |
| 98 public: | |
| 99 ~NoRefIUnknownImpl() { | |
| 100 // Let the base class clean up before destruction. It's dangerous for base | |
| 101 // class to do cleanup in destructor, because we usually create the | |
| 102 // object as: NoRefIUnknownImpl<WebActiveXSite>. So when it | |
| 103 // destructs, the outer NoRefIUnknownImpl destructs first, then virtual | |
| 104 // table pointer of IUnknown interface is modified. At this time if we call | |
| 105 // control's code like IOleInPlaceObject::InPlaceDeactivate, and it calls | |
| 106 // back to IUnknown of myself, it will cause "pure function call" exception. | |
| 107 // | |
| 108 // Using a FinalRelease is what ATL does. I found the reason after getting | |
| 109 // the crashes in base class' destructor. | |
| 110 FinalRelease(); | |
| 111 } | |
| 112 // IUnknown | |
| 113 virtual ULONG STDMETHODCALLTYPE AddRef() { return 1; } | |
| 114 virtual ULONG STDMETHODCALLTYPE Release() { return 0; } | |
| 115 // We don't add QueryInterface here cause normally the subclass should | |
| 116 // have its own implementation. | |
| 117 }; | |
| 118 | |
| 119 // A Minimum IDispatch implementation. Used for other classes who need | |
| 120 // the interface but lazy to implement all the typeinfo etc. | |
| 121 class MinimumIDispatchImpl : public IDispatch { | |
| 122 public: | |
| 123 virtual HRESULT STDMETHODCALLTYPE GetTypeInfoCount(UINT* ctinfo) { | |
| 124 *ctinfo = 0; | |
| 125 return S_OK; | |
| 126 } | |
| 127 virtual HRESULT STDMETHODCALLTYPE GetTypeInfo(UINT itinfo, LCID lcid, | |
| 128 ITypeInfo** tinfo) { | |
| 129 return E_NOTIMPL; | |
| 130 } | |
| 131 virtual HRESULT STDMETHODCALLTYPE GetIDsOfNames( | |
| 132 REFIID riid, | |
| 133 LPOLESTR* names, | |
| 134 UINT cnames, | |
| 135 LCID lcid, | |
| 136 DISPID* dispids) { | |
| 137 return E_NOTIMPL; | |
| 138 } | |
| 139 virtual HRESULT STDMETHODCALLTYPE Invoke( | |
| 140 DISPID dispid, | |
| 141 REFIID riid, | |
| 142 LCID lcid, | |
| 143 WORD flags, | |
| 144 DISPPARAMS* params, | |
| 145 VARIANT* result, | |
| 146 EXCEPINFO* except_info, | |
| 147 UINT* arg_error) { | |
| 148 return E_NOTIMPL; | |
| 149 } | |
| 150 }; | |
| 151 | |
| 152 // This struct is a simple wrap of VARIANT type, so that it could automatically | |
| 153 // initialize when constructed and clear when destructed. | |
| 154 // DO NOT add any virtual function or variable members to this struct, because | |
| 155 // it could be used in arrays. | |
| 156 struct ScopedVariant : public VARIANT { | |
| 157 ScopedVariant() { | |
| 158 VariantInit(this); | |
| 159 } | |
| 160 ~ScopedVariant() { | |
| 161 VariantClear(this); | |
| 162 } | |
| 163 }; | |
| 164 | |
| 165 } // namespace activex_shim | |
| 166 | |
| 167 #endif // #ifndef WEBKIT_ACTIVEX_SHIM_ACTIVEX_UTIL_H__ | |
| OLD | NEW |