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() const { return is_tracing_startup_
; }
121 const base::FilePath
& startup_trace_file() const {
122 return startup_trace_file_
;
125 void StopStartupTracingTimer();
127 #if defined(OS_MACOSX) && !defined(OS_IOS)
128 DeviceMonitorMac
* device_monitor_mac() const {
129 return device_monitor_mac_
.get();
134 class MemoryObserver
;
135 // For ShutdownThreadsAndCleanUp.
136 friend class BrowserShutdownImpl
;
138 void InitializeMainThread();
140 // Called just before creating the threads
141 int PreCreateThreads();
143 // Create all secondary threads.
146 // Called right after the browser threads have been started.
147 int BrowserThreadsStarted();
149 int PreMainMessageLoopRun();
151 void MainMessageLoopRun();
153 base::FilePath
GetStartupTraceFileName(
154 const base::CommandLine
& command_line
) const;
155 void InitStartupTracing(const base::CommandLine
& command_line
);
156 void EndStartupTracing();
158 bool UsingInProcessGpu() const;
160 // Quick reference for initialization order:
163 // EarlyInitialization()
164 // InitializeToolkit()
165 // PreMainMessageLoopStart()
166 // MainMessageLoopStart()
167 // InitializeMainThread()
168 // PostMainMessageLoopStart()
169 // InitStartupTracing()
170 // CreateStartupTasks()
171 // PreCreateThreads()
173 // BrowserThreadsStarted()
175 // Members initialized on construction ---------------------------------------
176 const MainFunctionParams
& parameters_
;
177 const base::CommandLine
& parsed_command_line_
;
179 bool created_threads_
; // True if the non-UI threads were created.
180 bool is_tracing_startup_
;
182 // Members initialized in |MainMessageLoopStart()| ---------------------------
183 scoped_ptr
<base::MessageLoop
> main_message_loop_
;
185 // Members initialized in |PostMainMessageLoopStart()| -----------------------
186 scoped_ptr
<base::SystemMonitor
> system_monitor_
;
187 scoped_ptr
<base::PowerMonitor
> power_monitor_
;
188 scoped_ptr
<base::HighResolutionTimerManager
> hi_res_timer_manager_
;
189 scoped_ptr
<net::NetworkChangeNotifier
> network_change_notifier_
;
191 // Per-process listener for online state changes.
192 scoped_ptr
<BrowserOnlineStateObserver
> online_state_observer_
;
194 scoped_ptr
<base::trace_event::TraceEventSystemStatsMonitor
>
195 system_stats_monitor_
;
198 scoped_ptr
<SystemMessageWindowWin
> system_message_window_
;
201 #if defined(OS_ANDROID)
202 // Android implementation of ScreenOrientationDelegate
203 scoped_ptr
<ScreenOrientationDelegate
> screen_orientation_delegate_
;
206 scoped_ptr
<MemoryObserver
> memory_observer_
;
207 scoped_ptr
<base::trace_event::TraceMemoryController
> trace_memory_controller_
;
209 // Members initialized in |InitStartupTracing()| -----------------------------
210 base::FilePath startup_trace_file_
;
212 // This timer initiates trace file saving.
213 base::OneShotTimer
<BrowserMainLoop
> startup_trace_timer_
;
215 // Members initialized in |Init()| -------------------------------------------
216 // Destroy |parts_| before |main_message_loop_| (required) and before other
217 // classes constructed in content (but after |main_thread_|).
218 scoped_ptr
<BrowserMainParts
> parts_
;
220 // Members initialized in |InitializeMainThread()| ---------------------------
221 // This must get destroyed before other threads that are created in |parts_|.
222 scoped_ptr
<BrowserThreadImpl
> main_thread_
;
224 // Members initialized in |CreateStartupTasks()| -----------------------------
225 scoped_ptr
<StartupTaskRunner
> startup_task_runner_
;
227 // Members initialized in |PreCreateThreads()| -------------------------------
228 // Torn down in ShutdownThreadsAndCleanUp.
229 scoped_ptr
<base::MemoryPressureMonitor
> memory_pressure_monitor_
;
231 // Members initialized in |CreateThreads()| ----------------------------------
232 scoped_ptr
<BrowserProcessSubThread
> db_thread_
;
233 scoped_ptr
<BrowserProcessSubThread
> file_user_blocking_thread_
;
234 scoped_ptr
<BrowserProcessSubThread
> file_thread_
;
235 scoped_ptr
<BrowserProcessSubThread
> process_launcher_thread_
;
236 scoped_ptr
<BrowserProcessSubThread
> cache_thread_
;
237 scoped_ptr
<BrowserProcessSubThread
> io_thread_
;
239 // Members initialized in |BrowserThreadsStarted()| --------------------------
240 scoped_ptr
<base::Thread
> indexed_db_thread_
;
241 scoped_ptr
<MojoShellContext
> mojo_shell_context_
;
243 // |user_input_monitor_| has to outlive |audio_manager_|, so declared first.
244 scoped_ptr
<media::UserInputMonitor
> user_input_monitor_
;
245 scoped_ptr
<media::AudioManager
> audio_manager_
;
247 scoped_ptr
<media::midi::MidiManager
> midi_manager_
;
249 #if defined(USE_UDEV)
250 scoped_ptr
<DeviceMonitorLinux
> device_monitor_linux_
;
251 #elif defined(OS_MACOSX) && !defined(OS_IOS)
252 scoped_ptr
<DeviceMonitorMac
> device_monitor_mac_
;
254 #if defined(USE_OZONE)
255 scoped_ptr
<ui::ClientNativePixmapFactory
> client_native_pixmap_factory_
;
258 scoped_ptr
<ResourceDispatcherHostImpl
> resource_dispatcher_host_
;
259 scoped_ptr
<MediaStreamManager
> media_stream_manager_
;
260 scoped_ptr
<SpeechRecognitionManagerImpl
> speech_recognition_manager_
;
261 scoped_ptr
<TimeZoneMonitor
> time_zone_monitor_
;
263 // DO NOT add members here. Add them to the right categories above.
265 DISALLOW_COPY_AND_ASSIGN(BrowserMainLoop
);
268 } // namespace content
270 #endif // CONTENT_BROWSER_BROWSER_MAIN_LOOP_H_