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/memory/ref_counted.h"
10 #include "base/memory/scoped_ptr.h"
11 #include "content/browser/browser_process_sub_thread.h"
12 #include "content/public/browser/browser_main_runner.h"
18 class HighResolutionTimerManager
;
23 class TraceMemoryController
;
24 class TraceEventSystemStatsMonitor
;
31 class UserInputMonitor
;
35 class NetworkChangeNotifier
;
39 class AudioMirroringManager
;
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 SystemMessageWindowWin
;
49 struct MainFunctionParams
;
52 class DeviceMonitorLinux
;
53 #elif defined(OS_MACOSX)
54 class DeviceMonitorMac
;
57 // Implements the main browser loop stages called from BrowserMainRunner.
58 // See comments in browser_main_parts.h for additional info.
59 class CONTENT_EXPORT BrowserMainLoop
{
61 // Returns the current instance. This is used to get access to the getters
62 // that return objects which are owned by this class.
63 static BrowserMainLoop
* GetInstance();
65 explicit BrowserMainLoop(const MainFunctionParams
& parameters
);
66 virtual ~BrowserMainLoop();
70 void EarlyInitialization();
71 void InitializeToolkit();
72 void MainMessageLoopStart();
74 // Create and start running the tasks we need to complete startup. Note that
75 // this can be called more than once (currently only on Android) if we get a
76 // request for synchronous startup while the tasks created by asynchronous
77 // startup are still running.
78 void CreateStartupTasks();
80 // Perform the default message loop run logic.
81 void RunMainMessageLoopParts();
83 // Performs the shutdown sequence, starting with PostMainMessageLoopRun
84 // through stopping threads to PostDestroyThreads.
85 void ShutdownThreadsAndCleanUp();
87 int GetResultCode() const { return result_code_
; }
89 media::AudioManager
* audio_manager() const { return audio_manager_
.get(); }
90 AudioMirroringManager
* audio_mirroring_manager() const {
91 return audio_mirroring_manager_
.get();
93 MediaStreamManager
* media_stream_manager() const {
94 return media_stream_manager_
.get();
96 media::UserInputMonitor
* user_input_monitor() const {
97 return user_input_monitor_
.get();
99 media::MidiManager
* midi_manager() const { return midi_manager_
.get(); }
100 base::Thread
* indexed_db_thread() const { return indexed_db_thread_
.get(); }
102 bool is_tracing_startup() const { return is_tracing_startup_
; }
105 class MemoryObserver
;
106 // For ShutdownThreadsAndCleanUp.
107 friend class BrowserShutdownImpl
;
109 void InitializeMainThread();
111 // Called just before creating the threads
112 int PreCreateThreads();
114 // Create all secondary threads.
117 // Called right after the browser threads have been started.
118 int BrowserThreadsStarted();
120 int PreMainMessageLoopRun();
122 void MainMessageLoopRun();
124 void InitStartupTracing(const CommandLine
& command_line
);
125 void EndStartupTracing(const base::FilePath
& trace_file
);
127 // Members initialized on construction ---------------------------------------
128 const MainFunctionParams
& parameters_
;
129 const CommandLine
& parsed_command_line_
;
131 // True if the non-UI threads were created.
132 bool created_threads_
;
134 // Members initialized in |MainMessageLoopStart()| ---------------------------
135 scoped_ptr
<base::MessageLoop
> main_message_loop_
;
136 scoped_ptr
<base::SystemMonitor
> system_monitor_
;
137 scoped_ptr
<base::PowerMonitor
> power_monitor_
;
138 scoped_ptr
<base::HighResolutionTimerManager
> hi_res_timer_manager_
;
139 scoped_ptr
<net::NetworkChangeNotifier
> network_change_notifier_
;
140 // user_input_monitor_ has to outlive audio_manager_, so declared first.
141 scoped_ptr
<media::UserInputMonitor
> user_input_monitor_
;
142 scoped_ptr
<media::AudioManager
> audio_manager_
;
143 scoped_ptr
<media::MidiManager
> midi_manager_
;
144 scoped_ptr
<AudioMirroringManager
> audio_mirroring_manager_
;
145 scoped_ptr
<MediaStreamManager
> media_stream_manager_
;
146 // Per-process listener for online state changes.
147 scoped_ptr
<BrowserOnlineStateObserver
> online_state_observer_
;
149 scoped_ptr
<SystemMessageWindowWin
> system_message_window_
;
150 #elif defined(USE_UDEV)
151 scoped_ptr
<DeviceMonitorLinux
> device_monitor_linux_
;
152 #elif defined(OS_MACOSX) && !defined(OS_IOS)
153 scoped_ptr
<DeviceMonitorMac
> device_monitor_mac_
;
155 // The startup task runner is created by CreateStartupTasks()
156 scoped_ptr
<StartupTaskRunner
> startup_task_runner_
;
158 // Destroy parts_ before main_message_loop_ (required) and before other
159 // classes constructed in content (but after main_thread_).
160 scoped_ptr
<BrowserMainParts
> parts_
;
162 // Members initialized in |InitializeMainThread()| ---------------------------
163 // This must get destroyed before other threads that are created in parts_.
164 scoped_ptr
<BrowserThreadImpl
> main_thread_
;
166 // Members initialized in |BrowserThreadsStarted()| --------------------------
167 scoped_ptr
<ResourceDispatcherHostImpl
> resource_dispatcher_host_
;
168 scoped_ptr
<SpeechRecognitionManagerImpl
> speech_recognition_manager_
;
170 // Members initialized in |RunMainMessageLoopParts()| ------------------------
171 scoped_ptr
<BrowserProcessSubThread
> db_thread_
;
172 scoped_ptr
<BrowserProcessSubThread
> file_user_blocking_thread_
;
173 scoped_ptr
<BrowserProcessSubThread
> file_thread_
;
174 scoped_ptr
<BrowserProcessSubThread
> process_launcher_thread_
;
175 scoped_ptr
<BrowserProcessSubThread
> cache_thread_
;
176 scoped_ptr
<BrowserProcessSubThread
> io_thread_
;
177 scoped_ptr
<base::Thread
> indexed_db_thread_
;
178 scoped_ptr
<MemoryObserver
> memory_observer_
;
179 scoped_ptr
<base::debug::TraceMemoryController
> trace_memory_controller_
;
180 scoped_ptr
<base::debug::TraceEventSystemStatsMonitor
> system_stats_monitor_
;
182 bool is_tracing_startup_
;
184 DISALLOW_COPY_AND_ASSIGN(BrowserMainLoop
);
187 } // namespace content
189 #endif // CONTENT_BROWSER_BROWSER_MAIN_LOOP_H_