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