1 /*
2 * Copyright (C) 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 *
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. Neither the name of Apple Computer, Inc. ("Apple") nor the names of
14 * its contributors may be used to endorse or promote products derived
15 * from this software without specific prior written permission.
16 *
17 * THIS SOFTWARE IS PROVIDED BY APPLE AND ITS CONTRIBUTORS "AS IS" AND ANY
18 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
19 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
20 * DISCLAIMED. IN NO EVENT SHALL APPLE OR ITS CONTRIBUTORS BE LIABLE FOR ANY
21 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
22 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
23 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
24 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 */
28
29 // This file is a temporary hack to implement the createInstance methods for
30 // the COM DOM bindings.
31
32 #include "config.h"
33 #include "DOMCreateInstance.h"
34
35 #include "GEN_DOMNode.h"
36 #include "GEN_DOMElement.h"
37 #include "GEN_DOMDocument.h"
38 #include "GEN_DOMAttr.h"
39 #include "GEN_DOMText.h"
40 #include "GEN_DOMCDATASection.h"
41 #include "GEN_DOMEntityReference.h"
42 #include "GEN_DOMEntity.h"
43 #include "GEN_DOMProcessingInstruction.h"
44 #include "GEN_DOMComment.h"
45 #include "GEN_DOMHTMLElement.h"
46 #include "GEN_DOMHTMLDocument.h"
47 #include "GEN_DOMHTMLCollection.h"
48 #include "GEN_DOMDocumentType.h"
49 #include "GEN_DOMDocumentFragment.h"
50 #include "GEN_DOMNotation.h"
51 #include "GEN_DOMCSSCharsetRule.h"
52 #include "GEN_DOMCSSFontFaceRule.h"
53 #include "GEN_DOMCSSImportRule.h"
54 #include "GEN_DOMCSSMediaRule.h"
55 #include "GEN_DOMCSSPageRule.h"
56 #include "GEN_DOMCSSPrimitiveValue.h"
57 #include "GEN_DOMCSSRule.h"
58 #include "GEN_DOMCSSRuleList.h"
59 #include "GEN_DOMCSSStyleDeclaration.h"
60 #include "GEN_DOMCSSStyleRule.h"
61 #include "GEN_DOMCSSStyleSheet.h"
62 #include "GEN_DOMCSSValueList.h"
63 #include "GEN_DOMStyleSheet.h"
64 #include "GEN_DOMDOMImplementation.h"
65 #include "GEN_DOMNamedNodeMap.h"
66 #include "GEN_DOMNodeList.h"
67 #include "GEN_DOMCounter.h"
68 #include "GEN_DOMCSSRuleList.h"
69 #include "GEN_DOMCSSStyleDeclaration.h"
70 #include "GEN_DOMMediaList.h"
71 #include "GEN_DOMRect.h"
72 #include "GEN_DOMStyleSheet.h"
73 #include "GEN_DOMStyleSheetList.h"
74 #include "GEN_DOMEvent.h"
75 #include "GEN_DOMEvent.h"
76 #include <wtf/HashMap.h>
77
78 #pragma warning(push, 0)
79 #include <WebCore/Node.h>
80 #include <WebCore/Element.h>
81 #include <WebCore/Document.h>
82 #include <WebCore/Attr.h>
83 #include <WebCore/Text.h>
84 #include <WebCore/COMPtr.h>
85 #include <WebCore/CDATASection.h>
86 #include <WebCore/EntityReference.h>
87 #include <WebCore/Entity.h>
88 #include <WebCore/ProcessingInstruction.h>
89 #include <WebCore/Comment.h>
90 #include <WebCore/HTMLDocument.h>
91 #include <WebCore/HTMLElement.h>
92 #include <WebCore/HTMLCollection.h>
93 #include <WebCore/DocumentType.h>
94 #include <WebCore/DocumentFragment.h>
95 #include <WebCore/Notation.h>
96 #include <WebCore/CSSCharsetRule.h>
97 #include <WebCore/CSSFontFaceRule.h>
98 #include <WebCore/CSSImportRule.h>
99 #include <WebCore/CSSMediaRule.h>
100 #include <WebCore/CSSPageRule.h>
101 #include <WebCore/CSSPrimitiveValue.h>
102 #include <WebCore/CSSRule.h>
103 #include <WebCore/CSSRuleList.h>
104 #include <WebCore/CSSStyleDeclaration.h>
105 #include <WebCore/CSSStyleRule.h>
106 #include <WebCore/CSSStyleSheet.h>
107 #include <WebCore/CSSValueList.h>
108 #include <WebCore/StyleSheet.h>
109 #include <WebCore/DOMImplementation.h>
110 #include <WebCore/NamedNodeMap.h>
111 #include <WebCore/NodeList.h>
112 #include <WebCore/Counter.h>
113 #include <WebCore/CSSRuleList.h>
114 #include <WebCore/CSSStyleDeclaration.h>
115 #include <WebCore/MediaList.h>
116 #include <WebCore/Rect.h>
117 #include <WebCore/StyleSheet.h>
118 #include <WebCore/StyleSheetList.h>
119 #include <WebCore/Event.h>
120 #include <WebCore/EventListener.h>
121 #pragma warning(pop)
122
123 typedef HashMap<void*, GEN_DOMObject*> DOMWrapperCache;
124
domWrapperCache()125 static DOMWrapperCache& domWrapperCache()
126 {
127 static DOMWrapperCache cache;
128 return cache;
129 }
130
getDOMWrapper(void * objectHandle)131 GEN_DOMObject* getDOMWrapper(void* objectHandle)
132 {
133 return domWrapperCache().get(objectHandle);
134 }
135
setDOMWrapper(void * objectHandle,GEN_DOMObject * wrapper)136 void setDOMWrapper(void* objectHandle, GEN_DOMObject* wrapper)
137 {
138 domWrapperCache().set(objectHandle, wrapper);
139 }
140
removeDOMWrapper(void * objectHandle)141 void removeDOMWrapper(void* objectHandle)
142 {
143 domWrapperCache().remove(objectHandle);
144 }
145
146 #define COM_DOM_PREFIX(Type) GEN_DOM##Type
147 #define CREATE_ONLY_SELF(Type) \
148 COM_DOM_PREFIX(Type)* COM_DOM_PREFIX(Type)::createInstance(WebCore::Type* impl) \
149 { \
150 if (!impl) \
151 return 0; \
152 if (GEN_DOMObject* cachedInstance = getDOMWrapper(impl)) { \
153 cachedInstance->AddRef(); \
154 return static_cast<COM_DOM_PREFIX(Type)*>(cachedInstance); \
155 } \
156 COMPtr<COM_DOM_PREFIX(Type)> comDOMObject = new COM_DOM_PREFIX(Type)(impl); \
157 setDOMWrapper(impl, comDOMObject.get()); \
158 return comDOMObject.releaseRef(); \
159 } \
160
161 // Core
162
createInstance(WebCore::Node * node)163 GEN_DOMNode* GEN_DOMNode::createInstance(WebCore::Node* node)
164 {
165 if (!node)
166 return 0;
167
168 if (GEN_DOMObject* cachedInstance = getDOMWrapper(node)) {
169 cachedInstance->AddRef();
170 return static_cast<GEN_DOMNode*>(cachedInstance);
171 }
172
173 COMPtr<GEN_DOMNode> domNode;
174 switch (node->nodeType()) {
175 case WebCore::Node::ELEMENT_NODE:
176 // FIXME: add support for creating subclasses of HTMLElement.
177 // FIXME: add support for creating SVGElements and its subclasses.
178 if (node->isHTMLElement())
179 domNode = new GEN_DOMHTMLElement(static_cast<WebCore::HTMLElement*>(node));
180 else
181 domNode = new GEN_DOMElement(static_cast<WebCore::Element*>(node));
182 break;
183 case WebCore::Node::ATTRIBUTE_NODE:
184 domNode = new GEN_DOMAttr(static_cast<WebCore::Attr*>(node));
185 break;
186 case WebCore::Node::TEXT_NODE:
187 domNode = new GEN_DOMText(static_cast<WebCore::Text*>(node));
188 break;
189 case WebCore::Node::CDATA_SECTION_NODE:
190 domNode = new GEN_DOMCDATASection(static_cast<WebCore::CDATASection*>(node));
191 break;
192 case WebCore::Node::ENTITY_REFERENCE_NODE:
193 domNode = new GEN_DOMEntityReference(static_cast<WebCore::EntityReference*>(node));
194 break;
195 case WebCore::Node::ENTITY_NODE:
196 domNode = new GEN_DOMEntity(static_cast<WebCore::Entity*>(node));
197 break;
198 case WebCore::Node::PROCESSING_INSTRUCTION_NODE:
199 domNode = new GEN_DOMProcessingInstruction(static_cast<WebCore::ProcessingInstruction*>(node));
200 break;
201 case WebCore::Node::COMMENT_NODE:
202 domNode = new GEN_DOMComment(static_cast<WebCore::Comment*>(node));
203 break;
204 case WebCore::Node::DOCUMENT_NODE:
205 // FIXME: add support for SVGDocument.
206 if (static_cast<WebCore::Document*>(node)->isHTMLDocument())
207 domNode = new GEN_DOMHTMLDocument(static_cast<WebCore::HTMLDocument*>(node));
208 else
209 domNode = new GEN_DOMDocument(static_cast<WebCore::Document*>(node));
210 break;
211 case WebCore::Node::DOCUMENT_TYPE_NODE:
212 domNode = new GEN_DOMDocumentType(static_cast<WebCore::DocumentType*>(node));
213 break;
214 case WebCore::Node::DOCUMENT_FRAGMENT_NODE:
215 domNode = new GEN_DOMDocumentFragment(static_cast<WebCore::DocumentFragment*>(node));
216 break;
217 case WebCore::Node::NOTATION_NODE:
218 domNode = new GEN_DOMNotation(static_cast<WebCore::Notation*>(node));
219 break;
220 default:
221 domNode = new GEN_DOMNode(node);
222 break;
223 }
224
225 setDOMWrapper(node, domNode.get());
226 return domNode.releaseRef();
227 }
228
createInstance(WebCore::DOMImplementation * impl)229 GEN_DOMImplementation* GEN_DOMImplementation::createInstance(WebCore::DOMImplementation* impl)
230 {
231 if (!impl)
232 return 0;
233
234 if (GEN_DOMObject* cachedInstance = getDOMWrapper(impl)) {
235 cachedInstance->AddRef();
236 return static_cast<GEN_DOMImplementation*>(cachedInstance);
237 }
238
239 COMPtr<GEN_DOMImplementation> comDOMObject = new GEN_DOMImplementation(impl);
240 setDOMWrapper(impl, comDOMObject.get());
241 return comDOMObject.releaseRef();
242 }
243
244 CREATE_ONLY_SELF(NamedNodeMap)
CREATE_ONLY_SELF(NodeList)245 CREATE_ONLY_SELF(NodeList)
246
247 // Events
248
249 // FIXME: Add the subclasses for Event when they get generated.
250 CREATE_ONLY_SELF(Event)
251
252
253 // CSS
254
255 GEN_DOMCSSRule* GEN_DOMCSSRule::createInstance(WebCore::CSSRule* rule)
256 {
257 if (!rule)
258 return 0;
259
260 if (GEN_DOMObject* cachedInstance = getDOMWrapper(rule)) {
261 cachedInstance->AddRef();
262 return static_cast<GEN_DOMCSSRule*>(cachedInstance);
263 }
264
265 COMPtr<GEN_DOMCSSRule> domRule;
266 switch (rule->type()) {
267 case WebCore::CSSRule::STYLE_RULE:
268 domRule = new GEN_DOMCSSStyleRule(static_cast<WebCore::CSSStyleRule*>(rule));
269 break;
270 case WebCore::CSSRule::CHARSET_RULE:
271 domRule = new GEN_DOMCSSCharsetRule(static_cast<WebCore::CSSCharsetRule*>(rule));
272 break;
273 case WebCore::CSSRule::IMPORT_RULE:
274 domRule = new GEN_DOMCSSImportRule(static_cast<WebCore::CSSImportRule*>(rule));
275 break;
276 case WebCore::CSSRule::MEDIA_RULE:
277 domRule = new GEN_DOMCSSMediaRule(static_cast<WebCore::CSSMediaRule*>(rule));
278 break;
279 case WebCore::CSSRule::FONT_FACE_RULE:
280 domRule = new GEN_DOMCSSFontFaceRule(static_cast<WebCore::CSSFontFaceRule*>(rule));
281 break;
282 case WebCore::CSSRule::PAGE_RULE:
283 domRule = new GEN_DOMCSSPageRule(static_cast<WebCore::CSSPageRule*>(rule));
284 break;
285 case WebCore::CSSRule::UNKNOWN_RULE:
286 default:
287 domRule = new GEN_DOMCSSRule(rule);
288 break;
289 }
290
291 setDOMWrapper(rule, domRule.get());
292 return domRule.releaseRef();
293 }
294
createInstance(WebCore::StyleSheet * styleSheet)295 GEN_DOMStyleSheet* GEN_DOMStyleSheet::createInstance(WebCore::StyleSheet* styleSheet)
296 {
297 if (!styleSheet)
298 return 0;
299
300 if (GEN_DOMObject* cachedInstance = getDOMWrapper(styleSheet)) {
301 cachedInstance->AddRef();
302 return static_cast<GEN_DOMStyleSheet*>(cachedInstance);
303 }
304
305 COMPtr<GEN_DOMStyleSheet> domStyleSheet;
306 if (styleSheet->isCSSStyleSheet())
307 domStyleSheet = new GEN_DOMCSSStyleSheet(static_cast<WebCore::CSSStyleSheet*>(styleSheet));
308 else
309 domStyleSheet = new GEN_DOMStyleSheet(styleSheet);
310
311 setDOMWrapper(styleSheet, domStyleSheet.get());
312 return domStyleSheet.releaseRef();
313 }
314
315
createInstance(WebCore::CSSValue * value)316 GEN_DOMCSSValue* GEN_DOMCSSValue::createInstance(WebCore::CSSValue* value)
317 {
318 if (!value)
319 return 0;
320
321 if (GEN_DOMObject* cachedInstance = getDOMWrapper(value)) {
322 cachedInstance->AddRef();
323 return static_cast<GEN_DOMCSSValue*>(cachedInstance);
324 }
325
326 COMPtr<GEN_DOMCSSValue> domValue;
327 switch (value->cssValueType()) {
328 case WebCore::CSSValue::CSS_PRIMITIVE_VALUE:
329 domValue = new GEN_DOMCSSPrimitiveValue(static_cast<WebCore::CSSPrimitiveValue*>(value));
330 break;
331 case WebCore::CSSValue::CSS_VALUE_LIST:
332 domValue = new GEN_DOMCSSValueList(static_cast<WebCore::CSSValueList*>(value));
333 break;
334 case WebCore::CSSValue::CSS_INHERIT:
335 domValue = new GEN_DOMCSSValue(value);
336 break;
337 case WebCore::CSSValue::CSS_CUSTOM:
338 // FIXME: add support for SVGPaint and SVGColor
339 domValue = new GEN_DOMCSSValue(value);
340 break;
341 }
342
343 setDOMWrapper(value, domValue.get());
344 return domValue.releaseRef();
345 }
346
347 CREATE_ONLY_SELF(Counter)
348 CREATE_ONLY_SELF(CSSRuleList)
349 CREATE_ONLY_SELF(CSSStyleDeclaration)
350 CREATE_ONLY_SELF(MediaList)
351 CREATE_ONLY_SELF(Rect)
352 CREATE_ONLY_SELF(StyleSheetList)
353
354
355 // HTML
356
357 CREATE_ONLY_SELF(HTMLCollection)
358