blob: 23cbce2b58f939d4eefaca72e1ab5fa7504d986b [file] [log] [blame]
[email protected]d15b7ca2012-01-04 01:42:581// Copyright (c) 2012 The Chromium Authors. All rights reserved.
[email protected]1152b7e2009-09-14 03:26:032// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
[email protected]e6b5bc22011-09-08 22:01:565#ifndef CHROME_BROWSER_CHROME_BROWSER_MAIN_H_
6#define CHROME_BROWSER_CHROME_BROWSER_MAIN_H_
[email protected]1152b7e2009-09-14 03:26:037
dcheng4af48582016-04-19 00:29:358#include <memory>
Sebastien Marchand87557292019-02-28 03:46:599#include <vector>
dcheng4af48582016-04-19 00:29:3510
avie4d7b6f2015-12-26 00:59:1811#include "base/macros.h"
Wez25fd35f2018-05-25 22:57:4112#include "build/build_config.h"
[email protected]9a47c432013-04-19 20:33:5513#include "chrome/browser/chrome_process_singleton.h"
[email protected]c1702d712011-09-08 19:41:4314#include "chrome/browser/first_run/first_run.h"
[email protected]66013922011-09-13 18:53:4215#include "chrome/browser/process_singleton.h"
[email protected]fe7c4872012-05-10 20:06:0316#include "chrome/browser/ui/startup/startup_browser_creator.h"
[email protected]b48c9182011-10-26 18:03:3017#include "content/public/browser/browser_main_parts.h"
[email protected]57624ab2013-08-01 16:01:5118#include "content/public/common/main_function_params.h"
[email protected]f8abf722010-07-07 19:46:2419
Greg Thompsonfb9ba592019-08-23 08:18:5120#if defined(OS_WIN)
21#include "chrome/browser/downgrade/downgrade_manager.h"
22#endif
23
[email protected]c1702d712011-09-08 19:41:4324class BrowserProcessImpl;
[email protected]50462bf02011-11-21 19:13:3125class ChromeBrowserMainExtraParts;
Xi Han85079c22019-04-18 21:43:0526class StartupData;
[email protected]edafd4c2011-05-10 17:18:5327class PrefService;
[email protected]c1702d712011-09-08 19:41:4328class Profile;
[email protected]fe7c4872012-05-10 20:06:0329class StartupBrowserCreator;
[email protected]08f1c5e2011-12-01 01:54:3430class StartupTimeBomb;
[email protected]f967b722011-09-07 00:58:0431class ShutdownWatcherHelper;
juncai1da12032016-07-15 19:10:1232class WebUsbDetector;
[email protected]5f988b92011-05-18 07:14:0833
Hans Wennborg9208992ba2019-10-14 14:02:4934namespace base {
35class RunLoop;
36}
37
Sebastien Marchanddbe43bab2019-02-06 18:25:5138namespace tracing {
39class TraceEventSystemStatsMonitor;
40}
41
Sebastien Marchand87557292019-02-28 03:46:5942namespace performance_monitor {
43class SystemMonitor;
44}
45
[email protected]f967b722011-09-07 00:58:0446class ChromeBrowserMainParts : public content::BrowserMainParts {
[email protected]f8abf722010-07-07 19:46:2447 public:
Daniel Chenga542fca2014-10-21 09:51:2948 ~ChromeBrowserMainParts() override;
[email protected]f8abf722010-07-07 19:46:2449
[email protected]50462bf02011-11-21 19:13:3150 // Add additional ChromeBrowserMainExtraParts.
51 virtual void AddParts(ChromeBrowserMainExtraParts* parts);
[email protected]069cd9952011-10-01 16:34:4552
Wez25fd35f2018-05-25 22:57:4153#if !defined(OS_ANDROID)
54 // Returns the RunLoop that would be run by MainMessageLoopRun. This is used
55 // by InProcessBrowserTests to allow them to run until the BrowserProcess is
56 // ready for the browser to exit.
57 static std::unique_ptr<base::RunLoop> TakeRunLoopForTest();
58#endif
59
[email protected]f8abf722010-07-07 19:46:2460 protected:
Xi Handdb1ab12018-08-27 22:18:5461 ChromeBrowserMainParts(const content::MainFunctionParams& parameters,
Xi Han85079c22019-04-18 21:43:0562 StartupData* startup_data);
[email protected]f8abf722010-07-07 19:46:2463
[email protected]50462bf02011-11-21 19:13:3164 // content::BrowserMainParts overrides.
[email protected]97ef83f2011-12-19 19:26:3665 // 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 Violet9068b4df2018-01-12 16:44:2168 int PreEarlyInitialization() override;
Daniel Chenga542fca2014-10-21 09:51:2969 void PostEarlyInitialization() override;
70 void ToolkitInitialized() override;
71 void PreMainMessageLoopStart() override;
72 void PostMainMessageLoopStart() override;
73 int PreCreateThreads() override;
Xi Cheng8c9653712018-01-24 06:07:2774 void PostCreateThreads() override;
Daniel Chenga542fca2014-10-21 09:51:2975 void PreMainMessageLoopRun() override;
76 bool MainMessageLoopRun(int* result_code) override;
77 void PostMainMessageLoopRun() override;
78 void PostDestroyThreads() override;
[email protected]b48c9182011-10-26 18:03:3079
[email protected]97ef83f2011-12-19 19:26:3680 // Additional stages for ChromeBrowserMainExtraParts. These stages are called
[email protected]278a34d2012-06-07 00:34:5881 // in order from PreMainMessageLoopRun(). See implementation for details.
[email protected]97ef83f2011-12-19 19:26:3682 virtual void PreProfileInit();
83 virtual void PostProfileInit();
84 virtual void PreBrowserStart();
85 virtual void PostBrowserStart();
86
[email protected]b48c9182011-10-26 18:03:3087 // Displays a warning message that we can't find any locale data files.
88 virtual void ShowMissingLocaleMessageBox() = 0;
[email protected]f8abf722010-07-07 19:46:2489
[email protected]50462bf02011-11-21 19:13:3190 const content::MainFunctionParams& parameters() const {
91 return parameters_;
92 }
[email protected]2f3b1cc2014-03-17 23:07:1593 const base::CommandLine& parsed_command_line() const {
[email protected]50462bf02011-11-21 19:13:3194 return parsed_command_line_;
95 }
mlermana312cec2015-01-28 21:13:3396 const base::FilePath& user_data_dir() const {
97 return user_data_dir_;
98 }
[email protected]50462bf02011-11-21 19:13:3199
100 Profile* profile() { return profile_; }
101
[email protected]1fec64352010-07-27 13:55:21102 private:
Scott Violet3d27f7f2018-04-03 17:13:22103 friend class ChromeBrowserMainPartsTestApi;
104
robliaoce2e7d32016-09-19 18:29:00105 // Constructs the metrics service and initializes metrics recording.
106 void SetupMetrics();
[email protected]edafd4c2011-05-10 17:18:53107
[email protected]5281db1d2012-01-11 22:51:54108 // Starts recording of metrics. This can only be called after we have a file
109 // thread.
Dan Harrington5b7dfc12019-08-21 18:14:30110 static void StartMetricsRecording();
[email protected]5281db1d2012-01-11 22:51:54111
[email protected]e8b6ca02013-07-10 18:00:51112 // Record time from process startup to present time in an UMA histogram.
[email protected]69e90692013-07-31 02:13:29113 void RecordBrowserStartupTime();
[email protected]e8b6ca02013-07-10 18:00:51114
iclelland27f4bcb2016-06-22 16:41:03115 // Reads origin trial policy data from local state and configures command line
116 // for child processes.
Scott Violet4fe34bfb2018-01-10 22:20:29117 void SetupOriginTrialsCommandLine(PrefService* local_state);
iclelland27f4bcb2016-06-22 16:41:03118
Xi Han349162b2018-10-26 12:44:23119 // Calling during PreEarlyInitialization() to complete the remaining tasks
120 // after the local state is loaded. Return value is an exit status,
121 // RESULT_CODE_NORMAL_EXIT indicates success. If the return value is
122 // RESULT_CODE_MISSING_DATA, then |failed_to_load_resource_bundle| indicates
123 // if the ResourceBundle couldn't be loaded.
124 int OnLocalStateLoaded(bool* failed_to_load_resource_bundle);
Scott Violet875789e2018-02-02 07:46:48125
Alexei Svitkined55a40d2018-11-25 19:18:18126 // Applies any preferences (to local state) needed for first run. This is
127 // always called and early outs if not first-run. Return value is an exit
128 // status, RESULT_CODE_NORMAL_EXIT indicates success.
129 int ApplyFirstRunPrefs();
130
[email protected]b48c9182011-10-26 18:03:30131 // Methods for Main Message Loop -------------------------------------------
132
[email protected]2e5b60a22011-11-28 15:56:41133 int PreCreateThreadsImpl();
[email protected]b48c9182011-10-26 18:03:30134 int PreMainMessageLoopRunImpl();
135
[email protected]f8abf722010-07-07 19:46:24136 // Members initialized on construction ---------------------------------------
137
[email protected]57624ab2013-08-01 16:01:51138 const content::MainFunctionParams parameters_;
Scott Violet875789e2018-02-02 07:46:48139 // TODO(sky): remove this. This class (and related calls), may mutate the
140 // CommandLine, so it is misleading keeping a const ref here.
[email protected]2f3b1cc2014-03-17 23:07:15141 const base::CommandLine& parsed_command_line_;
[email protected]b48c9182011-10-26 18:03:30142 int result_code_;
143
Dmitry Skibac0300e802018-05-02 17:34:04144#if !defined(OS_ANDROID)
[email protected]08f1c5e2011-12-01 01:54:34145 // Create StartupTimeBomb object for watching jank during startup.
dcheng4af48582016-04-19 00:29:35146 std::unique_ptr<StartupTimeBomb> startup_watcher_;
[email protected]08f1c5e2011-12-01 01:54:34147
[email protected]f967b722011-09-07 00:58:04148 // Create ShutdownWatcherHelper object for watching jank during shutdown.
149 // Please keep |shutdown_watcher| as the first object constructed, and hence
150 // it is destroyed last.
dcheng4af48582016-04-19 00:29:35151 std::unique_ptr<ShutdownWatcherHelper> shutdown_watcher_;
[email protected]f8abf722010-07-07 19:46:24152
juncai1da12032016-07-15 19:10:12153 std::unique_ptr<WebUsbDetector> web_usb_detector_;
Dmitry Skibac0300e802018-05-02 17:34:04154#endif // !defined(OS_ANDROID)
dhnishi3cf1d2aa2014-08-26 22:25:07155
[email protected]50462bf02011-11-21 19:13:31156 // Vector of additional ChromeBrowserMainExtraParts.
157 // Parts are deleted in the inverse order they are added.
158 std::vector<ChromeBrowserMainExtraParts*> chrome_extra_parts_;
159
Sebastien Marchand87557292019-02-28 03:46:59160 // The system monitor instance, used by some subsystems to collect the system
161 // metrics they need.
162 std::unique_ptr<performance_monitor::SystemMonitor> system_monitor_;
163
Sebastien Marchanddbe43bab2019-02-06 18:25:51164 // The system stats monitor used by chrome://tracing. This doesn't do anything
165 // until tracing of the |system_stats| category is enabled.
166 std::unique_ptr<tracing::TraceEventSystemStatsMonitor>
167 trace_event_system_stats_monitor_;
168
Scott Violet3d27f7f2018-04-03 17:13:22169 // Whether PerformPreMainMessageLoopStartup() is called on VariationsService.
170 // Initialized to true if |MainFunctionParams::ui_task| is null (meaning not
171 // running browser_tests), but may be forced to true for tests.
172 bool should_call_pre_main_loop_start_startup_on_variations_service_;
173
[email protected]0f247aea82011-09-03 03:05:51174 // Members initialized after / released before main_message_loop_ ------------
175
dcheng4af48582016-04-19 00:29:35176 std::unique_ptr<BrowserProcessImpl> browser_process_;
pkasting28ae5792016-05-20 21:39:14177
bshe0d948072016-01-05 19:06:20178#if !defined(OS_ANDROID)
[email protected]81054f812012-08-30 00:47:09179 // Browser creation happens on the Java side in Android.
dcheng4af48582016-04-19 00:29:35180 std::unique_ptr<StartupBrowserCreator> browser_creator_;
[email protected]81054f812012-08-30 00:47:09181
182 // Android doesn't support multiple browser processes, so it doesn't implement
183 // ProcessSingleton.
dcheng4af48582016-04-19 00:29:35184 std::unique_ptr<ChromeProcessSingleton> process_singleton_;
[email protected]d29f4e312013-08-07 05:28:31185
pkasting28ae5792016-05-20 21:39:14186 ProcessSingleton::NotifyResult notify_result_ =
187 ProcessSingleton::PROCESS_NONE;
188
189 // Members needed across shutdown methods.
190 bool restart_last_session_ = false;
Alexei Svitkine1ac2b952018-11-26 23:27:13191#endif // !defined(OS_ANDROID)
192
193#if !defined(OS_ANDROID) && !defined(OS_CHROMEOS)
194 // Android's first run is done in Java instead of native. Chrome OS does not
195 // use master preferences.
196 std::unique_ptr<first_run::MasterPrefs> master_prefs_;
bshe0d948072016-01-05 19:06:20197#endif
pkasting28ae5792016-05-20 21:39:14198
Greg Thompsonfb9ba592019-08-23 08:18:51199#if defined(OS_WIN)
200 downgrade::DowngradeManager downgrade_manager_;
201#endif
202
[email protected]c1702d712011-09-08 19:41:43203 Profile* profile_;
204 bool run_message_loop_;
[email protected]0f247aea82011-09-03 03:05:51205
[email protected]650b2d52013-02-10 03:41:45206 base::FilePath user_data_dir_;
[email protected]2e5b60a22011-11-28 15:56:41207
Xi Han85079c22019-04-18 21:43:05208 StartupData* startup_data_;
Xi Handdb1ab12018-08-27 22:18:54209
[email protected]f967b722011-09-07 00:58:04210 DISALLOW_COPY_AND_ASSIGN(ChromeBrowserMainParts);
[email protected]f8abf722010-07-07 19:46:24211};
212
[email protected]e6b5bc22011-09-08 22:01:56213#endif // CHROME_BROWSER_CHROME_BROWSER_MAIN_H_