1 // Copyright 2021 The Chromium Embedded Framework Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be found
3 // in the LICENSE file.
4
5 #include "libcef/browser/chrome/views/chrome_browser_view.h"
6
7 #include "libcef/browser/chrome/views/chrome_browser_frame.h"
8 #include "libcef/browser/views/browser_view_impl.h"
9
ChromeBrowserView(CefBrowserViewDelegate * cef_delegate,Delegate * browser_view_delegate)10 ChromeBrowserView::ChromeBrowserView(CefBrowserViewDelegate* cef_delegate,
11 Delegate* browser_view_delegate)
12 : ParentClass(cef_delegate), browser_view_delegate_(browser_view_delegate) {
13 DCHECK(browser_view_delegate_);
14 }
15
InitBrowser(std::unique_ptr<Browser> browser,CefRefPtr<CefBrowserView> browser_view)16 void ChromeBrowserView::InitBrowser(std::unique_ptr<Browser> browser,
17 CefRefPtr<CefBrowserView> browser_view) {
18 DCHECK(!browser_);
19 DCHECK(!web_view_);
20
21 browser_ = browser.get();
22 DCHECK(browser_);
23
24 // Initialize the BrowserFrame and BrowserView.
25 auto chrome_widget = static_cast<ChromeBrowserFrame*>(GetWidget());
26 chrome_widget->Init(this, std::move(browser));
27
28 // Retrieve the views::WebView that was created by the above initializations.
29 auto view_impl = static_cast<CefBrowserViewImpl*>(browser_view.get());
30 web_view_ = view_impl->web_view();
31 DCHECK(web_view_);
32
33 ParentClass::AddedToWidget();
34 }
35
Destroyed()36 void ChromeBrowserView::Destroyed() {
37 DCHECK(!destroyed_);
38 destroyed_ = true;
39 browser_ = nullptr;
40 web_view_ = nullptr;
41 }
42
ViewHierarchyChanged(const views::ViewHierarchyChangedDetails & details)43 void ChromeBrowserView::ViewHierarchyChanged(
44 const views::ViewHierarchyChangedDetails& details) {
45 ParentClass::ViewHierarchyChanged(details);
46 if (details.is_add && details.child == this) {
47 gfx::Size size = GetPreferredSize();
48 if (size.IsEmpty()) {
49 // No size was provided for this View. Size it to the parent by default
50 // or, depending on the Layout, the browser may be initially 0x0 size and
51 // will not display until the parent is next resized (resulting in a call
52 // to WebView::OnBoundsChanged). For example, this can happen when adding
53 // this View to a CefWindow with FillLayout and then calling
54 // CefWindow::Show() without first resizing the CefWindow.
55 size = details.parent->GetPreferredSize();
56 if (!size.IsEmpty())
57 SetSize(size);
58 }
59 }
60 }
61
AddedToWidget()62 void ChromeBrowserView::AddedToWidget() {
63 // Results in a call to InitBrowser which calls ParentClass::AddedToWidget.
64 browser_view_delegate_->OnBrowserViewAdded();
65 }
66
OnBoundsChanged(const gfx::Rect & previous_bounds)67 void ChromeBrowserView::OnBoundsChanged(const gfx::Rect& previous_bounds) {
68 ParentClass::OnBoundsChanged(previous_bounds);
69 browser_view_delegate_->OnBoundsChanged();
70 }
71
OverrideCreateToolbar(Browser * browser,BrowserView * browser_view)72 ToolbarView* ChromeBrowserView::OverrideCreateToolbar(
73 Browser* browser,
74 BrowserView* browser_view) {
75 if (cef_delegate()) {
76 auto toolbar_type = cef_delegate()->GetChromeToolbarType();
77 absl::optional<ToolbarView::DisplayMode> display_mode;
78 switch (toolbar_type) {
79 case CEF_CTT_NORMAL:
80 display_mode = ToolbarView::DisplayMode::NORMAL;
81 break;
82 case CEF_CTT_LOCATION:
83 display_mode = ToolbarView::DisplayMode::LOCATION;
84 break;
85 default:
86 break;
87 }
88 if (display_mode) {
89 cef_toolbar_ = CefToolbarViewImpl::Create(nullptr, browser, browser_view,
90 display_mode);
91 // Ownership will be taken by BrowserView.
92 view_util::PassOwnership(cef_toolbar_).release();
93 return cef_toolbar_->root_view();
94 }
95 }
96
97 return nullptr;
98 }
99