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