1 /*
2 * Copyright (C) 2008, 2009 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
27 #include "config.h"
28 #include "JSDOMGlobalObject.h"
29
30 #include "Document.h"
31 #include "JSDOMWindow.h"
32 #include "JSEventListener.h"
33
34 #if ENABLE(WORKERS)
35 #include "JSWorkerContext.h"
36 #include "WorkerContext.h"
37 #endif
38
39 using namespace JSC;
40
41 namespace WebCore {
42
JSDOMGlobalObjectData()43 JSDOMGlobalObject::JSDOMGlobalObjectData::JSDOMGlobalObjectData()
44 : evt(0)
45 {
46 }
47
JSDOMGlobalObject(PassRefPtr<Structure> structure,JSDOMGlobalObject::JSDOMGlobalObjectData * data,JSObject * thisValue)48 JSDOMGlobalObject::JSDOMGlobalObject(PassRefPtr<Structure> structure, JSDOMGlobalObject::JSDOMGlobalObjectData* data, JSObject* thisValue)
49 : JSGlobalObject(structure, data, thisValue)
50 {
51 }
52
~JSDOMGlobalObject()53 JSDOMGlobalObject::~JSDOMGlobalObject()
54 {
55 JSListenersMap::iterator it = d()->jsEventListeners.begin();
56 JSListenersMap::iterator end = d()->jsEventListeners.end();
57 for (; it != end; ++it)
58 it->second->clearGlobalObject();
59 }
60
markChildren(MarkStack & markStack)61 void JSDOMGlobalObject::markChildren(MarkStack& markStack)
62 {
63 Base::markChildren(markStack);
64
65 JSDOMStructureMap::iterator end = structures().end();
66 for (JSDOMStructureMap::iterator it = structures().begin(); it != end; ++it)
67 it->second->markAggregate(markStack);
68
69 JSDOMConstructorMap::iterator end2 = constructors().end();
70 for (JSDOMConstructorMap::iterator it2 = constructors().begin(); it2 != end2; ++it2)
71 markStack.append(it2->second);
72 }
73
findJSEventListener(JSValue val)74 JSEventListener* JSDOMGlobalObject::findJSEventListener(JSValue val)
75 {
76 if (!val.isObject())
77 return 0;
78
79 return d()->jsEventListeners.get(asObject(val));
80 }
81
findOrCreateJSEventListener(JSValue val)82 PassRefPtr<JSEventListener> JSDOMGlobalObject::findOrCreateJSEventListener(JSValue val)
83 {
84 if (JSEventListener* listener = findJSEventListener(val))
85 return listener;
86
87 if (!val.isObject())
88 return 0;
89
90 // The JSEventListener constructor adds it to our jsEventListeners map.
91 return JSEventListener::create(asObject(val), this, false).get();
92 }
93
createJSAttributeEventListener(JSValue val)94 PassRefPtr<JSEventListener> JSDOMGlobalObject::createJSAttributeEventListener(JSValue val)
95 {
96 if (!val.isObject())
97 return 0;
98
99 return JSEventListener::create(asObject(val), this, true).get();
100 }
101
jsEventListeners()102 JSDOMGlobalObject::JSListenersMap& JSDOMGlobalObject::jsEventListeners()
103 {
104 return d()->jsEventListeners;
105 }
106
setCurrentEvent(Event * evt)107 void JSDOMGlobalObject::setCurrentEvent(Event* evt)
108 {
109 d()->evt = evt;
110 }
111
currentEvent() const112 Event* JSDOMGlobalObject::currentEvent() const
113 {
114 return d()->evt;
115 }
116
toJSDOMGlobalObject(Document * document)117 JSDOMGlobalObject* toJSDOMGlobalObject(Document* document)
118 {
119 return toJSDOMWindow(document->frame());
120 }
121
toJSDOMGlobalObject(ScriptExecutionContext * scriptExecutionContext)122 JSDOMGlobalObject* toJSDOMGlobalObject(ScriptExecutionContext* scriptExecutionContext)
123 {
124 if (scriptExecutionContext->isDocument())
125 return toJSDOMGlobalObject(static_cast<Document*>(scriptExecutionContext));
126
127 #if ENABLE(WORKERS)
128 if (scriptExecutionContext->isWorkerContext())
129 return static_cast<WorkerContext*>(scriptExecutionContext)->script()->workerContextWrapper();
130 #endif
131
132 ASSERT_NOT_REACHED();
133 return 0;
134 }
135
136 } // namespace WebCore
137