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"
17 class HighResolutionTimerManager;
22 class TraceMemoryController;
23 class TraceEventSystemStatsMonitor;
30 class UserInputMonitor;
34 class NetworkChangeNotifier;
38 class AudioMirroringManager;
39 class BrowserMainParts;
40 class BrowserOnlineStateObserver;
41 class BrowserShutdownImpl;
42 class BrowserThreadImpl;
43 class MediaStreamManager;
44 class ResourceDispatcherHostImpl;
45 class SpeechRecognitionManagerImpl;
46 class StartupTaskRunner;
47 class SystemMessageWindowWin;
48 struct MainFunctionParams;
51 class DeviceMonitorLinux;
52 #elif defined(OS_MACOSX)
53 class DeviceMonitorMac;
56 // Implements the main browser loop stages called from BrowserMainRunner.
57 // See comments in browser_main_parts.h for additional info.
58 class CONTENT_EXPORT BrowserMainLoop {
60 // Returns the current instance. This is used to get access to the getters
61 // that return objects which are owned by this class.
62 static BrowserMainLoop* GetInstance();
64 explicit BrowserMainLoop(const MainFunctionParams& parameters);
65 virtual ~BrowserMainLoop();
69 void EarlyInitialization();
70 void InitializeToolkit();
71 void MainMessageLoopStart();
73 // Create and start running the tasks we need to complete startup. Note that
74 // this can be called more than once (currently only on Android) if we get a
75 // request for synchronous startup while the tasks created by asynchronous
76 // startup are still running.
77 void CreateStartupTasks();
79 // Perform the default message loop run logic.
80 void RunMainMessageLoopParts();
82 // Performs the shutdown sequence, starting with PostMainMessageLoopRun
83 // through stopping threads to PostDestroyThreads.
84 void ShutdownThreadsAndCleanUp();
86 int GetResultCode() const { return result_code_; }
88 media::AudioManager* audio_manager() const { return audio_manager_.get(); }
89 AudioMirroringManager* audio_mirroring_manager() const {
90 return audio_mirroring_manager_.get();
92 MediaStreamManager* media_stream_manager() const {
93 return media_stream_manager_.get();
95 media::UserInputMonitor* user_input_monitor() const {
96 return user_input_monitor_.get();
98 media::MIDIManager* midi_manager() const { return midi_manager_.get(); }
99 base::Thread* indexed_db_thread() const { return indexed_db_thread_.get(); }
102 class MemoryObserver;
103 // For ShutdownThreadsAndCleanUp.
104 friend class BrowserShutdownImpl;
106 void InitializeMainThread();
108 // Called just before creating the threads
109 int PreCreateThreads();
111 // Create all secondary threads.
114 // Called right after the browser threads have been started.
115 int BrowserThreadsStarted();
117 int PreMainMessageLoopRun();
119 void MainMessageLoopRun();
121 // Members initialized on construction ---------------------------------------
122 const MainFunctionParams& parameters_;
123 const CommandLine& parsed_command_line_;
125 // True if the non-UI threads were created.
126 bool created_threads_;
128 // Members initialized in |MainMessageLoopStart()| ---------------------------
129 scoped_ptr<base::MessageLoop> main_message_loop_;
130 scoped_ptr<base::SystemMonitor> system_monitor_;
131 scoped_ptr<base::PowerMonitor> power_monitor_;
132 scoped_ptr<base::HighResolutionTimerManager> hi_res_timer_manager_;
133 scoped_ptr<net::NetworkChangeNotifier> network_change_notifier_;
134 // user_input_monitor_ has to outlive audio_manager_, so declared first.
135 scoped_ptr<media::UserInputMonitor> user_input_monitor_;
136 scoped_ptr<media::AudioManager> audio_manager_;
137 scoped_ptr<media::MIDIManager> midi_manager_;
138 scoped_ptr<AudioMirroringManager> audio_mirroring_manager_;
139 scoped_ptr<MediaStreamManager> media_stream_manager_;
140 // Per-process listener for online state changes.
141 scoped_ptr<BrowserOnlineStateObserver> online_state_observer_;
143 scoped_ptr<SystemMessageWindowWin> system_message_window_;
144 #elif defined(OS_LINUX)
145 scoped_ptr<DeviceMonitorLinux> device_monitor_linux_;
146 #elif defined(OS_MACOSX) && !defined(OS_IOS)
147 scoped_ptr<DeviceMonitorMac> device_monitor_mac_;
149 // The startup task runner is created by CreateStartupTasks()
150 scoped_ptr<StartupTaskRunner> startup_task_runner_;
152 // Destroy parts_ before main_message_loop_ (required) and before other
153 // classes constructed in content (but after main_thread_).
154 scoped_ptr<BrowserMainParts> parts_;
156 // Members initialized in |InitializeMainThread()| ---------------------------
157 // This must get destroyed before other threads that are created in parts_.
158 scoped_ptr<BrowserThreadImpl> main_thread_;
160 // Members initialized in |BrowserThreadsStarted()| --------------------------
161 scoped_ptr<ResourceDispatcherHostImpl> resource_dispatcher_host_;
162 scoped_ptr<SpeechRecognitionManagerImpl> speech_recognition_manager_;
164 // Members initialized in |RunMainMessageLoopParts()| ------------------------
165 scoped_ptr<BrowserProcessSubThread> db_thread_;
166 scoped_ptr<BrowserProcessSubThread> file_user_blocking_thread_;
167 scoped_ptr<BrowserProcessSubThread> file_thread_;
168 scoped_ptr<BrowserProcessSubThread> process_launcher_thread_;
169 scoped_ptr<BrowserProcessSubThread> cache_thread_;
170 scoped_ptr<BrowserProcessSubThread> io_thread_;
171 scoped_ptr<base::Thread> indexed_db_thread_;
172 scoped_ptr<MemoryObserver> memory_observer_;
173 scoped_ptr<base::debug::TraceMemoryController> trace_memory_controller_;
174 scoped_ptr<base::debug::TraceEventSystemStatsMonitor> system_stats_monitor_;
176 DISALLOW_COPY_AND_ASSIGN(BrowserMainLoop);
179 } // namespace content
181 #endif // CONTENT_BROWSER_BROWSER_MAIN_LOOP_H_