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_HOST_IMPL_H_ 6 #define CEF_LIBCEF_BROWSER_CHROME_CHROME_BROWSER_HOST_IMPL_H_ 7 #pragma once 8 9 #include <memory> 10 11 #include "libcef/browser/browser_host_base.h" 12 #include "libcef/browser/chrome/browser_delegate.h" 13 14 class Browser; 15 class ChromeBrowserDelegate; 16 17 // CefBrowser implementation for the chrome runtime. Method calls are delegated 18 // to the chrome Browser object or the WebContents as appropriate. See the 19 // ChromeBrowserDelegate documentation for additional details. All methods are 20 // thread-safe unless otherwise indicated. 21 class ChromeBrowserHostImpl : public CefBrowserHostBase { 22 public: 23 // CEF-specific parameters passed via Browser::CreateParams::cef_params and 24 // possibly shared by multiple Browser instances. 25 class DelegateCreateParams : public cef::BrowserDelegate::CreateParams { 26 public: DelegateCreateParams(const CefBrowserCreateParams & create_params)27 DelegateCreateParams(const CefBrowserCreateParams& create_params) 28 : create_params_(create_params) {} 29 30 CefBrowserCreateParams create_params_; 31 }; 32 33 // Create a new Browser with a single tab (WebContents) and associated 34 // ChromeBrowserHostImpl instance. 35 static CefRefPtr<ChromeBrowserHostImpl> Create( 36 const CefBrowserCreateParams& params); 37 38 // Returns the browser associated with the specified RenderViewHost. 39 static CefRefPtr<ChromeBrowserHostImpl> GetBrowserForHost( 40 const content::RenderViewHost* host); 41 // Returns the browser associated with the specified RenderFrameHost. 42 static CefRefPtr<ChromeBrowserHostImpl> GetBrowserForHost( 43 const content::RenderFrameHost* host); 44 // Returns the browser associated with the specified WebContents. 45 static CefRefPtr<ChromeBrowserHostImpl> GetBrowserForContents( 46 const content::WebContents* contents); 47 // Returns the browser associated with the specified global ID. 48 static CefRefPtr<ChromeBrowserHostImpl> GetBrowserForGlobalId( 49 const content::GlobalRenderFrameHostId& global_id); 50 51 ~ChromeBrowserHostImpl() override; 52 53 // CefBrowserContentsDelegate::Observer methods: 54 void OnWebContentsDestroyed(content::WebContents* web_contents) override; 55 56 // CefBrowserHostBase methods called from CefFrameHostImpl: 57 void OnSetFocus(cef_focus_source_t source) override; 58 59 // CefBrowserHost methods: 60 void CloseBrowser(bool force_close) override; 61 bool TryCloseBrowser() override; 62 void SetFocus(bool focus) override; 63 CefWindowHandle GetWindowHandle() override; 64 CefWindowHandle GetOpenerWindowHandle() override; 65 double GetZoomLevel() override; 66 void SetZoomLevel(double zoomLevel) override; 67 void RunFileDialog(FileDialogMode mode, 68 const CefString& title, 69 const CefString& default_file_path, 70 const std::vector<CefString>& accept_filters, 71 int selected_accept_filter, 72 CefRefPtr<CefRunFileDialogCallback> callback) override; 73 void Print() override; 74 void PrintToPDF(const CefString& path, 75 const CefPdfPrintSettings& settings, 76 CefRefPtr<CefPdfPrintCallback> callback) override; 77 void Find(const CefString& searchText, 78 bool forward, 79 bool matchCase, 80 bool findNext) override; 81 void StopFinding(bool clearSelection) override; 82 void ShowDevTools(const CefWindowInfo& windowInfo, 83 CefRefPtr<CefClient> client, 84 const CefBrowserSettings& settings, 85 const CefPoint& inspect_element_at) override; 86 void CloseDevTools() override; 87 bool HasDevTools() override; 88 bool IsWindowRenderingDisabled() override; 89 void WasResized() override; 90 void WasHidden(bool hidden) override; 91 void NotifyScreenInfoChanged() override; 92 void Invalidate(PaintElementType type) override; 93 void SendExternalBeginFrame() override; 94 void SendTouchEvent(const CefTouchEvent& event) override; 95 void SendCaptureLostEvent() override; 96 void NotifyMoveOrResizeStarted() override; 97 int GetWindowlessFrameRate() override; 98 void SetWindowlessFrameRate(int frame_rate) override; 99 void ImeSetComposition(const CefString& text, 100 const std::vector<CefCompositionUnderline>& underlines, 101 const CefRange& replacement_range, 102 const CefRange& selection_range) override; 103 void ImeCommitText(const CefString& text, 104 const CefRange& replacement_range, 105 int relative_cursor_pos) override; 106 void ImeFinishComposingText(bool keep_selection) override; 107 void ImeCancelComposition() override; 108 void DragTargetDragEnter(CefRefPtr<CefDragData> drag_data, 109 const CefMouseEvent& event, 110 DragOperationsMask allowed_ops) override; 111 void DragTargetDragOver(const CefMouseEvent& event, 112 DragOperationsMask allowed_ops) override; 113 void DragTargetDragLeave() override; 114 void DragTargetDrop(const CefMouseEvent& event) override; 115 void DragSourceSystemDragEnded() override; 116 void DragSourceEndedAt(int x, int y, DragOperationsMask op) override; 117 void SetAudioMuted(bool mute) override; 118 bool IsAudioMuted() override; 119 void SetAccessibilityState(cef_state_t accessibility_state) override; 120 void SetAutoResizeEnabled(bool enabled, 121 const CefSize& min_size, 122 const CefSize& max_size) override; 123 CefRefPtr<CefExtension> GetExtension() override; 124 bool IsBackgroundHost() override; 125 126 protected: 127 bool Navigate(const content::OpenURLParams& params) override; 128 129 private: 130 friend class ChromeBrowserDelegate; 131 132 ChromeBrowserHostImpl( 133 const CefBrowserSettings& settings, 134 CefRefPtr<CefClient> client, 135 std::unique_ptr<CefBrowserPlatformDelegate> platform_delegate, 136 scoped_refptr<CefBrowserInfo> browser_info, 137 CefRefPtr<CefRequestContextImpl> request_context); 138 139 // Create a new Browser without initializing the WebContents. 140 static Browser* CreateBrowser(const CefBrowserCreateParams& params); 141 142 // Called from ChromeBrowserDelegate::CreateBrowser when this object is first 143 // created. Must be called on the UI thread. 144 void Attach(content::WebContents* web_contents, 145 CefRefPtr<ChromeBrowserHostImpl> opener); 146 147 // Called from ChromeBrowserDelegate::AddNewContents to take ownership of a 148 // popup WebContents. 149 void AddNewContents(std::unique_ptr<content::WebContents> contents); 150 151 // Called when this object changes Browser ownership (e.g. initially created, 152 // dragging between windows, etc). The old Browser, if any, will be cleared 153 // before the new Browser is added. Must be called on the UI thread. 154 void SetBrowser(Browser* browser); 155 156 // CefBrowserHostBase methods: 157 void WindowDestroyed() override; 158 void DestroyBrowser() override; 159 160 void DoCloseBrowser(bool force_close); 161 162 // Returns the current tab index for the associated WebContents, or 163 // TabStripModel::kNoTab if not found. 164 int GetCurrentTabIndex() const; 165 166 Browser* browser_ = nullptr; 167 }; 168 169 #endif // CEF_LIBCEF_BROWSER_CHROME_CHROME_BROWSER_HOST_IMPL_H_ 170