2 * Copyright (C) 2009, 2010, 2011, 2012 Google Inc. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are
8 * * Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * * Redistributions in binary form must reproduce the above
11 * copyright notice, this list of conditions and the following disclaimer
12 * in the documentation and/or other materials provided with the
14 * * Neither the name of Google Inc. nor the names of its
15 * contributors may be used to endorse or promote products derived from
16 * this software without specific prior written permission.
18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 #ifndef THIRD_PARTY_BLINK_PUBLIC_WEB_WEB_VIEW_H_
32 #define THIRD_PARTY_BLINK_PUBLIC_WEB_WEB_VIEW_H_
34 #include "third_party/abseil-cpp/absl/types/optional.h"
35 #include "third_party/blink/public/common/dom_storage/session_storage_namespace_id.h"
36 #include "third_party/blink/public/common/fenced_frame/redacted_fenced_frame_config.h"
37 #include "third_party/blink/public/common/page/browsing_context_group_info.h"
38 #include "third_party/blink/public/common/renderer_preferences/renderer_preferences.h"
39 #include "third_party/blink/public/common/web_preferences/web_preferences.h"
40 #include "third_party/blink/public/mojom/fenced_frame/fenced_frame.mojom-shared.h"
41 #include "third_party/blink/public/mojom/frame/frame.mojom-shared.h"
42 #include "third_party/blink/public/mojom/page/page.mojom-shared.h"
43 #include "third_party/blink/public/mojom/page/page_visibility_state.mojom-shared.h"
44 #include "third_party/blink/public/mojom/renderer_preference_watcher.mojom-shared.h"
45 #include "third_party/blink/public/platform/cross_variant_mojo_util.h"
46 #include "third_party/blink/public/platform/scheduler/web_agent_group_scheduler.h"
47 #include "third_party/blink/public/platform/web_common.h"
48 #if BUILDFLAG(IS_TIZEN_TV)
49 #include "third_party/blink/renderer/core/scroll/scroll_types.h"
51 #include "third_party/skia/include/core/SkColor.h"
52 #include "ui/display/mojom/screen_orientation.mojom-shared.h"
55 #include "third_party/skia/include/core/SkCanvas.h"
79 class WebHitTestResult;
81 class WebNoStatePrefetchClient;
88 struct DeviceEmulationParams;
89 struct WebWindowFeatures;
91 class BLINK_EXPORT WebView {
93 static const double kTextSizeMultiplierRatio;
94 static const double kMinTextSizeMultiplier;
95 static const double kMaxTextSizeMultiplier;
97 enum StyleInjectionTarget {
98 kInjectStyleInAllFrames,
99 kInjectStyleInTopFrameOnly
102 // Initialization ------------------------------------------------------
104 // Creates a WebView that is NOT yet initialized. To complete initialization,
105 // call WebLocalFrame::CreateMainFrame() or WebRemoteFrame::CreateMainFrame()
106 // as appropriate. It is legal to modify settings before completing
109 // The WebView is kept alive as long as the `page_handle` mojo interface
110 // is alive. The WebView will be destroyed when that interface closes, if
111 // a client wishes to close the WebView synchronously it can call `Close`
114 // clients may be null, but should both be null or not together.
115 // |is_hidden| defines the initial visibility of the page.
116 // |is_prerendering| defines whether the page is being prerendered by the
117 // Prerender2 feature (see content/browser/preloading/prerender/README.md).
118 // [is_inside_portal] defines whether the page is inside_portal.
119 // [is_fenced_frame] defines whether the page is for a fenced frame.
120 // |compositing_enabled| dictates whether accelerated compositing should be
121 // enabled for the page. It must be false if no clients are provided, or if a
122 // LayerTreeView will not be set for the WebWidget.
123 // TODO(danakj): This field should go away as WebWidgets always composite
125 // |page_handle| is only set for views that are part of a WebContents' frame
127 // |widgets_never_composited| is an indication that all WebWidgets associated
128 // with this WebView will never be user-visible and thus never need to produce
129 // pixels for display. This is separate from page visibility, as background
130 // pages can be marked visible in blink even though they are not user-visible.
131 // Page visibility controls blink behaviour for javascript, timers, and such
132 // to inform blink it is in the foreground or background. Whereas this bit
133 // refers to user-visibility and whether the tab needs to produce pixels to
134 // put on the screen at some point or not.
135 // |page_base_background_color| initial base background color used by the main
136 // frame. Set on create to avoid races. Passing in nullopt indicates the
137 // default base background color should be used.
138 // TODO(yuzus): Remove |is_hidden| and start using |PageVisibilityState|.
139 static WebView* Create(
142 bool is_prerendering,
143 bool is_inside_portal,
144 absl::optional<blink::FencedFrame::DeprecatedFencedFrameMode>
146 bool compositing_enabled,
147 bool widgets_never_composited,
149 CrossVariantMojoAssociatedReceiver<mojom::PageBroadcastInterfaceBase>
151 scheduler::WebAgentGroupScheduler& agent_group_scheduler,
152 const SessionStorageNamespaceId& session_storage_namespace_id,
153 absl::optional<SkColor> page_base_background_color,
154 const BrowsingContextGroupInfo& browsing_context_group_info);
156 // Destroys the WebView synchronously.
157 virtual void Close() = 0;
159 // Called to inform WebViewImpl that a local main frame has been attached.
160 // After this call MainFrameImpl() will return a valid frame until it is
162 virtual void DidAttachLocalMainFrame() = 0;
164 // Called while the main LocalFrame is being detached. The MainFrameImpl() is
165 // still valid until after this method is called.
166 virtual void DidDetachLocalMainFrame() = 0;
168 // Called to inform WebViewImpl that a remote main frame has been attached.
169 // Associated channels should be passed and bound.
170 virtual void DidAttachRemoteMainFrame(
171 CrossVariantMojoAssociatedRemote<mojom::RemoteMainFrameHostInterfaceBase>
173 CrossVariantMojoAssociatedReceiver<mojom::RemoteMainFrameInterfaceBase>
176 // Called to inform WebViewImpl that a remote main frame has been detached.
177 virtual void DidDetachRemoteMainFrame() = 0;
179 virtual void SetNoStatePrefetchClient(WebNoStatePrefetchClient*) = 0;
181 // Returns the session storage namespace id associated with this WebView.
182 virtual const SessionStorageNamespaceId& GetSessionStorageNamespaceId() = 0;
184 // Options -------------------------------------------------------------
186 // The returned pointer is valid for the lifetime of the WebView.
187 virtual WebSettings* GetSettings() = 0;
189 // Corresponds to the encoding of the main frame. Setting the page
190 // encoding may cause the main frame to reload.
191 virtual WebString PageEncoding() const = 0;
193 // Method that controls whether pressing Tab key cycles through page
194 // elements or inserts a '\t' char in the focused text area.
195 virtual void SetTabKeyCyclesThroughElements(bool) = 0;
197 // Controls the WebView's active state, which may affect the rendering
198 // of elements on the page (i.e., tinting of input elements).
199 virtual bool IsActive() const = 0;
200 virtual void SetIsActive(bool) = 0;
202 // Allows setting the state of the various bars exposed via BarProp
203 // properties on the window object. The size related fields of
204 // WebWindowFeatures are ignored.
205 virtual void SetWindowFeatures(const WebWindowFeatures&) = 0;
207 // Marks the WebView as being opened by a DOM call. This is relevant
208 // for whether window.close() may be called.
209 virtual void SetOpenedByDOM() = 0;
211 // Frames --------------------------------------------------------------
213 virtual WebFrame* MainFrame() = 0;
214 virtual const WebFrame* MainFrame() const = 0;
216 // Focus ---------------------------------------------------------------
218 virtual WebLocalFrame* FocusedFrame() = 0;
219 virtual void SetFocusedFrame(WebFrame*) = 0;
221 // Smooth scroll the root layer to |targetX|, |targetY| in |duration|.
222 virtual void SmoothScroll(int target_x,
224 base::TimeDelta duration) {}
226 // Advance the focus of the WebView forward to the next element or to the
227 // previous element in the tab sequence (if reverse is true).
228 virtual void AdvanceFocus(bool reverse) {}
230 // Zoom ----------------------------------------------------------------
232 // Returns the current zoom level. 0 is "original size", and each increment
233 // above or below represents zooming 20% larger or smaller to default limits
234 // of 300% and 50% of original size, respectively. Only plugins use
235 // non whole-numbers, since they might choose to have specific zoom level so
236 // that fixed-width content is fit-to-page-width, for example.
237 virtual double ZoomLevel() = 0;
239 // Changes the zoom level to the specified level, clamping at the limits
240 // noted above, and returns the current zoom level after applying the
242 virtual double SetZoomLevel(double) = 0;
244 // Gets the scale factor of the page, where 1.0 is the normal size, > 1.0
245 // is scaled up, < 1.0 is scaled down.
246 virtual float PageScaleFactor() const = 0;
248 // Scales the page without affecting layout by using the visual viewport.
249 virtual void SetPageScaleFactor(float) = 0;
251 // Minimum and Maximum as computed as a combination of default, page defined,
252 // UA, etc. constraints.
253 virtual float MinimumPageScaleFactor() const = 0;
254 virtual float MaximumPageScaleFactor() const = 0;
256 // Sets the offset of the visual viewport within the main frame, in
257 // fractional CSS pixels. The offset will be clamped so the visual viewport
258 // stays within the frame's bounds.
259 virtual void SetVisualViewportOffset(const gfx::PointF&) = 0;
261 // Gets the visual viewport's current offset within the page's main frame,
262 // in fractional CSS pixels.
263 virtual gfx::PointF VisualViewportOffset() const = 0;
265 // Get the visual viewport's size in CSS pixels.
266 virtual gfx::SizeF VisualViewportSize() const = 0;
268 // Sets the default minimum, and maximum page scale. These will be overridden
269 // by the page or by the overrides below if they are set.
270 virtual void SetDefaultPageScaleLimits(float min_scale, float max_scale) = 0;
272 // Sets the initial page scale to the given factor. This scale setting
274 // page scale set in the page's viewport meta tag.
275 virtual void SetInitialPageScaleOverride(float) = 0;
277 // Reset any saved values for the scroll and scale state.
278 virtual void ResetScrollAndScaleState() = 0;
280 // Returns the "preferred" contents size, defined as the preferred minimum
281 // width of the main document's contents and the minimum height required to
282 // display the main document without scrollbars. If the document is in quirks
283 // mode (does not have <!doctype html>), the height will stretch to fill the
284 // viewport. The returned size has the page zoom factor applied. The lifecycle
285 // must be updated to at least layout before calling (see: |UpdateLifecycle|).
287 // This may only be called when there is a local main frame attached to this
289 virtual gfx::Size ContentsPreferredMinimumSize() = 0;
291 // Check whether the preferred size has changed. This should only be called
292 // with up-to-date layout.
293 virtual void UpdatePreferredSize() = 0;
295 // Indicates that view's preferred size changes will be sent to the browser.
296 virtual void EnablePreferredSizeChangedMode() = 0;
298 // Sets the additional zoom factor used for device scale factor. This is used
299 // to scale the content by the device scale factor, without affecting zoom
301 virtual void SetZoomFactorForDeviceScaleFactor(float) = 0;
303 // Gets the device scale zoom that will be factored into the viewport layout
305 virtual float ZoomFactorForViewportLayout() = 0;
307 // Override the screen orientation override.
308 virtual void SetScreenOrientationOverrideForTesting(
309 absl::optional<display::mojom::ScreenOrientation> orientation) = 0;
311 // Set the window rect synchronously for testing. The normal flow is an
312 // asynchronous request to the browser.
313 virtual void SetWindowRectSynchronouslyForTesting(
314 const gfx::Rect& new_window_rect) = 0;
316 // Auto-Resize -----------------------------------------------------------
318 // Return the state of the auto resize mode.
319 virtual bool AutoResizeMode() = 0;
321 // Enable auto resize.
322 virtual void EnableAutoResizeForTesting(const gfx::Size& min_size,
323 const gfx::Size& max_size) = 0;
325 // Disable auto resize.
326 virtual void DisableAutoResizeForTesting(const gfx::Size& new_size) = 0;
328 // Data exchange -------------------------------------------------------
330 // Do a hit test equivalent to what would be done for a GestureTap event
331 // that has width/height corresponding to the supplied |tapArea|.
332 virtual WebHitTestResult HitTestResultForTap(const gfx::Point& tap_point,
333 const gfx::Size& tap_area) = 0;
335 // Developer tools -----------------------------------------------------
337 // Enables device emulation as specified in params.
338 virtual void EnableDeviceEmulation(const DeviceEmulationParams&) = 0;
340 // Cancel emulation started via |enableDeviceEmulation| call.
341 virtual void DisableDeviceEmulation() = 0;
343 // Context menu --------------------------------------------------------
345 virtual void PerformCustomContextMenuAction(unsigned action) = 0;
347 // Notify that context menu has been closed.
348 virtual void DidCloseContextMenu() = 0;
350 // Popup menu ----------------------------------------------------------
352 // Sets whether select popup menus should be rendered by the browser.
353 static void SetUseExternalPopupMenus(bool);
355 // Cancels and hides the current popup (datetime, select...) if any.
356 virtual void CancelPagePopup() = 0;
358 // Returns the current popup if any.
359 virtual WebPagePopup* GetPagePopup() const = 0;
361 // Visited link state --------------------------------------------------
363 // Tells all WebView instances to update the visited link state for the
365 static void UpdateVisitedLinkState(uint64_t hash);
367 // Tells all WebView instances to update the visited state for all
368 // their links. Use invalidateVisitedLinkHashes to inform that the visitedlink
369 // table was changed and the salt was changed too. And all cached visitedlink
370 // hashes need to be recalculated.
371 static void ResetVisitedLinkState(bool invalidate_visited_link_hashes);
373 // Custom colors -------------------------------------------------------
375 virtual void SetDeviceColorSpaceForTesting(
376 const gfx::ColorSpace& color_space) = 0;
378 #if BUILDFLAG(IS_EFL)
379 enum class TraverseFocusThrough : char {
380 EditableElement = 1 << 0,
381 SelectElement = 1 << 1,
382 EditableAndSelectElements = EditableElement | SelectElement
384 virtual bool MoveFocusToPrevious(
385 TraverseFocusThrough =
386 TraverseFocusThrough::EditableAndSelectElements) = 0;
387 virtual bool MoveFocusToNext(
388 TraverseFocusThrough =
389 TraverseFocusThrough::EditableAndSelectElements) = 0;
392 // Scheduling -----------------------------------------------------------
394 virtual PageScheduler* Scheduler() const = 0;
396 // Visibility -----------------------------------------------------------
398 // Sets the visibility of the WebView.
399 virtual void SetVisibilityState(mojom::PageVisibilityState visibility_state,
400 bool is_initial_state) = 0;
401 virtual mojom::PageVisibilityState GetVisibilityState() = 0;
403 // PageLifecycleState ----------------------------------------------------
405 // Sets the |visibility| and |pagehide_dispatch| properties for the
406 // PageLifecycleState of this page from a new page's commit. Should only be
407 // called from a main-frame same-site navigation where we did a proactive
408 // BrowsingInstance swap and we're reusing the old page's process.
409 // Note that unlike SetPageLifecycleState in PageBroadcast/WebViewImpl, we
410 // don't need to pass a callback here to notify the browser site that the
411 // PageLifecycleState has been successfully updated.
412 // TODO(rakina): When it's possible to pass PageLifecycleState here, pass
413 // PageLifecycleState instead.
414 virtual void SetPageLifecycleStateFromNewPageCommit(
415 mojom::PageVisibilityState visibility,
416 mojom::PagehideDispatch pagehide_dispatch) = 0;
418 // Lifecycle state ------------------------------------------------------
420 // Freezes or unfreezes the page and all the local frames.
421 virtual void SetPageFrozen(bool frozen) = 0;
423 #if BUILDFLAG(IS_TIZEN)
424 virtual void PauseScheduledTasks() = 0;
425 virtual void UnpauseScheduledTasks() = 0;
428 // Autoplay configuration -----------------------------------------------
430 // Sets the autoplay flags for this webview's page.
431 // The valid flags are defined in
432 // third_party/blink/public/platform/autoplay.mojom
433 virtual void AddAutoplayFlags(int32_t flags) = 0;
434 virtual void ClearAutoplayFlags() = 0;
435 virtual int32_t AutoplayFlagsForTest() = 0;
436 virtual gfx::Size GetPreferredSizeForTest() = 0;
438 // Non-composited support -----------------------------------------------
440 // Called to paint the rectangular region within the WebView's main frame
441 // onto the specified canvas at (viewport.x, viewport.y). This is to provide
442 // support for non-composited WebViews, and is used to paint into a
443 // PaintCanvas being supplied by another (composited) WebView.
445 // Before calling PaintContent(), the caller must ensure the lifecycle of the
446 // widget's frame is clean by calling
447 // UpdateLifecycle(WebLifecycleUpdate::All). It is okay to call paint multiple
448 // times once the lifecycle is clean, assuming no other changes are made to
449 // the WebWidget (e.g., once events are processed, it should be assumed that
450 // another call to UpdateLifecycle is warranted before painting again). Paints
451 // starting from the main LayoutView's property tree state, thus ignoring any
452 // transient transormations (e.g. pinch-zoom, dev tools emulation, etc.).
454 // The painting will be performed without applying the DevicePixelRatio as
455 // scaling is expected to already be applied to the PaintCanvas by the
456 // composited WebView which supplied the PaintCanvas. The canvas state may
457 // be modified and should be saved before calling this method and restored
459 virtual void PaintContent(cc::PaintCanvas*, const gfx::Rect& viewport) = 0;
461 // Renderer preferences ---------------------------------------------------
463 virtual void RegisterRendererPreferenceWatcher(
464 CrossVariantMojoRemote<mojom::RendererPreferenceWatcherInterfaceBase>
467 virtual void SetRendererPreferences(
468 const RendererPreferences& preferences) = 0;
469 virtual const RendererPreferences& GetRendererPreferences() const = 0;
471 // Web preferences ---------------------------------------------------
473 // Applies blink related preferences to this view.
474 static void ApplyWebPreferences(const web_pref::WebPreferences& prefs,
477 #if BUILDFLAG(IS_TIZEN_TV)
478 virtual void SetTranslatedURL(const WebString) = 0;
481 virtual void SetWebPreferences(
482 const web_pref::WebPreferences& preferences) = 0;
483 virtual const web_pref::WebPreferences& GetWebPreferences() = 0;
485 // TODO(lfg): Remove this once the refactor of WebView/WebWidget is
487 virtual WebFrameWidget* MainFrameWidget() = 0;
489 // History list ---------------------------------------------------------
490 virtual void SetHistoryListFromNavigation(
491 int32_t history_offset,
492 absl::optional<int32_t> history_length) = 0;
493 virtual void IncreaseHistoryListFromNavigation() = 0;
495 // Session history -----------------------------------------------------
496 // Returns the number of history items before/after the current
498 virtual int32_t HistoryBackListCount() const = 0;
499 virtual int32_t HistoryForwardListCount() const = 0;
501 // Returns whether this WebView represents a fenced frame root or not.
502 virtual bool IsFencedFrameRoot() const = 0;
504 #if defined(TIZEN_VIDEO_HOLE)
505 // Video hole support ---------------------------------------------------
506 virtual bool IsVideoHoleForRender() const = 0;
509 #if BUILDFLAG(IS_TIZEN_TV)
510 //Browser edge scroll
511 virtual bool EdgeScrollBy(const ScrollOffset&, const gfx::Point&) = 0;
514 // Misc -------------------------------------------------------------
516 // Returns the number of live WebView instances in this process.
517 static size_t GetWebViewCount();
519 #if BUILDFLAG(IS_TIZEN_TV)
520 virtual bool IsHitScrollbar() { return false; }
521 virtual bool IsMouseDownEventSwallowed() { return false; }
524 // Sets whether web or OS-level Attribution Reporting is supported. See
525 // https://github.com/WICG/attribution-reporting-api/blob/main/app_to_web.md
526 virtual void SetPageAttributionSupport(
527 network::mojom::AttributionSupport support) = 0;
529 #if BUILDFLAG(IS_EFL)
530 // Paints the rectangular region within the WebWidget
531 // onto the specified canvas, when the page is not having any 3D content.
532 virtual bool PaintSoftBitmap(SkCanvas*, const gfx::Rect&) = 0;
533 virtual bool HasAcceleratedCanvasWithinViewport() const = 0;
534 virtual SkColor BackgroundColor() const {
535 return 0xFFFFFFFF; /* Sk_ColorWHITE */
537 virtual void SetLongPollingGlobalTimeout(uint64_t timeout) = 0;
539 // Returns the selection rect encompassing text and images.
540 virtual gfx::Rect CurrentSelectionRect() const = 0;
542 #if BUILDFLAG(IS_TIZEN_TV)
543 // Suspends loaders for the main frame and all sub-frames.
544 virtual void SuspendNetworkLoading() = 0;
546 // Resumes perviously suspended frame loaders.
547 virtual void ResumeNetworkLoading() = 0;
548 #endif // IS_TIZEN_TV
551 #if BUILDFLAG(IS_TIZEN_TV)
552 virtual void SetFloatVideoWindowState(bool enable) = 0;
553 // Return if there is any active video in the view
554 virtual bool IsVideoPlaying() const = 0;
555 virtual void SetParentalRatingResult(const WebString&, bool) = 0;
556 virtual void SetPreferTextLang(const WebString&) = 0;
560 ~WebView() = default;
565 #if BUILDFLAG(IS_EFL)
566 static inline blink::WebView::TraverseFocusThrough operator|(
567 blink::WebView::TraverseFocusThrough a,
568 blink::WebView::TraverseFocusThrough b) {
569 return static_cast<blink::WebView::TraverseFocusThrough>(
570 static_cast<char>(a) | static_cast<char>(b));
572 static inline blink::WebView::TraverseFocusThrough operator&(
573 blink::WebView::TraverseFocusThrough a,
574 blink::WebView::TraverseFocusThrough b) {
575 return static_cast<blink::WebView::TraverseFocusThrough>(
576 static_cast<char>(a) & static_cast<char>(b));
580 #endif // THIRD_PARTY_BLINK_PUBLIC_WEB_WEB_VIEW_H_