1/*
2 * Copyright (C) 2017 Google 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. AND ITS CONTRIBUTORS ``AS IS''
14 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
15 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS
17 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
18 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
19 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
20 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
21 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
22 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
23 * THE POSSIBILITY OF SUCH DAMAGE.
24 */
25
26#include "config.h"
27#include "VisualViewport.h"
28
29#include "ContextDestructionObserver.h"
30#include "DOMWindow.h"
31#include "Document.h"
32#include "DocumentEventQueue.h"
33#include "Event.h"
34#include "EventNames.h"
35#include "Frame.h"
36#include "FrameView.h"
37#include "Page.h"
38#include <wtf/IsoMallocInlines.h>
39
40namespace WebCore {
41
42WTF_MAKE_ISO_ALLOCATED_IMPL(VisualViewport);
43
44VisualViewport::VisualViewport(DOMWindow& window)
45 : DOMWindowProperty(&window)
46{
47}
48
49EventTargetInterface VisualViewport::eventTargetInterface() const
50{
51 return VisualViewportEventTargetInterfaceType;
52}
53
54ScriptExecutionContext* VisualViewport::scriptExecutionContext() const
55{
56 auto window = this->window();
57 if (!window)
58 return nullptr;
59 return static_cast<ContextDestructionObserver*>(window)->scriptExecutionContext();
60}
61
62bool VisualViewport::addEventListener(const AtomString& eventType, Ref<EventListener>&& listener, const AddEventListenerOptions& options)
63{
64 if (!EventTarget::addEventListener(eventType, WTFMove(listener), options))
65 return false;
66
67 if (auto* frame = this->frame())
68 frame->document()->addListenerTypeIfNeeded(eventType);
69 return true;
70}
71
72void VisualViewport::updateFrameLayout() const
73{
74 ASSERT(frame());
75 frame()->document()->updateLayoutIgnorePendingStylesheets(Document::RunPostLayoutTasks::Synchronously);
76}
77
78double VisualViewport::offsetLeft() const
79{
80 if (!frame())
81 return 0;
82
83 updateFrameLayout();
84 return m_offsetLeft;
85}
86
87double VisualViewport::offsetTop() const
88{
89 if (!frame())
90 return 0;
91
92 updateFrameLayout();
93 return m_offsetTop;
94}
95
96double VisualViewport::pageLeft() const
97{
98 if (!frame())
99 return 0;
100
101 updateFrameLayout();
102 return m_pageLeft;
103}
104
105double VisualViewport::pageTop() const
106{
107 if (!frame())
108 return 0;
109
110 updateFrameLayout();
111 return m_pageTop;
112}
113
114double VisualViewport::width() const
115{
116 if (!frame())
117 return 0;
118
119 updateFrameLayout();
120 return m_width;
121}
122
123double VisualViewport::height() const
124{
125 if (!frame())
126 return 0;
127
128 updateFrameLayout();
129 return m_height;
130}
131
132double VisualViewport::scale() const
133{
134 // Subframes always have scale 1 since they aren't scaled relative to their parent frame.
135 auto* frame = this->frame();
136 if (!frame || !frame->isMainFrame())
137 return 1;
138
139 updateFrameLayout();
140 return m_scale;
141}
142
143void VisualViewport::update()
144{
145 double offsetLeft = 0;
146 double offsetTop = 0;
147 m_pageLeft = 0;
148 m_pageTop = 0;
149 double width = 0;
150 double height = 0;
151 double scale = 1;
152
153 if (auto* frame = this->frame()) {
154 if (auto* view = frame->view()) {
155 auto visualViewportRect = view->visualViewportRect();
156 auto layoutViewportRect = view->layoutViewportRect();
157 auto pageZoomFactor = frame->pageZoomFactor();
158 ASSERT(pageZoomFactor);
159 offsetLeft = (visualViewportRect.x() - layoutViewportRect.x()) / pageZoomFactor;
160 offsetTop = (visualViewportRect.y() - layoutViewportRect.y()) / pageZoomFactor;
161 m_pageLeft = visualViewportRect.x() / pageZoomFactor;
162 m_pageTop = visualViewportRect.y() / pageZoomFactor;
163 width = visualViewportRect.width() / pageZoomFactor;
164 height = visualViewportRect.height() / pageZoomFactor;
165 }
166 if (auto* page = frame->page())
167 scale = page->pageScaleFactor();
168 }
169
170 if (m_offsetLeft != offsetLeft || m_offsetTop != offsetTop) {
171 enqueueScrollEvent();
172 m_offsetLeft = offsetLeft;
173 m_offsetTop = offsetTop;
174 }
175 if (m_width != width || m_height != height || m_scale != scale) {
176 enqueueResizeEvent();
177 m_width = width;
178 m_height = height;
179 m_scale = scale;
180 }
181}
182
183void VisualViewport::enqueueResizeEvent()
184{
185 auto* frame = this->frame();
186 if (!frame)
187 return;
188
189 frame->document()->eventQueue().enqueueResizeEvent(*this, Event::CanBubble::No, Event::IsCancelable::No);
190}
191
192void VisualViewport::enqueueScrollEvent()
193{
194 auto* frame = this->frame();
195 if (!frame)
196 return;
197
198 frame->document()->eventQueue().enqueueScrollEvent(*this, Event::CanBubble::No, Event::IsCancelable::No);
199}
200
201} // namespace WebCore
202