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.
5 #ifndef CHROME_BROWSER_CHROME_BROWSER_MAIN_H_
6 #define CHROME_BROWSER_CHROME_BROWSER_MAIN_H_
11 #include "base/macros.h"
12 #include "base/run_loop.h"
13 #include "build/build_config.h"
14 #include "chrome/browser/chrome_process_singleton.h"
15 #include "chrome/browser/first_run/first_run.h"
16 #include "chrome/browser/process_singleton.h"
17 #include "chrome/browser/ui/startup/startup_browser_creator.h"
18 #include "chrome/common/buildflags.h"
19 #include "content/public/browser/browser_main_parts.h"
20 #include "content/public/common/main_function_params.h"
22 #if BUILDFLAG(ENABLE_DOWNGRADE_PROCESSING)
23 #include "chrome/browser/downgrade/downgrade_manager.h"
26 class BrowserProcessImpl;
27 class ChromeBrowserMainExtraParts;
31 class StartupBrowserCreator;
32 class ShutdownWatcherHelper;
40 class TraceEventSystemStatsMonitor;
43 namespace performance_monitor {
47 class ChromeBrowserMainParts : public content::BrowserMainParts {
49 ~ChromeBrowserMainParts() override;
51 // Add additional ChromeBrowserMainExtraParts.
52 virtual void AddParts(ChromeBrowserMainExtraParts* parts);
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();
62 ChromeBrowserMainParts(const content::MainFunctionParams& parameters,
63 StartupData* startup_data);
65 // content::BrowserMainParts overrides.
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.
69 int PreEarlyInitialization() override;
70 void PostEarlyInitialization() override;
71 void ToolkitInitialized() override;
72 void PreMainMessageLoopStart() override;
73 void PostMainMessageLoopStart() override;
74 int PreCreateThreads() override;
75 void PostCreateThreads() override;
76 void PreMainMessageLoopRun() override;
77 bool MainMessageLoopRun(int* result_code) override;
78 void PostMainMessageLoopRun() override;
79 void PostDestroyThreads() override;
81 // Additional stages for ChromeBrowserMainExtraParts. These stages are called
82 // in order from PreMainMessageLoopRun(). See implementation for details.
83 virtual void PreProfileInit();
84 virtual void PostProfileInit();
85 virtual void PreBrowserStart();
86 virtual void PostBrowserStart();
88 // Displays a warning message that we can't find any locale data files.
89 virtual void ShowMissingLocaleMessageBox() = 0;
91 const content::MainFunctionParams& parameters() const {
94 const base::CommandLine& parsed_command_line() const {
95 return parsed_command_line_;
97 const base::FilePath& user_data_dir() const {
98 return user_data_dir_;
101 Profile* profile() { return profile_; }
104 friend class ChromeBrowserMainPartsTestApi;
106 // Constructs the metrics service and initializes metrics recording.
109 // Starts recording of metrics. This can only be called after we have a file
111 static void StartMetricsRecording();
113 // Record time from process startup to present time in an UMA histogram.
114 void RecordBrowserStartupTime();
116 // Reads origin trial policy data from local state and configures command line
117 // for child processes.
118 void SetupOriginTrialsCommandLine(PrefService* local_state);
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);
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();
132 // Methods for Main Message Loop -------------------------------------------
134 int PreCreateThreadsImpl();
135 int PreMainMessageLoopRunImpl();
137 // Members initialized on construction ---------------------------------------
139 const content::MainFunctionParams parameters_;
140 // TODO(sky): remove this. This class (and related calls), may mutate the
141 // CommandLine, so it is misleading keeping a const ref here.
142 const base::CommandLine& parsed_command_line_;
145 #if !defined(OS_ANDROID)
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.
149 std::unique_ptr<ShutdownWatcherHelper> shutdown_watcher_;
151 std::unique_ptr<WebUsbDetector> web_usb_detector_;
152 #endif // !defined(OS_ANDROID)
154 // Vector of additional ChromeBrowserMainExtraParts.
155 // Parts are deleted in the inverse order they are added.
156 std::vector<ChromeBrowserMainExtraParts*> chrome_extra_parts_;
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_;
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_;
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_;
172 // Members initialized after / released before main_message_loop_ ------------
174 std::unique_ptr<BrowserProcessImpl> browser_process_;
176 #if !defined(OS_ANDROID)
177 // Browser creation happens on the Java side in Android.
178 std::unique_ptr<StartupBrowserCreator> browser_creator_;
180 // Android doesn't support multiple browser processes, so it doesn't implement
182 std::unique_ptr<ChromeProcessSingleton> process_singleton_;
184 ProcessSingleton::NotifyResult notify_result_ =
185 ProcessSingleton::PROCESS_NONE;
187 // Members needed across shutdown methods.
188 bool restart_last_session_ = false;
189 #endif // !defined(OS_ANDROID)
191 #if BUILDFLAG(ENABLE_DOWNGRADE_PROCESSING)
192 downgrade::DowngradeManager downgrade_manager_;
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_;
202 bool run_message_loop_;
204 base::FilePath user_data_dir_;
206 StartupData* startup_data_;
208 DISALLOW_COPY_AND_ASSIGN(ChromeBrowserMainParts);
211 #endif // CHROME_BROWSER_CHROME_BROWSER_MAIN_H_