1 // Copyright 2015 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 #include "libcef/browser/browser_platform_delegate.h"
6
7 #include <utility>
8
9 #include "libcef/browser/context.h"
10
11 #include "base/memory/ptr_util.h"
12 #include "build/build_config.h"
13
14 #include "libcef/browser/browser_host_base.h"
15 #include "libcef/browser/chrome/browser_platform_delegate_chrome.h"
16 #include "libcef/browser/extensions/browser_platform_delegate_background.h"
17 #include "libcef/features/runtime_checks.h"
18
19 #if BUILDFLAG(IS_WIN)
20 #include "libcef/browser/native/browser_platform_delegate_native_win.h"
21 #include "libcef/browser/osr/browser_platform_delegate_osr_win.h"
22 #elif BUILDFLAG(IS_MAC)
23 #include "libcef/browser/native/browser_platform_delegate_native_mac.h"
24 #include "libcef/browser/osr/browser_platform_delegate_osr_mac.h"
25 #elif BUILDFLAG(IS_LINUX)
26 #include "libcef/browser/native/browser_platform_delegate_native_linux.h"
27 #include "libcef/browser/osr/browser_platform_delegate_osr_linux.h"
28 #else
29 #error A delegate implementation is not available for your platform.
30 #endif
31
32 #if defined(TOOLKIT_VIEWS)
33 #include "libcef/browser/chrome/views/browser_platform_delegate_chrome_views.h"
34 #include "libcef/browser/views/browser_platform_delegate_views.h"
35 #endif
36
37 namespace {
38
CreateNativeDelegate(const CefWindowInfo & window_info,SkColor background_color)39 std::unique_ptr<CefBrowserPlatformDelegateNative> CreateNativeDelegate(
40 const CefWindowInfo& window_info,
41 SkColor background_color) {
42 #if BUILDFLAG(IS_WIN)
43 return std::make_unique<CefBrowserPlatformDelegateNativeWin>(
44 window_info, background_color);
45 #elif BUILDFLAG(IS_MAC)
46 return std::make_unique<CefBrowserPlatformDelegateNativeMac>(
47 window_info, background_color);
48 #elif BUILDFLAG(IS_LINUX)
49 return std::make_unique<CefBrowserPlatformDelegateNativeLinux>(
50 window_info, background_color);
51 #endif
52 }
53
CreateOSRDelegate(std::unique_ptr<CefBrowserPlatformDelegateNative> native_delegate,bool use_shared_texture,bool use_external_begin_frame)54 std::unique_ptr<CefBrowserPlatformDelegateOsr> CreateOSRDelegate(
55 std::unique_ptr<CefBrowserPlatformDelegateNative> native_delegate,
56 bool use_shared_texture,
57 bool use_external_begin_frame) {
58 #if BUILDFLAG(IS_WIN)
59 return std::make_unique<CefBrowserPlatformDelegateOsrWin>(
60 std::move(native_delegate), use_shared_texture, use_external_begin_frame);
61 #elif BUILDFLAG(IS_MAC)
62 return std::make_unique<CefBrowserPlatformDelegateOsrMac>(
63 std::move(native_delegate));
64 #elif BUILDFLAG(IS_LINUX)
65 return std::make_unique<CefBrowserPlatformDelegateOsrLinux>(
66 std::move(native_delegate), use_external_begin_frame);
67 #endif
68 }
69
70 } // namespace
71
72 // static
Create(const CefBrowserCreateParams & create_params)73 std::unique_ptr<CefBrowserPlatformDelegate> CefBrowserPlatformDelegate::Create(
74 const CefBrowserCreateParams& create_params) {
75 const bool is_windowless =
76 create_params.window_info &&
77 create_params.window_info->windowless_rendering_enabled &&
78 create_params.client && create_params.client->GetRenderHandler().get();
79 const SkColor background_color = CefContext::Get()->GetBackgroundColor(
80 &create_params.settings, is_windowless ? STATE_ENABLED : STATE_DISABLED);
81
82 if (cef::IsChromeRuntimeEnabled()) {
83 // CefWindowInfo is not used in this case.
84 std::unique_ptr<CefBrowserPlatformDelegateNative> native_delegate =
85 CreateNativeDelegate(CefWindowInfo(), background_color);
86 #if defined(TOOLKIT_VIEWS)
87 if (create_params.browser_view ||
88 create_params.popup_with_views_hosted_opener) {
89 return std::make_unique<CefBrowserPlatformDelegateChromeViews>(
90 std::move(native_delegate),
91 static_cast<CefBrowserViewImpl*>(create_params.browser_view.get()));
92 }
93 #endif
94 return std::make_unique<CefBrowserPlatformDelegateChrome>(
95 std::move(native_delegate));
96 }
97
98 if (create_params.window_info) {
99 std::unique_ptr<CefBrowserPlatformDelegateNative> native_delegate =
100 CreateNativeDelegate(*create_params.window_info.get(),
101 background_color);
102 if (is_windowless) {
103 REQUIRE_ALLOY_RUNTIME();
104
105 const bool use_shared_texture =
106 create_params.window_info &&
107 create_params.window_info->shared_texture_enabled;
108
109 const bool use_external_begin_frame =
110 create_params.window_info &&
111 create_params.window_info->external_begin_frame_enabled;
112
113 return CreateOSRDelegate(std::move(native_delegate), use_shared_texture,
114 use_external_begin_frame);
115 }
116 return std::move(native_delegate);
117 } else if (create_params.extension_host_type ==
118 extensions::mojom::ViewType::kExtensionBackgroundPage) {
119 // Creating a background extension host without a window.
120 std::unique_ptr<CefBrowserPlatformDelegateNative> native_delegate =
121 CreateNativeDelegate(CefWindowInfo(), background_color);
122 return std::make_unique<CefBrowserPlatformDelegateBackground>(
123 std::move(native_delegate));
124 }
125 #if defined(TOOLKIT_VIEWS)
126 else {
127 // CefWindowInfo is not used in this case.
128 std::unique_ptr<CefBrowserPlatformDelegateNative> native_delegate =
129 CreateNativeDelegate(CefWindowInfo(), background_color);
130 return std::make_unique<CefBrowserPlatformDelegateViews>(
131 std::move(native_delegate),
132 static_cast<CefBrowserViewImpl*>(create_params.browser_view.get()));
133 }
134 #endif // defined(TOOLKIT_VIEWS)
135
136 NOTREACHED();
137 return nullptr;
138 }
139