• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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_DOMEventListener.h"
76 #include "GEN_DOMEvent.h"
77 #include <wtf/HashMap.h>
78 
79 #pragma warning(push, 0)
80 #include <WebCore/Node.h>
81 #include <WebCore/Element.h>
82 #include <WebCore/Document.h>
83 #include <WebCore/Attr.h>
84 #include <WebCore/Text.h>
85 #include <WebCore/COMPtr.h>
86 #include <WebCore/CDATASection.h>
87 #include <WebCore/EntityReference.h>
88 #include <WebCore/Entity.h>
89 #include <WebCore/ProcessingInstruction.h>
90 #include <WebCore/Comment.h>
91 #include <WebCore/HTMLDocument.h>
92 #include <WebCore/HTMLElement.h>
93 #include <WebCore/HTMLCollection.h>
94 #include <WebCore/DocumentType.h>
95 #include <WebCore/DocumentFragment.h>
96 #include <WebCore/Notation.h>
97 #include <WebCore/CSSCharsetRule.h>
98 #include <WebCore/CSSFontFaceRule.h>
99 #include <WebCore/CSSImportRule.h>
100 #include <WebCore/CSSMediaRule.h>
101 #include <WebCore/CSSPageRule.h>
102 #include <WebCore/CSSPrimitiveValue.h>
103 #include <WebCore/CSSRule.h>
104 #include <WebCore/CSSRuleList.h>
105 #include <WebCore/CSSStyleDeclaration.h>
106 #include <WebCore/CSSStyleRule.h>
107 #include <WebCore/CSSStyleSheet.h>
108 #include <WebCore/CSSValueList.h>
109 #include <WebCore/StyleSheet.h>
110 #include <WebCore/DOMImplementation.h>
111 #include <WebCore/NamedNodeMap.h>
112 #include <WebCore/NodeList.h>
113 #include <WebCore/Counter.h>
114 #include <WebCore/CSSRuleList.h>
115 #include <WebCore/CSSStyleDeclaration.h>
116 #include <WebCore/MediaList.h>
117 #include <WebCore/Rect.h>
118 #include <WebCore/StyleSheet.h>
119 #include <WebCore/StyleSheetList.h>
120 #include <WebCore/Event.h>
121 #include <WebCore/EventListener.h>
122 #pragma warning(pop)
123 
124 typedef HashMap<void*, GEN_DOMObject*> DOMWrapperCache;
125 
domWrapperCache()126 static DOMWrapperCache& domWrapperCache()
127 {
128     static DOMWrapperCache cache;
129     return cache;
130 }
131 
getDOMWrapper(void * objectHandle)132 GEN_DOMObject* getDOMWrapper(void* objectHandle)
133 {
134     return domWrapperCache().get(objectHandle);
135 }
136 
setDOMWrapper(void * objectHandle,GEN_DOMObject * wrapper)137 void setDOMWrapper(void* objectHandle, GEN_DOMObject* wrapper)
138 {
139     domWrapperCache().set(objectHandle, wrapper);
140 }
141 
removeDOMWrapper(void * objectHandle)142 void removeDOMWrapper(void* objectHandle)
143 {
144     domWrapperCache().remove(objectHandle);
145 }
146 
147 #define COM_DOM_PREFIX(Type) GEN_DOM##Type
148 #define CREATE_ONLY_SELF(Type) \
149     COM_DOM_PREFIX(Type)* COM_DOM_PREFIX(Type)::createInstance(WebCore::Type* impl) \
150     { \
151         if (!impl) \
152             return 0; \
153         if (GEN_DOMObject* cachedInstance = getDOMWrapper(impl)) { \
154             cachedInstance->AddRef(); \
155             return static_cast<COM_DOM_PREFIX(Type)*>(cachedInstance); \
156         } \
157         COMPtr<COM_DOM_PREFIX(Type)> comDOMObject = new COM_DOM_PREFIX(Type)(impl); \
158         setDOMWrapper(impl, comDOMObject.get()); \
159         return comDOMObject.releaseRef(); \
160     } \
161 
162 // Core
163 
createInstance(WebCore::Node * node)164 GEN_DOMNode* GEN_DOMNode::createInstance(WebCore::Node* node)
165 {
166     if (!node)
167         return 0;
168 
169     if (GEN_DOMObject* cachedInstance = getDOMWrapper(node)) {
170         cachedInstance->AddRef();
171         return static_cast<GEN_DOMNode*>(cachedInstance);
172     }
173 
174     COMPtr<GEN_DOMNode> domNode;
175     switch (node->nodeType()) {
176         case WebCore::Node::ELEMENT_NODE:
177             // FIXME: add support for creating subclasses of HTMLElement.
178             // FIXME: add support for creating SVGElements and its subclasses.
179             if (node->isHTMLElement())
180                 domNode = new GEN_DOMHTMLElement(static_cast<WebCore::HTMLElement*>(node));
181             else
182                 domNode = new GEN_DOMElement(static_cast<WebCore::Element*>(node));
183             break;
184         case WebCore::Node::ATTRIBUTE_NODE:
185             domNode = new GEN_DOMAttr(static_cast<WebCore::Attr*>(node));
186             break;
187         case WebCore::Node::TEXT_NODE:
188             domNode = new GEN_DOMText(static_cast<WebCore::Text*>(node));
189             break;
190         case WebCore::Node::CDATA_SECTION_NODE:
191             domNode = new GEN_DOMCDATASection(static_cast<WebCore::CDATASection*>(node));
192             break;
193         case WebCore::Node::ENTITY_REFERENCE_NODE:
194             domNode = new GEN_DOMEntityReference(static_cast<WebCore::EntityReference*>(node));
195             break;
196         case WebCore::Node::ENTITY_NODE:
197             domNode = new GEN_DOMEntity(static_cast<WebCore::Entity*>(node));
198             break;
199         case WebCore::Node::PROCESSING_INSTRUCTION_NODE:
200             domNode = new GEN_DOMProcessingInstruction(static_cast<WebCore::ProcessingInstruction*>(node));
201             break;
202         case WebCore::Node::COMMENT_NODE:
203             domNode = new GEN_DOMComment(static_cast<WebCore::Comment*>(node));
204             break;
205         case WebCore::Node::DOCUMENT_NODE:
206             // FIXME: add support for SVGDocument.
207             if (static_cast<WebCore::Document*>(node)->isHTMLDocument())
208                 domNode = new GEN_DOMHTMLDocument(static_cast<WebCore::HTMLDocument*>(node));
209             else
210                 domNode = new GEN_DOMDocument(static_cast<WebCore::Document*>(node));
211             break;
212         case WebCore::Node::DOCUMENT_TYPE_NODE:
213             domNode = new GEN_DOMDocumentType(static_cast<WebCore::DocumentType*>(node));
214             break;
215         case WebCore::Node::DOCUMENT_FRAGMENT_NODE:
216             domNode = new GEN_DOMDocumentFragment(static_cast<WebCore::DocumentFragment*>(node));
217             break;
218         case WebCore::Node::NOTATION_NODE:
219             domNode = new GEN_DOMNotation(static_cast<WebCore::Notation*>(node));
220             break;
221         default:
222             domNode = new GEN_DOMNode(node);
223             break;
224     }
225 
226     setDOMWrapper(node, domNode.get());
227     return domNode.releaseRef();
228 }
229 
createInstance(WebCore::DOMImplementation * impl)230 GEN_DOMImplementation* GEN_DOMImplementation::createInstance(WebCore::DOMImplementation* impl)
231 {
232     if (!impl)
233         return 0;
234 
235     if (GEN_DOMObject* cachedInstance = getDOMWrapper(impl)) {
236         cachedInstance->AddRef();
237         return static_cast<GEN_DOMImplementation*>(cachedInstance);
238     }
239 
240     COMPtr<GEN_DOMImplementation> comDOMObject = new GEN_DOMImplementation(impl);
241     setDOMWrapper(impl, comDOMObject.get());
242     return comDOMObject.releaseRef();
243 }
244 
245 CREATE_ONLY_SELF(NamedNodeMap)
CREATE_ONLY_SELF(NodeList)246 CREATE_ONLY_SELF(NodeList)
247 
248 // Events
249 
250 // FIXME: Add the subclasses for Event when they get generated.
251 CREATE_ONLY_SELF(Event)
252 CREATE_ONLY_SELF(EventListener)
253 
254 
255 // CSS
256 
257 GEN_DOMCSSRule* GEN_DOMCSSRule::createInstance(WebCore::CSSRule* rule)
258 {
259     if (!rule)
260         return 0;
261 
262     if (GEN_DOMObject* cachedInstance = getDOMWrapper(rule)) {
263         cachedInstance->AddRef();
264         return static_cast<GEN_DOMCSSRule*>(cachedInstance);
265     }
266 
267     COMPtr<GEN_DOMCSSRule> domRule;
268     switch (rule->type()) {
269         case WebCore::CSSRule::STYLE_RULE:
270             domRule = new GEN_DOMCSSStyleRule(static_cast<WebCore::CSSStyleRule*>(rule));
271             break;
272         case WebCore::CSSRule::CHARSET_RULE:
273             domRule = new GEN_DOMCSSCharsetRule(static_cast<WebCore::CSSCharsetRule*>(rule));
274             break;
275         case WebCore::CSSRule::IMPORT_RULE:
276             domRule = new GEN_DOMCSSImportRule(static_cast<WebCore::CSSImportRule*>(rule));
277             break;
278         case WebCore::CSSRule::MEDIA_RULE:
279             domRule = new GEN_DOMCSSMediaRule(static_cast<WebCore::CSSMediaRule*>(rule));
280             break;
281         case WebCore::CSSRule::FONT_FACE_RULE:
282             domRule = new GEN_DOMCSSFontFaceRule(static_cast<WebCore::CSSFontFaceRule*>(rule));
283             break;
284         case WebCore::CSSRule::PAGE_RULE:
285             domRule = new GEN_DOMCSSPageRule(static_cast<WebCore::CSSPageRule*>(rule));
286             break;
287         case WebCore::CSSRule::UNKNOWN_RULE:
288         default:
289             domRule = new GEN_DOMCSSRule(rule);
290             break;
291     }
292 
293     setDOMWrapper(rule, domRule.get());
294     return domRule.releaseRef();
295 }
296 
createInstance(WebCore::StyleSheet * styleSheet)297 GEN_DOMStyleSheet* GEN_DOMStyleSheet::createInstance(WebCore::StyleSheet* styleSheet)
298 {
299     if (!styleSheet)
300         return 0;
301 
302     if (GEN_DOMObject* cachedInstance = getDOMWrapper(styleSheet)) {
303         cachedInstance->AddRef();
304         return static_cast<GEN_DOMStyleSheet*>(cachedInstance);
305     }
306 
307     COMPtr<GEN_DOMStyleSheet> domStyleSheet;
308     if (styleSheet->isCSSStyleSheet())
309         domStyleSheet = new GEN_DOMCSSStyleSheet(static_cast<WebCore::CSSStyleSheet*>(styleSheet));
310     else
311         domStyleSheet = new GEN_DOMStyleSheet(styleSheet);
312 
313     setDOMWrapper(styleSheet, domStyleSheet.get());
314     return domStyleSheet.releaseRef();
315 }
316 
317 
createInstance(WebCore::CSSValue * value)318 GEN_DOMCSSValue* GEN_DOMCSSValue::createInstance(WebCore::CSSValue* value)
319 {
320     if (!value)
321         return 0;
322 
323     if (GEN_DOMObject* cachedInstance = getDOMWrapper(value)) {
324         cachedInstance->AddRef();
325         return static_cast<GEN_DOMCSSValue*>(cachedInstance);
326     }
327 
328     COMPtr<GEN_DOMCSSValue> domValue;
329     switch (value->cssValueType()) {
330         case WebCore::CSSValue::CSS_PRIMITIVE_VALUE:
331             domValue = new GEN_DOMCSSPrimitiveValue(static_cast<WebCore::CSSPrimitiveValue*>(value));
332             break;
333         case WebCore::CSSValue::CSS_VALUE_LIST:
334             domValue = new GEN_DOMCSSValueList(static_cast<WebCore::CSSValueList*>(value));
335             break;
336         case WebCore::CSSValue::CSS_INHERIT:
337             domValue = new GEN_DOMCSSValue(value);
338             break;
339         case WebCore::CSSValue::CSS_CUSTOM:
340             // FIXME: add support for SVGPaint and SVGColor
341             domValue = new GEN_DOMCSSValue(value);
342             break;
343     }
344 
345     setDOMWrapper(value, domValue.get());
346     return domValue.releaseRef();
347 }
348 
349 CREATE_ONLY_SELF(Counter)
350 CREATE_ONLY_SELF(CSSRuleList)
351 CREATE_ONLY_SELF(CSSStyleDeclaration)
352 CREATE_ONLY_SELF(MediaList)
353 CREATE_ONLY_SELF(Rect)
354 CREATE_ONLY_SELF(StyleSheetList)
355 
356 
357 // HTML
358 
359 CREATE_ONLY_SELF(HTMLCollection)
360