[M120 Migration]Fix for crash during chrome exit
[platform/framework/web/chromium-efl.git] / chrome / browser / chrome_browser_main_linux.cc
1 // Copyright 2012 The Chromium Authors
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 "chrome/browser/chrome_browser_main_linux.h"
6
7 #include <memory>
8 #include <string>
9 #include <utility>
10
11 #include "base/command_line.h"
12 #include "base/feature_list.h"
13 #include "base/files/file_path.h"
14 #include "base/functional/bind.h"
15 #include "base/task/single_thread_task_runner.h"
16 #include "base/task/thread_pool.h"
17 #include "build/build_config.h"
18 #include "build/chromeos_buildflags.h"
19 #include "chrome/browser/browser_process.h"
20 #include "chrome/common/chrome_features.h"
21 #include "chrome/grit/branded_strings.h"
22 #include "content/public/browser/browser_thread.h"
23 #include "device/bluetooth/dbus/bluez_dbus_manager.h"
24 #include "device/bluetooth/dbus/bluez_dbus_thread_manager.h"
25 #include "ui/base/l10n/l10n_util.h"
26
27 #if BUILDFLAG(IS_CHROMEOS)
28 #include "chrome/browser/chromeos/tast_support/stack_sampling_recorder.h"
29 #include "chrome/installer/util/google_update_settings.h"
30 #endif
31
32 #if BUILDFLAG(IS_CHROMEOS_LACROS)
33 #include "chromeos/lacros/dbus/lacros_dbus_thread_manager.h"
34 #endif
35
36 #if defined(USE_DBUS) && !BUILDFLAG(IS_CHROMEOS)
37 #include "chrome/browser/dbus_memory_pressure_evaluator_linux.h"
38 #endif
39
40 #if !BUILDFLAG(IS_CHROMEOS_ASH)
41 #include "base/linux_util.h"
42 #include "chrome/common/chrome_paths_internal.h"
43 #include "chrome/common/chrome_switches.h"
44 #include "components/os_crypt/sync/key_storage_config_linux.h"
45 #include "components/os_crypt/sync/os_crypt.h"
46 #endif
47
48 ChromeBrowserMainPartsLinux::ChromeBrowserMainPartsLinux(
49     bool is_integration_test,
50     StartupData* startup_data)
51     : ChromeBrowserMainPartsPosix(is_integration_test, startup_data) {}
52
53 ChromeBrowserMainPartsLinux::~ChromeBrowserMainPartsLinux() {
54 }
55
56 void ChromeBrowserMainPartsLinux::PostCreateMainMessageLoop() {
57   base::CommandLine* command_line = base::CommandLine::ForCurrentProcess();
58 #if BUILDFLAG(IS_CHROMEOS)
59   if (command_line->HasSwitch(
60           chromeos::tast_support::kRecordStackSamplingDataSwitch)) {
61     stack_sampling_recorder_ =
62         base::MakeRefCounted<chromeos::tast_support::StackSamplingRecorder>();
63     stack_sampling_recorder_->Start();
64   }
65   // Don't initialize DBus here. Ash and Lacros Bluetooth DBusManager
66   // initialization depend on FeatureList, and is done elsewhere.
67 #endif  // BUILDFLAG(IS_CHROMEOS)
68
69   bluez::BluezDBusManager::Initialize(nullptr /* system_bus */);
70
71 #if !BUILDFLAG(IS_CHROMEOS) && !BUILDFLAG(IS_TIZEN)
72   // Set up crypt config. This needs to be done before anything starts the
73   // network service, as the raw encryption key needs to be shared with the
74   // network service for encrypted cookie storage.
75   // Chrome OS does not need a crypt config as its user data directories are
76   // already encrypted and none of the true encryption backends used by desktop
77   // Linux are available on Chrome OS anyway.
78   std::unique_ptr<os_crypt::Config> config =
79       std::make_unique<os_crypt::Config>();
80   // Forward to os_crypt the flag to use a specific password store.
81   config->store = command_line->GetSwitchValueASCII(switches::kPasswordStore);
82   // Forward the product name
83   config->product_name = l10n_util::GetStringUTF8(IDS_PRODUCT_NAME);
84   // OSCrypt can be disabled in a special settings file.
85   config->should_use_preference =
86       base::CommandLine::ForCurrentProcess()->HasSwitch(
87           switches::kEnableEncryptionSelection);
88   chrome::GetDefaultUserDataDirectory(&config->user_data_path);
89   OSCrypt::SetConfig(std::move(config));
90 #endif  // !BUILDFLAG(IS_CHROMEOS)
91
92   ChromeBrowserMainPartsPosix::PostCreateMainMessageLoop();
93 }
94
95 void ChromeBrowserMainPartsLinux::PreProfileInit() {
96 #if !BUILDFLAG(IS_CHROMEOS_ASH)
97   // Needs to be called after we have chrome::DIR_USER_DATA and
98   // g_browser_process.  This happens in PreCreateThreads.
99   // base::GetLinuxDistro() will initialize its value if needed.
100   base::ThreadPool::PostTask(
101       FROM_HERE, {base::MayBlock(), base::TaskPriority::BEST_EFFORT},
102       base::BindOnce(base::IgnoreResult(&base::GetLinuxDistro)));
103 #endif
104
105   ChromeBrowserMainPartsPosix::PreProfileInit();
106 }
107
108 #if defined(USE_DBUS) && !BUILDFLAG(IS_CHROMEOS)
109 void ChromeBrowserMainPartsLinux::PostBrowserStart() {
110   // static_cast is safe because this is the only implementation of
111   // MemoryPressureMonitor.
112   auto* monitor =
113       static_cast<memory_pressure::MultiSourceMemoryPressureMonitor*>(
114           base::MemoryPressureMonitor::Get());
115   if (monitor &&
116       base::FeatureList::IsEnabled(features::kLinuxLowMemoryMonitor)) {
117     monitor->SetSystemEvaluator(
118         std::make_unique<DbusMemoryPressureEvaluatorLinux>(
119             monitor->CreateVoter()));
120   }
121
122   ChromeBrowserMainPartsPosix::PostBrowserStart();
123 }
124 #endif  // defined(USE_DBUS) && !BUILDFLAG(IS_CHROMEOS)
125
126 void ChromeBrowserMainPartsLinux::PostDestroyThreads() {
127 #if BUILDFLAG(IS_CHROMEOS)
128   // No-op; per PostBrowserStart() comment, this is done elsewhere.
129 #else
130   bluez::BluezDBusManager::Shutdown();
131   bluez::BluezDBusThreadManager::Shutdown();
132 #endif  // BUILDFLAG(IS_CHROMEOS)
133
134   ChromeBrowserMainPartsPosix::PostDestroyThreads();
135 }