1 // Copyright 2006, Google Inc.
3 // Redistribution and use in source and binary forms, with or without
4 // modification, are permitted provided that the following conditions are met:
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 // A collection of static utility methods.
28 #ifndef GEARS_BASE_IE_ACTIVEX_UTILS_H__
29 #define GEARS_BASE_IE_ACTIVEX_UTILS_H__
31 #include "gears/base/common/string16.h"
32 #include "gears/base/ie/atl_headers.h"
39 struct IPIEHTMLDocument
;
40 struct IPIEHTMLDocument2
;
41 struct IPIEHTMLWindow2
;
42 typedef IPIEHTMLDocument IHTMLDocument
;
43 typedef IPIEHTMLDocument2 IHTMLDocument2
;
44 // WinMo 5 SDK defines IHTMLWindow2, so we can't typedef IPIEHTMLWindow2.
49 // Returns the location url of the containing webBrowser as determined by
50 // IWebBrowser2.LocationURL.
51 // TODO(cprince): across the codebase, change PageOrigin to PageSecurityOrigin
52 // and PageLocation to PageLocationUrl, for consistency.
53 static bool GetPageLocation(IUnknown
*site
, std::string16
*page_location_url
);
54 static bool GetPageOrigin(IUnknown
*site
, SecurityOrigin
*security_origin
);
57 // We're not able to get IWebBrowser2 on WinCE. Instead we obtain the window
58 // and document objects from the script engine's IDispatch pointer.
60 // Returns the IWebBrowser2 interface corresponding to the given site.
61 static HRESULT
GetWebBrowser2(IUnknown
*site
,
62 IWebBrowser2
**browser2
);
65 // Returns the IHTMLDocument2 interface corresponding to the given site.
66 static HRESULT
GetHtmlDocument2(IUnknown
*site
,
67 IHTMLDocument2
**document2
);
69 // Returns the IHTMLWindow2 interface corresponding to the given site.
70 static HRESULT
GetHtmlWindow2(IUnknown
*site
,
72 IPIEHTMLWindow2
**window2
);
74 IHTMLWindow2
**window2
);
78 // WinCE does not provide I(PIE)HTMLWindow3, but we do not need it.
80 // Returns the IHTMLWindow3 interface corresponding to the given site.
81 // Can be used with our HtmlEventMonitor.
82 static HRESULT
GetHtmlWindow3(IUnknown
*site
,
83 IHTMLWindow3
**window3
);
86 // Returns the IDispatch interface for the script engine at the given site.
87 // TODO(zork): Remove dump_on_error.
88 static HRESULT
GetScriptDispatch(IUnknown
*site
,
89 IDispatch
**script_dispatch
,
90 bool dump_on_error
= false);
92 // Returns the dispatch id of the named member.
93 static HRESULT
GetDispatchMemberId(IDispatch
*dispatch
, const WCHAR
*name
,
95 *dispid
= DISPID_UNKNOWN
;
96 return dispatch
->GetIDsOfNames(IID_NULL
, const_cast<WCHAR
**>(&name
), 1, 0,
100 // Determine whether a variant is, ahem, null or undefined.
101 static bool VariantIsNullOrUndefined(const VARIANT
*var
) {
102 return var
->vt
== VT_NULL
|| var
->vt
== VT_EMPTY
;
105 // Determine whether an optional variant parameter was specified. Handles
106 // unpassed params, as well as <undefined> and <null> valued params.
107 // TODO(cprince): When JsParamFetcher is implemented for IE, move this
109 static bool OptionalVariantIsPresent(const VARIANT
*arg
) {
110 // If an optional variant was not passed, we get this
111 // See: http://msdn2.microsoft.com/en-us/library/ms931135.aspx
112 if (arg
->vt
== VT_ERROR
&& arg
->scode
== DISP_E_PARAMNOTFOUND
) {
116 if (VariantIsNullOrUndefined(arg
)) {
123 // Returns the property value by name.
124 // The caller is responsible for freeing the returned VARIANT.
125 static HRESULT
GetDispatchProperty(IDispatch
*dispatch
, const WCHAR
*name
,
128 HRESULT hr
= GetDispatchMemberId(dispatch
, name
, &dispid
);
129 if (FAILED(hr
)) return hr
;
130 return GetDispatchProperty(dispatch
, dispid
, value
);
133 // Returns the property value by dispid.
134 // The caller is responsible for freeing the returned VARIANT.
135 static HRESULT
GetDispatchProperty(IDispatch
*dispatch
, DISPID dispid
,
138 // Sets the property value by name.
139 static HRESULT
SetDispatchProperty(IDispatch
*dispatch
, const WCHAR
*name
,
140 const VARIANT
*value
) {
142 HRESULT hr
= GetDispatchMemberId(dispatch
, name
, &dispid
);
143 if (FAILED(hr
)) return hr
;
144 return SetDispatchProperty(dispatch
, dispid
, value
);
147 // Sets the property value by dispid.
148 static HRESULT
SetDispatchProperty(IDispatch
*dispatch
, DISPID dispid
,
149 const VARIANT
*value
);
151 // Adds a new property to object.
152 // If the property already exists then it is returned.
154 // dispatch - in - the properties are added to this object
155 // name - in - the case sensitive name of the property
156 // dispid - out - the identifier for the property
157 static HRESULT
AddDispatchProperty(IDispatch
* dispatch
, const char16
* name
,
160 // Convenience function for calling AddDispatchProperty
161 // and then SetDispathProperty.
162 static HRESULT
AddAndSetDispatchProperty(IDispatch
* dispatch
,
164 const VARIANT
* value
);
167 // TODO(andreip): implement on Windows Mobile.
169 // Returns the html attribute value of the given HTMLElement.
170 // The caller is responsible for freeing the returned VARIANT.
171 static HRESULT
GetHTMLElementAttributeValue(IHTMLElement
*element
,
176 // Convert NULL BSTR to the empty string.
177 static const CComBSTR kEmptyBSTR
;
178 static const BSTR
SafeBSTR(const BSTR value
) {
179 return value
? value
: kEmptyBSTR
.m_str
;
181 // Returns true if there the browser is in 'online' mode and the local
182 // system is connected to a network.
183 static bool IsOnline();
187 #endif // GEARS_BASE_IE_ACTIVEX_UTILS_H__