[M120 Migration]Fix for crash during chrome exit
[platform/framework/web/chromium-efl.git] / chrome / browser / chrome_browser_main.h
1 // Copyright 2012 The Chromium Authors
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #ifndef CHROME_BROWSER_CHROME_BROWSER_MAIN_H_
6 #define CHROME_BROWSER_CHROME_BROWSER_MAIN_H_
7
8 #include <memory>
9 #include <vector>
10
11 #include "base/memory/raw_ptr.h"
12 #include "base/run_loop.h"
13 #include "build/build_config.h"
14 #include "build/chromeos_buildflags.h"
15 #include "chrome/browser/buildflags.h"
16 #include "chrome/browser/first_run/first_run.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/result_codes.h"
21
22 #if BUILDFLAG(ENABLE_DOWNGRADE_PROCESSING)
23 #include "chrome/browser/downgrade/downgrade_manager.h"
24 #endif
25
26 class BrowserProcessImpl;
27 class ChromeBrowserMainExtraParts;
28 class StartupData;
29 class Profile;
30 class StartupBrowserCreator;
31 class ShutdownWatcherHelper;
32 class WebUsbDetector;
33
34 namespace base {
35 class CommandLine;
36 class RunLoop;
37 }
38
39 namespace tracing {
40 class TraceEventSystemStatsMonitor;
41 }
42
43 class ChromeBrowserMainParts : public content::BrowserMainParts {
44  public:
45   ChromeBrowserMainParts(const ChromeBrowserMainParts&) = delete;
46   ChromeBrowserMainParts& operator=(const ChromeBrowserMainParts&) = delete;
47   ~ChromeBrowserMainParts() override;
48
49   // Add additional ChromeBrowserMainExtraParts.
50   void AddParts(std::unique_ptr<ChromeBrowserMainExtraParts> parts);
51
52 #if !BUILDFLAG(IS_ANDROID)
53   // Returns the RunLoop that would be run by MainMessageLoopRun. This is used
54   // by InProcessBrowserTests to allow them to run until the BrowserProcess is
55   // ready for the browser to exit.
56   static std::unique_ptr<base::RunLoop> TakeRunLoopForTest();
57 #endif  // !BUILDFLAG(IS_ANDROID)
58
59 #if BUILDFLAG(ENABLE_PROCESS_SINGLETON)
60   // Handles notifications from other processes. The function receives the
61   // command line and directory with which the other Chrome process was
62   // launched. Return true if the command line will be handled within the
63   // current browser instance or false if the remote process should handle it
64   // (i.e., because the current process is shutting down).
65   static bool ProcessSingletonNotificationCallback(
66       const base::CommandLine& command_line,
67       const base::FilePath& current_directory);
68 #endif  // BUILDFLAG(ENABLE_PROCESS_SINGLETON)
69
70  protected:
71   ChromeBrowserMainParts(bool is_integration_test, StartupData* startup_data);
72
73   // content::BrowserMainParts overrides.
74   // These are called in-order by content::BrowserMainLoop.
75   // Each stage calls the same stages in any ChromeBrowserMainExtraParts added
76   // with AddParts() from ChromeContentBrowserClient::CreateBrowserMainParts.
77   int PreEarlyInitialization() override;
78   void PostEarlyInitialization() override;
79   void ToolkitInitialized() override;
80   void PreCreateMainMessageLoop() override;
81   void PostCreateMainMessageLoop() override;
82   int PreCreateThreads() override;
83   void PostCreateThreads() override;
84   int PreMainMessageLoopRun() override;
85 #if !BUILDFLAG(IS_ANDROID)
86   bool ShouldInterceptMainMessageLoopRun() override;
87 #endif
88   void WillRunMainMessageLoop(
89       std::unique_ptr<base::RunLoop>& run_loop) override;
90   void OnFirstIdle() override;
91   void PostMainMessageLoopRun() override;
92   void PostDestroyThreads() override;
93
94   // Additional stages for ChromeBrowserMainExtraParts. These stages are called
95   // in order from PreMainMessageLoopRun(). See implementation for details.
96   virtual void PreProfileInit();
97   // `PostProfileInit()` is called for each regular profile that is created. The
98   // first call has `is_initial_profile`=true, and subsequent calls have
99   // `is_initial_profile`=false.
100   // It may be called during startup if a profile is loaded immediately, or
101   // later if the profile picker is shown.
102   virtual void PostProfileInit(Profile* profile, bool is_initial_profile);
103   virtual void PreBrowserStart();
104   virtual void PostBrowserStart();
105
106   // Displays a warning message that we can't find any locale data files.
107   virtual void ShowMissingLocaleMessageBox() = 0;
108
109   const base::FilePath& user_data_dir() const {
110     return user_data_dir_;
111   }
112
113  protected:
114   // Returns whether ChromeContentBrowserClient::CreateBrowserMainParts was
115   // invoked as part of an integration (browser) test.
116   // Avoid writing test-only conditions in product code if at all possible.
117   bool is_integration_test() const { return is_integration_test_; }
118
119  private:
120   class ProfileInitManager;
121   friend class ChromeBrowserMainPartsTestApi;
122
123   // Constructs the metrics service and initializes metrics recording.
124   void SetupMetrics();
125
126   // Starts recording of metrics. This can only be called after we have a file
127   // thread.
128   static void StartMetricsRecording();
129
130   // Record time from process startup to present time in an UMA histogram.
131   void RecordBrowserStartupTime();
132
133   // Calling during PreEarlyInitialization() to complete the remaining tasks
134   // after the local state is loaded. Return value is an exit status,
135   // RESULT_CODE_NORMAL_EXIT indicates success. If the return value is
136   // RESULT_CODE_MISSING_DATA, then |failed_to_load_resource_bundle| indicates
137   // if the ResourceBundle couldn't be loaded.
138   int OnLocalStateLoaded(bool* failed_to_load_resource_bundle);
139
140   // Applies any preferences (to local state) needed for first run. This is
141   // always called and early outs if not first-run. Return value is an exit
142   // status, RESULT_CODE_NORMAL_EXIT indicates success.
143   int ApplyFirstRunPrefs();
144
145   // Methods for Main Message Loop -------------------------------------------
146
147   int PreCreateThreadsImpl();
148   int PreMainMessageLoopRunImpl();
149
150   // Wrapper for `PostProfileInit()` that provides to it the right
151   // `is_initial_profile` value.
152   void CallPostProfileInit(Profile* profile);
153
154   // Members initialized on construction ---------------------------------------
155   const bool is_integration_test_;
156   const raw_ptr<StartupData> startup_data_;
157
158   int result_code_ = content::RESULT_CODE_NORMAL_EXIT;
159
160 #if !BUILDFLAG(IS_ANDROID)
161   // Create ShutdownWatcherHelper object for watching jank during shutdown.
162   // Please keep |shutdown_watcher| as the first object constructed, and hence
163   // it is destroyed last.
164   std::unique_ptr<ShutdownWatcherHelper> shutdown_watcher_;
165
166   std::unique_ptr<WebUsbDetector> web_usb_detector_;
167 #endif  // !BUILDFLAG(IS_ANDROID)
168
169   // Vector of additional ChromeBrowserMainExtraParts.
170   // Parts are deleted in the inverse order they are added.
171   std::vector<std::unique_ptr<ChromeBrowserMainExtraParts>> chrome_extra_parts_;
172
173   // The system stats monitor used by chrome://tracing. This doesn't do anything
174   // until tracing of the |system_stats| category is enabled.
175   std::unique_ptr<tracing::TraceEventSystemStatsMonitor>
176       trace_event_system_stats_monitor_;
177
178   // Members initialized after / released before main_message_loop_ ------------
179
180   std::unique_ptr<BrowserProcessImpl> browser_process_;
181
182 #if !BUILDFLAG(IS_ANDROID)
183   // Browser creation happens on the Java side in Android.
184   std::unique_ptr<StartupBrowserCreator> browser_creator_;
185 #endif  // !BUILDFLAG(IS_ANDROID)
186
187 #if !BUILDFLAG(IS_ANDROID)
188   // Members needed across shutdown methods.
189   bool restart_last_session_ = false;
190 #endif  // !BUILDFLAG(IS_ANDROID)
191
192 #if BUILDFLAG(ENABLE_DOWNGRADE_PROCESSING)
193   downgrade::DowngradeManager downgrade_manager_;
194 #endif
195
196 #if !BUILDFLAG(IS_ANDROID) && !BUILDFLAG(IS_CHROMEOS_ASH)
197   // Android's first run is done in Java instead of native. Chrome OS does not
198   // use master preferences.
199   std::unique_ptr<first_run::MasterPrefs> master_prefs_;
200 #endif
201
202   base::FilePath user_data_dir_;
203
204   // Indicates that the initial profile has been created and we started
205   // executing `PostProfileInit()` for it.
206   bool initialized_initial_profile_ = false;
207
208   // Observer that triggers `PostProfileInit()` when new user profiles are
209   // created.
210   // Must be deleted before `browser_process_`.
211   std::unique_ptr<ProfileInitManager> profile_init_manager_;
212 };
213
214 #endif  // CHROME_BROWSER_CHROME_BROWSER_MAIN_H_