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 CONTENT_BROWSER_BROWSER_MAIN_LOOP_H_
6 #define CONTENT_BROWSER_BROWSER_MAIN_LOOP_H_
8 #include "base/basictypes.h"
9 #include "base/files/file_path.h"
10 #include "base/memory/ref_counted.h"
11 #include "base/memory/scoped_ptr.h"
12 #include "base/timer/timer.h"
13 #include "content/browser/browser_process_sub_thread.h"
14 #include "content/public/browser/browser_main_runner.h"
19 class HighResolutionTimerManager
;
23 #if defined(OS_CHROMEOS)
24 class MemoryPressureMonitorChromeOS
;
25 #elif (defined(OS_MACOSX) && !defined(OS_IOS)) || defined(OS_WIN)
26 class MemoryPressureMonitor
;
28 namespace trace_event
{
29 class TraceMemoryController
;
30 class TraceEventSystemStatsMonitor
;
31 } // namespace trace_event
36 class UserInputMonitor
;
43 class NetworkChangeNotifier
;
47 class BrowserMainParts
;
48 class BrowserOnlineStateObserver
;
49 class BrowserShutdownImpl
;
50 class BrowserThreadImpl
;
51 class MediaStreamManager
;
52 class ResourceDispatcherHostImpl
;
53 class SpeechRecognitionManagerImpl
;
54 class StartupTaskRunner
;
55 class TimeZoneMonitor
;
56 struct MainFunctionParams
;
58 #if defined(OS_ANDROID)
59 class ScreenOrientationDelegate
;
60 #elif defined(OS_LINUX)
61 class DeviceMonitorLinux
;
62 #elif defined(OS_MACOSX)
63 class DeviceMonitorMac
;
65 class SystemMessageWindowWin
;
68 // Implements the main browser loop stages called from BrowserMainRunner.
69 // See comments in browser_main_parts.h for additional info.
70 class CONTENT_EXPORT BrowserMainLoop
{
72 // Returns the current instance. This is used to get access to the getters
73 // that return objects which are owned by this class.
74 static BrowserMainLoop
* GetInstance();
76 explicit BrowserMainLoop(const MainFunctionParams
& parameters
);
77 virtual ~BrowserMainLoop();
81 void EarlyInitialization();
82 // Initializes the toolkit. Returns whether the toolkit initialization was
84 bool InitializeToolkit();
85 void MainMessageLoopStart();
87 // Create and start running the tasks we need to complete startup. Note that
88 // this can be called more than once (currently only on Android) if we get a
89 // request for synchronous startup while the tasks created by asynchronous
90 // startup are still running.
91 void CreateStartupTasks();
93 // Perform the default message loop run logic.
94 void RunMainMessageLoopParts();
96 // Performs the shutdown sequence, starting with PostMainMessageLoopRun
97 // through stopping threads to PostDestroyThreads.
98 void ShutdownThreadsAndCleanUp();
100 int GetResultCode() const { return result_code_
; }
102 media::AudioManager
* audio_manager() const { return audio_manager_
.get(); }
103 MediaStreamManager
* media_stream_manager() const {
104 return media_stream_manager_
.get();
106 media::UserInputMonitor
* user_input_monitor() const {
107 return user_input_monitor_
.get();
109 media::midi::MidiManager
* midi_manager() const { return midi_manager_
.get(); }
110 base::Thread
* indexed_db_thread() const { return indexed_db_thread_
.get(); }
112 bool is_tracing_startup() const { return is_tracing_startup_
; }
114 const base::FilePath
& startup_trace_file() const {
115 return startup_trace_file_
;
118 void StopStartupTracingTimer();
120 #if defined(OS_MACOSX) && !defined(OS_IOS)
121 DeviceMonitorMac
* device_monitor_mac() const {
122 return device_monitor_mac_
.get();
127 class MemoryObserver
;
128 // For ShutdownThreadsAndCleanUp.
129 friend class BrowserShutdownImpl
;
131 void InitializeMainThread();
133 // Called just before creating the threads
134 int PreCreateThreads();
136 // Create all secondary threads.
139 // Called right after the browser threads have been started.
140 int BrowserThreadsStarted();
142 int PreMainMessageLoopRun();
144 void MainMessageLoopRun();
146 base::FilePath
GetStartupTraceFileName(
147 const base::CommandLine
& command_line
) const;
148 void InitStartupTracing(const base::CommandLine
& command_line
);
149 void EndStartupTracing();
151 bool UsingInProcessGpu() const;
152 void InitializeGpuDataManager();
154 // Members initialized on construction ---------------------------------------
155 const MainFunctionParams
& parameters_
;
156 const base::CommandLine
& parsed_command_line_
;
158 // True if the non-UI threads were created.
159 bool created_threads_
;
161 // Members initialized in |MainMessageLoopStart()| ---------------------------
162 scoped_ptr
<base::MessageLoop
> main_message_loop_
;
163 scoped_ptr
<base::SystemMonitor
> system_monitor_
;
164 scoped_ptr
<base::PowerMonitor
> power_monitor_
;
165 scoped_ptr
<base::HighResolutionTimerManager
> hi_res_timer_manager_
;
166 scoped_ptr
<net::NetworkChangeNotifier
> network_change_notifier_
;
167 // user_input_monitor_ has to outlive audio_manager_, so declared first.
168 scoped_ptr
<media::UserInputMonitor
> user_input_monitor_
;
169 scoped_ptr
<media::AudioManager
> audio_manager_
;
170 scoped_ptr
<media::midi::MidiManager
> midi_manager_
;
171 scoped_ptr
<MediaStreamManager
> media_stream_manager_
;
172 // Per-process listener for online state changes.
173 scoped_ptr
<BrowserOnlineStateObserver
> online_state_observer_
;
175 scoped_ptr
<SystemMessageWindowWin
> system_message_window_
;
176 #elif defined(USE_UDEV)
177 scoped_ptr
<DeviceMonitorLinux
> device_monitor_linux_
;
178 #elif defined(OS_MACOSX) && !defined(OS_IOS)
179 scoped_ptr
<DeviceMonitorMac
> device_monitor_mac_
;
181 #if defined(OS_ANDROID)
182 // Android implementation of ScreenOrientationDelegate
183 scoped_ptr
<ScreenOrientationDelegate
> screen_orientation_delegate_
;
185 #if defined(OS_CHROMEOS)
186 scoped_ptr
<base::MemoryPressureMonitorChromeOS
> memory_pressure_monitor_
;
187 #elif (defined(OS_MACOSX) && !defined(OS_IOS)) || defined(OS_WIN)
188 scoped_ptr
<base::MemoryPressureMonitor
> memory_pressure_monitor_
;
190 // The startup task runner is created by CreateStartupTasks()
191 scoped_ptr
<StartupTaskRunner
> startup_task_runner_
;
193 // Destroy parts_ before main_message_loop_ (required) and before other
194 // classes constructed in content (but after main_thread_).
195 scoped_ptr
<BrowserMainParts
> parts_
;
197 // Members initialized in |InitializeMainThread()| ---------------------------
198 // This must get destroyed before other threads that are created in parts_.
199 scoped_ptr
<BrowserThreadImpl
> main_thread_
;
201 // Members initialized in |BrowserThreadsStarted()| --------------------------
202 scoped_ptr
<ResourceDispatcherHostImpl
> resource_dispatcher_host_
;
203 scoped_ptr
<SpeechRecognitionManagerImpl
> speech_recognition_manager_
;
204 scoped_ptr
<TimeZoneMonitor
> time_zone_monitor_
;
206 // Members initialized in |RunMainMessageLoopParts()| ------------------------
207 scoped_ptr
<BrowserProcessSubThread
> db_thread_
;
208 scoped_ptr
<BrowserProcessSubThread
> file_user_blocking_thread_
;
209 scoped_ptr
<BrowserProcessSubThread
> file_thread_
;
210 scoped_ptr
<BrowserProcessSubThread
> process_launcher_thread_
;
211 scoped_ptr
<BrowserProcessSubThread
> cache_thread_
;
212 scoped_ptr
<BrowserProcessSubThread
> io_thread_
;
213 scoped_ptr
<base::Thread
> indexed_db_thread_
;
214 scoped_ptr
<MemoryObserver
> memory_observer_
;
215 scoped_ptr
<base::trace_event::TraceMemoryController
> trace_memory_controller_
;
216 scoped_ptr
<base::trace_event::TraceEventSystemStatsMonitor
>
217 system_stats_monitor_
;
219 bool is_tracing_startup_
;
220 base::FilePath startup_trace_file_
;
222 // This timer initiates trace file saving.
223 base::OneShotTimer
<BrowserMainLoop
> startup_trace_timer_
;
225 DISALLOW_COPY_AND_ASSIGN(BrowserMainLoop
);
228 } // namespace content
230 #endif // CONTENT_BROWSER_BROWSER_MAIN_LOOP_H_