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 class MemoryPressureMonitor
;
24 namespace trace_event
{
25 class TraceMemoryController
;
26 class TraceEventSystemStatsMonitor
;
27 } // namespace trace_event
32 class UserInputMonitor
;
39 class NetworkChangeNotifier
;
42 #if defined(USE_OZONE)
44 class ClientNativePixmapFactory
;
49 class BrowserMainParts
;
50 class BrowserOnlineStateObserver
;
51 class BrowserShutdownImpl
;
52 class BrowserThreadImpl
;
53 class MediaStreamManager
;
54 class MojoShellContext
;
55 class ResourceDispatcherHostImpl
;
56 class SpeechRecognitionManagerImpl
;
57 class StartupTaskRunner
;
58 class TimeZoneMonitor
;
59 struct MainFunctionParams
;
61 #if defined(OS_ANDROID)
62 class ScreenOrientationDelegate
;
63 #elif defined(OS_LINUX)
64 class DeviceMonitorLinux
;
65 #elif defined(OS_MACOSX)
66 class DeviceMonitorMac
;
68 class SystemMessageWindowWin
;
71 // Implements the main browser loop stages called from BrowserMainRunner.
72 // See comments in browser_main_parts.h for additional info.
73 class CONTENT_EXPORT BrowserMainLoop
{
75 // Returns the current instance. This is used to get access to the getters
76 // that return objects which are owned by this class.
77 static BrowserMainLoop
* GetInstance();
79 explicit BrowserMainLoop(const MainFunctionParams
& parameters
);
80 virtual ~BrowserMainLoop();
84 void EarlyInitialization();
86 // Initializes the toolkit. Returns whether the toolkit initialization was
88 bool InitializeToolkit();
90 void PreMainMessageLoopStart();
91 void MainMessageLoopStart();
92 void PostMainMessageLoopStart();
94 // Create and start running the tasks we need to complete startup. Note that
95 // this can be called more than once (currently only on Android) if we get a
96 // request for synchronous startup while the tasks created by asynchronous
97 // startup are still running.
98 void CreateStartupTasks();
100 // Perform the default message loop run logic.
101 void RunMainMessageLoopParts();
103 // Performs the shutdown sequence, starting with PostMainMessageLoopRun
104 // through stopping threads to PostDestroyThreads.
105 void ShutdownThreadsAndCleanUp();
107 int GetResultCode() const { return result_code_
; }
109 media::AudioManager
* audio_manager() const { return audio_manager_
.get(); }
110 MediaStreamManager
* media_stream_manager() const {
111 return media_stream_manager_
.get();
113 media::UserInputMonitor
* user_input_monitor() const {
114 return user_input_monitor_
.get();
116 media::midi::MidiManager
* midi_manager() const { return midi_manager_
.get(); }
117 base::Thread
* indexed_db_thread() const { return indexed_db_thread_
.get(); }
119 bool is_tracing_startup_for_duration() const {
120 return is_tracing_startup_for_duration_
;
123 const base::FilePath
& startup_trace_file() const {
124 return startup_trace_file_
;
127 void StopStartupTracingTimer();
129 #if defined(OS_MACOSX) && !defined(OS_IOS)
130 DeviceMonitorMac
* device_monitor_mac() const {
131 return device_monitor_mac_
.get();
136 class MemoryObserver
;
137 // For ShutdownThreadsAndCleanUp.
138 friend class BrowserShutdownImpl
;
140 void InitializeMainThread();
142 // Called just before creating the threads
143 int PreCreateThreads();
145 // Create all secondary threads.
148 // Called right after the browser threads have been started.
149 int BrowserThreadsStarted();
151 int PreMainMessageLoopRun();
153 void MainMessageLoopRun();
155 base::FilePath
GetStartupTraceFileName(
156 const base::CommandLine
& command_line
) const;
157 void InitStartupTracingForDuration(const base::CommandLine
& command_line
);
158 void EndStartupTracing();
160 bool UsingInProcessGpu() const;
162 // Quick reference for initialization order:
165 // EarlyInitialization()
166 // InitializeToolkit()
167 // PreMainMessageLoopStart()
168 // MainMessageLoopStart()
169 // InitializeMainThread()
170 // PostMainMessageLoopStart()
171 // InitStartupTracingForDuration()
172 // CreateStartupTasks()
173 // PreCreateThreads()
175 // BrowserThreadsStarted()
177 // Members initialized on construction ---------------------------------------
178 const MainFunctionParams
& parameters_
;
179 const base::CommandLine
& parsed_command_line_
;
181 bool created_threads_
; // True if the non-UI threads were created.
182 bool is_tracing_startup_for_duration_
;
184 // Members initialized in |MainMessageLoopStart()| ---------------------------
185 scoped_ptr
<base::MessageLoop
> main_message_loop_
;
187 // Members initialized in |PostMainMessageLoopStart()| -----------------------
188 scoped_ptr
<base::SystemMonitor
> system_monitor_
;
189 scoped_ptr
<base::PowerMonitor
> power_monitor_
;
190 scoped_ptr
<base::HighResolutionTimerManager
> hi_res_timer_manager_
;
191 scoped_ptr
<net::NetworkChangeNotifier
> network_change_notifier_
;
193 // Per-process listener for online state changes.
194 scoped_ptr
<BrowserOnlineStateObserver
> online_state_observer_
;
196 scoped_ptr
<base::trace_event::TraceEventSystemStatsMonitor
>
197 system_stats_monitor_
;
200 scoped_ptr
<SystemMessageWindowWin
> system_message_window_
;
203 #if defined(OS_ANDROID)
204 // Android implementation of ScreenOrientationDelegate
205 scoped_ptr
<ScreenOrientationDelegate
> screen_orientation_delegate_
;
208 scoped_ptr
<MemoryObserver
> memory_observer_
;
209 scoped_ptr
<base::trace_event::TraceMemoryController
> trace_memory_controller_
;
211 // Members initialized in |InitStartupTracingForDuration()| ------------------
212 base::FilePath startup_trace_file_
;
214 // This timer initiates trace file saving.
215 base::OneShotTimer
<BrowserMainLoop
> startup_trace_timer_
;
217 // Members initialized in |Init()| -------------------------------------------
218 // Destroy |parts_| before |main_message_loop_| (required) and before other
219 // classes constructed in content (but after |main_thread_|).
220 scoped_ptr
<BrowserMainParts
> parts_
;
222 // Members initialized in |InitializeMainThread()| ---------------------------
223 // This must get destroyed before other threads that are created in |parts_|.
224 scoped_ptr
<BrowserThreadImpl
> main_thread_
;
226 // Members initialized in |CreateStartupTasks()| -----------------------------
227 scoped_ptr
<StartupTaskRunner
> startup_task_runner_
;
229 // Members initialized in |PreCreateThreads()| -------------------------------
230 // Torn down in ShutdownThreadsAndCleanUp.
231 scoped_ptr
<base::MemoryPressureMonitor
> memory_pressure_monitor_
;
233 // Members initialized in |CreateThreads()| ----------------------------------
234 scoped_ptr
<BrowserProcessSubThread
> db_thread_
;
235 scoped_ptr
<BrowserProcessSubThread
> file_user_blocking_thread_
;
236 scoped_ptr
<BrowserProcessSubThread
> file_thread_
;
237 scoped_ptr
<BrowserProcessSubThread
> process_launcher_thread_
;
238 scoped_ptr
<BrowserProcessSubThread
> cache_thread_
;
239 scoped_ptr
<BrowserProcessSubThread
> io_thread_
;
241 // Members initialized in |BrowserThreadsStarted()| --------------------------
242 scoped_ptr
<base::Thread
> indexed_db_thread_
;
243 scoped_ptr
<MojoShellContext
> mojo_shell_context_
;
245 // |user_input_monitor_| has to outlive |audio_manager_|, so declared first.
246 scoped_ptr
<media::UserInputMonitor
> user_input_monitor_
;
247 scoped_ptr
<media::AudioManager
> audio_manager_
;
249 scoped_ptr
<media::midi::MidiManager
> midi_manager_
;
251 #if defined(USE_UDEV)
252 scoped_ptr
<DeviceMonitorLinux
> device_monitor_linux_
;
253 #elif defined(OS_MACOSX) && !defined(OS_IOS)
254 scoped_ptr
<DeviceMonitorMac
> device_monitor_mac_
;
256 #if defined(USE_OZONE)
257 scoped_ptr
<ui::ClientNativePixmapFactory
> client_native_pixmap_factory_
;
260 scoped_ptr
<ResourceDispatcherHostImpl
> resource_dispatcher_host_
;
261 scoped_ptr
<MediaStreamManager
> media_stream_manager_
;
262 scoped_ptr
<SpeechRecognitionManagerImpl
> speech_recognition_manager_
;
263 scoped_ptr
<TimeZoneMonitor
> time_zone_monitor_
;
265 // DO NOT add members here. Add them to the right categories above.
267 DISALLOW_COPY_AND_ASSIGN(BrowserMainLoop
);
270 } // namespace content
272 #endif // CONTENT_BROWSER_BROWSER_MAIN_LOOP_H_