- add sources.
[platform/framework/web/crosswalk.git] / src / content / browser / browser_main_loop.h
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 class CommandLine;
15
16 namespace base {
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 SystemMessageWindowWin;
48 struct MainFunctionParams;
49
50 #if defined(OS_LINUX)
51 class DeviceMonitorLinux;
52 #elif defined(OS_MACOSX)
53 class DeviceMonitorMac;
54 #endif
55
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 {
59  public:
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();
63
64   explicit BrowserMainLoop(const MainFunctionParams& parameters);
65   virtual ~BrowserMainLoop();
66
67   void Init();
68
69   void EarlyInitialization();
70   void InitializeToolkit();
71   void MainMessageLoopStart();
72
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();
78
79   // Perform the default message loop run logic.
80   void RunMainMessageLoopParts();
81
82   // Performs the shutdown sequence, starting with PostMainMessageLoopRun
83   // through stopping threads to PostDestroyThreads.
84   void ShutdownThreadsAndCleanUp();
85
86   int GetResultCode() const { return result_code_; }
87
88   media::AudioManager* audio_manager() const { return audio_manager_.get(); }
89   AudioMirroringManager* audio_mirroring_manager() const {
90     return audio_mirroring_manager_.get();
91   }
92   MediaStreamManager* media_stream_manager() const {
93     return media_stream_manager_.get();
94   }
95   media::UserInputMonitor* user_input_monitor() const {
96     return user_input_monitor_.get();
97   }
98   media::MIDIManager* midi_manager() const { return midi_manager_.get(); }
99   base::Thread* indexed_db_thread() const { return indexed_db_thread_.get(); }
100
101  private:
102   class MemoryObserver;
103   // For ShutdownThreadsAndCleanUp.
104   friend class BrowserShutdownImpl;
105
106   void InitializeMainThread();
107
108   // Called just before creating the threads
109   int PreCreateThreads();
110
111   // Create all secondary threads.
112   int CreateThreads();
113
114   // Called right after the browser threads have been started.
115   int BrowserThreadsStarted();
116
117   int PreMainMessageLoopRun();
118
119   void MainMessageLoopRun();
120
121   // Members initialized on construction ---------------------------------------
122   const MainFunctionParams& parameters_;
123   const CommandLine& parsed_command_line_;
124   int result_code_;
125   // True if the non-UI threads were created.
126   bool created_threads_;
127
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_;
142 #if defined(OS_WIN)
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_;
148 #endif
149   // The startup task runner is created by CreateStartupTasks()
150   scoped_ptr<StartupTaskRunner> startup_task_runner_;
151
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_;
155
156   // Members initialized in |InitializeMainThread()| ---------------------------
157   // This must get destroyed before other threads that are created in parts_.
158   scoped_ptr<BrowserThreadImpl> main_thread_;
159
160   // Members initialized in |BrowserThreadsStarted()| --------------------------
161   scoped_ptr<ResourceDispatcherHostImpl> resource_dispatcher_host_;
162   scoped_ptr<SpeechRecognitionManagerImpl> speech_recognition_manager_;
163
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_;
175
176   DISALLOW_COPY_AND_ASSIGN(BrowserMainLoop);
177 };
178
179 }  // namespace content
180
181 #endif  // CONTENT_BROWSER_BROWSER_MAIN_LOOP_H_