• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright (c) 2012 The Chromium 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/alloy/alloy_browser_main.h"
6 
7 #include <stdint.h>
8 
9 #include <string>
10 
11 #include "libcef/browser/browser_context.h"
12 #include "libcef/browser/browser_context_keyed_service_factories.h"
13 #include "libcef/browser/context.h"
14 #include "libcef/browser/devtools/devtools_manager_delegate.h"
15 #include "libcef/browser/extensions/extension_system_factory.h"
16 #include "libcef/browser/net/chrome_scheme_handler.h"
17 #include "libcef/browser/printing/constrained_window_views_client.h"
18 #include "libcef/browser/thread_util.h"
19 #include "libcef/common/app_manager.h"
20 #include "libcef/common/extensions/extensions_util.h"
21 #include "libcef/common/net/net_resource_provider.h"
22 
23 #include "base/bind.h"
24 #include "base/strings/string_number_conversions.h"
25 #include "base/task/post_task.h"
26 #include "base/task/thread_pool.h"
27 #include "chrome/browser/browser_process.h"
28 #include "chrome/browser/media/router/chrome_media_router_factory.h"
29 #include "chrome/browser/net/system_network_context_manager.h"
30 #include "chrome/browser/plugins/plugin_finder.h"
31 #include "chrome/common/chrome_switches.h"
32 #include "components/constrained_window/constrained_window_views.h"
33 #include "content/public/browser/gpu_data_manager.h"
34 #include "content/public/browser/network_service_instance.h"
35 #include "content/public/common/result_codes.h"
36 #include "extensions/browser/extensions_browser_client.h"
37 #include "extensions/common/constants.h"
38 #include "net/base/net_module.h"
39 #include "third_party/widevine/cdm/buildflags.h"
40 #include "ui/base/resource/resource_bundle.h"
41 
42 #if BUILDFLAG(IS_LINUX)
43 #include "ui/ozone/buildflags.h"
44 #if defined(USE_AURA) && BUILDFLAG(OZONE_PLATFORM_X11)
45 #include "ui/events/devices/x11/touch_factory_x11.h"
46 #endif
47 #endif
48 
49 #if defined(USE_AURA)
50 #include "ui/aura/env.h"
51 #include "ui/display/screen.h"
52 #include "ui/views/widget/desktop_aura/desktop_screen.h"
53 #include "ui/wm/core/wm_state.h"
54 
55 #if BUILDFLAG(IS_WIN)
56 #include "chrome/browser/chrome_browser_main_win.h"
57 #include "chrome/browser/win/parental_controls.h"
58 #endif
59 #endif  // defined(USE_AURA)
60 
61 #if defined(TOOLKIT_VIEWS)
62 #if BUILDFLAG(IS_MAC)
63 #include "chrome/browser/ui/views/chrome_layout_provider.h"
64 #include "chrome/browser/ui/views/chrome_views_delegate.h"
65 #else
66 #include "ui/views/test/desktop_test_views_delegate.h"
67 #endif
68 #endif  // defined(TOOLKIT_VIEWS)
69 
70 #if defined(USE_AURA) && BUILDFLAG(IS_LINUX)
71 #include "ui/base/ime/init/input_method_initializer.h"
72 #endif
73 
74 #if BUILDFLAG(IS_WIN) || BUILDFLAG(IS_LINUX)
75 #include "components/os_crypt/os_crypt.h"
76 #endif
77 
78 #if BUILDFLAG(IS_LINUX)
79 #include "base/path_service.h"
80 #include "chrome/common/chrome_paths.h"
81 #include "chrome/grit/chromium_strings.h"
82 #include "components/os_crypt/key_storage_config_linux.h"
83 #include "libcef/browser/printing/print_dialog_linux.h"
84 #include "ui/base/l10n/l10n_util.h"
85 #endif  // BUILDFLAG(IS_LINUX)
86 
87 #if BUILDFLAG(ENABLE_MEDIA_FOUNDATION_WIDEVINE_CDM)
88 #include "chrome/browser/component_updater/media_foundation_widevine_cdm_component_installer.h"
89 #endif
90 
91 #if BUILDFLAG(ENABLE_WIDEVINE_CDM_COMPONENT)
92 #include "chrome/browser/component_updater/widevine_cdm_component_installer.h"
93 #endif
94 
AlloyBrowserMainParts(content::MainFunctionParams parameters)95 AlloyBrowserMainParts::AlloyBrowserMainParts(
96     content::MainFunctionParams parameters)
97     : BrowserMainParts(), parameters_(std::move(parameters)) {}
98 
~AlloyBrowserMainParts()99 AlloyBrowserMainParts::~AlloyBrowserMainParts() {
100   constrained_window::SetConstrainedWindowViewsClient(nullptr);
101 }
102 
PreEarlyInitialization()103 int AlloyBrowserMainParts::PreEarlyInitialization() {
104 #if defined(USE_AURA) && BUILDFLAG(IS_LINUX)
105   // TODO(linux): Consider using a real input method or
106   // views::LinuxUI::SetInstance.
107   ui::InitializeInputMethodForTesting();
108 #endif
109 
110   return content::RESULT_CODE_NORMAL_EXIT;
111 }
112 
ToolkitInitialized()113 void AlloyBrowserMainParts::ToolkitInitialized() {
114   SetConstrainedWindowViewsClient(CreateCefConstrainedWindowViewsClient());
115 #if defined(USE_AURA)
116   CHECK(aura::Env::GetInstance());
117 
118   wm_state_.reset(new wm::WMState);
119 #endif  // defined(USE_AURA)
120 
121 #if defined(TOOLKIT_VIEWS)
122 #if BUILDFLAG(IS_MAC)
123   views_delegate_ = std::make_unique<ChromeViewsDelegate>();
124   layout_provider_ = ChromeLayoutProvider::CreateLayoutProvider();
125 #else
126   views_delegate_ = std::make_unique<views::DesktopTestViewsDelegate>();
127 #endif
128 #endif  // defined(TOOLKIT_VIEWS)
129 }
130 
PreCreateMainMessageLoop()131 void AlloyBrowserMainParts::PreCreateMainMessageLoop() {
132 #if BUILDFLAG(IS_LINUX)
133 #if defined(USE_AURA) && BUILDFLAG(OZONE_PLATFORM_X11)
134   ui::TouchFactory::SetTouchDeviceListFromCommandLine();
135 #endif
136 #endif
137 
138 #if BUILDFLAG(IS_WIN)
139   // Initialize the OSCrypt.
140   PrefService* local_state = g_browser_process->local_state();
141   DCHECK(local_state);
142   bool os_crypt_init = OSCrypt::Init(local_state);
143   DCHECK(os_crypt_init);
144 
145   // installer_util references strings that are normally compiled into
146   // setup.exe.  In Chrome, these strings are in the locale files.
147   ChromeBrowserMainPartsWin::SetupInstallerUtilStrings();
148 #endif  // BUILDFLAG(IS_WIN)
149 
150   media_router::ChromeMediaRouterFactory::DoPlatformInit();
151 }
152 
PostCreateMainMessageLoop()153 void AlloyBrowserMainParts::PostCreateMainMessageLoop() {
154 #if BUILDFLAG(IS_LINUX)
155   printing::PrintingContextLinux::SetCreatePrintDialogFunction(
156       &CefPrintDialogLinux::CreatePrintDialog);
157   printing::PrintingContextLinux::SetPdfPaperSizeFunction(
158       &CefPrintDialogLinux::GetPdfPaperSize);
159 
160   const base::CommandLine* command_line =
161       base::CommandLine::ForCurrentProcess();
162 
163   // Set up crypt config. This needs to be done before anything starts the
164   // network service, as the raw encryption key needs to be shared with the
165   // network service for encrypted cookie storage.
166   // Based on ChromeBrowserMainPartsLinux::PostCreateMainMessageLoop.
167   std::unique_ptr<os_crypt::Config> config =
168       std::make_unique<os_crypt::Config>();
169   // Forward to os_crypt the flag to use a specific password store.
170   config->store = command_line->GetSwitchValueASCII(switches::kPasswordStore);
171   // Forward the product name (defaults to "Chromium").
172   config->product_name = l10n_util::GetStringUTF8(IDS_PRODUCT_NAME);
173   // OSCrypt may target keyring, which requires calls from the main thread.
174   config->main_thread_runner = content::GetUIThreadTaskRunner({});
175   // OSCrypt can be disabled in a special settings file.
176   config->should_use_preference =
177       command_line->HasSwitch(switches::kEnableEncryptionSelection);
178   base::PathService::Get(chrome::DIR_USER_DATA, &config->user_data_path);
179   DCHECK(!config->user_data_path.empty());
180   OSCrypt::SetConfig(std::move(config));
181 #endif  // BUILDFLAG(IS_LINUX)
182 }
183 
PreCreateThreads()184 int AlloyBrowserMainParts::PreCreateThreads() {
185 #if BUILDFLAG(IS_WIN)
186   PlatformInitialize();
187 #endif
188 
189   net::NetModule::SetResourceProvider(&NetResourceProvider);
190 
191   // Initialize these objects before IO access restrictions are applied and
192   // before the IO thread is started.
193   content::GpuDataManager::GetInstance();
194   SystemNetworkContextManager::CreateInstance(g_browser_process->local_state());
195 
196   return 0;
197 }
198 
PreMainMessageLoopRun()199 int AlloyBrowserMainParts::PreMainMessageLoopRun() {
200 #if defined(USE_AURA)
201   screen_ = views::CreateDesktopScreen();
202   display::Screen::SetScreenInstance(screen_.get());
203 #endif
204 
205   if (extensions::ExtensionsEnabled()) {
206     // This should be set in ChromeBrowserProcessAlloy::Initialize.
207     DCHECK(extensions::ExtensionsBrowserClient::Get());
208     // Initialize extension global objects before creating the global
209     // BrowserContext.
210     extensions::CefExtensionSystemFactory::GetInstance();
211   }
212 
213   // Register additional KeyedService factories here. See
214   // ChromeBrowserMainExtraPartsProfiles for details.
215   cef::EnsureBrowserContextKeyedServiceFactoriesBuilt();
216 
217   background_task_runner_ = base::ThreadPool::CreateSingleThreadTaskRunner(
218       {base::TaskPriority::BEST_EFFORT,
219        base::TaskShutdownBehavior::BLOCK_SHUTDOWN, base::MayBlock()});
220   user_visible_task_runner_ = base::ThreadPool::CreateSingleThreadTaskRunner(
221       {base::TaskPriority::USER_VISIBLE,
222        base::TaskShutdownBehavior::BLOCK_SHUTDOWN, base::MayBlock()});
223   user_blocking_task_runner_ = base::ThreadPool::CreateSingleThreadTaskRunner(
224       {base::TaskPriority::USER_BLOCKING,
225        base::TaskShutdownBehavior::BLOCK_SHUTDOWN, base::MayBlock()});
226 
227   CefRequestContextSettings settings;
228   CefContext::Get()->PopulateGlobalRequestContextSettings(&settings);
229 
230   // Create the global RequestContext.
231   global_request_context_ =
232       CefRequestContextImpl::CreateGlobalRequestContext(settings);
233   auto browser_context =
234       global_request_context_->GetBrowserContext()->AsBrowserContext();
235 
236   CefDevToolsManagerDelegate::StartHttpHandler(browser_context);
237 
238 #if BUILDFLAG(IS_WIN)
239   // Windows parental controls calls can be slow, so we do an early init here
240   // that calculates this value off of the UI thread.
241   InitializeWinParentalControls();
242 #endif
243 
244   // Triggers initialization of the singleton instance on UI thread.
245   PluginFinder::GetInstance()->Init();
246 
247   scheme::RegisterWebUIControllerFactory();
248 
249 #if BUILDFLAG(ENABLE_MEDIA_FOUNDATION_WIDEVINE_CDM) || \
250     BUILDFLAG(ENABLE_WIDEVINE_CDM_COMPONENT)
251   const base::CommandLine* command_line =
252       base::CommandLine::ForCurrentProcess();
253   if (!command_line->HasSwitch(switches::kDisableComponentUpdate)) {
254     auto* const cus = g_browser_process->component_updater();
255 
256 #if BUILDFLAG(ENABLE_MEDIA_FOUNDATION_WIDEVINE_CDM)
257     RegisterMediaFoundationWidevineCdmComponent(cus);
258 #endif
259 
260 #if BUILDFLAG(ENABLE_WIDEVINE_CDM_COMPONENT)
261     RegisterWidevineCdmComponent(cus);
262 #endif
263   }
264 #endif
265 
266   return content::RESULT_CODE_NORMAL_EXIT;
267 }
268 
PostMainMessageLoopRun()269 void AlloyBrowserMainParts::PostMainMessageLoopRun() {
270   // NOTE: Destroy objects in reverse order of creation.
271   CefDevToolsManagerDelegate::StopHttpHandler();
272 
273   // There should be no additional references to the global CefRequestContext
274   // during shutdown. Did you forget to release a CefBrowser reference?
275   DCHECK(global_request_context_->HasOneRef());
276   global_request_context_ = nullptr;
277 }
278 
PostDestroyThreads()279 void AlloyBrowserMainParts::PostDestroyThreads() {
280 #if defined(TOOLKIT_VIEWS)
281   views_delegate_.reset();
282 #if BUILDFLAG(IS_MAC)
283   layout_provider_.reset();
284 #endif
285 #endif  // defined(TOOLKIT_VIEWS)
286 }
287