[email protected] | d15b7ca | 2012-01-04 01:42:58 | [diff] [blame] | 1 | // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
[email protected] | 1152b7e | 2009-09-14 03:26:03 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
[email protected] | e6b5bc2 | 2011-09-08 22:01:56 | [diff] [blame] | 5 | #ifndef CHROME_BROWSER_CHROME_BROWSER_MAIN_H_ |
| 6 | #define CHROME_BROWSER_CHROME_BROWSER_MAIN_H_ |
[email protected] | 1152b7e | 2009-09-14 03:26:03 | [diff] [blame] | 7 | |
dcheng | 4af4858 | 2016-04-19 00:29:35 | [diff] [blame] | 8 | #include <memory> |
Sebastien Marchand | 8755729 | 2019-02-28 03:46:59 | [diff] [blame] | 9 | #include <vector> |
dcheng | 4af4858 | 2016-04-19 00:29:35 | [diff] [blame] | 10 | |
avi | e4d7b6f | 2015-12-26 00:59:18 | [diff] [blame] | 11 | #include "base/macros.h" |
Hans Wennborg | 5ffd139 | 2019-10-16 11:00:02 | [diff] [blame] | 12 | #include "base/run_loop.h" |
Wez | 25fd35f | 2018-05-25 22:57:41 | [diff] [blame] | 13 | #include "build/build_config.h" |
[email protected] | 9a47c43 | 2013-04-19 20:33:55 | [diff] [blame] | 14 | #include "chrome/browser/chrome_process_singleton.h" |
[email protected] | c1702d71 | 2011-09-08 19:41:43 | [diff] [blame] | 15 | #include "chrome/browser/first_run/first_run.h" |
[email protected] | 6601392 | 2011-09-13 18:53:42 | [diff] [blame] | 16 | #include "chrome/browser/process_singleton.h" |
[email protected] | fe7c487 | 2012-05-10 20:06:03 | [diff] [blame] | 17 | #include "chrome/browser/ui/startup/startup_browser_creator.h" |
Yann Dago | 78100ea | 2020-02-04 17:46:34 | [diff] [blame] | 18 | #include "chrome/common/buildflags.h" |
[email protected] | b48c918 | 2011-10-26 18:03:30 | [diff] [blame] | 19 | #include "content/public/browser/browser_main_parts.h" |
[email protected] | 57624ab | 2013-08-01 16:01:51 | [diff] [blame] | 20 | #include "content/public/common/main_function_params.h" |
[email protected] | f8abf72 | 2010-07-07 19:46:24 | [diff] [blame] | 21 | |
Yann Dago | 78100ea | 2020-02-04 17:46:34 | [diff] [blame] | 22 | #if BUILDFLAG(ENABLE_DOWNGRADE_PROCESSING) |
Greg Thompson | fb9ba59 | 2019-08-23 08:18:51 | [diff] [blame] | 23 | #include "chrome/browser/downgrade/downgrade_manager.h" |
| 24 | #endif |
| 25 | |
[email protected] | c1702d71 | 2011-09-08 19:41:43 | [diff] [blame] | 26 | class BrowserProcessImpl; |
[email protected] | 50462bf0 | 2011-11-21 19:13:31 | [diff] [blame] | 27 | class ChromeBrowserMainExtraParts; |
Xi Han | 85079c2 | 2019-04-18 21:43:05 | [diff] [blame] | 28 | class StartupData; |
[email protected] | edafd4c | 2011-05-10 17:18:53 | [diff] [blame] | 29 | class PrefService; |
[email protected] | c1702d71 | 2011-09-08 19:41:43 | [diff] [blame] | 30 | class Profile; |
[email protected] | fe7c487 | 2012-05-10 20:06:03 | [diff] [blame] | 31 | class StartupBrowserCreator; |
[email protected] | f967b72 | 2011-09-07 00:58:04 | [diff] [blame] | 32 | class ShutdownWatcherHelper; |
juncai | 1da1203 | 2016-07-15 19:10:12 | [diff] [blame] | 33 | class WebUsbDetector; |
[email protected] | 5f988b9 | 2011-05-18 07:14:08 | [diff] [blame] | 34 | |
Hans Wennborg | 9208992ba | 2019-10-14 14:02:49 | [diff] [blame] | 35 | namespace base { |
| 36 | class RunLoop; |
| 37 | } |
| 38 | |
Sebastien Marchand | dbe43bab | 2019-02-06 18:25:51 | [diff] [blame] | 39 | namespace tracing { |
| 40 | class TraceEventSystemStatsMonitor; |
| 41 | } |
| 42 | |
Sebastien Marchand | 8755729 | 2019-02-28 03:46:59 | [diff] [blame] | 43 | namespace performance_monitor { |
| 44 | class SystemMonitor; |
| 45 | } |
| 46 | |
[email protected] | f967b72 | 2011-09-07 00:58:04 | [diff] [blame] | 47 | class ChromeBrowserMainParts : public content::BrowserMainParts { |
[email protected] | f8abf72 | 2010-07-07 19:46:24 | [diff] [blame] | 48 | public: |
Daniel Cheng | a542fca | 2014-10-21 09:51:29 | [diff] [blame] | 49 | ~ChromeBrowserMainParts() override; |
[email protected] | f8abf72 | 2010-07-07 19:46:24 | [diff] [blame] | 50 | |
[email protected] | 50462bf0 | 2011-11-21 19:13:31 | [diff] [blame] | 51 | // Add additional ChromeBrowserMainExtraParts. |
James Cook | 42e93306 | 2020-07-06 20:56:12 | [diff] [blame] | 52 | void AddParts(std::unique_ptr<ChromeBrowserMainExtraParts> parts); |
[email protected] | 069cd995 | 2011-10-01 16:34:45 | [diff] [blame] | 53 | |
Wez | 25fd35f | 2018-05-25 22:57:41 | [diff] [blame] | 54 | #if !defined(OS_ANDROID) |
| 55 | // Returns the RunLoop that would be run by MainMessageLoopRun. This is used |
| 56 | // by InProcessBrowserTests to allow them to run until the BrowserProcess is |
| 57 | // ready for the browser to exit. |
| 58 | static std::unique_ptr<base::RunLoop> TakeRunLoopForTest(); |
| 59 | #endif |
| 60 | |
[email protected] | f8abf72 | 2010-07-07 19:46:24 | [diff] [blame] | 61 | protected: |
Xi Han | ddb1ab1 | 2018-08-27 22:18:54 | [diff] [blame] | 62 | ChromeBrowserMainParts(const content::MainFunctionParams& parameters, |
Xi Han | 85079c2 | 2019-04-18 21:43:05 | [diff] [blame] | 63 | StartupData* startup_data); |
[email protected] | f8abf72 | 2010-07-07 19:46:24 | [diff] [blame] | 64 | |
[email protected] | 50462bf0 | 2011-11-21 19:13:31 | [diff] [blame] | 65 | // content::BrowserMainParts overrides. |
[email protected] | 97ef83f | 2011-12-19 19:26:36 | [diff] [blame] | 66 | // These are called in-order by content::BrowserMainLoop. |
| 67 | // Each stage calls the same stages in any ChromeBrowserMainExtraParts added |
| 68 | // with AddParts() from ChromeContentBrowserClient::CreateBrowserMainParts. |
Scott Violet | 9068b4df | 2018-01-12 16:44:21 | [diff] [blame] | 69 | int PreEarlyInitialization() override; |
Daniel Cheng | a542fca | 2014-10-21 09:51:29 | [diff] [blame] | 70 | void PostEarlyInitialization() override; |
| 71 | void ToolkitInitialized() override; |
| 72 | void PreMainMessageLoopStart() override; |
| 73 | void PostMainMessageLoopStart() override; |
| 74 | int PreCreateThreads() override; |
Xi Cheng | 8c965371 | 2018-01-24 06:07:27 | [diff] [blame] | 75 | void PostCreateThreads() override; |
Daniel Cheng | a542fca | 2014-10-21 09:51:29 | [diff] [blame] | 76 | void PreMainMessageLoopRun() override; |
| 77 | bool MainMessageLoopRun(int* result_code) override; |
| 78 | void PostMainMessageLoopRun() override; |
| 79 | void PostDestroyThreads() override; |
[email protected] | b48c918 | 2011-10-26 18:03:30 | [diff] [blame] | 80 | |
[email protected] | 97ef83f | 2011-12-19 19:26:36 | [diff] [blame] | 81 | // Additional stages for ChromeBrowserMainExtraParts. These stages are called |
[email protected] | 278a34d | 2012-06-07 00:34:58 | [diff] [blame] | 82 | // in order from PreMainMessageLoopRun(). See implementation for details. |
[email protected] | 97ef83f | 2011-12-19 19:26:36 | [diff] [blame] | 83 | virtual void PreProfileInit(); |
| 84 | virtual void PostProfileInit(); |
| 85 | virtual void PreBrowserStart(); |
| 86 | virtual void PostBrowserStart(); |
| 87 | |
[email protected] | b48c918 | 2011-10-26 18:03:30 | [diff] [blame] | 88 | // Displays a warning message that we can't find any locale data files. |
| 89 | virtual void ShowMissingLocaleMessageBox() = 0; |
[email protected] | f8abf72 | 2010-07-07 19:46:24 | [diff] [blame] | 90 | |
[email protected] | 50462bf0 | 2011-11-21 19:13:31 | [diff] [blame] | 91 | const content::MainFunctionParams& parameters() const { |
| 92 | return parameters_; |
| 93 | } |
[email protected] | 2f3b1cc | 2014-03-17 23:07:15 | [diff] [blame] | 94 | const base::CommandLine& parsed_command_line() const { |
[email protected] | 50462bf0 | 2011-11-21 19:13:31 | [diff] [blame] | 95 | return parsed_command_line_; |
| 96 | } |
mlerman | a312cec | 2015-01-28 21:13:33 | [diff] [blame] | 97 | const base::FilePath& user_data_dir() const { |
| 98 | return user_data_dir_; |
| 99 | } |
[email protected] | 50462bf0 | 2011-11-21 19:13:31 | [diff] [blame] | 100 | |
| 101 | Profile* profile() { return profile_; } |
| 102 | |
[email protected] | 1fec6435 | 2010-07-27 13:55:21 | [diff] [blame] | 103 | private: |
Scott Violet | 3d27f7f | 2018-04-03 17:13:22 | [diff] [blame] | 104 | friend class ChromeBrowserMainPartsTestApi; |
| 105 | |
robliao | ce2e7d3 | 2016-09-19 18:29:00 | [diff] [blame] | 106 | // Constructs the metrics service and initializes metrics recording. |
| 107 | void SetupMetrics(); |
[email protected] | edafd4c | 2011-05-10 17:18:53 | [diff] [blame] | 108 | |
[email protected] | 5281db1d | 2012-01-11 22:51:54 | [diff] [blame] | 109 | // Starts recording of metrics. This can only be called after we have a file |
| 110 | // thread. |
Dan Harrington | 5b7dfc1 | 2019-08-21 18:14:30 | [diff] [blame] | 111 | static void StartMetricsRecording(); |
[email protected] | 5281db1d | 2012-01-11 22:51:54 | [diff] [blame] | 112 | |
[email protected] | e8b6ca0 | 2013-07-10 18:00:51 | [diff] [blame] | 113 | // Record time from process startup to present time in an UMA histogram. |
[email protected] | 69e9069 | 2013-07-31 02:13:29 | [diff] [blame] | 114 | void RecordBrowserStartupTime(); |
[email protected] | e8b6ca0 | 2013-07-10 18:00:51 | [diff] [blame] | 115 | |
iclelland | 27f4bcb | 2016-06-22 16:41:03 | [diff] [blame] | 116 | // Reads origin trial policy data from local state and configures command line |
| 117 | // for child processes. |
Scott Violet | 4fe34bfb | 2018-01-10 22:20:29 | [diff] [blame] | 118 | void SetupOriginTrialsCommandLine(PrefService* local_state); |
iclelland | 27f4bcb | 2016-06-22 16:41:03 | [diff] [blame] | 119 | |
Xi Han | 349162b | 2018-10-26 12:44:23 | [diff] [blame] | 120 | // Calling during PreEarlyInitialization() to complete the remaining tasks |
| 121 | // after the local state is loaded. Return value is an exit status, |
| 122 | // RESULT_CODE_NORMAL_EXIT indicates success. If the return value is |
| 123 | // RESULT_CODE_MISSING_DATA, then |failed_to_load_resource_bundle| indicates |
| 124 | // if the ResourceBundle couldn't be loaded. |
| 125 | int OnLocalStateLoaded(bool* failed_to_load_resource_bundle); |
Scott Violet | 875789e | 2018-02-02 07:46:48 | [diff] [blame] | 126 | |
Alexei Svitkine | d55a40d | 2018-11-25 19:18:18 | [diff] [blame] | 127 | // Applies any preferences (to local state) needed for first run. This is |
| 128 | // always called and early outs if not first-run. Return value is an exit |
| 129 | // status, RESULT_CODE_NORMAL_EXIT indicates success. |
| 130 | int ApplyFirstRunPrefs(); |
| 131 | |
[email protected] | b48c918 | 2011-10-26 18:03:30 | [diff] [blame] | 132 | // Methods for Main Message Loop ------------------------------------------- |
| 133 | |
[email protected] | 2e5b60a2 | 2011-11-28 15:56:41 | [diff] [blame] | 134 | int PreCreateThreadsImpl(); |
[email protected] | b48c918 | 2011-10-26 18:03:30 | [diff] [blame] | 135 | int PreMainMessageLoopRunImpl(); |
| 136 | |
[email protected] | f8abf72 | 2010-07-07 19:46:24 | [diff] [blame] | 137 | // Members initialized on construction --------------------------------------- |
| 138 | |
[email protected] | 57624ab | 2013-08-01 16:01:51 | [diff] [blame] | 139 | const content::MainFunctionParams parameters_; |
Scott Violet | 875789e | 2018-02-02 07:46:48 | [diff] [blame] | 140 | // TODO(sky): remove this. This class (and related calls), may mutate the |
| 141 | // CommandLine, so it is misleading keeping a const ref here. |
[email protected] | 2f3b1cc | 2014-03-17 23:07:15 | [diff] [blame] | 142 | const base::CommandLine& parsed_command_line_; |
[email protected] | b48c918 | 2011-10-26 18:03:30 | [diff] [blame] | 143 | int result_code_; |
| 144 | |
Dmitry Skiba | c0300e80 | 2018-05-02 17:34:04 | [diff] [blame] | 145 | #if !defined(OS_ANDROID) |
[email protected] | f967b72 | 2011-09-07 00:58:04 | [diff] [blame] | 146 | // Create ShutdownWatcherHelper object for watching jank during shutdown. |
| 147 | // Please keep |shutdown_watcher| as the first object constructed, and hence |
| 148 | // it is destroyed last. |
dcheng | 4af4858 | 2016-04-19 00:29:35 | [diff] [blame] | 149 | std::unique_ptr<ShutdownWatcherHelper> shutdown_watcher_; |
[email protected] | f8abf72 | 2010-07-07 19:46:24 | [diff] [blame] | 150 | |
juncai | 1da1203 | 2016-07-15 19:10:12 | [diff] [blame] | 151 | std::unique_ptr<WebUsbDetector> web_usb_detector_; |
Dmitry Skiba | c0300e80 | 2018-05-02 17:34:04 | [diff] [blame] | 152 | #endif // !defined(OS_ANDROID) |
dhnishi | 3cf1d2aa | 2014-08-26 22:25:07 | [diff] [blame] | 153 | |
[email protected] | 50462bf0 | 2011-11-21 19:13:31 | [diff] [blame] | 154 | // Vector of additional ChromeBrowserMainExtraParts. |
| 155 | // Parts are deleted in the inverse order they are added. |
James Cook | 42e93306 | 2020-07-06 20:56:12 | [diff] [blame] | 156 | std::vector<std::unique_ptr<ChromeBrowserMainExtraParts>> chrome_extra_parts_; |
[email protected] | 50462bf0 | 2011-11-21 19:13:31 | [diff] [blame] | 157 | |
Sebastien Marchand | 8755729 | 2019-02-28 03:46:59 | [diff] [blame] | 158 | // The system monitor instance, used by some subsystems to collect the system |
| 159 | // metrics they need. |
| 160 | std::unique_ptr<performance_monitor::SystemMonitor> system_monitor_; |
| 161 | |
Sebastien Marchand | dbe43bab | 2019-02-06 18:25:51 | [diff] [blame] | 162 | // The system stats monitor used by chrome://tracing. This doesn't do anything |
| 163 | // until tracing of the |system_stats| category is enabled. |
| 164 | std::unique_ptr<tracing::TraceEventSystemStatsMonitor> |
| 165 | trace_event_system_stats_monitor_; |
| 166 | |
Scott Violet | 3d27f7f | 2018-04-03 17:13:22 | [diff] [blame] | 167 | // Whether PerformPreMainMessageLoopStartup() is called on VariationsService. |
| 168 | // Initialized to true if |MainFunctionParams::ui_task| is null (meaning not |
| 169 | // running browser_tests), but may be forced to true for tests. |
| 170 | bool should_call_pre_main_loop_start_startup_on_variations_service_; |
| 171 | |
[email protected] | 0f247aea8 | 2011-09-03 03:05:51 | [diff] [blame] | 172 | // Members initialized after / released before main_message_loop_ ------------ |
| 173 | |
dcheng | 4af4858 | 2016-04-19 00:29:35 | [diff] [blame] | 174 | std::unique_ptr<BrowserProcessImpl> browser_process_; |
pkasting | 28ae579 | 2016-05-20 21:39:14 | [diff] [blame] | 175 | |
bshe | 0d94807 | 2016-01-05 19:06:20 | [diff] [blame] | 176 | #if !defined(OS_ANDROID) |
[email protected] | 81054f81 | 2012-08-30 00:47:09 | [diff] [blame] | 177 | // Browser creation happens on the Java side in Android. |
dcheng | 4af4858 | 2016-04-19 00:29:35 | [diff] [blame] | 178 | std::unique_ptr<StartupBrowserCreator> browser_creator_; |
[email protected] | 81054f81 | 2012-08-30 00:47:09 | [diff] [blame] | 179 | |
| 180 | // Android doesn't support multiple browser processes, so it doesn't implement |
| 181 | // ProcessSingleton. |
dcheng | 4af4858 | 2016-04-19 00:29:35 | [diff] [blame] | 182 | std::unique_ptr<ChromeProcessSingleton> process_singleton_; |
[email protected] | d29f4e31 | 2013-08-07 05:28:31 | [diff] [blame] | 183 | |
pkasting | 28ae579 | 2016-05-20 21:39:14 | [diff] [blame] | 184 | ProcessSingleton::NotifyResult notify_result_ = |
| 185 | ProcessSingleton::PROCESS_NONE; |
| 186 | |
| 187 | // Members needed across shutdown methods. |
| 188 | bool restart_last_session_ = false; |
Alexei Svitkine | 1ac2b95 | 2018-11-26 23:27:13 | [diff] [blame] | 189 | #endif // !defined(OS_ANDROID) |
| 190 | |
Yann Dago | 78100ea | 2020-02-04 17:46:34 | [diff] [blame] | 191 | #if BUILDFLAG(ENABLE_DOWNGRADE_PROCESSING) |
| 192 | downgrade::DowngradeManager downgrade_manager_; |
| 193 | #endif |
| 194 | |
Alexei Svitkine | 1ac2b95 | 2018-11-26 23:27:13 | [diff] [blame] | 195 | #if !defined(OS_ANDROID) && !defined(OS_CHROMEOS) |
| 196 | // Android's first run is done in Java instead of native. Chrome OS does not |
| 197 | // use master preferences. |
| 198 | std::unique_ptr<first_run::MasterPrefs> master_prefs_; |
bshe | 0d94807 | 2016-01-05 19:06:20 | [diff] [blame] | 199 | #endif |
pkasting | 28ae579 | 2016-05-20 21:39:14 | [diff] [blame] | 200 | |
[email protected] | c1702d71 | 2011-09-08 19:41:43 | [diff] [blame] | 201 | Profile* profile_; |
| 202 | bool run_message_loop_; |
[email protected] | 0f247aea8 | 2011-09-03 03:05:51 | [diff] [blame] | 203 | |
[email protected] | 650b2d5 | 2013-02-10 03:41:45 | [diff] [blame] | 204 | base::FilePath user_data_dir_; |
[email protected] | 2e5b60a2 | 2011-11-28 15:56:41 | [diff] [blame] | 205 | |
Xi Han | 85079c2 | 2019-04-18 21:43:05 | [diff] [blame] | 206 | StartupData* startup_data_; |
Xi Han | ddb1ab1 | 2018-08-27 22:18:54 | [diff] [blame] | 207 | |
[email protected] | f967b72 | 2011-09-07 00:58:04 | [diff] [blame] | 208 | DISALLOW_COPY_AND_ASSIGN(ChromeBrowserMainParts); |
[email protected] | f8abf72 | 2010-07-07 19:46:24 | [diff] [blame] | 209 | }; |
| 210 | |
[email protected] | e6b5bc2 | 2011-09-08 22:01:56 | [diff] [blame] | 211 | #endif // CHROME_BROWSER_CHROME_BROWSER_MAIN_H_ |