|
1 /* |
|
2 * Copyright (C) 2006, 2007 Apple Inc. All rights reserved. |
|
3 * |
|
4 * Redistribution and use in source and binary forms, with or without |
|
5 * modification, are permitted provided that the following conditions |
|
6 * are met: |
|
7 * 1. Redistributions of source code must retain the above copyright |
|
8 * notice, this list of conditions and the following disclaimer. |
|
9 * 2. Redistributions in binary form must reproduce the above copyright |
|
10 * notice, this list of conditions and the following disclaimer in the |
|
11 * documentation and/or other materials provided with the distribution. |
|
12 * |
|
13 * THIS SOFTWARE IS PROVIDED BY APPLE COMPUTER, INC. ``AS IS'' AND ANY |
|
14 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
|
15 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR |
|
16 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE COMPUTER, INC. OR |
|
17 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, |
|
18 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, |
|
19 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR |
|
20 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY |
|
21 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
|
22 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
|
23 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
|
24 */ |
|
25 |
|
26 #include "config.h" |
|
27 #include "WebKitDLL.h" |
|
28 #include "WebElementPropertyBag.h" |
|
29 |
|
30 #include "MarshallingHelpers.h" |
|
31 #include "DOMCoreClasses.h" |
|
32 #include "WebFrame.h" |
|
33 #pragma warning(push, 0) |
|
34 #include <WebCore/Document.h> |
|
35 #include <WebCore/Frame.h> |
|
36 #include <WebCore/HitTestResult.h> |
|
37 #include <WebCore/FrameLoader.h> |
|
38 #include <WebCore/Image.h> |
|
39 #include <WebCore/KURL.h> |
|
40 #pragma warning(pop) |
|
41 |
|
42 using namespace WebCore; |
|
43 |
|
44 // WebElementPropertyBag ----------------------------------------------- |
|
45 WebElementPropertyBag::WebElementPropertyBag(const HitTestResult& result) |
|
46 : m_result(new HitTestResult(result)) |
|
47 , m_refCount(0) |
|
48 { |
|
49 } |
|
50 |
|
51 WebElementPropertyBag::~WebElementPropertyBag() |
|
52 { |
|
53 } |
|
54 |
|
55 WebElementPropertyBag* WebElementPropertyBag::createInstance(const HitTestResult& result) |
|
56 { |
|
57 WebElementPropertyBag* instance = new WebElementPropertyBag(result); |
|
58 instance->AddRef(); |
|
59 |
|
60 return instance; |
|
61 } |
|
62 |
|
63 // IUnknown ------------------------------------------------------------------- |
|
64 |
|
65 HRESULT STDMETHODCALLTYPE WebElementPropertyBag::QueryInterface(REFIID riid, void** ppvObject) |
|
66 { |
|
67 *ppvObject = 0; |
|
68 if (IsEqualGUID(riid, IID_IUnknown)) |
|
69 *ppvObject = static_cast<IPropertyBag*>(this); |
|
70 else if (IsEqualGUID(riid, IID_IPropertyBag)) |
|
71 *ppvObject = static_cast<IPropertyBag*>(this); |
|
72 else |
|
73 return E_NOINTERFACE; |
|
74 |
|
75 AddRef(); |
|
76 return S_OK; |
|
77 } |
|
78 |
|
79 ULONG STDMETHODCALLTYPE WebElementPropertyBag::AddRef(void) |
|
80 { |
|
81 return ++m_refCount; |
|
82 } |
|
83 |
|
84 ULONG STDMETHODCALLTYPE WebElementPropertyBag::Release(void) |
|
85 { |
|
86 ULONG newRef = --m_refCount; |
|
87 if (!newRef) |
|
88 delete this; |
|
89 |
|
90 return newRef; |
|
91 } |
|
92 |
|
93 static bool isEqual(LPCWSTR s1, LPCWSTR s2) |
|
94 { |
|
95 return !wcscmp(s1, s2); |
|
96 } |
|
97 |
|
98 static HRESULT convertStringToVariant(VARIANT* pVar, const String& string) |
|
99 { |
|
100 V_VT(pVar) = VT_BSTR; |
|
101 V_BSTR(pVar) = SysAllocStringLen(string.characters(), string.length()); |
|
102 if (string.length() && !V_BSTR(pVar)) |
|
103 return E_OUTOFMEMORY; |
|
104 |
|
105 return S_OK; |
|
106 } |
|
107 |
|
108 |
|
109 HRESULT STDMETHODCALLTYPE WebElementPropertyBag::Read(LPCOLESTR pszPropName, VARIANT *pVar, IErrorLog * /*pErrorLog*/) |
|
110 { |
|
111 if (!pszPropName) |
|
112 return E_POINTER; |
|
113 |
|
114 if (!m_result) |
|
115 return E_FAIL; |
|
116 |
|
117 BSTR key = (BSTR)pszPropName; |
|
118 VariantClear(pVar); |
|
119 if (isEqual(WebElementDOMNodeKey, key)) { |
|
120 IDOMNode* node = DOMNode::createInstance(m_result->innerNonSharedNode()); |
|
121 V_VT(pVar) = VT_UNKNOWN; |
|
122 V_UNKNOWN(pVar) = node; |
|
123 return S_OK; |
|
124 } else if (isEqual(WebElementFrameKey, key)) { |
|
125 if (!(m_result->innerNonSharedNode() && m_result->innerNonSharedNode()->document() |
|
126 && m_result->innerNonSharedNode()->document()->frame())) |
|
127 return E_FAIL; |
|
128 Frame* coreFrame = m_result->innerNonSharedNode()->document()->frame(); |
|
129 WebFrame* webFrame = static_cast<WebFrame*>(coreFrame->loader()->client()); |
|
130 IWebFrame* iWebFrame; |
|
131 if (FAILED(webFrame->QueryInterface(IID_IWebFrame, (void**)&iWebFrame))) |
|
132 return E_FAIL; |
|
133 V_VT(pVar) = VT_UNKNOWN; |
|
134 V_UNKNOWN(pVar) = iWebFrame; |
|
135 return S_OK; |
|
136 } else if (isEqual(WebElementImageAltStringKey, key)) |
|
137 return convertStringToVariant(pVar, m_result->altDisplayString()); |
|
138 else if (isEqual(WebElementImageKey, key)) { |
|
139 V_VT(pVar) = VT_BYREF; |
|
140 V_BYREF(pVar) = m_result->image(); |
|
141 return S_OK; |
|
142 } else if (isEqual(WebElementImageRectKey, key)) { |
|
143 V_VT(pVar) = VT_ARRAY; |
|
144 V_ARRAY(pVar) = MarshallingHelpers::intRectToSafeArray(m_result->boundingBox()); |
|
145 return S_OK; |
|
146 } else if (isEqual(WebElementImageURLKey, key)) |
|
147 return convertStringToVariant(pVar, m_result->absoluteImageURL().url()); |
|
148 else if (isEqual(WebElementIsSelectedKey, key)) { |
|
149 V_VT(pVar) = VT_BOOL; |
|
150 if (m_result->isSelected()) |
|
151 V_BOOL(pVar) = VARIANT_TRUE; |
|
152 else |
|
153 V_BOOL(pVar) = VARIANT_FALSE; |
|
154 return S_OK; |
|
155 } else if (isEqual(WebElementSpellingToolTipKey, key)) |
|
156 return convertStringToVariant(pVar, m_result->spellingToolTip()); |
|
157 else if (isEqual(WebElementTitleKey, key)) |
|
158 return convertStringToVariant(pVar, m_result->title()); |
|
159 else if (isEqual(WebElementLinkURLKey, key)) |
|
160 return convertStringToVariant(pVar, m_result->absoluteLinkURL().url()); |
|
161 else if (isEqual(WebElementLinkTargetFrameKey, key)) { |
|
162 if (!m_result->targetFrame()) |
|
163 return E_FAIL; |
|
164 WebFrame* webFrame = kit(m_result->targetFrame()); |
|
165 IWebFrame* iWebFrame; |
|
166 if (FAILED(webFrame->QueryInterface(IID_IWebFrame, (void**)&iWebFrame))) |
|
167 return E_FAIL; |
|
168 V_VT(pVar) = VT_UNKNOWN; |
|
169 V_UNKNOWN(pVar) = iWebFrame; |
|
170 return S_OK; |
|
171 } else if (isEqual(WebElementLinkTitleKey, key)) |
|
172 return convertStringToVariant(pVar, m_result->titleDisplayString()); |
|
173 else if (isEqual(WebElementLinkLabelKey, key)) |
|
174 return convertStringToVariant(pVar, m_result->textContent()); |
|
175 else if (isEqual(WebElementIsContentEditableKey, key)) { |
|
176 V_VT(pVar) = VT_BOOL; |
|
177 if (m_result->isContentEditable()) |
|
178 V_BOOL(pVar) = VARIANT_TRUE; |
|
179 else |
|
180 V_BOOL(pVar) = VARIANT_FALSE; |
|
181 return S_OK; |
|
182 } |
|
183 |
|
184 return E_INVALIDARG; |
|
185 } |
|
186 |
|
187 HRESULT STDMETHODCALLTYPE WebElementPropertyBag::Write(LPCOLESTR pszPropName, VARIANT* pVar) |
|
188 { |
|
189 if (!pszPropName || !pVar) |
|
190 return E_POINTER; |
|
191 VariantClear(pVar); |
|
192 return E_FAIL; |
|
193 } |