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
;
24 class TraceMemoryController
;
25 class TraceEventSystemStatsMonitor
;
32 class UserInputMonitor
;
36 class NetworkChangeNotifier
;
40 class BrowserMainParts
;
41 class BrowserOnlineStateObserver
;
42 class BrowserShutdownImpl
;
43 class BrowserThreadImpl
;
44 class MediaStreamManager
;
45 class ResourceDispatcherHostImpl
;
46 class SpeechRecognitionManagerImpl
;
47 class StartupTaskRunner
;
48 class TimeZoneMonitor
;
49 struct MainFunctionParams
;
51 #if defined(OS_ANDROID)
52 class ScreenOrientationDelegate
;
53 #elif defined(OS_LINUX)
54 class DeviceMonitorLinux
;
55 #elif defined(OS_MACOSX)
56 class DeviceMonitorMac
;
58 class SystemMessageWindowWin
;
61 // Implements the main browser loop stages called from BrowserMainRunner.
62 // See comments in browser_main_parts.h for additional info.
63 class CONTENT_EXPORT BrowserMainLoop
{
65 // Returns the current instance. This is used to get access to the getters
66 // that return objects which are owned by this class.
67 static BrowserMainLoop
* GetInstance();
69 explicit BrowserMainLoop(const MainFunctionParams
& parameters
);
70 virtual ~BrowserMainLoop();
74 void EarlyInitialization();
75 // Initializes the toolkit. Returns whether the toolkit initialization was
77 bool InitializeToolkit();
78 void MainMessageLoopStart();
80 // Create and start running the tasks we need to complete startup. Note that
81 // this can be called more than once (currently only on Android) if we get a
82 // request for synchronous startup while the tasks created by asynchronous
83 // startup are still running.
84 void CreateStartupTasks();
86 // Perform the default message loop run logic.
87 void RunMainMessageLoopParts();
89 // Performs the shutdown sequence, starting with PostMainMessageLoopRun
90 // through stopping threads to PostDestroyThreads.
91 void ShutdownThreadsAndCleanUp();
93 int GetResultCode() const { return result_code_
; }
95 media::AudioManager
* audio_manager() const { return audio_manager_
.get(); }
96 MediaStreamManager
* media_stream_manager() const {
97 return media_stream_manager_
.get();
99 media::UserInputMonitor
* user_input_monitor() const {
100 return user_input_monitor_
.get();
102 media::MidiManager
* midi_manager() const { return midi_manager_
.get(); }
103 base::Thread
* indexed_db_thread() const { return indexed_db_thread_
.get(); }
105 bool is_tracing_startup() const { return is_tracing_startup_
; }
107 const base::FilePath
& startup_trace_file() const {
108 return startup_trace_file_
;
111 void StopStartupTracingTimer();
113 #if defined(OS_MACOSX) && !defined(OS_IOS)
114 DeviceMonitorMac
* device_monitor_mac() const {
115 return device_monitor_mac_
.get();
120 class MemoryObserver
;
121 // For ShutdownThreadsAndCleanUp.
122 friend class BrowserShutdownImpl
;
124 void InitializeMainThread();
126 // Called just before creating the threads
127 int PreCreateThreads();
129 // Create all secondary threads.
132 // Called right after the browser threads have been started.
133 int BrowserThreadsStarted();
135 int PreMainMessageLoopRun();
137 void MainMessageLoopRun();
139 base::FilePath
GetStartupTraceFileName(
140 const base::CommandLine
& command_line
) const;
141 void InitStartupTracing(const base::CommandLine
& command_line
);
142 void EndStartupTracing();
144 // Members initialized on construction ---------------------------------------
145 const MainFunctionParams
& parameters_
;
146 const base::CommandLine
& parsed_command_line_
;
148 // True if the non-UI threads were created.
149 bool created_threads_
;
151 // Members initialized in |MainMessageLoopStart()| ---------------------------
152 scoped_ptr
<base::MessageLoop
> main_message_loop_
;
153 scoped_ptr
<base::SystemMonitor
> system_monitor_
;
154 scoped_ptr
<base::PowerMonitor
> power_monitor_
;
155 scoped_ptr
<base::HighResolutionTimerManager
> hi_res_timer_manager_
;
156 scoped_ptr
<net::NetworkChangeNotifier
> network_change_notifier_
;
157 // user_input_monitor_ has to outlive audio_manager_, so declared first.
158 scoped_ptr
<media::UserInputMonitor
> user_input_monitor_
;
159 scoped_ptr
<media::AudioManager
> audio_manager_
;
160 scoped_ptr
<media::MidiManager
> midi_manager_
;
161 scoped_ptr
<MediaStreamManager
> media_stream_manager_
;
162 // Per-process listener for online state changes.
163 scoped_ptr
<BrowserOnlineStateObserver
> online_state_observer_
;
165 scoped_ptr
<SystemMessageWindowWin
> system_message_window_
;
166 #elif defined(USE_UDEV)
167 scoped_ptr
<DeviceMonitorLinux
> device_monitor_linux_
;
168 #elif defined(OS_MACOSX) && !defined(OS_IOS)
169 scoped_ptr
<DeviceMonitorMac
> device_monitor_mac_
;
171 #if defined(OS_ANDROID)
172 // Android implementation of ScreenOrientationDelegate
173 scoped_ptr
<ScreenOrientationDelegate
> screen_orientation_delegate_
;
175 // The startup task runner is created by CreateStartupTasks()
176 scoped_ptr
<StartupTaskRunner
> startup_task_runner_
;
178 // Destroy parts_ before main_message_loop_ (required) and before other
179 // classes constructed in content (but after main_thread_).
180 scoped_ptr
<BrowserMainParts
> parts_
;
182 // Members initialized in |InitializeMainThread()| ---------------------------
183 // This must get destroyed before other threads that are created in parts_.
184 scoped_ptr
<BrowserThreadImpl
> main_thread_
;
186 // Members initialized in |BrowserThreadsStarted()| --------------------------
187 scoped_ptr
<ResourceDispatcherHostImpl
> resource_dispatcher_host_
;
188 scoped_ptr
<SpeechRecognitionManagerImpl
> speech_recognition_manager_
;
189 scoped_ptr
<TimeZoneMonitor
> time_zone_monitor_
;
191 // Members initialized in |RunMainMessageLoopParts()| ------------------------
192 scoped_ptr
<BrowserProcessSubThread
> db_thread_
;
193 scoped_ptr
<BrowserProcessSubThread
> file_user_blocking_thread_
;
194 scoped_ptr
<BrowserProcessSubThread
> file_thread_
;
195 scoped_ptr
<BrowserProcessSubThread
> process_launcher_thread_
;
196 scoped_ptr
<BrowserProcessSubThread
> cache_thread_
;
197 scoped_ptr
<BrowserProcessSubThread
> io_thread_
;
198 scoped_ptr
<base::Thread
> indexed_db_thread_
;
199 scoped_ptr
<MemoryObserver
> memory_observer_
;
200 scoped_ptr
<base::debug::TraceMemoryController
> trace_memory_controller_
;
201 scoped_ptr
<base::debug::TraceEventSystemStatsMonitor
> system_stats_monitor_
;
203 bool is_tracing_startup_
;
204 base::FilePath startup_trace_file_
;
206 // This timer initiates trace file saving.
207 base::OneShotTimer
<BrowserMainLoop
> startup_trace_timer_
;
209 DISALLOW_COPY_AND_ASSIGN(BrowserMainLoop
);
212 } // namespace content
214 #endif // CONTENT_BROWSER_BROWSER_MAIN_LOOP_H_