Roll src/third_party/WebKit f298044:aa8346d (svn 202628:202629)
[chromium-blink-merge.git] / content / browser / browser_main_loop.h
blob822589e702db6f1c28ff515eb65a7dfa612f8051
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"
16 namespace base {
17 class CommandLine;
18 class FilePath;
19 class HighResolutionTimerManager;
20 class MessageLoop;
21 class PowerMonitor;
22 class SystemMonitor;
23 class MemoryPressureMonitor;
24 namespace trace_event {
25 class TraceMemoryController;
26 class TraceEventSystemStatsMonitor;
27 } // namespace trace_event
28 } // namespace base
30 namespace media {
31 class AudioManager;
32 class UserInputMonitor;
33 namespace midi {
34 class MidiManager;
35 } // namespace midi
36 } // namespace media
38 namespace net {
39 class NetworkChangeNotifier;
40 } // namespace net
42 #if defined(USE_OZONE)
43 namespace ui {
44 class ClientNativePixmapFactory;
45 } // namespace ui
46 #endif
48 namespace content {
49 class BrowserMainParts;
50 class BrowserOnlineStateObserver;
51 class BrowserShutdownImpl;
52 class BrowserThreadImpl;
53 class MediaStreamManager;
54 class MojoShellContext;
55 class ResourceDispatcherHostImpl;
56 class SpeechRecognitionManagerImpl;
57 class StartupTaskRunner;
58 class TimeZoneMonitor;
59 struct MainFunctionParams;
61 #if defined(OS_ANDROID)
62 class ScreenOrientationDelegate;
63 #elif defined(OS_LINUX)
64 class DeviceMonitorLinux;
65 #elif defined(OS_MACOSX)
66 class DeviceMonitorMac;
67 #elif defined(OS_WIN)
68 class SystemMessageWindowWin;
69 #endif
71 // Implements the main browser loop stages called from BrowserMainRunner.
72 // See comments in browser_main_parts.h for additional info.
73 class CONTENT_EXPORT BrowserMainLoop {
74 public:
75 // Returns the current instance. This is used to get access to the getters
76 // that return objects which are owned by this class.
77 static BrowserMainLoop* GetInstance();
79 explicit BrowserMainLoop(const MainFunctionParams& parameters);
80 virtual ~BrowserMainLoop();
82 void Init();
84 void EarlyInitialization();
86 // Initializes the toolkit. Returns whether the toolkit initialization was
87 // successful or not.
88 bool InitializeToolkit();
90 void PreMainMessageLoopStart();
91 void MainMessageLoopStart();
92 void PostMainMessageLoopStart();
94 // Create and start running the tasks we need to complete startup. Note that
95 // this can be called more than once (currently only on Android) if we get a
96 // request for synchronous startup while the tasks created by asynchronous
97 // startup are still running.
98 void CreateStartupTasks();
100 // Perform the default message loop run logic.
101 void RunMainMessageLoopParts();
103 // Performs the shutdown sequence, starting with PostMainMessageLoopRun
104 // through stopping threads to PostDestroyThreads.
105 void ShutdownThreadsAndCleanUp();
107 int GetResultCode() const { return result_code_; }
109 media::AudioManager* audio_manager() const { return audio_manager_.get(); }
110 MediaStreamManager* media_stream_manager() const {
111 return media_stream_manager_.get();
113 media::UserInputMonitor* user_input_monitor() const {
114 return user_input_monitor_.get();
116 media::midi::MidiManager* midi_manager() const { return midi_manager_.get(); }
117 base::Thread* indexed_db_thread() const { return indexed_db_thread_.get(); }
119 bool is_tracing_startup_for_duration() const {
120 return is_tracing_startup_for_duration_;
123 const base::FilePath& startup_trace_file() const {
124 return startup_trace_file_;
127 void StopStartupTracingTimer();
129 #if defined(OS_MACOSX) && !defined(OS_IOS)
130 DeviceMonitorMac* device_monitor_mac() const {
131 return device_monitor_mac_.get();
133 #endif
135 private:
136 class MemoryObserver;
137 // For ShutdownThreadsAndCleanUp.
138 friend class BrowserShutdownImpl;
140 void InitializeMainThread();
142 // Called just before creating the threads
143 int PreCreateThreads();
145 // Create all secondary threads.
146 int CreateThreads();
148 // Called right after the browser threads have been started.
149 int BrowserThreadsStarted();
151 int PreMainMessageLoopRun();
153 void MainMessageLoopRun();
155 base::FilePath GetStartupTraceFileName(
156 const base::CommandLine& command_line) const;
157 void InitStartupTracingForDuration(const base::CommandLine& command_line);
158 void EndStartupTracing();
160 bool UsingInProcessGpu() const;
162 // Quick reference for initialization order:
163 // Constructor
164 // Init()
165 // EarlyInitialization()
166 // InitializeToolkit()
167 // PreMainMessageLoopStart()
168 // MainMessageLoopStart()
169 // InitializeMainThread()
170 // PostMainMessageLoopStart()
171 // InitStartupTracingForDuration()
172 // CreateStartupTasks()
173 // PreCreateThreads()
174 // CreateThreads()
175 // BrowserThreadsStarted()
177 // Members initialized on construction ---------------------------------------
178 const MainFunctionParams& parameters_;
179 const base::CommandLine& parsed_command_line_;
180 int result_code_;
181 bool created_threads_; // True if the non-UI threads were created.
182 bool is_tracing_startup_for_duration_;
184 // Members initialized in |MainMessageLoopStart()| ---------------------------
185 scoped_ptr<base::MessageLoop> main_message_loop_;
187 // Members initialized in |PostMainMessageLoopStart()| -----------------------
188 scoped_ptr<base::SystemMonitor> system_monitor_;
189 scoped_ptr<base::PowerMonitor> power_monitor_;
190 scoped_ptr<base::HighResolutionTimerManager> hi_res_timer_manager_;
191 scoped_ptr<net::NetworkChangeNotifier> network_change_notifier_;
193 // Per-process listener for online state changes.
194 scoped_ptr<BrowserOnlineStateObserver> online_state_observer_;
196 scoped_ptr<base::trace_event::TraceEventSystemStatsMonitor>
197 system_stats_monitor_;
199 #if defined(OS_WIN)
200 scoped_ptr<SystemMessageWindowWin> system_message_window_;
201 #endif
203 #if defined(OS_ANDROID)
204 // Android implementation of ScreenOrientationDelegate
205 scoped_ptr<ScreenOrientationDelegate> screen_orientation_delegate_;
206 #endif
208 scoped_ptr<MemoryObserver> memory_observer_;
209 scoped_ptr<base::trace_event::TraceMemoryController> trace_memory_controller_;
211 // Members initialized in |InitStartupTracingForDuration()| ------------------
212 base::FilePath startup_trace_file_;
214 // This timer initiates trace file saving.
215 base::OneShotTimer<BrowserMainLoop> startup_trace_timer_;
217 // Members initialized in |Init()| -------------------------------------------
218 // Destroy |parts_| before |main_message_loop_| (required) and before other
219 // classes constructed in content (but after |main_thread_|).
220 scoped_ptr<BrowserMainParts> parts_;
222 // Members initialized in |InitializeMainThread()| ---------------------------
223 // This must get destroyed before other threads that are created in |parts_|.
224 scoped_ptr<BrowserThreadImpl> main_thread_;
226 // Members initialized in |CreateStartupTasks()| -----------------------------
227 scoped_ptr<StartupTaskRunner> startup_task_runner_;
229 // Members initialized in |PreCreateThreads()| -------------------------------
230 // Torn down in ShutdownThreadsAndCleanUp.
231 scoped_ptr<base::MemoryPressureMonitor> memory_pressure_monitor_;
233 // Members initialized in |CreateThreads()| ----------------------------------
234 scoped_ptr<BrowserProcessSubThread> db_thread_;
235 scoped_ptr<BrowserProcessSubThread> file_user_blocking_thread_;
236 scoped_ptr<BrowserProcessSubThread> file_thread_;
237 scoped_ptr<BrowserProcessSubThread> process_launcher_thread_;
238 scoped_ptr<BrowserProcessSubThread> cache_thread_;
239 scoped_ptr<BrowserProcessSubThread> io_thread_;
241 // Members initialized in |BrowserThreadsStarted()| --------------------------
242 scoped_ptr<base::Thread> indexed_db_thread_;
243 scoped_ptr<MojoShellContext> mojo_shell_context_;
245 // |user_input_monitor_| has to outlive |audio_manager_|, so declared first.
246 scoped_ptr<media::UserInputMonitor> user_input_monitor_;
247 scoped_ptr<media::AudioManager> audio_manager_;
249 scoped_ptr<media::midi::MidiManager> midi_manager_;
251 #if defined(USE_UDEV)
252 scoped_ptr<DeviceMonitorLinux> device_monitor_linux_;
253 #elif defined(OS_MACOSX) && !defined(OS_IOS)
254 scoped_ptr<DeviceMonitorMac> device_monitor_mac_;
255 #endif
256 #if defined(USE_OZONE)
257 scoped_ptr<ui::ClientNativePixmapFactory> client_native_pixmap_factory_;
258 #endif
260 scoped_ptr<ResourceDispatcherHostImpl> resource_dispatcher_host_;
261 scoped_ptr<MediaStreamManager> media_stream_manager_;
262 scoped_ptr<SpeechRecognitionManagerImpl> speech_recognition_manager_;
263 scoped_ptr<TimeZoneMonitor> time_zone_monitor_;
265 // DO NOT add members here. Add them to the right categories above.
267 DISALLOW_COPY_AND_ASSIGN(BrowserMainLoop);
270 } // namespace content
272 #endif // CONTENT_BROWSER_BROWSER_MAIN_LOOP_H_