[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> |
wittman | 832321f5 | 2016-10-10 18:18:38 | [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" |
avi | e4d7b6f | 2015-12-26 00:59:18 | [diff] [blame] | 12 | #include "build/build_config.h" |
[email protected] | ae9e12a | 2012-07-31 17:48:12 | [diff] [blame] | 13 | #include "chrome/browser/chrome_browser_field_trials.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" |
Mike Wittman | 4bb6af2 | 2018-03-01 00:22:06 | [diff] [blame] | 18 | #include "chrome/common/thread_profiler.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 | |
[email protected] | c1702d71 | 2011-09-08 19:41:43 | [diff] [blame] | 22 | class BrowserProcessImpl; |
[email protected] | 50462bf0 | 2011-11-21 19:13:31 | [diff] [blame] | 23 | class ChromeBrowserMainExtraParts; |
[email protected] | edafd4c | 2011-05-10 17:18:53 | [diff] [blame] | 24 | class FieldTrialSynchronizer; |
[email protected] | edafd4c | 2011-05-10 17:18:53 | [diff] [blame] | 25 | class PrefService; |
[email protected] | c1702d71 | 2011-09-08 19:41:43 | [diff] [blame] | 26 | class Profile; |
[email protected] | fe7c487 | 2012-05-10 20:06:03 | [diff] [blame] | 27 | class StartupBrowserCreator; |
[email protected] | 08f1c5e | 2011-12-01 01:54:34 | [diff] [blame] | 28 | class StartupTimeBomb; |
[email protected] | f967b72 | 2011-09-07 00:58:04 | [diff] [blame] | 29 | class ShutdownWatcherHelper; |
[email protected] | 4432769 | 2013-02-26 21:21:22 | [diff] [blame] | 30 | class ThreeDAPIObserver; |
juncai | 1da1203 | 2016-07-15 19:10:12 | [diff] [blame] | 31 | class WebUsbDetector; |
[email protected] | 5f988b9 | 2011-05-18 07:14:08 | [diff] [blame] | 32 | |
Scott Violet | 875789e | 2018-02-02 07:46:48 | [diff] [blame] | 33 | namespace base { |
34 | class SequencedTaskRunner; | ||||
35 | } | ||||
36 | |||||
[email protected] | cbce4724 | 2011-08-12 21:40:59 | [diff] [blame] | 37 | namespace chrome_browser { |
38 | // For use by ShowMissingLocaleMessageBox. | ||||
[email protected] | 49aa5a3 | 2014-02-03 10:00:44 | [diff] [blame] | 39 | #if defined(OS_WIN) |
[email protected] | cbce4724 | 2011-08-12 21:40:59 | [diff] [blame] | 40 | extern const char kMissingLocaleDataTitle[]; |
[email protected] | 49aa5a3 | 2014-02-03 10:00:44 | [diff] [blame] | 41 | #endif |
42 | |||||
[email protected] | 893124a2 | 2014-04-15 00:45:28 | [diff] [blame] | 43 | #if defined(OS_WIN) |
[email protected] | cbce4724 | 2011-08-12 21:40:59 | [diff] [blame] | 44 | extern const char kMissingLocaleDataMessage[]; |
[email protected] | 49aa5a3 | 2014-02-03 10:00:44 | [diff] [blame] | 45 | #endif |
[email protected] | cbce4724 | 2011-08-12 21:40:59 | [diff] [blame] | 46 | } |
47 | |||||
[email protected] | f967b72 | 2011-09-07 00:58:04 | [diff] [blame] | 48 | class ChromeBrowserMainParts : public content::BrowserMainParts { |
[email protected] | f8abf72 | 2010-07-07 19:46:24 | [diff] [blame] | 49 | public: |
Daniel Cheng | a542fca | 2014-10-21 09:51:29 | [diff] [blame] | 50 | ~ChromeBrowserMainParts() override; |
[email protected] | f8abf72 | 2010-07-07 19:46:24 | [diff] [blame] | 51 | |
[email protected] | 50462bf0 | 2011-11-21 19:13:31 | [diff] [blame] | 52 | // Add additional ChromeBrowserMainExtraParts. |
53 | virtual void AddParts(ChromeBrowserMainExtraParts* parts); | ||||
[email protected] | 069cd995 | 2011-10-01 16:34:45 | [diff] [blame] | 54 | |
[email protected] | f8abf72 | 2010-07-07 19:46:24 | [diff] [blame] | 55 | protected: |
Scott Violet | 875789e | 2018-02-02 07:46:48 | [diff] [blame] | 56 | #if !defined(OS_ANDROID) |
57 | class DeferringTaskRunner; | ||||
58 | #endif | ||||
59 | |||||
[email protected] | 4573fbd | 2011-10-31 20:25:18 | [diff] [blame] | 60 | explicit ChromeBrowserMainParts( |
61 | const content::MainFunctionParams& parameters); | ||||
[email protected] | f8abf72 | 2010-07-07 19:46:24 | [diff] [blame] | 62 | |
[email protected] | 50462bf0 | 2011-11-21 19:13:31 | [diff] [blame] | 63 | // content::BrowserMainParts overrides. |
Scott Violet | 9a37562 | 2018-02-20 18:04:51 | [diff] [blame] | 64 | bool ShouldContentCreateFeatureList() override; |
[email protected] | 97ef83f | 2011-12-19 19:26:36 | [diff] [blame] | 65 | // These are called in-order by content::BrowserMainLoop. |
66 | // Each stage calls the same stages in any ChromeBrowserMainExtraParts added | ||||
67 | // with AddParts() from ChromeContentBrowserClient::CreateBrowserMainParts. | ||||
Scott Violet | 9068b4df | 2018-01-12 16:44:21 | [diff] [blame] | 68 | int PreEarlyInitialization() override; |
Daniel Cheng | a542fca | 2014-10-21 09:51:29 | [diff] [blame] | 69 | void PostEarlyInitialization() override; |
70 | void ToolkitInitialized() override; | ||||
71 | void PreMainMessageLoopStart() override; | ||||
72 | void PostMainMessageLoopStart() override; | ||||
73 | int PreCreateThreads() override; | ||||
Xi Cheng | 8c965371 | 2018-01-24 06:07:27 | [diff] [blame] | 74 | void PostCreateThreads() override; |
ben | d32292b | 2016-10-07 00:21:58 | [diff] [blame] | 75 | void ServiceManagerConnectionStarted( |
76 | content::ServiceManagerConnection* connection) override; | ||||
Daniel Cheng | a542fca | 2014-10-21 09:51:29 | [diff] [blame] | 77 | void PreMainMessageLoopRun() override; |
78 | bool MainMessageLoopRun(int* result_code) override; | ||||
79 | void PostMainMessageLoopRun() override; | ||||
bcwhite | da097d3 | 2017-01-12 23:55:03 | [diff] [blame] | 80 | void PreShutdown() override; |
Daniel Cheng | a542fca | 2014-10-21 09:51:29 | [diff] [blame] | 81 | void PostDestroyThreads() override; |
[email protected] | b48c918 | 2011-10-26 18:03:30 | [diff] [blame] | 82 | |
[email protected] | 97ef83f | 2011-12-19 19:26:36 | [diff] [blame] | 83 | // Additional stages for ChromeBrowserMainExtraParts. These stages are called |
[email protected] | 278a34d | 2012-06-07 00:34:58 | [diff] [blame] | 84 | // in order from PreMainMessageLoopRun(). See implementation for details. |
[email protected] | 97ef83f | 2011-12-19 19:26:36 | [diff] [blame] | 85 | virtual void PreProfileInit(); |
86 | virtual void PostProfileInit(); | ||||
87 | virtual void PreBrowserStart(); | ||||
88 | virtual void PostBrowserStart(); | ||||
89 | |||||
[email protected] | b48c918 | 2011-10-26 18:03:30 | [diff] [blame] | 90 | // Displays a warning message that we can't find any locale data files. |
91 | virtual void ShowMissingLocaleMessageBox() = 0; | ||||
[email protected] | f8abf72 | 2010-07-07 19:46:24 | [diff] [blame] | 92 | |
[email protected] | 50462bf0 | 2011-11-21 19:13:31 | [diff] [blame] | 93 | const content::MainFunctionParams& parameters() const { |
94 | return parameters_; | ||||
95 | } | ||||
[email protected] | 2f3b1cc | 2014-03-17 23:07:15 | [diff] [blame] | 96 | const base::CommandLine& parsed_command_line() const { |
[email protected] | 50462bf0 | 2011-11-21 19:13:31 | [diff] [blame] | 97 | return parsed_command_line_; |
98 | } | ||||
mlerman | a312cec | 2015-01-28 21:13:33 | [diff] [blame] | 99 | const base::FilePath& user_data_dir() const { |
100 | return user_data_dir_; | ||||
101 | } | ||||
[email protected] | 50462bf0 | 2011-11-21 19:13:31 | [diff] [blame] | 102 | |
103 | Profile* profile() { return profile_; } | ||||
104 | |||||
[email protected] | 1fec6435 | 2010-07-27 13:55:21 | [diff] [blame] | 105 | private: |
Scott Violet | 3d27f7f | 2018-04-03 17:13:22 | [diff] [blame^] | 106 | friend class ChromeBrowserMainPartsTestApi; |
107 | |||||
robliao | ce2e7d3 | 2016-09-19 18:29:00 | [diff] [blame] | 108 | // Sets up the field trials and related initialization. Call only after |
109 | // about:flags have been converted to switches. | ||||
110 | void SetupFieldTrials(); | ||||
[email protected] | 069cd995 | 2011-10-01 16:34:45 | [diff] [blame] | 111 | |
robliao | ce2e7d3 | 2016-09-19 18:29:00 | [diff] [blame] | 112 | // Constructs the metrics service and initializes metrics recording. |
113 | void SetupMetrics(); | ||||
[email protected] | edafd4c | 2011-05-10 17:18:53 | [diff] [blame] | 114 | |
[email protected] | 5281db1d | 2012-01-11 22:51:54 | [diff] [blame] | 115 | // Starts recording of metrics. This can only be called after we have a file |
116 | // thread. | ||||
117 | void StartMetricsRecording(); | ||||
118 | |||||
[email protected] | e8b6ca0 | 2013-07-10 18:00:51 | [diff] [blame] | 119 | // Record time from process startup to present time in an UMA histogram. |
[email protected] | 69e9069 | 2013-07-31 02:13:29 | [diff] [blame] | 120 | void RecordBrowserStartupTime(); |
[email protected] | e8b6ca0 | 2013-07-10 18:00:51 | [diff] [blame] | 121 | |
iclelland | 27f4bcb | 2016-06-22 16:41:03 | [diff] [blame] | 122 | // Reads origin trial policy data from local state and configures command line |
123 | // for child processes. | ||||
Scott Violet | 4fe34bfb | 2018-01-10 22:20:29 | [diff] [blame] | 124 | void SetupOriginTrialsCommandLine(PrefService* local_state); |
iclelland | 27f4bcb | 2016-06-22 16:41:03 | [diff] [blame] | 125 | |
Scott Violet | 875789e | 2018-02-02 07:46:48 | [diff] [blame] | 126 | // Calling during PreEarlyInitialization() to load local state. Return value |
127 | // is an exit status, RESULT_CODE_NORMAL_EXIT indicates success. | ||||
128 | // If the return value is RESULT_CODE_MISSING_DATA, then | ||||
129 | // |failed_to_load_resource_bundle| indicates if the ResourceBundle couldn't | ||||
130 | // be loaded. | ||||
131 | int LoadLocalState(base::SequencedTaskRunner* local_state_task_runner, | ||||
132 | bool* failed_to_load_resource_bundle); | ||||
133 | |||||
134 | // Applies any preferences (to local state) needed for first run. This is | ||||
135 | // always called and early outs if not first-run. Return value is an exit | ||||
136 | // status, RESULT_CODE_NORMAL_EXIT indicates success. | ||||
137 | int ApplyFirstRunPrefs(); | ||||
138 | |||||
[email protected] | b48c918 | 2011-10-26 18:03:30 | [diff] [blame] | 139 | // Methods for Main Message Loop ------------------------------------------- |
140 | |||||
[email protected] | 2e5b60a2 | 2011-11-28 15:56:41 | [diff] [blame] | 141 | int PreCreateThreadsImpl(); |
[email protected] | b48c918 | 2011-10-26 18:03:30 | [diff] [blame] | 142 | int PreMainMessageLoopRunImpl(); |
143 | |||||
[email protected] | f8abf72 | 2010-07-07 19:46:24 | [diff] [blame] | 144 | // Members initialized on construction --------------------------------------- |
145 | |||||
[email protected] | 57624ab | 2013-08-01 16:01:51 | [diff] [blame] | 146 | const content::MainFunctionParams parameters_; |
Scott Violet | 875789e | 2018-02-02 07:46:48 | [diff] [blame] | 147 | // TODO(sky): remove this. This class (and related calls), may mutate the |
148 | // CommandLine, so it is misleading keeping a const ref here. | ||||
[email protected] | 2f3b1cc | 2014-03-17 23:07:15 | [diff] [blame] | 149 | const base::CommandLine& parsed_command_line_; |
[email protected] | b48c918 | 2011-10-26 18:03:30 | [diff] [blame] | 150 | int result_code_; |
151 | |||||
[email protected] | 08f1c5e | 2011-12-01 01:54:34 | [diff] [blame] | 152 | // Create StartupTimeBomb object for watching jank during startup. |
dcheng | 4af4858 | 2016-04-19 00:29:35 | [diff] [blame] | 153 | std::unique_ptr<StartupTimeBomb> startup_watcher_; |
[email protected] | 08f1c5e | 2011-12-01 01:54:34 | [diff] [blame] | 154 | |
[email protected] | f967b72 | 2011-09-07 00:58:04 | [diff] [blame] | 155 | // Create ShutdownWatcherHelper object for watching jank during shutdown. |
156 | // Please keep |shutdown_watcher| as the first object constructed, and hence | ||||
157 | // it is destroyed last. | ||||
dcheng | 4af4858 | 2016-04-19 00:29:35 | [diff] [blame] | 158 | std::unique_ptr<ShutdownWatcherHelper> shutdown_watcher_; |
[email protected] | f8abf72 | 2010-07-07 19:46:24 | [diff] [blame] | 159 | |
[email protected] | ae9e12a | 2012-07-31 17:48:12 | [diff] [blame] | 160 | ChromeBrowserFieldTrials browser_field_trials_; |
161 | |||||
thestig | 0c55ee3 | 2017-06-20 22:17:51 | [diff] [blame] | 162 | #if !defined(OS_ANDROID) |
juncai | 1da1203 | 2016-07-15 19:10:12 | [diff] [blame] | 163 | std::unique_ptr<WebUsbDetector> web_usb_detector_; |
dhnishi | 3cf1d2aa | 2014-08-26 22:25:07 | [diff] [blame] | 164 | #endif |
165 | |||||
[email protected] | 50462bf0 | 2011-11-21 19:13:31 | [diff] [blame] | 166 | // Vector of additional ChromeBrowserMainExtraParts. |
167 | // Parts are deleted in the inverse order they are added. | ||||
168 | std::vector<ChromeBrowserMainExtraParts*> chrome_extra_parts_; | ||||
169 | |||||
Xi Cheng | 8c965371 | 2018-01-24 06:07:27 | [diff] [blame] | 170 | // A profiler that periodically samples stack traces on the UI thread. |
Mike Wittman | 4bb6af2 | 2018-03-01 00:22:06 | [diff] [blame] | 171 | std::unique_ptr<ThreadProfiler> ui_thread_profiler_; |
Jamie Madill | aba2fd20 | 2017-10-11 14:20:37 | [diff] [blame] | 172 | |
Scott Violet | 3d27f7f | 2018-04-03 17:13:22 | [diff] [blame^] | 173 | // Whether PerformPreMainMessageLoopStartup() is called on VariationsService. |
174 | // Initialized to true if |MainFunctionParams::ui_task| is null (meaning not | ||||
175 | // running browser_tests), but may be forced to true for tests. | ||||
176 | bool should_call_pre_main_loop_start_startup_on_variations_service_; | ||||
177 | |||||
[email protected] | 0f247aea8 | 2011-09-03 03:05:51 | [diff] [blame] | 178 | // Members initialized after / released before main_message_loop_ ------------ |
179 | |||||
dcheng | 4af4858 | 2016-04-19 00:29:35 | [diff] [blame] | 180 | std::unique_ptr<BrowserProcessImpl> browser_process_; |
pkasting | 28ae579 | 2016-05-20 21:39:14 | [diff] [blame] | 181 | |
bshe | 0d94807 | 2016-01-05 19:06:20 | [diff] [blame] | 182 | #if !defined(OS_ANDROID) |
[email protected] | 81054f81 | 2012-08-30 00:47:09 | [diff] [blame] | 183 | // Browser creation happens on the Java side in Android. |
dcheng | 4af4858 | 2016-04-19 00:29:35 | [diff] [blame] | 184 | std::unique_ptr<StartupBrowserCreator> browser_creator_; |
[email protected] | 81054f81 | 2012-08-30 00:47:09 | [diff] [blame] | 185 | |
186 | // Android doesn't support multiple browser processes, so it doesn't implement | ||||
187 | // ProcessSingleton. | ||||
dcheng | 4af4858 | 2016-04-19 00:29:35 | [diff] [blame] | 188 | std::unique_ptr<ChromeProcessSingleton> process_singleton_; |
[email protected] | d29f4e31 | 2013-08-07 05:28:31 | [diff] [blame] | 189 | |
190 | // Android's first run is done in Java instead of native. | ||||
dcheng | 4af4858 | 2016-04-19 00:29:35 | [diff] [blame] | 191 | std::unique_ptr<first_run::MasterPrefs> master_prefs_; |
pkasting | 28ae579 | 2016-05-20 21:39:14 | [diff] [blame] | 192 | |
193 | ProcessSingleton::NotifyResult notify_result_ = | ||||
194 | ProcessSingleton::PROCESS_NONE; | ||||
195 | |||||
196 | // Members needed across shutdown methods. | ||||
197 | bool restart_last_session_ = false; | ||||
bshe | 0d94807 | 2016-01-05 19:06:20 | [diff] [blame] | 198 | #endif |
pkasting | 28ae579 | 2016-05-20 21:39:14 | [diff] [blame] | 199 | |
[email protected] | c1702d71 | 2011-09-08 19:41:43 | [diff] [blame] | 200 | Profile* profile_; |
201 | bool run_message_loop_; | ||||
dcheng | 4af4858 | 2016-04-19 00:29:35 | [diff] [blame] | 202 | std::unique_ptr<ThreeDAPIObserver> three_d_observer_; |
[email protected] | 0f247aea8 | 2011-09-03 03:05:51 | [diff] [blame] | 203 | |
robliao | ce2e7d3 | 2016-09-19 18:29:00 | [diff] [blame] | 204 | // Initialized in |SetupFieldTrials()|. |
[email protected] | edafd4c | 2011-05-10 17:18:53 | [diff] [blame] | 205 | scoped_refptr<FieldTrialSynchronizer> field_trial_synchronizer_; |
206 | |||||
[email protected] | 650b2d5 | 2013-02-10 03:41:45 | [diff] [blame] | 207 | base::FilePath user_data_dir_; |
[email protected] | 2e5b60a2 | 2011-11-28 15:56:41 | [diff] [blame] | 208 | |
Scott Violet | 875789e | 2018-02-02 07:46:48 | [diff] [blame] | 209 | #if !defined(OS_ANDROID) |
210 | // This TaskRunner is created and the constructor and destroyed in | ||||
211 | // PreCreateThreadsImpl(). It's used to queue any tasks scheduled before the | ||||
212 | // real task scheduler has been created. | ||||
213 | scoped_refptr<DeferringTaskRunner> initial_task_runner_; | ||||
214 | #endif | ||||
215 | |||||
[email protected] | f967b72 | 2011-09-07 00:58:04 | [diff] [blame] | 216 | DISALLOW_COPY_AND_ASSIGN(ChromeBrowserMainParts); |
[email protected] | f8abf72 | 2010-07-07 19:46:24 | [diff] [blame] | 217 | }; |
218 | |||||
[email protected] | e6b5bc2 | 2011-09-08 22:01:56 | [diff] [blame] | 219 | #endif // CHROME_BROWSER_CHROME_BROWSER_MAIN_H_ |