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(DOMWindow* window, DOMWrapperWorld& world)
39 : m_window(makeWeakPtr(window))
40 , m_world(world)
41 , m_wasDetached(false)
42{
43 ASSERT(this->frame());
44 if (m_window)
45 m_window->registerObserver(*this);
46}
47
48DOMWindowExtension::~DOMWindowExtension()
49{
50 if (m_window)
51 m_window->unregisterObserver(*this);
52}
53
54Frame* DOMWindowExtension::frame() const
55{
56 return m_window ? m_window->frame() : nullptr;
57}
58
59void DOMWindowExtension::suspendForPageCache()
60{
61 // Calling out to the client might result in this DOMWindowExtension being destroyed
62 // while there is still work to do.
63 Ref<DOMWindowExtension> protectedThis(*this);
64
65 auto frame = makeRef(*this->frame());
66 frame->loader().client().dispatchWillDisconnectDOMWindowExtensionFromGlobalObject(this);
67
68 m_disconnectedFrame = WTFMove(frame);
69}
70
71void DOMWindowExtension::resumeFromPageCache()
72{
73 ASSERT(frame());
74 ASSERT(m_disconnectedFrame == frame());
75 ASSERT(frame()->document()->domWindow() == m_window);
76
77 m_disconnectedFrame = nullptr;
78
79 frame()->loader().client().dispatchDidReconnectDOMWindowExtensionToGlobalObject(this);
80}
81
82void DOMWindowExtension::willDestroyGlobalObjectInCachedFrame()
83{
84 ASSERT(m_disconnectedFrame);
85
86 // Calling out to the client might result in this DOMWindowExtension being destroyed
87 // while there is still work to do.
88 Ref<DOMWindowExtension> protectedThis(*this);
89
90 m_disconnectedFrame->loader().client().dispatchWillDestroyGlobalObjectForDOMWindowExtension(this);
91 m_disconnectedFrame = nullptr;
92
93 // DOMWindowExtension lifetime isn't tied directly to the DOMWindow itself so it is important that it unregister
94 // itself from any DOMWindow it is associated with if that DOMWindow is going away.
95 ASSERT(m_window);
96 if (m_window)
97 m_window->unregisterObserver(*this);
98 m_window = nullptr;
99}
100
101void DOMWindowExtension::willDestroyGlobalObjectInFrame()
102{
103 ASSERT(!m_disconnectedFrame);
104
105 // Calling out to the client might result in this DOMWindowExtension being destroyed
106 // while there is still work to do.
107 Ref<DOMWindowExtension> protectedThis(*this);
108
109 if (!m_wasDetached) {
110 Frame* frame = this->frame();
111 ASSERT(frame);
112 frame->loader().client().dispatchWillDestroyGlobalObjectForDOMWindowExtension(this);
113 }
114
115 // DOMWindowExtension lifetime isn't tied directly to the DOMWindow itself so it is important that it unregister
116 // itself from any DOMWindow it is associated with if that DOMWindow is going away.
117 ASSERT(m_window);
118 if (m_window)
119 m_window->unregisterObserver(*this);
120 m_window = nullptr;
121}
122
123void DOMWindowExtension::willDetachGlobalObjectFromFrame()
124{
125 ASSERT(!m_disconnectedFrame);
126 ASSERT(!m_wasDetached);
127
128 // Calling out to the client might result in this DOMWindowExtension being destroyed
129 // while there is still work to do.
130 Ref<DOMWindowExtension> protectedThis(*this);
131
132 Frame* frame = this->frame();
133 ASSERT(frame);
134 frame->loader().client().dispatchWillDestroyGlobalObjectForDOMWindowExtension(this);
135
136 m_wasDetached = true;
137}
138
139} // namespace WebCore
140