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. 4 5 #ifndef CONTENT_BROWSER_BROWSER_MAIN_LOOP_H_ 6 #define CONTENT_BROWSER_BROWSER_MAIN_LOOP_H_ 7 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" 13 14 namespace base { 15 class CommandLine; 16 class FilePath; 17 class HighResolutionTimerManager; 18 class MessageLoop; 19 class PowerMonitor; 20 class SystemMonitor; 21 namespace debug { 22 class TraceMemoryController; 23 class TraceEventSystemStatsMonitor; 24 } // namespace debug 25 } // namespace base 26 27 namespace media { 28 class AudioManager; 29 class MidiManager; 30 class UserInputMonitor; 31 } // namespace media 32 33 namespace net { 34 class NetworkChangeNotifier; 35 } // namespace net 36 37 namespace content { 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 TimeZoneMonitor; 48 struct MainFunctionParams; 49 50 #if defined(OS_LINUX) 51 class DeviceMonitorLinux; 52 #elif defined(OS_MACOSX) 53 class DeviceMonitorMac; 54 #elif defined(OS_WIN) 55 class SystemMessageWindowWin; 56 #endif 57 58 // Implements the main browser loop stages called from BrowserMainRunner. 59 // See comments in browser_main_parts.h for additional info. 60 class CONTENT_EXPORT BrowserMainLoop { 61 public: 62 // Returns the current instance. This is used to get access to the getters 63 // that return objects which are owned by this class. 64 static BrowserMainLoop* GetInstance(); 65 66 explicit BrowserMainLoop(const MainFunctionParams& parameters); 67 virtual ~BrowserMainLoop(); 68 69 void Init(); 70 71 void EarlyInitialization(); 72 // Initializes the toolkit. Returns whether the toolkit initialization was 73 // successful or not. 74 bool InitializeToolkit(); 75 void MainMessageLoopStart(); 76 77 // Create and start running the tasks we need to complete startup. Note that 78 // this can be called more than once (currently only on Android) if we get a 79 // request for synchronous startup while the tasks created by asynchronous 80 // startup are still running. 81 void CreateStartupTasks(); 82 83 // Perform the default message loop run logic. 84 void RunMainMessageLoopParts(); 85 86 // Performs the shutdown sequence, starting with PostMainMessageLoopRun 87 // through stopping threads to PostDestroyThreads. 88 void ShutdownThreadsAndCleanUp(); 89 90 int GetResultCode() const { return result_code_; } 91 92 media::AudioManager* audio_manager() const { return audio_manager_.get(); } 93 AudioMirroringManager* audio_mirroring_manager() const { 94 return audio_mirroring_manager_.get(); 95 } 96 MediaStreamManager* media_stream_manager() const { 97 return media_stream_manager_.get(); 98 } 99 media::UserInputMonitor* user_input_monitor() const { 100 return user_input_monitor_.get(); 101 } 102 media::MidiManager* midi_manager() const { return midi_manager_.get(); } 103 base::Thread* indexed_db_thread() const { return indexed_db_thread_.get(); } 104 105 bool is_tracing_startup() const { return is_tracing_startup_; } 106 107 #if defined(OS_MACOSX) && !defined(OS_IOS) 108 DeviceMonitorMac* device_monitor_mac() const { 109 return device_monitor_mac_.get(); 110 } 111 #endif 112 113 private: 114 class MemoryObserver; 115 // For ShutdownThreadsAndCleanUp. 116 friend class BrowserShutdownImpl; 117 118 void InitializeMainThread(); 119 120 // Called just before creating the threads 121 int PreCreateThreads(); 122 123 // Create all secondary threads. 124 int CreateThreads(); 125 126 // Called right after the browser threads have been started. 127 int BrowserThreadsStarted(); 128 129 int PreMainMessageLoopRun(); 130 131 void MainMessageLoopRun(); 132 133 void InitStartupTracing(const base::CommandLine& command_line); 134 void EndStartupTracing(const base::FilePath& trace_file); 135 136 // Members initialized on construction --------------------------------------- 137 const MainFunctionParams& parameters_; 138 const base::CommandLine& parsed_command_line_; 139 int result_code_; 140 // True if the non-UI threads were created. 141 bool created_threads_; 142 143 // Members initialized in |MainMessageLoopStart()| --------------------------- 144 scoped_ptr<base::MessageLoop> main_message_loop_; 145 scoped_ptr<base::SystemMonitor> system_monitor_; 146 scoped_ptr<base::PowerMonitor> power_monitor_; 147 scoped_ptr<base::HighResolutionTimerManager> hi_res_timer_manager_; 148 scoped_ptr<net::NetworkChangeNotifier> network_change_notifier_; 149 // user_input_monitor_ has to outlive audio_manager_, so declared first. 150 scoped_ptr<media::UserInputMonitor> user_input_monitor_; 151 scoped_ptr<media::AudioManager> audio_manager_; 152 scoped_ptr<media::MidiManager> midi_manager_; 153 scoped_ptr<AudioMirroringManager> audio_mirroring_manager_; 154 scoped_ptr<MediaStreamManager> media_stream_manager_; 155 // Per-process listener for online state changes. 156 scoped_ptr<BrowserOnlineStateObserver> online_state_observer_; 157 #if defined(OS_WIN) 158 scoped_ptr<SystemMessageWindowWin> system_message_window_; 159 #elif defined(USE_UDEV) 160 scoped_ptr<DeviceMonitorLinux> device_monitor_linux_; 161 #elif defined(OS_MACOSX) && !defined(OS_IOS) 162 scoped_ptr<DeviceMonitorMac> device_monitor_mac_; 163 #endif 164 // The startup task runner is created by CreateStartupTasks() 165 scoped_ptr<StartupTaskRunner> startup_task_runner_; 166 167 // Destroy parts_ before main_message_loop_ (required) and before other 168 // classes constructed in content (but after main_thread_). 169 scoped_ptr<BrowserMainParts> parts_; 170 171 // Members initialized in |InitializeMainThread()| --------------------------- 172 // This must get destroyed before other threads that are created in parts_. 173 scoped_ptr<BrowserThreadImpl> main_thread_; 174 175 // Members initialized in |BrowserThreadsStarted()| -------------------------- 176 scoped_ptr<ResourceDispatcherHostImpl> resource_dispatcher_host_; 177 scoped_ptr<SpeechRecognitionManagerImpl> speech_recognition_manager_; 178 scoped_ptr<TimeZoneMonitor> time_zone_monitor_; 179 180 // Members initialized in |RunMainMessageLoopParts()| ------------------------ 181 scoped_ptr<BrowserProcessSubThread> db_thread_; 182 scoped_ptr<BrowserProcessSubThread> file_user_blocking_thread_; 183 scoped_ptr<BrowserProcessSubThread> file_thread_; 184 scoped_ptr<BrowserProcessSubThread> process_launcher_thread_; 185 scoped_ptr<BrowserProcessSubThread> cache_thread_; 186 scoped_ptr<BrowserProcessSubThread> io_thread_; 187 scoped_ptr<base::Thread> indexed_db_thread_; 188 scoped_ptr<MemoryObserver> memory_observer_; 189 scoped_ptr<base::debug::TraceMemoryController> trace_memory_controller_; 190 scoped_ptr<base::debug::TraceEventSystemStatsMonitor> system_stats_monitor_; 191 192 bool is_tracing_startup_; 193 194 DISALLOW_COPY_AND_ASSIGN(BrowserMainLoop); 195 }; 196 197 } // namespace content 198 199 #endif // CONTENT_BROWSER_BROWSER_MAIN_LOOP_H_ 200