1 // Copyright (c) 2014 The Chromium Embedded Framework Authors. 2 // Portions copyright (c) 2012 The Chromium Authors. All rights reserved. 3 // Use of this source code is governed by a BSD-style license that can be 4 // found in the LICENSE file. 5 6 #ifndef CEF_LIBCEF_BROWSER_OSR_RENDER_WIDGET_HOST_VIEW_OSR_H_ 7 #define CEF_LIBCEF_BROWSER_OSR_RENDER_WIDGET_HOST_VIEW_OSR_H_ 8 #pragma once 9 10 #include <map> 11 #include <set> 12 #include <vector> 13 14 #include "include/cef_base.h" 15 #include "include/cef_browser.h" 16 17 #include "libcef/browser/alloy/alloy_browser_host_impl.h" 18 #include "libcef/browser/osr/host_display_client_osr.h" 19 #include "libcef/browser/osr/motion_event_osr.h" 20 21 #include "base/memory/weak_ptr.h" 22 #include "build/build_config.h" 23 #include "cc/layers/deadline_policy.h" 24 #include "components/viz/common/frame_sinks/begin_frame_source.h" 25 #include "components/viz/common/surfaces/parent_local_surface_id_allocator.h" 26 #include "content/browser/renderer_host/input/mouse_wheel_phase_handler.h" 27 #include "content/browser/renderer_host/render_widget_host_view_base.h" 28 #include "content/browser/renderer_host/text_input_manager.h" 29 #include "content/public/browser/render_frame_metadata_provider.h" 30 #include "content/public/common/widget_type.h" 31 #include "third_party/abseil-cpp/absl/types/optional.h" 32 #include "third_party/blink/public/mojom/widget/record_content_to_visible_time_request.mojom-forward.h" 33 #include "ui/base/cursor/cursor.h" 34 #include "ui/base/cursor/mojom/cursor_type.mojom-shared.h" 35 #include "ui/compositor/compositor.h" 36 #include "ui/events/base_event_utils.h" 37 #include "ui/events/gesture_detection/filtered_gesture_provider.h" 38 #include "ui/events/gesture_detection/gesture_configuration.h" 39 #include "ui/events/gesture_detection/motion_event_generic.h" 40 #include "ui/gfx/geometry/rect.h" 41 42 #if BUILDFLAG(IS_MAC) 43 #include "content/browser/renderer_host/browser_compositor_view_mac.h" 44 #endif 45 46 #if BUILDFLAG(IS_WIN) 47 #include "ui/gfx/win/window_impl.h" 48 #endif 49 50 namespace content { 51 class DelegatedFrameHost; 52 class DelegatedFrameHostClient; 53 class RenderWidgetHost; 54 class RenderWidgetHostImpl; 55 class RenderWidgetHostViewGuest; 56 class BackingStore; 57 class CursorManager; 58 } // namespace content 59 60 class CefCopyFrameGenerator; 61 class CefSoftwareOutputDeviceOSR; 62 class CefVideoConsumerOSR; 63 class CefWebContentsViewOSR; 64 65 /////////////////////////////////////////////////////////////////////////////// 66 // CefRenderWidgetHostViewOSR 67 // 68 // An object representing the "View" of a rendered web page. This object is 69 // responsible for sending paint events to the the CefRenderHandler 70 // when window rendering is disabled. It is the implementation of the 71 // RenderWidgetHostView that the cross-platform RenderWidgetHost object uses 72 // to display the data. 73 // 74 // Comment excerpted from render_widget_host.h: 75 // 76 // "The lifetime of the RenderWidgetHostView is tied to the render process. 77 // If the render process dies, the RenderWidgetHostView goes away and all 78 // references to it must become NULL." 79 // 80 // RenderWidgetHostView class hierarchy described in render_widget_host_view.h. 81 /////////////////////////////////////////////////////////////////////////////// 82 83 #if BUILDFLAG(IS_MAC) 84 class MacHelper; 85 #endif 86 87 class CefRenderWidgetHostViewOSR 88 : public content::RenderWidgetHostViewBase, 89 public content::RenderFrameMetadataProvider::Observer, 90 public ui::CompositorDelegate, 91 public content::TextInputManager::Observer, 92 public ui::GestureProviderClient { 93 public: 94 CefRenderWidgetHostViewOSR(SkColor background_color, 95 bool use_shared_texture, 96 bool use_external_begin_frame, 97 content::RenderWidgetHost* widget, 98 CefRenderWidgetHostViewOSR* parent_host_view); 99 100 CefRenderWidgetHostViewOSR(const CefRenderWidgetHostViewOSR&) = delete; 101 CefRenderWidgetHostViewOSR& operator=(const CefRenderWidgetHostViewOSR&) = 102 delete; 103 104 ~CefRenderWidgetHostViewOSR() override; 105 106 // RenderWidgetHostView implementation. 107 void InitAsChild(gfx::NativeView parent_view) override; 108 void SetSize(const gfx::Size& size) override; 109 void SetBounds(const gfx::Rect& rect) override; 110 gfx::NativeView GetNativeView() override; 111 gfx::NativeViewAccessible GetNativeViewAccessible() override; 112 void Focus() override; 113 bool HasFocus() override; 114 uint32_t GetCaptureSequenceNumber() const override; 115 bool IsSurfaceAvailableForCopy() override; 116 void ShowWithVisibility( 117 content::PageVisibilityState page_visibility) override; 118 void Hide() override; 119 bool IsShowing() override; 120 void EnsureSurfaceSynchronizedForWebTest() override; 121 gfx::Rect GetViewBounds() override; 122 void SetBackgroundColor(SkColor color) override; 123 absl::optional<SkColor> GetBackgroundColor() override; 124 void UpdateBackgroundColor() override; 125 absl::optional<content::DisplayFeature> GetDisplayFeature() override; 126 void SetDisplayFeatureForTesting( 127 const content::DisplayFeature* display_feature) override; 128 blink::mojom::PointerLockResult LockMouse( 129 bool request_unadjusted_movement) override; 130 blink::mojom::PointerLockResult ChangeMouseLock( 131 bool request_unadjusted_movement) override; 132 void UnlockMouse() override; 133 void TakeFallbackContentFrom(content::RenderWidgetHostView* view) override; 134 135 #if BUILDFLAG(IS_MAC) 136 void SetActive(bool active) override; 137 void ShowDefinitionForSelection() override; 138 void SpeakSelection() override; 139 void SetWindowFrameInScreen(const gfx::Rect& rect) override; 140 void ShowSharePicker( 141 const std::string& title, 142 const std::string& text, 143 const std::string& url, 144 const std::vector<std::string>& file_paths, 145 blink::mojom::ShareService::ShareCallback callback) override; 146 #endif // BUILDFLAG(IS_MAC) 147 148 // RenderWidgetHostViewBase implementation. 149 void ResetFallbackToFirstNavigationSurface() override; 150 void InitAsPopup(content::RenderWidgetHostView* parent_host_view, 151 const gfx::Rect& bounds, 152 const gfx::Rect& anchor_rect) override; 153 void UpdateCursor(const content::WebCursor& cursor) override; 154 void SetIsLoading(bool is_loading) override; 155 void RenderProcessGone() override; 156 void Destroy() override; 157 void UpdateTooltipUnderCursor(const std::u16string& tooltip_text) override; 158 content::CursorManager* GetCursorManager() override; 159 gfx::Size GetCompositorViewportPixelSize() override; 160 void CopyFromSurface( 161 const gfx::Rect& src_rect, 162 const gfx::Size& output_size, 163 base::OnceCallback<void(const SkBitmap&)> callback) override; 164 display::ScreenInfos GetNewScreenInfosForUpdate() override; 165 void TransformPointToRootSurface(gfx::PointF* point) override; 166 gfx::Rect GetBoundsInRootWindow() override; 167 168 #if !BUILDFLAG(IS_MAC) 169 viz::ScopedSurfaceIdAllocator DidUpdateVisualProperties( 170 const cc::RenderFrameMetadata& metadata) override; 171 #endif 172 173 viz::SurfaceId GetCurrentSurfaceId() const override; 174 void ImeCompositionRangeChanged( 175 const gfx::Range& range, 176 const std::vector<gfx::Rect>& character_bounds) override; 177 std::unique_ptr<content::SyntheticGestureTarget> 178 CreateSyntheticGestureTarget() override; 179 bool TransformPointToCoordSpaceForView( 180 const gfx::PointF& point, 181 RenderWidgetHostViewBase* target_view, 182 gfx::PointF* transformed_point) override; 183 void DidNavigate() override; 184 void SelectionChanged(const std::u16string& text, 185 size_t offset, 186 const gfx::Range& range) override; 187 const viz::LocalSurfaceId& GetLocalSurfaceId() const override; 188 const viz::FrameSinkId& GetFrameSinkId() const override; 189 viz::FrameSinkId GetRootFrameSinkId() override; 190 void NotifyHostAndDelegateOnWasShown( 191 blink::mojom::RecordContentToVisibleTimeRequestPtr visible_time_request) 192 override; 193 void RequestPresentationTimeFromHostOrDelegate( 194 blink::mojom::RecordContentToVisibleTimeRequestPtr visible_time_request) 195 override; 196 void CancelPresentationTimeRequestForHostAndDelegate() override; 197 198 void OnFrameComplete(const viz::BeginFrameAck& ack); 199 200 // RenderFrameMetadataProvider::Observer implementation. OnRenderFrameMetadataChangedBeforeActivation(const cc::RenderFrameMetadata & metadata)201 void OnRenderFrameMetadataChangedBeforeActivation( 202 const cc::RenderFrameMetadata& metadata) override {} 203 void OnRenderFrameMetadataChangedAfterActivation( 204 base::TimeTicks activation_time) override; OnRenderFrameSubmission()205 void OnRenderFrameSubmission() override {} OnLocalSurfaceIdChanged(const cc::RenderFrameMetadata & metadata)206 void OnLocalSurfaceIdChanged( 207 const cc::RenderFrameMetadata& metadata) override {} 208 209 // ui::CompositorDelegate implementation. 210 std::unique_ptr<viz::HostDisplayClient> CreateHostDisplayClient() override; 211 212 // TextInputManager::Observer implementation. 213 void OnUpdateTextInputStateCalled( 214 content::TextInputManager* text_input_manager, 215 RenderWidgetHostViewBase* updated_view, 216 bool did_update_state) override; 217 218 // ui::GestureProviderClient implementation. 219 void ProcessAckedTouchEvent( 220 const content::TouchEventWithLatencyInfo& touch, 221 blink::mojom::InputEventResultState ack_result) override; 222 void OnGestureEvent(const ui::GestureEventData& gesture) override; 223 224 bool InstallTransparency(); 225 226 void WasResized(); 227 void SynchronizeVisualProperties( 228 const cc::DeadlinePolicy& deadline_policy, 229 const absl::optional<viz::LocalSurfaceId>& child_local_surface_id); 230 void OnScreenInfoChanged(); 231 void Invalidate(CefBrowserHost::PaintElementType type); 232 void SendExternalBeginFrame(); 233 void SendKeyEvent(const content::NativeWebKeyboardEvent& event); 234 void SendMouseEvent(const blink::WebMouseEvent& event); 235 void SendMouseWheelEvent(const blink::WebMouseWheelEvent& event); 236 void SendTouchEvent(const CefTouchEvent& event); 237 bool ShouldRouteEvents() const; 238 void SetFocus(bool focus); 239 void UpdateFrameRate(); 240 241 gfx::Size SizeInPixels(); 242 void OnPaint(const gfx::Rect& damage_rect, 243 const gfx::Size& pixel_size, 244 const void* pixels); 245 246 void OnBeginFame(base::TimeTicks frame_time); 247 IsPopupWidget()248 bool IsPopupWidget() const { 249 return widget_type_ == content::WidgetType::kPopup; 250 } 251 252 void ImeSetComposition(const CefString& text, 253 const std::vector<CefCompositionUnderline>& underlines, 254 const CefRange& replacement_range, 255 const CefRange& selection_range); 256 void ImeCommitText(const CefString& text, 257 const CefRange& replacement_range, 258 int relative_cursor_pos); 259 void ImeFinishComposingText(bool keep_selection); 260 void ImeCancelComposition() override; 261 browser_impl()262 CefRefPtr<AlloyBrowserHostImpl> browser_impl() const { return browser_impl_; } set_browser_impl(CefRefPtr<AlloyBrowserHostImpl> browser)263 void set_browser_impl(CefRefPtr<AlloyBrowserHostImpl> browser) { 264 browser_impl_ = browser; 265 } 266 set_popup_host_view(CefRenderWidgetHostViewOSR * popup_view)267 void set_popup_host_view(CefRenderWidgetHostViewOSR* popup_view) { 268 if (popup_view != popup_host_view_) 269 forward_touch_to_popup_ = false; 270 popup_host_view_ = popup_view; 271 } set_child_host_view(CefRenderWidgetHostViewOSR * popup_view)272 void set_child_host_view(CefRenderWidgetHostViewOSR* popup_view) { 273 child_host_view_ = popup_view; 274 } 275 render_widget_host()276 content::RenderWidgetHostImpl* render_widget_host() const { 277 return render_widget_host_; 278 } 279 ui::Layer* GetRootLayer() const; 280 281 void OnPresentCompositorFrame(); 282 283 void OnDidUpdateVisualPropertiesComplete( 284 const cc::RenderFrameMetadata& metadata); 285 286 void ReleaseCompositor(); 287 288 // Marks the current viz::LocalSurfaceId as invalid. AllocateLocalSurfaceId 289 // must be called before submitting new CompositorFrames. May be called by 290 // content::DelegatedFrameHostClient::InvalidateLocalSurfaceIdOnEviction. 291 void InvalidateLocalSurfaceId(); 292 293 private: 294 void SetFrameRate(); 295 bool SetScreenInfo(); 296 bool SetViewBounds(); 297 bool SetRootLayerSize(bool force); 298 299 // Manages resizing so that only one resize request is in-flight at a time. 300 bool ResizeRootLayer(); 301 void ReleaseResizeHold(); 302 303 void CancelWidget(); 304 305 void OnScrollOffsetChanged(); 306 307 void AddGuestHostView(CefRenderWidgetHostViewOSR* guest_host); 308 void RemoveGuestHostView(CefRenderWidgetHostViewOSR* guest_host); 309 310 // Register a callback that will be executed when |guest_host_view| receives 311 // OnSwapCompositorFrame. The callback triggers repaint of the embedder view. 312 void RegisterGuestViewFrameSwappedCallback( 313 content::RenderWidgetHostViewGuest* guest_host_view); 314 315 void OnGuestViewFrameSwapped( 316 content::RenderWidgetHostViewGuest* guest_host_view); 317 318 void InvalidateInternal(const gfx::Rect& bounds_in_pixels); 319 320 void RequestImeCompositionUpdate(bool start_monitoring); 321 322 viz::FrameSinkId AllocateFrameSinkId(); 323 324 // Forces the view to allocate a new viz::LocalSurfaceId for the next 325 // CompositorFrame submission in anticipation of a synchronization operation 326 // that does not involve a resize or a device scale factor change. 327 void AllocateLocalSurfaceId(); 328 const viz::LocalSurfaceId& GetCurrentLocalSurfaceId() const; 329 330 // Sets the current viz::LocalSurfaceId, in cases where the embedded client 331 // has allocated one. Also sets child sequence number component of the 332 // viz::LocalSurfaceId allocator. 333 void UpdateLocalSurfaceIdFromEmbeddedClient( 334 const absl::optional<viz::LocalSurfaceId>& local_surface_id); 335 336 // Returns the current viz::LocalSurfaceIdAllocation. 337 const viz::LocalSurfaceId& GetOrCreateLocalSurfaceId(); 338 339 void AddDamageRect(uint32_t sequence, const gfx::Rect& rect); 340 341 // Applies background color without notifying the RenderWidget about 342 // opaqueness changes. 343 void UpdateBackgroundColorFromRenderer(SkColor color); 344 345 // The background color of the web content. 346 SkColor background_color_; 347 348 int frame_rate_threshold_us_ = 0; 349 350 std::unique_ptr<ui::Compositor> compositor_; 351 std::unique_ptr<content::DelegatedFrameHost> delegated_frame_host_; 352 std::unique_ptr<content::DelegatedFrameHostClient> 353 delegated_frame_host_client_; 354 std::unique_ptr<ui::Layer> root_layer_; 355 356 // Used to allocate LocalSurfaceIds when this is embedding external content. 357 std::unique_ptr<viz::ParentLocalSurfaceIdAllocator> 358 parent_local_surface_id_allocator_; 359 viz::ParentLocalSurfaceIdAllocator compositor_local_surface_id_allocator_; 360 361 std::unique_ptr<content::CursorManager> cursor_manager_; 362 363 // Provides |source_id| for BeginFrameArgs that we create. 364 viz::StubBeginFrameSource begin_frame_source_; 365 uint64_t begin_frame_number_ = viz::BeginFrameArgs::kStartingFrameNumber; 366 bool begin_frame_pending_ = false; 367 368 bool sync_frame_rate_ = false; 369 bool external_begin_frame_enabled_ = false; 370 bool needs_external_begin_frames_ = false; 371 372 CefHostDisplayClientOSR* host_display_client_ = nullptr; 373 std::unique_ptr<CefVideoConsumerOSR> video_consumer_; 374 375 bool hold_resize_ = false; 376 bool pending_resize_ = false; 377 378 float cached_scale_factor_ = 0.0f; 379 380 // The associated Model. While |this| is being Destroyed, 381 // |render_widget_host_| is NULL and the message loop is run one last time 382 // Message handlers must check for a NULL |render_widget_host_|. 383 content::RenderWidgetHostImpl* render_widget_host_; 384 385 bool has_parent_; 386 CefRenderWidgetHostViewOSR* parent_host_view_; 387 CefRenderWidgetHostViewOSR* popup_host_view_ = nullptr; 388 CefRenderWidgetHostViewOSR* child_host_view_ = nullptr; 389 std::set<CefRenderWidgetHostViewOSR*> guest_host_views_; 390 391 CefRefPtr<AlloyBrowserHostImpl> browser_impl_; 392 393 bool is_showing_ = false; 394 bool is_destroyed_ = false; 395 bool is_first_navigation_ = true; 396 gfx::Rect current_view_bounds_; 397 gfx::Rect popup_position_; 398 base::Lock damage_rect_lock_; 399 std::map<uint32_t, gfx::Rect> damage_rects_; 400 401 // Whether pinch-to-zoom should be enabled and pinch events forwarded to the 402 // renderer. 403 bool pinch_zoom_enabled_; 404 405 // The last scroll offset of the view. 406 gfx::PointF last_scroll_offset_; 407 bool is_scroll_offset_changed_pending_ = false; 408 409 content::MouseWheelPhaseHandler mouse_wheel_phase_handler_; 410 411 // Latest capture sequence number which is incremented when the caller 412 // requests surfaces be synchronized via 413 // EnsureSurfaceSynchronizedForLayoutTest(). 414 uint32_t latest_capture_sequence_number_ = 0u; 415 416 // ui::GestureProviderClient implementation. 417 ui::FilteredGestureProvider gesture_provider_; 418 419 CefMotionEventOSR pointer_state_; 420 bool forward_touch_to_popup_ = false; 421 422 base::WeakPtrFactory<CefRenderWidgetHostViewOSR> weak_ptr_factory_; 423 }; 424 425 #endif // CEF_LIBCEF_BROWSER_OSR_RENDER_WIDGET_HOST_VIEW_OSR_H_ 426