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
;
52 class DeviceMonitorLinux
;
53 #elif defined(OS_MACOSX)
54 class DeviceMonitorMac
;
56 class SystemMessageWindowWin
;
59 // Implements the main browser loop stages called from BrowserMainRunner.
60 // See comments in browser_main_parts.h for additional info.
61 class CONTENT_EXPORT BrowserMainLoop
{
63 // Returns the current instance. This is used to get access to the getters
64 // that return objects which are owned by this class.
65 static BrowserMainLoop
* GetInstance();
67 explicit BrowserMainLoop(const MainFunctionParams
& parameters
);
68 virtual ~BrowserMainLoop();
72 void EarlyInitialization();
73 // Initializes the toolkit. Returns whether the toolkit initialization was
75 bool InitializeToolkit();
76 void MainMessageLoopStart();
78 // Create and start running the tasks we need to complete startup. Note that
79 // this can be called more than once (currently only on Android) if we get a
80 // request for synchronous startup while the tasks created by asynchronous
81 // startup are still running.
82 void CreateStartupTasks();
84 // Perform the default message loop run logic.
85 void RunMainMessageLoopParts();
87 // Performs the shutdown sequence, starting with PostMainMessageLoopRun
88 // through stopping threads to PostDestroyThreads.
89 void ShutdownThreadsAndCleanUp();
91 int GetResultCode() const { return result_code_
; }
93 media::AudioManager
* audio_manager() const { return audio_manager_
.get(); }
94 MediaStreamManager
* media_stream_manager() const {
95 return media_stream_manager_
.get();
97 media::UserInputMonitor
* user_input_monitor() const {
98 return user_input_monitor_
.get();
100 media::MidiManager
* midi_manager() const { return midi_manager_
.get(); }
101 base::Thread
* indexed_db_thread() const { return indexed_db_thread_
.get(); }
103 bool is_tracing_startup() const { return is_tracing_startup_
; }
105 const base::FilePath
& startup_trace_file() const {
106 return startup_trace_file_
;
109 void StopStartupTracingTimer();
111 #if defined(OS_MACOSX) && !defined(OS_IOS)
112 DeviceMonitorMac
* device_monitor_mac() const {
113 return device_monitor_mac_
.get();
118 class MemoryObserver
;
119 // For ShutdownThreadsAndCleanUp.
120 friend class BrowserShutdownImpl
;
122 void InitializeMainThread();
124 // Called just before creating the threads
125 int PreCreateThreads();
127 // Create all secondary threads.
130 // Called right after the browser threads have been started.
131 int BrowserThreadsStarted();
133 int PreMainMessageLoopRun();
135 void MainMessageLoopRun();
137 base::FilePath
GetStartupTraceFileName(
138 const base::CommandLine
& command_line
) const;
139 void InitStartupTracing(const base::CommandLine
& command_line
);
140 void EndStartupTracing();
142 // Members initialized on construction ---------------------------------------
143 const MainFunctionParams
& parameters_
;
144 const base::CommandLine
& parsed_command_line_
;
146 // True if the non-UI threads were created.
147 bool created_threads_
;
149 // Members initialized in |MainMessageLoopStart()| ---------------------------
150 scoped_ptr
<base::MessageLoop
> main_message_loop_
;
151 scoped_ptr
<base::SystemMonitor
> system_monitor_
;
152 scoped_ptr
<base::PowerMonitor
> power_monitor_
;
153 scoped_ptr
<base::HighResolutionTimerManager
> hi_res_timer_manager_
;
154 scoped_ptr
<net::NetworkChangeNotifier
> network_change_notifier_
;
155 // user_input_monitor_ has to outlive audio_manager_, so declared first.
156 scoped_ptr
<media::UserInputMonitor
> user_input_monitor_
;
157 scoped_ptr
<media::AudioManager
> audio_manager_
;
158 scoped_ptr
<media::MidiManager
> midi_manager_
;
159 scoped_ptr
<MediaStreamManager
> media_stream_manager_
;
160 // Per-process listener for online state changes.
161 scoped_ptr
<BrowserOnlineStateObserver
> online_state_observer_
;
163 scoped_ptr
<SystemMessageWindowWin
> system_message_window_
;
164 #elif defined(USE_UDEV)
165 scoped_ptr
<DeviceMonitorLinux
> device_monitor_linux_
;
166 #elif defined(OS_MACOSX) && !defined(OS_IOS)
167 scoped_ptr
<DeviceMonitorMac
> device_monitor_mac_
;
169 // The startup task runner is created by CreateStartupTasks()
170 scoped_ptr
<StartupTaskRunner
> startup_task_runner_
;
172 // Destroy parts_ before main_message_loop_ (required) and before other
173 // classes constructed in content (but after main_thread_).
174 scoped_ptr
<BrowserMainParts
> parts_
;
176 // Members initialized in |InitializeMainThread()| ---------------------------
177 // This must get destroyed before other threads that are created in parts_.
178 scoped_ptr
<BrowserThreadImpl
> main_thread_
;
180 // Members initialized in |BrowserThreadsStarted()| --------------------------
181 scoped_ptr
<ResourceDispatcherHostImpl
> resource_dispatcher_host_
;
182 scoped_ptr
<SpeechRecognitionManagerImpl
> speech_recognition_manager_
;
183 scoped_ptr
<TimeZoneMonitor
> time_zone_monitor_
;
185 // Members initialized in |RunMainMessageLoopParts()| ------------------------
186 scoped_ptr
<BrowserProcessSubThread
> db_thread_
;
187 scoped_ptr
<BrowserProcessSubThread
> file_user_blocking_thread_
;
188 scoped_ptr
<BrowserProcessSubThread
> file_thread_
;
189 scoped_ptr
<BrowserProcessSubThread
> process_launcher_thread_
;
190 scoped_ptr
<BrowserProcessSubThread
> cache_thread_
;
191 scoped_ptr
<BrowserProcessSubThread
> io_thread_
;
192 scoped_ptr
<base::Thread
> indexed_db_thread_
;
193 scoped_ptr
<MemoryObserver
> memory_observer_
;
194 scoped_ptr
<base::debug::TraceMemoryController
> trace_memory_controller_
;
195 scoped_ptr
<base::debug::TraceEventSystemStatsMonitor
> system_stats_monitor_
;
197 bool is_tracing_startup_
;
198 base::FilePath startup_trace_file_
;
200 // This timer initiates trace file saving.
201 base::OneShotTimer
<BrowserMainLoop
> startup_trace_timer_
;
203 DISALLOW_COPY_AND_ASSIGN(BrowserMainLoop
);
206 } // namespace content
208 #endif // CONTENT_BROWSER_BROWSER_MAIN_LOOP_H_