1/*
2 * Copyright (C) 2012 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 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 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#include "config.h"
27#include "DOMWindowExtension.h"
28
29#include "DOMWindow.h"
30#include "DOMWrapperWorld.h"
31#include "Frame.h"
32#include "FrameLoader.h"
33#include "FrameLoaderClient.h"
34#include <wtf/Ref.h>
35
36namespace WebCore {
37
38DOMWindowExtension::DOMWindowExtension(Frame* frame, DOMWrapperWorld& world)
39    : DOMWindowProperty(frame)
40    , m_world(&world)
41    , m_wasDetached(false)
42{
43    ASSERT(this->frame());
44}
45
46void DOMWindowExtension::disconnectFrameForPageCache()
47{
48    // Calling out to the client might result in this DOMWindowExtension being destroyed
49    // while there is still work to do.
50    Ref<DOMWindowExtension> protect(*this);
51
52    Frame* frame = this->frame();
53    frame->loader().client().dispatchWillDisconnectDOMWindowExtensionFromGlobalObject(this);
54
55    m_disconnectedFrame = frame;
56
57    DOMWindowProperty::disconnectFrameForPageCache();
58}
59
60void DOMWindowExtension::reconnectFrameFromPageCache(Frame* frame)
61{
62    ASSERT(m_disconnectedFrame == frame);
63
64    DOMWindowProperty::reconnectFrameFromPageCache(frame);
65    m_disconnectedFrame = 0;
66
67    this->frame()->loader().client().dispatchDidReconnectDOMWindowExtensionToGlobalObject(this);
68}
69
70void DOMWindowExtension::willDestroyGlobalObjectInCachedFrame()
71{
72    ASSERT(m_disconnectedFrame);
73
74    // Calling out to the client might result in this DOMWindowExtension being destroyed
75    // while there is still work to do.
76    Ref<DOMWindowExtension> protect(*this);
77
78    m_disconnectedFrame->loader().client().dispatchWillDestroyGlobalObjectForDOMWindowExtension(this);
79    m_disconnectedFrame = 0;
80
81    DOMWindowProperty::willDestroyGlobalObjectInCachedFrame();
82}
83
84void DOMWindowExtension::willDestroyGlobalObjectInFrame()
85{
86    ASSERT(!m_disconnectedFrame);
87
88    // Calling out to the client might result in this DOMWindowExtension being destroyed
89    // while there is still work to do.
90    Ref<DOMWindowExtension> protect(*this);
91
92    if (!m_wasDetached) {
93        Frame* frame = this->frame();
94        ASSERT(frame);
95        frame->loader().client().dispatchWillDestroyGlobalObjectForDOMWindowExtension(this);
96    }
97
98    DOMWindowProperty::willDestroyGlobalObjectInFrame();
99}
100
101void DOMWindowExtension::willDetachGlobalObjectFromFrame()
102{
103    ASSERT(!m_disconnectedFrame);
104    ASSERT(!m_wasDetached);
105
106    // Calling out to the client might result in this DOMWindowExtension being destroyed
107    // while there is still work to do.
108    Ref<DOMWindowExtension> protect(*this);
109
110    Frame* frame = this->frame();
111    ASSERT(frame);
112    frame->loader().client().dispatchWillDestroyGlobalObjectForDOMWindowExtension(this);
113
114    m_wasDetached = true;
115    DOMWindowProperty::willDetachGlobalObjectFromFrame();
116}
117
118} // namespace WebCore
119