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