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 "GCController.h"
34#include "JSDOMWindow.h"
35#include "DOMWindow.h"
36#include "ScriptController.h"
37#include <heap/StrongInlines.h>
38#include <runtime/JSObject.h>
39
40using namespace JSC;
41
42namespace WebCore {
43
44const ClassInfo JSDOMWindowShell::s_info = { "JSDOMWindowShell", &Base::s_info, 0, 0, CREATE_METHOD_TABLE(JSDOMWindowShell) };
45
46JSDOMWindowShell::JSDOMWindowShell(Structure* structure, DOMWrapperWorld* world)
47    : Base(*world->vm(), structure)
48    , m_world(world)
49{
50}
51
52void JSDOMWindowShell::finishCreation(VM& vm, PassRefPtr<DOMWindow> window)
53{
54    Base::finishCreation(vm);
55    ASSERT(inherits(&s_info));
56    setWindow(window);
57}
58
59void JSDOMWindowShell::destroy(JSCell* cell)
60{
61    static_cast<JSDOMWindowShell*>(cell)->JSDOMWindowShell::~JSDOMWindowShell();
62}
63
64void JSDOMWindowShell::setWindow(JSC::VM& vm, JSDOMWindow* window)
65{
66    ASSERT_ARG(window, window);
67    setTarget(vm, window);
68    structure()->setGlobalObject(*JSDOMWindow::commonVM(), window);
69    gcController().garbageCollectSoon();
70}
71
72void JSDOMWindowShell::setWindow(PassRefPtr<DOMWindow> domWindow)
73{
74    // Replacing JSDOMWindow via telling JSDOMWindowShell to use the same DOMWindow it already uses makes no sense,
75    // so we'd better never try to.
76    ASSERT(!window() || domWindow.get() != window()->impl());
77    // Explicitly protect the global object's prototype so it isn't collected
78    // when we allocate the global object. (Once the global object is fully
79    // constructed, it can mark its own prototype.)
80    Structure* prototypeStructure = JSDOMWindowPrototype::createStructure(*JSDOMWindow::commonVM(), 0, jsNull());
81    Strong<JSDOMWindowPrototype> prototype(*JSDOMWindow::commonVM(), JSDOMWindowPrototype::create(*JSDOMWindow::commonVM(), 0, prototypeStructure));
82
83    Structure* structure = JSDOMWindow::createStructure(*JSDOMWindow::commonVM(), 0, prototype.get());
84    JSDOMWindow* jsDOMWindow = JSDOMWindow::create(*JSDOMWindow::commonVM(), structure, domWindow, this);
85    prototype->structure()->setGlobalObject(*JSDOMWindow::commonVM(), jsDOMWindow);
86    setWindow(*JSDOMWindow::commonVM(), jsDOMWindow);
87    ASSERT(jsDOMWindow->globalObject() == jsDOMWindow);
88    ASSERT(prototype->globalObject() == jsDOMWindow);
89}
90
91// ----
92// JSDOMWindow methods
93// ----
94
95DOMWindow* JSDOMWindowShell::impl() const
96{
97    return window()->impl();
98}
99
100// ----
101// Conversion methods
102// ----
103
104JSValue toJS(ExecState* exec, Frame* frame)
105{
106    if (!frame)
107        return jsNull();
108    return frame->script()->windowShell(currentWorld(exec));
109}
110
111JSDOMWindowShell* toJSDOMWindowShell(Frame* frame, DOMWrapperWorld* isolatedWorld)
112{
113    if (!frame)
114        return 0;
115    return frame->script()->windowShell(isolatedWorld);
116}
117
118} // namespace WebCore
119