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
;
43 class BrowserMainParts
;
44 class BrowserOnlineStateObserver
;
45 class BrowserShutdownImpl
;
46 class BrowserThreadImpl
;
47 class MediaStreamManager
;
48 class MojoShellContext
;
49 class ResourceDispatcherHostImpl
;
50 class SpeechRecognitionManagerImpl
;
51 class StartupTaskRunner
;
52 class TimeZoneMonitor
;
53 struct MainFunctionParams
;
55 #if defined(OS_ANDROID)
56 class ScreenOrientationDelegate
;
57 #elif defined(OS_LINUX)
58 class DeviceMonitorLinux
;
59 #elif defined(OS_MACOSX)
60 class DeviceMonitorMac
;
62 class SystemMessageWindowWin
;
65 // Implements the main browser loop stages called from BrowserMainRunner.
66 // See comments in browser_main_parts.h for additional info.
67 class CONTENT_EXPORT BrowserMainLoop
{
69 // Returns the current instance. This is used to get access to the getters
70 // that return objects which are owned by this class.
71 static BrowserMainLoop
* GetInstance();
73 explicit BrowserMainLoop(const MainFunctionParams
& parameters
);
74 virtual ~BrowserMainLoop();
78 void EarlyInitialization();
80 // Initializes the toolkit. Returns whether the toolkit initialization was
82 bool InitializeToolkit();
84 void PreMainMessageLoopStart();
85 void MainMessageLoopStart();
86 void PostMainMessageLoopStart();
88 // Create and start running the tasks we need to complete startup. Note that
89 // this can be called more than once (currently only on Android) if we get a
90 // request for synchronous startup while the tasks created by asynchronous
91 // startup are still running.
92 void CreateStartupTasks();
94 // Perform the default message loop run logic.
95 void RunMainMessageLoopParts();
97 // Performs the shutdown sequence, starting with PostMainMessageLoopRun
98 // through stopping threads to PostDestroyThreads.
99 void ShutdownThreadsAndCleanUp();
101 int GetResultCode() const { return result_code_
; }
103 media::AudioManager
* audio_manager() const { return audio_manager_
.get(); }
104 MediaStreamManager
* media_stream_manager() const {
105 return media_stream_manager_
.get();
107 media::UserInputMonitor
* user_input_monitor() const {
108 return user_input_monitor_
.get();
110 media::midi::MidiManager
* midi_manager() const { return midi_manager_
.get(); }
111 base::Thread
* indexed_db_thread() const { return indexed_db_thread_
.get(); }
113 bool is_tracing_startup() const { return is_tracing_startup_
; }
115 const base::FilePath
& startup_trace_file() const {
116 return startup_trace_file_
;
119 void StopStartupTracingTimer();
121 #if defined(OS_MACOSX) && !defined(OS_IOS)
122 DeviceMonitorMac
* device_monitor_mac() const {
123 return device_monitor_mac_
.get();
128 class MemoryObserver
;
129 // For ShutdownThreadsAndCleanUp.
130 friend class BrowserShutdownImpl
;
132 void InitializeMainThread();
134 // Called just before creating the threads
135 int PreCreateThreads();
137 // Create all secondary threads.
140 // Called right after the browser threads have been started.
141 int BrowserThreadsStarted();
143 int PreMainMessageLoopRun();
145 void MainMessageLoopRun();
147 base::FilePath
GetStartupTraceFileName(
148 const base::CommandLine
& command_line
) const;
149 void InitStartupTracing(const base::CommandLine
& command_line
);
150 void EndStartupTracing();
152 bool UsingInProcessGpu() const;
154 // Quick reference for initialization order:
157 // EarlyInitialization()
158 // InitializeToolkit()
159 // PreMainMessageLoopStart()
160 // MainMessageLoopStart()
161 // InitializeMainThread()
162 // PostMainMessageLoopStart()
163 // InitStartupTracing()
164 // CreateStartupTasks()
165 // PreCreateThreads()
167 // BrowserThreadsStarted()
169 // Members initialized on construction ---------------------------------------
170 const MainFunctionParams
& parameters_
;
171 const base::CommandLine
& parsed_command_line_
;
173 bool created_threads_
; // True if the non-UI threads were created.
174 bool is_tracing_startup_
;
176 // Members initialized in |MainMessageLoopStart()| ---------------------------
177 scoped_ptr
<base::MessageLoop
> main_message_loop_
;
179 // Members initialized in |PostMainMessageLoopStart()| -----------------------
180 scoped_ptr
<base::SystemMonitor
> system_monitor_
;
181 scoped_ptr
<base::PowerMonitor
> power_monitor_
;
182 scoped_ptr
<base::HighResolutionTimerManager
> hi_res_timer_manager_
;
183 scoped_ptr
<net::NetworkChangeNotifier
> network_change_notifier_
;
185 // Per-process listener for online state changes.
186 scoped_ptr
<BrowserOnlineStateObserver
> online_state_observer_
;
188 scoped_ptr
<base::trace_event::TraceEventSystemStatsMonitor
>
189 system_stats_monitor_
;
192 scoped_ptr
<SystemMessageWindowWin
> system_message_window_
;
195 #if defined(OS_ANDROID)
196 // Android implementation of ScreenOrientationDelegate
197 scoped_ptr
<ScreenOrientationDelegate
> screen_orientation_delegate_
;
200 scoped_ptr
<MemoryObserver
> memory_observer_
;
201 scoped_ptr
<base::trace_event::TraceMemoryController
> trace_memory_controller_
;
203 // Members initialized in |InitStartupTracing()| -----------------------------
204 base::FilePath startup_trace_file_
;
206 // This timer initiates trace file saving.
207 base::OneShotTimer
<BrowserMainLoop
> startup_trace_timer_
;
209 // Members initialized in |Init()| -------------------------------------------
210 // Destroy |parts_| before |main_message_loop_| (required) and before other
211 // classes constructed in content (but after |main_thread_|).
212 scoped_ptr
<BrowserMainParts
> parts_
;
214 // Members initialized in |InitializeMainThread()| ---------------------------
215 // This must get destroyed before other threads that are created in |parts_|.
216 scoped_ptr
<BrowserThreadImpl
> main_thread_
;
218 // Members initialized in |CreateStartupTasks()| -----------------------------
219 scoped_ptr
<StartupTaskRunner
> startup_task_runner_
;
221 // Members initialized in |PreCreateThreads()| -------------------------------
222 // Torn down in ShutdownThreadsAndCleanUp.
223 scoped_ptr
<base::MemoryPressureMonitor
> memory_pressure_monitor_
;
225 // Members initialized in |CreateThreads()| ----------------------------------
226 scoped_ptr
<BrowserProcessSubThread
> db_thread_
;
227 scoped_ptr
<BrowserProcessSubThread
> file_user_blocking_thread_
;
228 scoped_ptr
<BrowserProcessSubThread
> file_thread_
;
229 scoped_ptr
<BrowserProcessSubThread
> process_launcher_thread_
;
230 scoped_ptr
<BrowserProcessSubThread
> cache_thread_
;
231 scoped_ptr
<BrowserProcessSubThread
> io_thread_
;
233 // Members initialized in |BrowserThreadsStarted()| --------------------------
234 scoped_ptr
<base::Thread
> indexed_db_thread_
;
235 scoped_ptr
<MojoShellContext
> mojo_shell_context_
;
237 // |user_input_monitor_| has to outlive |audio_manager_|, so declared first.
238 scoped_ptr
<media::UserInputMonitor
> user_input_monitor_
;
239 scoped_ptr
<media::AudioManager
> audio_manager_
;
241 scoped_ptr
<media::midi::MidiManager
> midi_manager_
;
243 #if defined(USE_UDEV)
244 scoped_ptr
<DeviceMonitorLinux
> device_monitor_linux_
;
245 #elif defined(OS_MACOSX) && !defined(OS_IOS)
246 scoped_ptr
<DeviceMonitorMac
> device_monitor_mac_
;
249 scoped_ptr
<ResourceDispatcherHostImpl
> resource_dispatcher_host_
;
250 scoped_ptr
<MediaStreamManager
> media_stream_manager_
;
251 scoped_ptr
<SpeechRecognitionManagerImpl
> speech_recognition_manager_
;
252 scoped_ptr
<TimeZoneMonitor
> time_zone_monitor_
;
254 // DO NOT add members here. Add them to the right categories above.
256 DISALLOW_COPY_AND_ASSIGN(BrowserMainLoop
);
259 } // namespace content
261 #endif // CONTENT_BROWSER_BROWSER_MAIN_LOOP_H_