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 *
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 #include "config.h"
30 #include "JSDOMWindowShell.h"
31
32 #include "Frame.h"
33 #include "JSDOMWindow.h"
34 #include "DOMWindow.h"
35 #include "ScriptController.h"
36 #include <runtime/JSObject.h>
37
38 using namespace JSC;
39
40 namespace WebCore {
41
42 ASSERT_CLASS_FITS_IN_CELL(JSDOMWindowShell);
43
44 const ClassInfo JSDOMWindowShell::s_info = { "JSDOMWindowShell", &Base::s_info, 0, 0 };
45
JSDOMWindowShell(PassRefPtr<DOMWindow> window,DOMWrapperWorld * world)46 JSDOMWindowShell::JSDOMWindowShell(PassRefPtr<DOMWindow> window, DOMWrapperWorld* world)
47 : Base(*world->globalData(), JSDOMWindowShell::createStructure(*world->globalData(), jsNull()))
48 , m_world(world)
49 {
50 ASSERT(inherits(&s_info));
51 setWindow(window);
52 }
53
~JSDOMWindowShell()54 JSDOMWindowShell::~JSDOMWindowShell()
55 {
56 }
57
setWindow(PassRefPtr<DOMWindow> domWindow)58 void JSDOMWindowShell::setWindow(PassRefPtr<DOMWindow> domWindow)
59 {
60 // Explicitly protect the global object's prototype so it isn't collected
61 // when we allocate the global object. (Once the global object is fully
62 // constructed, it can mark its own prototype.)
63 Structure* prototypeStructure = JSDOMWindowPrototype::createStructure(*JSDOMWindow::commonJSGlobalData(), jsNull());
64 Strong<JSDOMWindowPrototype> prototype(*JSDOMWindow::commonJSGlobalData(), new JSDOMWindowPrototype(*JSDOMWindow::commonJSGlobalData(), 0, prototypeStructure));
65
66 Structure* structure = JSDOMWindow::createStructure(*JSDOMWindow::commonJSGlobalData(), prototype.get());
67 JSDOMWindow* jsDOMWindow = new (JSDOMWindow::commonJSGlobalData()) JSDOMWindow(*JSDOMWindow::commonJSGlobalData(), structure, domWindow, this);
68 prototype->putAnonymousValue(*JSDOMWindow::commonJSGlobalData(), 0, jsDOMWindow);
69 setWindow(*JSDOMWindow::commonJSGlobalData(), jsDOMWindow);
70 }
71
72 // ----
73 // JSObject methods
74 // ----
75
markChildren(MarkStack & markStack)76 void JSDOMWindowShell::markChildren(MarkStack& markStack)
77 {
78 Base::markChildren(markStack);
79 if (m_window)
80 markStack.append(&m_window);
81 }
82
className() const83 UString JSDOMWindowShell::className() const
84 {
85 return m_window->className();
86 }
87
getOwnPropertySlot(ExecState * exec,const Identifier & propertyName,PropertySlot & slot)88 bool JSDOMWindowShell::getOwnPropertySlot(ExecState* exec, const Identifier& propertyName, PropertySlot& slot)
89 {
90 return m_window->getOwnPropertySlot(exec, propertyName, slot);
91 }
92
getOwnPropertyDescriptor(ExecState * exec,const Identifier & propertyName,PropertyDescriptor & descriptor)93 bool JSDOMWindowShell::getOwnPropertyDescriptor(ExecState* exec, const Identifier& propertyName, PropertyDescriptor& descriptor)
94 {
95 return m_window->getOwnPropertyDescriptor(exec, propertyName, descriptor);
96 }
97
put(ExecState * exec,const Identifier & propertyName,JSValue value,PutPropertySlot & slot)98 void JSDOMWindowShell::put(ExecState* exec, const Identifier& propertyName, JSValue value, PutPropertySlot& slot)
99 {
100 m_window->put(exec, propertyName, value, slot);
101 }
102
putWithAttributes(ExecState * exec,const Identifier & propertyName,JSValue value,unsigned attributes)103 void JSDOMWindowShell::putWithAttributes(ExecState* exec, const Identifier& propertyName, JSValue value, unsigned attributes)
104 {
105 m_window->putWithAttributes(exec, propertyName, value, attributes);
106 }
107
defineOwnProperty(JSC::ExecState * exec,const JSC::Identifier & propertyName,JSC::PropertyDescriptor & descriptor,bool shouldThrow)108 bool JSDOMWindowShell::defineOwnProperty(JSC::ExecState* exec, const JSC::Identifier& propertyName, JSC::PropertyDescriptor& descriptor, bool shouldThrow)
109 {
110 return m_window->defineOwnProperty(exec, propertyName, descriptor, shouldThrow);
111 }
112
deleteProperty(ExecState * exec,const Identifier & propertyName)113 bool JSDOMWindowShell::deleteProperty(ExecState* exec, const Identifier& propertyName)
114 {
115 return m_window->deleteProperty(exec, propertyName);
116 }
117
getPropertyNames(ExecState * exec,PropertyNameArray & propertyNames,EnumerationMode mode)118 void JSDOMWindowShell::getPropertyNames(ExecState* exec, PropertyNameArray& propertyNames, EnumerationMode mode)
119 {
120 m_window->getPropertyNames(exec, propertyNames, mode);
121 }
122
getOwnPropertyNames(ExecState * exec,PropertyNameArray & propertyNames,EnumerationMode mode)123 void JSDOMWindowShell::getOwnPropertyNames(ExecState* exec, PropertyNameArray& propertyNames, EnumerationMode mode)
124 {
125 m_window->getOwnPropertyNames(exec, propertyNames, mode);
126 }
127
defineGetter(ExecState * exec,const Identifier & propertyName,JSObject * getterFunction,unsigned attributes)128 void JSDOMWindowShell::defineGetter(ExecState* exec, const Identifier& propertyName, JSObject* getterFunction, unsigned attributes)
129 {
130 m_window->defineGetter(exec, propertyName, getterFunction, attributes);
131 }
132
defineSetter(ExecState * exec,const Identifier & propertyName,JSObject * setterFunction,unsigned attributes)133 void JSDOMWindowShell::defineSetter(ExecState* exec, const Identifier& propertyName, JSObject* setterFunction, unsigned attributes)
134 {
135 m_window->defineSetter(exec, propertyName, setterFunction, attributes);
136 }
137
lookupGetter(ExecState * exec,const Identifier & propertyName)138 JSValue JSDOMWindowShell::lookupGetter(ExecState* exec, const Identifier& propertyName)
139 {
140 return m_window->lookupGetter(exec, propertyName);
141 }
142
lookupSetter(ExecState * exec,const Identifier & propertyName)143 JSValue JSDOMWindowShell::lookupSetter(ExecState* exec, const Identifier& propertyName)
144 {
145 return m_window->lookupSetter(exec, propertyName);
146 }
147
unwrappedObject()148 JSObject* JSDOMWindowShell::unwrappedObject()
149 {
150 return m_window.get();
151 }
152
153 // ----
154 // JSDOMWindow methods
155 // ----
156
impl() const157 DOMWindow* JSDOMWindowShell::impl() const
158 {
159 return m_window->impl();
160 }
161
operator new(size_t size)162 void* JSDOMWindowShell::operator new(size_t size)
163 {
164 return JSDOMWindow::commonJSGlobalData()->heap.allocate(size);
165 }
166
167 // ----
168 // Conversion methods
169 // ----
170
toJS(ExecState * exec,Frame * frame)171 JSValue toJS(ExecState* exec, Frame* frame)
172 {
173 if (!frame)
174 return jsNull();
175 return frame->script()->windowShell(currentWorld(exec));
176 }
177
toJSDOMWindowShell(Frame * frame,DOMWrapperWorld * isolatedWorld)178 JSDOMWindowShell* toJSDOMWindowShell(Frame* frame, DOMWrapperWorld* isolatedWorld)
179 {
180 if (!frame)
181 return 0;
182 return frame->script()->windowShell(isolatedWorld);
183 }
184
185 } // namespace WebCore
186