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
;
26 namespace trace_event
{
27 class TraceMemoryController
;
28 class TraceEventSystemStatsMonitor
;
29 } // namespace trace_event
35 class UserInputMonitor
;
39 class NetworkChangeNotifier
;
43 class BrowserMainParts
;
44 class BrowserOnlineStateObserver
;
45 class BrowserShutdownImpl
;
46 class BrowserThreadImpl
;
47 class MediaStreamManager
;
48 class ResourceDispatcherHostImpl
;
49 class SpeechRecognitionManagerImpl
;
50 class StartupTaskRunner
;
51 class TimeZoneMonitor
;
52 struct MainFunctionParams
;
54 #if defined(OS_ANDROID)
55 class ScreenOrientationDelegate
;
56 #elif defined(OS_LINUX)
57 class DeviceMonitorLinux
;
58 #elif defined(OS_MACOSX)
59 class DeviceMonitorMac
;
61 class SystemMessageWindowWin
;
64 // Implements the main browser loop stages called from BrowserMainRunner.
65 // See comments in browser_main_parts.h for additional info.
66 class CONTENT_EXPORT BrowserMainLoop
{
68 // Returns the current instance. This is used to get access to the getters
69 // that return objects which are owned by this class.
70 static BrowserMainLoop
* GetInstance();
72 explicit BrowserMainLoop(const MainFunctionParams
& parameters
);
73 virtual ~BrowserMainLoop();
77 void EarlyInitialization();
78 // Initializes the toolkit. Returns whether the toolkit initialization was
80 bool InitializeToolkit();
81 void MainMessageLoopStart();
83 // Create and start running the tasks we need to complete startup. Note that
84 // this can be called more than once (currently only on Android) if we get a
85 // request for synchronous startup while the tasks created by asynchronous
86 // startup are still running.
87 void CreateStartupTasks();
89 // Perform the default message loop run logic.
90 void RunMainMessageLoopParts();
92 // Performs the shutdown sequence, starting with PostMainMessageLoopRun
93 // through stopping threads to PostDestroyThreads.
94 void ShutdownThreadsAndCleanUp();
96 int GetResultCode() const { return result_code_
; }
98 media::AudioManager
* audio_manager() const { return audio_manager_
.get(); }
99 MediaStreamManager
* media_stream_manager() const {
100 return media_stream_manager_
.get();
102 media::UserInputMonitor
* user_input_monitor() const {
103 return user_input_monitor_
.get();
105 media::MidiManager
* midi_manager() const { return midi_manager_
.get(); }
106 base::Thread
* indexed_db_thread() const { return indexed_db_thread_
.get(); }
108 bool is_tracing_startup() const { return is_tracing_startup_
; }
110 const base::FilePath
& startup_trace_file() const {
111 return startup_trace_file_
;
114 void StopStartupTracingTimer();
116 #if defined(OS_MACOSX) && !defined(OS_IOS)
117 DeviceMonitorMac
* device_monitor_mac() const {
118 return device_monitor_mac_
.get();
123 class MemoryObserver
;
124 // For ShutdownThreadsAndCleanUp.
125 friend class BrowserShutdownImpl
;
127 void InitializeMainThread();
129 // Called just before creating the threads
130 int PreCreateThreads();
132 // Create all secondary threads.
135 // Called right after the browser threads have been started.
136 int BrowserThreadsStarted();
138 int PreMainMessageLoopRun();
140 void MainMessageLoopRun();
142 base::FilePath
GetStartupTraceFileName(
143 const base::CommandLine
& command_line
) const;
144 void InitStartupTracing(const base::CommandLine
& command_line
);
145 void EndStartupTracing();
147 bool UsingInProcessGpu() const;
148 void InitializeGpuDataManager();
150 // Members initialized on construction ---------------------------------------
151 const MainFunctionParams
& parameters_
;
152 const base::CommandLine
& parsed_command_line_
;
154 // True if the non-UI threads were created.
155 bool created_threads_
;
157 // Members initialized in |MainMessageLoopStart()| ---------------------------
158 scoped_ptr
<base::MessageLoop
> main_message_loop_
;
159 scoped_ptr
<base::SystemMonitor
> system_monitor_
;
160 scoped_ptr
<base::PowerMonitor
> power_monitor_
;
161 scoped_ptr
<base::HighResolutionTimerManager
> hi_res_timer_manager_
;
162 scoped_ptr
<net::NetworkChangeNotifier
> network_change_notifier_
;
163 // user_input_monitor_ has to outlive audio_manager_, so declared first.
164 scoped_ptr
<media::UserInputMonitor
> user_input_monitor_
;
165 scoped_ptr
<media::AudioManager
> audio_manager_
;
166 scoped_ptr
<media::MidiManager
> midi_manager_
;
167 scoped_ptr
<MediaStreamManager
> media_stream_manager_
;
168 // Per-process listener for online state changes.
169 scoped_ptr
<BrowserOnlineStateObserver
> online_state_observer_
;
171 scoped_ptr
<SystemMessageWindowWin
> system_message_window_
;
172 #elif defined(USE_UDEV)
173 scoped_ptr
<DeviceMonitorLinux
> device_monitor_linux_
;
174 #elif defined(OS_MACOSX) && !defined(OS_IOS)
175 scoped_ptr
<DeviceMonitorMac
> device_monitor_mac_
;
177 #if defined(OS_ANDROID)
178 // Android implementation of ScreenOrientationDelegate
179 scoped_ptr
<ScreenOrientationDelegate
> screen_orientation_delegate_
;
181 #if defined(OS_CHROMEOS)
182 scoped_ptr
<base::MemoryPressureMonitorChromeOS
> memory_pressure_monitor_
;
184 // The startup task runner is created by CreateStartupTasks()
185 scoped_ptr
<StartupTaskRunner
> startup_task_runner_
;
187 // Destroy parts_ before main_message_loop_ (required) and before other
188 // classes constructed in content (but after main_thread_).
189 scoped_ptr
<BrowserMainParts
> parts_
;
191 // Members initialized in |InitializeMainThread()| ---------------------------
192 // This must get destroyed before other threads that are created in parts_.
193 scoped_ptr
<BrowserThreadImpl
> main_thread_
;
195 // Members initialized in |BrowserThreadsStarted()| --------------------------
196 scoped_ptr
<ResourceDispatcherHostImpl
> resource_dispatcher_host_
;
197 scoped_ptr
<SpeechRecognitionManagerImpl
> speech_recognition_manager_
;
198 scoped_ptr
<TimeZoneMonitor
> time_zone_monitor_
;
200 // Members initialized in |RunMainMessageLoopParts()| ------------------------
201 scoped_ptr
<BrowserProcessSubThread
> db_thread_
;
202 scoped_ptr
<BrowserProcessSubThread
> file_user_blocking_thread_
;
203 scoped_ptr
<BrowserProcessSubThread
> file_thread_
;
204 scoped_ptr
<BrowserProcessSubThread
> process_launcher_thread_
;
205 scoped_ptr
<BrowserProcessSubThread
> cache_thread_
;
206 scoped_ptr
<BrowserProcessSubThread
> io_thread_
;
207 scoped_ptr
<base::Thread
> indexed_db_thread_
;
208 scoped_ptr
<MemoryObserver
> memory_observer_
;
209 scoped_ptr
<base::trace_event::TraceMemoryController
> trace_memory_controller_
;
210 scoped_ptr
<base::trace_event::TraceEventSystemStatsMonitor
>
211 system_stats_monitor_
;
213 bool is_tracing_startup_
;
214 base::FilePath startup_trace_file_
;
216 // This timer initiates trace file saving.
217 base::OneShotTimer
<BrowserMainLoop
> startup_trace_timer_
;
219 DISALLOW_COPY_AND_ASSIGN(BrowserMainLoop
);
222 } // namespace content
224 #endif // CONTENT_BROWSER_BROWSER_MAIN_LOOP_H_