1 /*
2 * Copyright (C) 2008 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 #ifdef ANDROID_FIX // these are generated files, need to check ENABLE(WORKERS)
34 #if ENABLE(WORKERS)
35 #include "JSWorkerContext.h"
36 #endif
37 #endif
38 #include "WorkerContext.h"
39
40 using namespace JSC;
41
42 namespace WebCore {
43
JSDOMGlobalObjectData()44 JSDOMGlobalObject::JSDOMGlobalObjectData::JSDOMGlobalObjectData()
45 : evt(0)
46 {
47 }
48
JSDOMGlobalObject(PassRefPtr<Structure> structure,JSDOMGlobalObject::JSDOMGlobalObjectData * data,JSObject * thisValue)49 JSDOMGlobalObject::JSDOMGlobalObject(PassRefPtr<Structure> structure, JSDOMGlobalObject::JSDOMGlobalObjectData* data, JSObject* thisValue)
50 : JSGlobalObject(structure, data, thisValue)
51 {
52 }
53
~JSDOMGlobalObject()54 JSDOMGlobalObject::~JSDOMGlobalObject()
55 {
56 // Clear any backpointers to the window
57 ListenersMap::iterator i1 = d()->jsEventListeners.begin();
58 ListenersMap::iterator e1 = d()->jsEventListeners.end();
59 for (; i1 != e1; ++i1)
60 i1->second->clearGlobalObject();
61
62 i1 = d()->jsInlineEventListeners.begin();
63 e1 = d()->jsInlineEventListeners.end();
64 for (; i1 != e1; ++i1)
65 i1->second->clearGlobalObject();
66
67 UnprotectedListenersMap::iterator i2 = d()->jsUnprotectedEventListeners.begin();
68 UnprotectedListenersMap::iterator e2 = d()->jsUnprotectedEventListeners.end();
69 for (; i2 != e2; ++i2)
70 i2->second->clearGlobalObject();
71
72 i2 = d()->jsUnprotectedInlineEventListeners.begin();
73 e2 = d()->jsUnprotectedInlineEventListeners.end();
74 for (; i2 != e2; ++i2)
75 i2->second->clearGlobalObject();
76 }
77
mark()78 void JSDOMGlobalObject::mark()
79 {
80 Base::mark();
81
82 JSDOMStructureMap::iterator end = structures().end();
83 for (JSDOMStructureMap::iterator it = structures().begin(); it != end; ++it)
84 it->second->mark();
85
86 JSDOMConstructorMap::iterator end2 = constructors().end();
87 for (JSDOMConstructorMap::iterator it2 = constructors().begin(); it2 != end2; ++it2) {
88 if (!it2->second->marked())
89 it2->second->mark();
90 }
91 }
92
findJSEventListener(JSValuePtr val,bool isInline)93 JSEventListener* JSDOMGlobalObject::findJSEventListener(JSValuePtr val, bool isInline)
94 {
95 if (!val.isObject())
96 return 0;
97 JSObject* object = asObject(val);
98 ListenersMap& listeners = isInline ? d()->jsInlineEventListeners : d()->jsEventListeners;
99 return listeners.get(object);
100 }
101
findOrCreateJSEventListener(ExecState *,JSValuePtr val,bool isInline)102 PassRefPtr<JSEventListener> JSDOMGlobalObject::findOrCreateJSEventListener(ExecState*, JSValuePtr val, bool isInline)
103 {
104 if (JSEventListener* listener = findJSEventListener(val, isInline))
105 return listener;
106
107 if (!val.isObject())
108 return 0;
109
110 // The JSEventListener constructor adds it to our jsEventListeners map.
111 return JSEventListener::create(asObject(val), this, isInline).get();
112 }
113
findJSUnprotectedEventListener(ExecState *,JSValuePtr val,bool isInline)114 JSUnprotectedEventListener* JSDOMGlobalObject::findJSUnprotectedEventListener(ExecState*, JSValuePtr val, bool isInline)
115 {
116 if (!val.isObject())
117 return 0;
118
119 UnprotectedListenersMap& listeners = isInline ? d()->jsUnprotectedInlineEventListeners : d()->jsUnprotectedEventListeners;
120 return listeners.get(asObject(val));
121 }
122
findOrCreateJSUnprotectedEventListener(ExecState * exec,JSValuePtr val,bool isInline)123 PassRefPtr<JSUnprotectedEventListener> JSDOMGlobalObject::findOrCreateJSUnprotectedEventListener(ExecState* exec, JSValuePtr val, bool isInline)
124 {
125 if (JSUnprotectedEventListener* listener = findJSUnprotectedEventListener(exec, val, isInline))
126 return listener;
127
128 if (!val.isObject())
129 return 0;
130
131 // The JSUnprotectedEventListener constructor adds it to our jsUnprotectedEventListeners map.
132 return JSUnprotectedEventListener::create(asObject(val), this, isInline).get();
133 }
134
jsEventListeners()135 JSDOMGlobalObject::ListenersMap& JSDOMGlobalObject::jsEventListeners()
136 {
137 return d()->jsEventListeners;
138 }
139
jsInlineEventListeners()140 JSDOMGlobalObject::ListenersMap& JSDOMGlobalObject::jsInlineEventListeners()
141 {
142 return d()->jsInlineEventListeners;
143 }
144
jsUnprotectedEventListeners()145 JSDOMGlobalObject::UnprotectedListenersMap& JSDOMGlobalObject::jsUnprotectedEventListeners()
146 {
147 return d()->jsUnprotectedEventListeners;
148 }
149
jsUnprotectedInlineEventListeners()150 JSDOMGlobalObject::UnprotectedListenersMap& JSDOMGlobalObject::jsUnprotectedInlineEventListeners()
151 {
152 return d()->jsUnprotectedInlineEventListeners;
153 }
154
setCurrentEvent(Event * evt)155 void JSDOMGlobalObject::setCurrentEvent(Event* evt)
156 {
157 d()->evt = evt;
158 }
159
currentEvent()160 Event* JSDOMGlobalObject::currentEvent()
161 {
162 return d()->evt;
163 }
164
toJSDOMGlobalObject(ScriptExecutionContext * scriptExecutionContext)165 JSDOMGlobalObject* toJSDOMGlobalObject(ScriptExecutionContext* scriptExecutionContext)
166 {
167 if (scriptExecutionContext->isDocument())
168 return toJSDOMWindow(static_cast<Document*>(scriptExecutionContext)->frame());
169
170 #if ENABLE(WORKERS)
171 if (scriptExecutionContext->isWorkerContext())
172 return static_cast<WorkerContext*>(scriptExecutionContext)->script()->workerContextWrapper();
173 #endif
174
175 ASSERT_NOT_REACHED();
176 return 0;
177 }
178
179 } // namespace WebCore
180