1 // Copyright 2020 The Chromium Embedded Framework Authors. All rights reserved. 2 // Use of this source code is governed by a BSD-style license that can be 3 // found in the LICENSE file. 4 5 #ifndef CEF_LIBCEF_BROWSER_CHROME_CHROME_BROWSER_DELEGATE_H_ 6 #define CEF_LIBCEF_BROWSER_CHROME_CHROME_BROWSER_DELEGATE_H_ 7 #pragma once 8 9 #include <memory> 10 11 #include "libcef/browser/browser_host_base.h" 12 #include "libcef/browser/browser_info.h" 13 #include "libcef/browser/chrome/browser_delegate.h" 14 15 #include "third_party/abseil-cpp/absl/types/optional.h" 16 17 class CefBrowserContentsDelegate; 18 class CefRequestContextImpl; 19 class ChromeBrowserHostImpl; 20 21 // Implementation of the cef::BrowserDelegate interface. Lifespan is controlled 22 // by the Browser object. Only accessed on the UI thread. 23 // 24 // The Browser object represents the top-level Chrome browser window. One or 25 // more tabs (WebContents) are then owned by the Browser object via 26 // TabStripModel. A new Browser object can be created programmatically using 27 // "new Browser" or Browser::Create, or as a result of user action such as 28 // dragging a tab out of an existing window. New or existing tabs can also be 29 // added to an already existing Browser object. 30 // 31 // The Browser object acts as the WebContentsDelegate for all attached tabs. CEF 32 // integration requires WebContentsDelegate callbacks and notification of tab 33 // attach/detach. To support this integration a cef::BrowserDelegate 34 // (ChromeBrowserDelegate) member is created in the Browser constructor and 35 // receives delegation for the Browser callbacks. ChromeBrowserDelegate creates 36 // a new ChromeBrowserHostImpl when a tab is added to a Browser for the first 37 // time, and that ChromeBrowserHostImpl continues to exist until the tab's 38 // WebContents is destroyed. The associated WebContents object does not change, 39 // but the Browser object will change when the tab is dragged between windows. 40 class ChromeBrowserDelegate : public cef::BrowserDelegate { 41 public: 42 ChromeBrowserDelegate(Browser* browser, 43 const CefBrowserCreateParams& create_params); 44 45 ChromeBrowserDelegate(const ChromeBrowserDelegate&) = delete; 46 ChromeBrowserDelegate& operator=(const ChromeBrowserDelegate&) = delete; 47 48 ~ChromeBrowserDelegate() override; 49 50 // cef::BrowserDelegate methods: 51 void OnWebContentsCreated(content::WebContents* new_contents) override; 52 void SetAsDelegate(content::WebContents* web_contents, 53 bool set_delegate) override; 54 55 // WebContentsDelegate methods: 56 void WebContentsCreated(content::WebContents* source_contents, 57 int opener_render_process_id, 58 int opener_render_frame_id, 59 const std::string& frame_name, 60 const GURL& target_url, 61 content::WebContents* new_contents) override; 62 void AddNewContents(content::WebContents* source_contents, 63 std::unique_ptr<content::WebContents> new_contents, 64 const GURL& target_url, 65 WindowOpenDisposition disposition, 66 const gfx::Rect& initial_rect, 67 bool user_gesture, 68 bool* was_blocked) override; 69 content::WebContents* OpenURLFromTab( 70 content::WebContents* source, 71 const content::OpenURLParams& params) override; 72 void LoadingStateChanged(content::WebContents* source, 73 bool should_show_loading_ui) override; 74 void UpdateTargetURL(content::WebContents* source, const GURL& url) override; 75 bool DidAddMessageToConsole(content::WebContents* source, 76 blink::mojom::ConsoleMessageLevel log_level, 77 const std::u16string& message, 78 int32_t line_no, 79 const std::u16string& source_id) override; 80 void DidNavigatePrimaryMainFramePostCommit( 81 content::WebContents* web_contents) override; 82 void EnterFullscreenModeForTab( 83 content::RenderFrameHost* requesting_frame, 84 const blink::mojom::FullscreenOptions& options) override; 85 void ExitFullscreenModeForTab(content::WebContents* web_contents) override; 86 content::KeyboardEventProcessingResult PreHandleKeyboardEvent( 87 content::WebContents* source, 88 const content::NativeWebKeyboardEvent& event) override; 89 bool HandleKeyboardEvent( 90 content::WebContents* source, 91 const content::NativeWebKeyboardEvent& event) override; 92 browser()93 Browser* browser() const { return browser_; } 94 95 private: 96 void CreateBrowser( 97 content::WebContents* web_contents, 98 CefBrowserSettings settings, 99 CefRefPtr<CefClient> client, 100 std::unique_ptr<CefBrowserPlatformDelegate> platform_delegate, 101 scoped_refptr<CefBrowserInfo> browser_info, 102 CefRefPtr<ChromeBrowserHostImpl> opener, 103 CefRefPtr<CefRequestContextImpl> request_context_impl); 104 105 CefBrowserContentsDelegate* GetDelegateForWebContents( 106 content::WebContents* web_contents); 107 108 Browser* const browser_; 109 110 // Used when creating a new browser host. 111 const CefBrowserCreateParams create_params_; 112 }; 113 114 #endif // CEF_LIBCEF_BROWSER_CHROME_CHROME_BROWSER_DELEGATE_H_ 115