Allow overlapping sync and async startup requests
[chromium-blink-merge.git] / content / browser / browser_main_loop.h
blobb3ec73c8490e23b1f5dbcaed03d3da80cc65173a
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"
14 class CommandLine;
16 namespace base {
17 class HighResolutionTimerManager;
18 class MessageLoop;
19 class PowerMonitor;
20 class SystemMonitor;
21 namespace debug {
22 class TraceMemoryController;
23 } // namespace debug
24 } // namespace base
26 namespace media {
27 class AudioManager;
28 class MIDIManager;
29 class UserInputMonitor;
30 } // namespace media
32 namespace net {
33 class NetworkChangeNotifier;
34 } // namespace net
36 namespace content {
37 class AudioMirroringManager;
38 class BrowserMainParts;
39 class BrowserOnlineStateObserver;
40 class BrowserShutdownImpl;
41 class BrowserThreadImpl;
42 class MediaStreamManager;
43 class ResourceDispatcherHostImpl;
44 class SpeechRecognitionManagerImpl;
45 class StartupTaskRunner;
46 class SystemMessageWindowWin;
47 struct MainFunctionParams;
49 #if defined(OS_LINUX)
50 class DeviceMonitorLinux;
51 #elif defined(OS_MACOSX)
52 class DeviceMonitorMac;
53 #endif
55 // Implements the main browser loop stages called from BrowserMainRunner.
56 // See comments in browser_main_parts.h for additional info.
57 class CONTENT_EXPORT BrowserMainLoop {
58 public:
59 // Returns the current instance. This is used to get access to the getters
60 // that return objects which are owned by this class.
61 static BrowserMainLoop* GetInstance();
63 explicit BrowserMainLoop(const MainFunctionParams& parameters);
64 virtual ~BrowserMainLoop();
66 void Init();
68 void EarlyInitialization();
69 void InitializeToolkit();
70 void MainMessageLoopStart();
72 // Create and start running the tasks we need to complete startup. Note that
73 // this can be called more than once (currently only on Android) if we get a
74 // request for synchronous startup while the tasks created by asynchronous
75 // startup are still running.
76 void CreateStartupTasks();
78 // Perform the default message loop run logic.
79 void RunMainMessageLoopParts();
81 // Performs the shutdown sequence, starting with PostMainMessageLoopRun
82 // through stopping threads to PostDestroyThreads.
83 void ShutdownThreadsAndCleanUp();
85 int GetResultCode() const { return result_code_; }
87 media::AudioManager* audio_manager() const { return audio_manager_.get(); }
88 AudioMirroringManager* audio_mirroring_manager() const {
89 return audio_mirroring_manager_.get();
91 MediaStreamManager* media_stream_manager() const {
92 return media_stream_manager_.get();
94 media::UserInputMonitor* user_input_monitor() const {
95 return user_input_monitor_.get();
97 media::MIDIManager* midi_manager() const { return midi_manager_.get(); }
98 base::Thread* indexed_db_thread() const { return indexed_db_thread_.get(); }
100 private:
101 class MemoryObserver;
102 // For ShutdownThreadsAndCleanUp.
103 friend class BrowserShutdownImpl;
105 void InitializeMainThread();
107 // Called just before creating the threads
108 int PreCreateThreads();
110 // Create all secondary threads.
111 int CreateThreads();
113 // Called right after the browser threads have been started.
114 int BrowserThreadsStarted();
116 int PreMainMessageLoopRun();
118 void MainMessageLoopRun();
120 // Members initialized on construction ---------------------------------------
121 const MainFunctionParams& parameters_;
122 const CommandLine& parsed_command_line_;
123 int result_code_;
124 // True if the non-UI threads were created.
125 bool created_threads_;
127 // Members initialized in |MainMessageLoopStart()| ---------------------------
128 scoped_ptr<base::MessageLoop> main_message_loop_;
129 scoped_ptr<base::SystemMonitor> system_monitor_;
130 scoped_ptr<base::PowerMonitor> power_monitor_;
131 scoped_ptr<base::HighResolutionTimerManager> hi_res_timer_manager_;
132 scoped_ptr<net::NetworkChangeNotifier> network_change_notifier_;
133 // user_input_monitor_ has to outlive audio_manager_, so declared first.
134 scoped_ptr<media::UserInputMonitor> user_input_monitor_;
135 scoped_ptr<media::AudioManager> audio_manager_;
136 scoped_ptr<media::MIDIManager> midi_manager_;
137 scoped_ptr<AudioMirroringManager> audio_mirroring_manager_;
138 scoped_ptr<MediaStreamManager> media_stream_manager_;
139 // Per-process listener for online state changes.
140 scoped_ptr<BrowserOnlineStateObserver> online_state_observer_;
141 #if defined(OS_WIN)
142 scoped_ptr<SystemMessageWindowWin> system_message_window_;
143 #elif defined(OS_LINUX)
144 scoped_ptr<DeviceMonitorLinux> device_monitor_linux_;
145 #elif defined(OS_MACOSX) && !defined(OS_IOS)
146 scoped_ptr<DeviceMonitorMac> device_monitor_mac_;
147 #endif
148 // The startup task runner is created by CreateStartupTasks()
149 scoped_ptr<StartupTaskRunner> startup_task_runner_;
151 // Destroy parts_ before main_message_loop_ (required) and before other
152 // classes constructed in content (but after main_thread_).
153 scoped_ptr<BrowserMainParts> parts_;
155 // Members initialized in |InitializeMainThread()| ---------------------------
156 // This must get destroyed before other threads that are created in parts_.
157 scoped_ptr<BrowserThreadImpl> main_thread_;
159 // Members initialized in |BrowserThreadsStarted()| --------------------------
160 scoped_ptr<ResourceDispatcherHostImpl> resource_dispatcher_host_;
161 scoped_ptr<SpeechRecognitionManagerImpl> speech_recognition_manager_;
163 // Members initialized in |RunMainMessageLoopParts()| ------------------------
164 scoped_ptr<BrowserProcessSubThread> db_thread_;
165 scoped_ptr<BrowserProcessSubThread> file_user_blocking_thread_;
166 scoped_ptr<BrowserProcessSubThread> file_thread_;
167 scoped_ptr<BrowserProcessSubThread> process_launcher_thread_;
168 scoped_ptr<BrowserProcessSubThread> cache_thread_;
169 scoped_ptr<BrowserProcessSubThread> io_thread_;
170 scoped_ptr<base::Thread> indexed_db_thread_;
171 scoped_ptr<MemoryObserver> memory_observer_;
172 scoped_ptr<base::debug::TraceMemoryController> trace_memory_controller_;
174 DISALLOW_COPY_AND_ASSIGN(BrowserMainLoop);
177 } // namespace content
179 #endif // CONTENT_BROWSER_BROWSER_MAIN_LOOP_H_