1/*
2 * Copyright (C) 1998, 1999 Torben Weis <weis@kde.org>
3 * 1999-2001 Lars Knoll <knoll@kde.org>
4 * 1999-2001 Antti Koivisto <koivisto@kde.org>
5 * 2000-2001 Simon Hausmann <hausmann@kde.org>
6 * 2000-2001 Dirk Mueller <mueller@kde.org>
7 * 2000 Stefan Schimanski <1Stein@gmx.de>
8 * Copyright (C) 2004-2018 Apple Inc. All rights reserved.
9 * Copyright (C) 2008 Nokia Corporation and/or its subsidiary(-ies)
10 * Copyright (C) 2008 Eric Seidel <eric@webkit.org>
11 *
12 * This library is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU Library General Public
14 * License as published by the Free Software Foundation; either
15 * version 2 of the License, or (at your option) any later version.
16 *
17 * This library is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
20 * Library General Public License for more details.
21 *
22 * You should have received a copy of the GNU Library General Public License
23 * along with this library; see the file COPYING.LIB. If not, write to
24 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
25 * Boston, MA 02110-1301, USA.
26 */
27
28#pragma once
29
30#include "AbstractFrame.h"
31#include "AdjustViewSizeOrNot.h"
32#include "FrameTree.h"
33#include "ScrollTypes.h"
34#include "UserScriptTypes.h"
35#include <wtf/HashSet.h>
36#include <wtf/UniqueRef.h>
37
38#if PLATFORM(IOS_FAMILY)
39#include "Timer.h"
40#include "ViewportArguments.h"
41#include "VisibleSelection.h"
42#endif
43
44#if PLATFORM(WIN)
45#include "FrameWin.h"
46#endif
47
48#if PLATFORM(COCOA)
49OBJC_CLASS NSArray;
50#endif
51
52#if PLATFORM(WIN)
53typedef struct HBITMAP__* HBITMAP;
54#endif
55
56namespace JSC { namespace Yarr {
57class RegularExpression;
58} }
59
60namespace WebCore {
61
62class CSSAnimationController;
63class Color;
64class DOMWindow;
65class Document;
66class Editor;
67class Element;
68class EventHandler;
69class FloatSize;
70class FrameDestructionObserver;
71class FrameLoader;
72class FrameLoaderClient;
73class FrameSelection;
74class FrameView;
75class HTMLFrameOwnerElement;
76class HTMLTableCellElement;
77class HitTestResult;
78class ImageBuffer;
79class IntPoint;
80class IntRect;
81class IntSize;
82class NavigationScheduler;
83class Node;
84class Page;
85class Range;
86class RenderLayer;
87class RenderView;
88class RenderWidget;
89class ScriptController;
90class SecurityOrigin;
91class Settings;
92class VisiblePosition;
93class Widget;
94
95#if PLATFORM(IOS_FAMILY)
96enum {
97 OverflowScrollNone = 0,
98 OverflowScrollLeft = 1 << 0,
99 OverflowScrollRight = 1 << 1,
100 OverflowScrollUp = 1 << 2,
101 OverflowScrollDown = 1 << 3
102};
103
104enum OverflowScrollAction { DoNotPerformOverflowScroll, PerformOverflowScroll };
105using NodeQualifier = Function<Node* (const HitTestResult&, Node* terminationNode, IntRect* nodeBounds)>;
106#endif
107
108enum {
109 LayerTreeFlagsIncludeDebugInfo = 1 << 0,
110 LayerTreeFlagsIncludeVisibleRects = 1 << 1,
111 LayerTreeFlagsIncludeTileCaches = 1 << 2,
112 LayerTreeFlagsIncludeRepaintRects = 1 << 3,
113 LayerTreeFlagsIncludePaintingPhases = 1 << 4,
114 LayerTreeFlagsIncludeContentLayers = 1 << 5,
115 LayerTreeFlagsIncludeAcceleratesDrawing = 1 << 6,
116 LayerTreeFlagsIncludeBackingStoreAttached = 1 << 7,
117 LayerTreeFlagsIncludeRootLayerProperties = 1 << 8,
118 LayerTreeFlagsIncludeEventRegion = 1 << 9,
119};
120typedef unsigned LayerTreeFlags;
121
122// FIXME: Rename Frame to LocalFrame and AbstractFrame to Frame.
123class Frame final : public AbstractFrame {
124public:
125 WEBCORE_EXPORT static Ref<Frame> create(Page*, HTMLFrameOwnerElement*, FrameLoaderClient*);
126
127 WEBCORE_EXPORT void init();
128#if PLATFORM(IOS_FAMILY)
129 // Creates <html><body style="..."></body></html> doing minimal amount of work.
130 WEBCORE_EXPORT void initWithSimpleHTMLDocument(const String& style, const URL&);
131#endif
132 WEBCORE_EXPORT void setView(RefPtr<FrameView>&&);
133 WEBCORE_EXPORT void createView(const IntSize&, const Optional<Color>& backgroundColor,
134 const IntSize& fixedLayoutSize, const IntRect& fixedVisibleContentRect,
135 bool useFixedLayout = false, ScrollbarMode = ScrollbarAuto, bool horizontalLock = false,
136 ScrollbarMode = ScrollbarAuto, bool verticalLock = false);
137
138 WEBCORE_EXPORT ~Frame();
139
140 WEBCORE_EXPORT DOMWindow* window() const;
141
142 void addDestructionObserver(FrameDestructionObserver*);
143 void removeDestructionObserver(FrameDestructionObserver*);
144
145 WEBCORE_EXPORT void willDetachPage();
146 void detachFromPage();
147 void disconnectOwnerElement();
148
149 Frame& mainFrame() const;
150 bool isMainFrame() const { return this == static_cast<void*>(&m_mainFrame); }
151
152 Page* page() const;
153 HTMLFrameOwnerElement* ownerElement() const;
154
155 Document* document() const;
156 FrameView* view() const;
157
158 Editor& editor() { return m_editor; }
159 const Editor& editor() const { return m_editor; }
160 EventHandler& eventHandler() { return m_eventHandler; }
161 const EventHandler& eventHandler() const { return m_eventHandler; }
162 FrameLoader& loader() const;
163 NavigationScheduler& navigationScheduler() const;
164 FrameSelection& selection() { return m_selection; }
165 const FrameSelection& selection() const { return m_selection; }
166 FrameTree& tree() const;
167 CSSAnimationController& animation() { return m_animationController; }
168 const CSSAnimationController& animation() const { return m_animationController; }
169 ScriptController& script() { return m_script; }
170 const ScriptController& script() const { return m_script; }
171
172 WEBCORE_EXPORT RenderView* contentRenderer() const; // Root of the render tree for the document contained in this frame.
173 WEBCORE_EXPORT RenderWidget* ownerRenderer() const; // Renderer for the element that contains this frame.
174
175 bool documentIsBeingReplaced() const { return m_documentIsBeingReplaced; }
176
177 bool hasHadUserInteraction() const { return m_hasHadUserInteraction; }
178 void setHasHadUserInteraction() { m_hasHadUserInteraction = true; }
179
180 bool requestDOMPasteAccess();
181
182// ======== All public functions below this point are candidates to move out of Frame into another class. ========
183
184 WEBCORE_EXPORT void injectUserScripts(UserScriptInjectionTime);
185 WEBCORE_EXPORT void injectUserScriptImmediately(DOMWrapperWorld&, const UserScript&);
186
187 WEBCORE_EXPORT String layerTreeAsText(LayerTreeFlags = 0) const;
188 WEBCORE_EXPORT String trackedRepaintRectsAsText() const;
189
190 WEBCORE_EXPORT static Frame* frameForWidget(const Widget&);
191
192 Settings& settings() const { return *m_settings; }
193
194 void setPrinting(bool printing, const FloatSize& pageSize, const FloatSize& originalPageSize, float maximumShrinkRatio, AdjustViewSizeOrNot);
195 bool shouldUsePrintingLayout() const;
196 WEBCORE_EXPORT FloatSize resizePageRectsKeepingRatio(const FloatSize& originalSize, const FloatSize& expectedSize);
197
198 void setDocument(RefPtr<Document>&&);
199
200 WEBCORE_EXPORT void setPageZoomFactor(float);
201 float pageZoomFactor() const { return m_pageZoomFactor; }
202 WEBCORE_EXPORT void setTextZoomFactor(float);
203 float textZoomFactor() const { return m_textZoomFactor; }
204 WEBCORE_EXPORT void setPageAndTextZoomFactors(float pageZoomFactor, float textZoomFactor);
205
206 // Scale factor of this frame with respect to the container.
207 WEBCORE_EXPORT float frameScaleFactor() const;
208
209 void deviceOrPageScaleFactorChanged();
210
211#if ENABLE(DATA_DETECTION)
212 void setDataDetectionResults(NSArray *results) { m_dataDetectionResults = results; }
213 NSArray *dataDetectionResults() const { return m_dataDetectionResults.get(); }
214#endif
215
216#if PLATFORM(IOS_FAMILY)
217 const ViewportArguments& viewportArguments() const;
218 WEBCORE_EXPORT void setViewportArguments(const ViewportArguments&);
219
220 WEBCORE_EXPORT Node* deepestNodeAtLocation(const FloatPoint& viewportLocation);
221 WEBCORE_EXPORT Node* nodeRespondingToClickEvents(const FloatPoint& viewportLocation, FloatPoint& adjustedViewportLocation, SecurityOrigin* = nullptr);
222 WEBCORE_EXPORT Node* nodeRespondingToDoubleClickEvent(const FloatPoint& viewportLocation, FloatPoint& adjustedViewportLocation);
223 WEBCORE_EXPORT Node* nodeRespondingToScrollWheelEvents(const FloatPoint& viewportLocation);
224
225 WEBCORE_EXPORT NSArray *wordsInCurrentParagraph() const;
226 WEBCORE_EXPORT CGRect renderRectForPoint(CGPoint, bool* isReplaced, float* fontSize) const;
227
228 WEBCORE_EXPORT void setSelectionChangeCallbacksDisabled(bool = true);
229 bool selectionChangeCallbacksDisabled() const;
230
231 enum ViewportOffsetChangeType { IncrementalScrollOffset, CompletedScrollOffset };
232 WEBCORE_EXPORT void viewportOffsetChanged(ViewportOffsetChangeType);
233 bool containsTiledBackingLayers() const;
234
235 WEBCORE_EXPORT void overflowScrollPositionChangedForNode(const IntPoint&, Node*, bool isUserScroll);
236
237 WEBCORE_EXPORT void resetAllGeolocationPermission();
238#endif
239
240#if ENABLE(ORIENTATION_EVENTS)
241 // Orientation is the interface orientation in degrees. Some examples are:
242 // 0 is straight up; -90 is when the device is rotated 90 clockwise;
243 // 90 is when rotated counter clockwise.
244 WEBCORE_EXPORT void orientationChanged();
245 int orientation() const;
246#endif
247
248 void clearTimers();
249 static void clearTimers(FrameView*, Document*);
250
251 WEBCORE_EXPORT String displayStringModifiedByEncoding(const String&) const;
252
253 WEBCORE_EXPORT VisiblePosition visiblePositionForPoint(const IntPoint& framePoint) const;
254 Document* documentAtPoint(const IntPoint& windowPoint);
255 WEBCORE_EXPORT RefPtr<Range> rangeForPoint(const IntPoint& framePoint);
256
257 WEBCORE_EXPORT String searchForLabelsAboveCell(const JSC::Yarr::RegularExpression&, HTMLTableCellElement*, size_t* resultDistanceFromStartOfCell);
258 String searchForLabelsBeforeElement(const Vector<String>& labels, Element*, size_t* resultDistance, bool* resultIsInCellAbove);
259 String matchLabelsAgainstElement(const Vector<String>& labels, Element*);
260
261#if PLATFORM(IOS_FAMILY)
262 WEBCORE_EXPORT int preferredHeight() const;
263 WEBCORE_EXPORT void updateLayout() const;
264 WEBCORE_EXPORT NSRect caretRect();
265 WEBCORE_EXPORT NSRect rectForScrollToVisible();
266 WEBCORE_EXPORT unsigned formElementsCharacterCount() const;
267
268 // This function is used by Legacy WebKit.
269 WEBCORE_EXPORT void setTimersPaused(bool);
270
271 WEBCORE_EXPORT void dispatchPageHideEventBeforePause();
272 WEBCORE_EXPORT void dispatchPageShowEventBeforeResume();
273 WEBCORE_EXPORT void setRangedSelectionBaseToCurrentSelection();
274 WEBCORE_EXPORT void setRangedSelectionBaseToCurrentSelectionStart();
275 WEBCORE_EXPORT void setRangedSelectionBaseToCurrentSelectionEnd();
276 WEBCORE_EXPORT void clearRangedSelectionInitialExtent();
277 WEBCORE_EXPORT void setRangedSelectionInitialExtentToCurrentSelectionStart();
278 WEBCORE_EXPORT void setRangedSelectionInitialExtentToCurrentSelectionEnd();
279 WEBCORE_EXPORT VisibleSelection rangedSelectionBase() const;
280 WEBCORE_EXPORT VisibleSelection rangedSelectionInitialExtent() const;
281 WEBCORE_EXPORT void recursiveSetUpdateAppearanceEnabled(bool);
282 WEBCORE_EXPORT NSArray *interpretationsForCurrentRoot() const;
283#endif
284 void suspendActiveDOMObjectsAndAnimations();
285 void resumeActiveDOMObjectsAndAnimations();
286 bool activeDOMObjectsAndAnimationsSuspended() const { return m_activeDOMObjectsAndAnimationsSuspendedCount > 0; }
287
288 bool isURLAllowed(const URL&) const;
289 WEBCORE_EXPORT bool isAlwaysOnLoggingAllowed() const;
290
291// ========
292
293 void selfOnlyRef();
294 void selfOnlyDeref();
295
296private:
297 friend class NavigationDisabler;
298
299 Frame(Page&, HTMLFrameOwnerElement*, FrameLoaderClient&);
300
301 void dropChildren();
302
303 bool isLocalFrame() const final { return true; }
304 bool isRemoteFrame() const final { return false; }
305
306 AbstractDOMWindow* virtualWindow() const final;
307
308 HashSet<FrameDestructionObserver*> m_destructionObservers;
309
310 Frame& m_mainFrame;
311 Page* m_page;
312 const RefPtr<Settings> m_settings;
313 mutable FrameTree m_treeNode;
314 mutable UniqueRef<FrameLoader> m_loader;
315 mutable UniqueRef<NavigationScheduler> m_navigationScheduler;
316
317 HTMLFrameOwnerElement* m_ownerElement;
318 RefPtr<FrameView> m_view;
319 RefPtr<Document> m_doc;
320
321 UniqueRef<ScriptController> m_script;
322 UniqueRef<Editor> m_editor;
323 UniqueRef<FrameSelection> m_selection;
324 UniqueRef<CSSAnimationController> m_animationController;
325
326#if ENABLE(DATA_DETECTION)
327 RetainPtr<NSArray> m_dataDetectionResults;
328#endif
329#if PLATFORM(IOS_FAMILY)
330 void betterApproximateNode(const IntPoint& testPoint, const NodeQualifier&, Node*& best, Node* failedNode, IntPoint& bestPoint, IntRect& bestRect, const IntRect& testRect);
331 bool hitTestResultAtViewportLocation(const FloatPoint& viewportLocation, HitTestResult&, IntPoint& center);
332 Node* qualifyingNodeAtViewportLocation(const FloatPoint& viewportLocation, FloatPoint& adjustedViewportLocation, const NodeQualifier&, bool shouldApproximate);
333
334 void setTimersPausedInternal(bool);
335
336 ViewportArguments m_viewportArguments;
337 bool m_selectionChangeCallbacksDisabled { false };
338 VisibleSelection m_rangedSelectionBase;
339 VisibleSelection m_rangedSelectionInitialExtent;
340#endif
341
342 float m_pageZoomFactor;
343 float m_textZoomFactor;
344
345 int m_activeDOMObjectsAndAnimationsSuspendedCount { 0 };
346 bool m_documentIsBeingReplaced { false };
347 unsigned m_navigationDisableCount { 0 };
348 unsigned m_selfOnlyRefCount { 0 };
349 bool m_hasHadUserInteraction { false };
350
351protected:
352 UniqueRef<EventHandler> m_eventHandler;
353};
354
355inline FrameLoader& Frame::loader() const
356{
357 return m_loader.get();
358}
359
360inline NavigationScheduler& Frame::navigationScheduler() const
361{
362 return m_navigationScheduler.get();
363}
364
365inline FrameView* Frame::view() const
366{
367 return m_view.get();
368}
369
370inline Document* Frame::document() const
371{
372 return m_doc.get();
373}
374
375inline HTMLFrameOwnerElement* Frame::ownerElement() const
376{
377 return m_ownerElement;
378}
379
380inline FrameTree& Frame::tree() const
381{
382 return m_treeNode;
383}
384
385inline Page* Frame::page() const
386{
387 return m_page;
388}
389
390inline void Frame::detachFromPage()
391{
392 m_page = nullptr;
393}
394
395inline Frame& Frame::mainFrame() const
396{
397 return m_mainFrame;
398}
399
400} // namespace WebCore
401
402SPECIALIZE_TYPE_TRAITS_BEGIN(WebCore::Frame)
403 static bool isType(const WebCore::AbstractFrame& frame) { return frame.isLocalFrame(); }
404SPECIALIZE_TYPE_TRAITS_END()
405