blob: 4d0e815c2195d822a1f1dd1bde28b0b2f1eb8117 [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>
9
avie4d7b6f2015-12-26 00:59:1810#include "base/macros.h"
Wez25fd35f2018-05-25 22:57:4111#include "build/build_config.h"
[email protected]ae9e12a2012-07-31 17:48:1212#include "chrome/browser/chrome_browser_field_trials.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"
Mike Wittman4bb6af22018-03-01 00:22:0617#include "chrome/common/thread_profiler.h"
[email protected]b48c9182011-10-26 18:03:3018#include "content/public/browser/browser_main_parts.h"
[email protected]57624ab2013-08-01 16:01:5119#include "content/public/common/main_function_params.h"
Ran Jia96d43d42018-05-02 17:14:5320#include "ui/base/resource/data_pack.h"
[email protected]f8abf722010-07-07 19:46:2421
[email protected]c1702d712011-09-08 19:41:4322class BrowserProcessImpl;
[email protected]50462bf02011-11-21 19:13:3123class ChromeBrowserMainExtraParts;
[email protected]edafd4c2011-05-10 17:18:5324class FieldTrialSynchronizer;
[email protected]edafd4c2011-05-10 17:18:5325class PrefService;
[email protected]c1702d712011-09-08 19:41:4326class Profile;
[email protected]fe7c4872012-05-10 20:06:0327class StartupBrowserCreator;
[email protected]08f1c5e2011-12-01 01:54:3428class StartupTimeBomb;
[email protected]f967b722011-09-07 00:58:0429class ShutdownWatcherHelper;
juncai1da12032016-07-15 19:10:1230class WebUsbDetector;
[email protected]5f988b92011-05-18 07:14:0831
[email protected]cbce47242011-08-12 21:40:5932namespace chrome_browser {
33// For use by ShowMissingLocaleMessageBox.
[email protected]49aa5a32014-02-03 10:00:4434#if defined(OS_WIN)
[email protected]cbce47242011-08-12 21:40:5935extern const char kMissingLocaleDataTitle[];
[email protected]49aa5a32014-02-03 10:00:4436#endif
37
[email protected]893124a22014-04-15 00:45:2838#if defined(OS_WIN)
[email protected]cbce47242011-08-12 21:40:5939extern const char kMissingLocaleDataMessage[];
[email protected]49aa5a32014-02-03 10:00:4440#endif
[email protected]cbce47242011-08-12 21:40:5941}
42
[email protected]f967b722011-09-07 00:58:0443class ChromeBrowserMainParts : public content::BrowserMainParts {
[email protected]f8abf722010-07-07 19:46:2444 public:
Daniel Chenga542fca2014-10-21 09:51:2945 ~ChromeBrowserMainParts() override;
[email protected]f8abf722010-07-07 19:46:2446
[email protected]50462bf02011-11-21 19:13:3147 // Add additional ChromeBrowserMainExtraParts.
48 virtual void AddParts(ChromeBrowserMainExtraParts* parts);
[email protected]069cd9952011-10-01 16:34:4549
Wez25fd35f2018-05-25 22:57:4150#if !defined(OS_ANDROID)
51 // Returns the RunLoop that would be run by MainMessageLoopRun. This is used
52 // by InProcessBrowserTests to allow them to run until the BrowserProcess is
53 // ready for the browser to exit.
54 static std::unique_ptr<base::RunLoop> TakeRunLoopForTest();
55#endif
56
[email protected]f8abf722010-07-07 19:46:2457 protected:
Ran Jia96d43d42018-05-02 17:14:5358 explicit ChromeBrowserMainParts(const content::MainFunctionParams& parameters,
59 std::unique_ptr<ui::DataPack> data_pack);
[email protected]f8abf722010-07-07 19:46:2460
[email protected]50462bf02011-11-21 19:13:3161 // content::BrowserMainParts overrides.
Scott Violet9a375622018-02-20 18:04:5162 bool ShouldContentCreateFeatureList() override;
[email protected]97ef83f2011-12-19 19:26:3663 // These are called in-order by content::BrowserMainLoop.
64 // Each stage calls the same stages in any ChromeBrowserMainExtraParts added
65 // with AddParts() from ChromeContentBrowserClient::CreateBrowserMainParts.
Scott Violet9068b4df2018-01-12 16:44:2166 int PreEarlyInitialization() override;
Daniel Chenga542fca2014-10-21 09:51:2967 void PostEarlyInitialization() override;
68 void ToolkitInitialized() override;
69 void PreMainMessageLoopStart() override;
70 void PostMainMessageLoopStart() override;
71 int PreCreateThreads() override;
Xi Cheng8c9653712018-01-24 06:07:2772 void PostCreateThreads() override;
bend32292b2016-10-07 00:21:5873 void ServiceManagerConnectionStarted(
74 content::ServiceManagerConnection* connection) override;
Daniel Chenga542fca2014-10-21 09:51:2975 void PreMainMessageLoopRun() override;
76 bool MainMessageLoopRun(int* result_code) override;
77 void PostMainMessageLoopRun() override;
bcwhiteda097d32017-01-12 23:55:0378 void PreShutdown() override;
Daniel Chenga542fca2014-10-21 09:51:2979 void PostDestroyThreads() override;
[email protected]b48c9182011-10-26 18:03:3080
[email protected]97ef83f2011-12-19 19:26:3681 // Additional stages for ChromeBrowserMainExtraParts. These stages are called
[email protected]278a34d2012-06-07 00:34:5882 // in order from PreMainMessageLoopRun(). See implementation for details.
[email protected]97ef83f2011-12-19 19:26:3683 virtual void PreProfileInit();
84 virtual void PostProfileInit();
85 virtual void PreBrowserStart();
86 virtual void PostBrowserStart();
87
[email protected]b48c9182011-10-26 18:03:3088 // Displays a warning message that we can't find any locale data files.
89 virtual void ShowMissingLocaleMessageBox() = 0;
[email protected]f8abf722010-07-07 19:46:2490
[email protected]50462bf02011-11-21 19:13:3191 const content::MainFunctionParams& parameters() const {
92 return parameters_;
93 }
[email protected]2f3b1cc2014-03-17 23:07:1594 const base::CommandLine& parsed_command_line() const {
[email protected]50462bf02011-11-21 19:13:3195 return parsed_command_line_;
96 }
mlermana312cec2015-01-28 21:13:3397 const base::FilePath& user_data_dir() const {
98 return user_data_dir_;
99 }
[email protected]50462bf02011-11-21 19:13:31100
101 Profile* profile() { return profile_; }
102
[email protected]1fec64352010-07-27 13:55:21103 private:
Scott Violet3d27f7f2018-04-03 17:13:22104 friend class ChromeBrowserMainPartsTestApi;
105
robliaoce2e7d32016-09-19 18:29:00106 // Sets up the field trials and related initialization. Call only after
107 // about:flags have been converted to switches.
108 void SetupFieldTrials();
[email protected]069cd9952011-10-01 16:34:45109
robliaoce2e7d32016-09-19 18:29:00110 // Constructs the metrics service and initializes metrics recording.
111 void SetupMetrics();
[email protected]edafd4c2011-05-10 17:18:53112
[email protected]5281db1d2012-01-11 22:51:54113 // Starts recording of metrics. This can only be called after we have a file
114 // thread.
115 void StartMetricsRecording();
116
[email protected]e8b6ca02013-07-10 18:00:51117 // Record time from process startup to present time in an UMA histogram.
[email protected]69e90692013-07-31 02:13:29118 void RecordBrowserStartupTime();
[email protected]e8b6ca02013-07-10 18:00:51119
iclelland27f4bcb2016-06-22 16:41:03120 // Reads origin trial policy data from local state and configures command line
121 // for child processes.
Scott Violet4fe34bfb2018-01-10 22:20:29122 void SetupOriginTrialsCommandLine(PrefService* local_state);
iclelland27f4bcb2016-06-22 16:41:03123
Scott Violet875789e2018-02-02 07:46:48124 // Calling during PreEarlyInitialization() to load local state. Return value
125 // is an exit status, RESULT_CODE_NORMAL_EXIT indicates success.
126 // If the return value is RESULT_CODE_MISSING_DATA, then
127 // |failed_to_load_resource_bundle| indicates if the ResourceBundle couldn't
128 // be loaded.
Gabriel Charette1f53ca32018-08-07 21:31:17129 int LoadLocalState(bool* failed_to_load_resource_bundle);
Scott Violet875789e2018-02-02 07:46:48130
131 // Applies any preferences (to local state) needed for first run. This is
132 // always called and early outs if not first-run. Return value is an exit
133 // status, RESULT_CODE_NORMAL_EXIT indicates success.
134 int ApplyFirstRunPrefs();
135
[email protected]b48c9182011-10-26 18:03:30136 // Methods for Main Message Loop -------------------------------------------
137
[email protected]2e5b60a22011-11-28 15:56:41138 int PreCreateThreadsImpl();
[email protected]b48c9182011-10-26 18:03:30139 int PreMainMessageLoopRunImpl();
140
[email protected]f8abf722010-07-07 19:46:24141 // Members initialized on construction ---------------------------------------
142
[email protected]57624ab2013-08-01 16:01:51143 const content::MainFunctionParams parameters_;
Scott Violet875789e2018-02-02 07:46:48144 // TODO(sky): remove this. This class (and related calls), may mutate the
145 // CommandLine, so it is misleading keeping a const ref here.
[email protected]2f3b1cc2014-03-17 23:07:15146 const base::CommandLine& parsed_command_line_;
[email protected]b48c9182011-10-26 18:03:30147 int result_code_;
148
Dmitry Skibac0300e802018-05-02 17:34:04149 ChromeBrowserFieldTrials browser_field_trials_;
150
151#if !defined(OS_ANDROID)
[email protected]08f1c5e2011-12-01 01:54:34152 // Create StartupTimeBomb object for watching jank during startup.
dcheng4af48582016-04-19 00:29:35153 std::unique_ptr<StartupTimeBomb> startup_watcher_;
[email protected]08f1c5e2011-12-01 01:54:34154
[email protected]f967b722011-09-07 00:58:04155 // 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.
dcheng4af48582016-04-19 00:29:35158 std::unique_ptr<ShutdownWatcherHelper> shutdown_watcher_;
[email protected]f8abf722010-07-07 19:46:24159
juncai1da12032016-07-15 19:10:12160 std::unique_ptr<WebUsbDetector> web_usb_detector_;
Dmitry Skibac0300e802018-05-02 17:34:04161#endif // !defined(OS_ANDROID)
dhnishi3cf1d2aa2014-08-26 22:25:07162
[email protected]50462bf02011-11-21 19:13:31163 // Vector of additional ChromeBrowserMainExtraParts.
164 // Parts are deleted in the inverse order they are added.
165 std::vector<ChromeBrowserMainExtraParts*> chrome_extra_parts_;
166
Xi Cheng8c9653712018-01-24 06:07:27167 // A profiler that periodically samples stack traces on the UI thread.
Mike Wittman4bb6af22018-03-01 00:22:06168 std::unique_ptr<ThreadProfiler> ui_thread_profiler_;
Jamie Madillaba2fd202017-10-11 14:20:37169
Scott Violet3d27f7f2018-04-03 17:13:22170 // Whether PerformPreMainMessageLoopStartup() is called on VariationsService.
171 // Initialized to true if |MainFunctionParams::ui_task| is null (meaning not
172 // running browser_tests), but may be forced to true for tests.
173 bool should_call_pre_main_loop_start_startup_on_variations_service_;
174
[email protected]0f247aea82011-09-03 03:05:51175 // Members initialized after / released before main_message_loop_ ------------
176
dcheng4af48582016-04-19 00:29:35177 std::unique_ptr<BrowserProcessImpl> browser_process_;
pkasting28ae5792016-05-20 21:39:14178
bshe0d948072016-01-05 19:06:20179#if !defined(OS_ANDROID)
[email protected]81054f812012-08-30 00:47:09180 // Browser creation happens on the Java side in Android.
dcheng4af48582016-04-19 00:29:35181 std::unique_ptr<StartupBrowserCreator> browser_creator_;
[email protected]81054f812012-08-30 00:47:09182
183 // Android doesn't support multiple browser processes, so it doesn't implement
184 // ProcessSingleton.
dcheng4af48582016-04-19 00:29:35185 std::unique_ptr<ChromeProcessSingleton> process_singleton_;
[email protected]d29f4e312013-08-07 05:28:31186
187 // Android's first run is done in Java instead of native.
dcheng4af48582016-04-19 00:29:35188 std::unique_ptr<first_run::MasterPrefs> master_prefs_;
pkasting28ae5792016-05-20 21:39:14189
190 ProcessSingleton::NotifyResult notify_result_ =
191 ProcessSingleton::PROCESS_NONE;
192
193 // Members needed across shutdown methods.
194 bool restart_last_session_ = false;
bshe0d948072016-01-05 19:06:20195#endif
pkasting28ae5792016-05-20 21:39:14196
[email protected]c1702d712011-09-08 19:41:43197 Profile* profile_;
198 bool run_message_loop_;
[email protected]0f247aea82011-09-03 03:05:51199
robliaoce2e7d32016-09-19 18:29:00200 // Initialized in |SetupFieldTrials()|.
[email protected]edafd4c2011-05-10 17:18:53201 scoped_refptr<FieldTrialSynchronizer> field_trial_synchronizer_;
202
[email protected]650b2d52013-02-10 03:41:45203 base::FilePath user_data_dir_;
[email protected]2e5b60a22011-11-28 15:56:41204
Ran Jia96d43d42018-05-02 17:14:53205 // This is used to store the ui data pack. The data pack is moved when
206 // resource bundle gets created.
207 std::unique_ptr<ui::DataPack> service_manifest_data_pack_;
208
[email protected]f967b722011-09-07 00:58:04209 DISALLOW_COPY_AND_ASSIGN(ChromeBrowserMainParts);
[email protected]f8abf722010-07-07 19:46:24210};
211
[email protected]e6b5bc22011-09-08 22:01:56212#endif // CHROME_BROWSER_CHROME_BROWSER_MAIN_H_