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 MemoryPressureObserverChromeOS
;
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();
121 #if defined(OS_CHROMEOS)
122 // Return the MemoryPressureObserver which might be NULL.
123 base::MemoryPressureObserverChromeOS
* memory_pressure_observer() {
124 return memory_pressure_observer_
.get();
129 class MemoryObserver
;
130 // For ShutdownThreadsAndCleanUp.
131 friend class BrowserShutdownImpl
;
133 void InitializeMainThread();
135 // Called just before creating the threads
136 int PreCreateThreads();
138 // Create all secondary threads.
141 // Called right after the browser threads have been started.
142 int BrowserThreadsStarted();
144 int PreMainMessageLoopRun();
146 void MainMessageLoopRun();
148 base::FilePath
GetStartupTraceFileName(
149 const base::CommandLine
& command_line
) const;
150 void InitStartupTracing(const base::CommandLine
& command_line
);
151 void EndStartupTracing();
153 bool UsingInProcessGpu() const;
154 void InitializeGpuDataManager();
156 // Members initialized on construction ---------------------------------------
157 const MainFunctionParams
& parameters_
;
158 const base::CommandLine
& parsed_command_line_
;
160 // True if the non-UI threads were created.
161 bool created_threads_
;
163 // Members initialized in |MainMessageLoopStart()| ---------------------------
164 scoped_ptr
<base::MessageLoop
> main_message_loop_
;
165 scoped_ptr
<base::SystemMonitor
> system_monitor_
;
166 scoped_ptr
<base::PowerMonitor
> power_monitor_
;
167 scoped_ptr
<base::HighResolutionTimerManager
> hi_res_timer_manager_
;
168 scoped_ptr
<net::NetworkChangeNotifier
> network_change_notifier_
;
169 // user_input_monitor_ has to outlive audio_manager_, so declared first.
170 scoped_ptr
<media::UserInputMonitor
> user_input_monitor_
;
171 scoped_ptr
<media::AudioManager
> audio_manager_
;
172 scoped_ptr
<media::MidiManager
> midi_manager_
;
173 scoped_ptr
<MediaStreamManager
> media_stream_manager_
;
174 // Per-process listener for online state changes.
175 scoped_ptr
<BrowserOnlineStateObserver
> online_state_observer_
;
177 scoped_ptr
<SystemMessageWindowWin
> system_message_window_
;
178 #elif defined(USE_UDEV)
179 scoped_ptr
<DeviceMonitorLinux
> device_monitor_linux_
;
180 #elif defined(OS_MACOSX) && !defined(OS_IOS)
181 scoped_ptr
<DeviceMonitorMac
> device_monitor_mac_
;
183 #if defined(OS_ANDROID)
184 // Android implementation of ScreenOrientationDelegate
185 scoped_ptr
<ScreenOrientationDelegate
> screen_orientation_delegate_
;
187 #if defined(OS_CHROMEOS)
188 scoped_ptr
<base::MemoryPressureObserverChromeOS
> memory_pressure_observer_
;
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_