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