Home | History | Annotate | Download | only in browser
      1 // Copyright (c) 2011 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 CHROME_BROWSER_BROWSER_MAIN_H_
      6 #define CHROME_BROWSER_BROWSER_MAIN_H_
      7 #pragma once
      8 
      9 #include "base/basictypes.h"
     10 #include "base/memory/scoped_ptr.h"
     11 #include "base/metrics/field_trial.h"
     12 #include "base/tracked_objects.h"
     13 
     14 class BrowserThread;
     15 class CommandLine;
     16 class HighResolutionTimerManager;
     17 struct MainFunctionParams;
     18 class MessageLoop;
     19 class MetricsService;
     20 
     21 namespace net {
     22 class NetworkChangeNotifier;
     23 }
     24 
     25 namespace ui {
     26 class SystemMonitor;
     27 }
     28 
     29 // BrowserMainParts:
     30 // This class contains different "stages" to be executed in |BrowserMain()|,
     31 // mostly initialization. This is made into a class rather than just functions
     32 // so each stage can create and maintain state. Each part is represented by a
     33 // single method (e.g., "EarlyInitialization()"), which does the following:
     34 //  - calls a method (e.g., "PreEarlyInitialization()") which individual
     35 //    platforms can override to provide platform-specific code which is to be
     36 //    executed before the common code;
     37 //  - calls various methods for things common to all platforms (for that given
     38 //    stage); and
     39 //  - calls a method (e.g., "PostEarlyInitialization()") for platform-specific
     40 //    code to be called after the common code.
     41 // As indicated above, platforms should override the default "Pre...()" and
     42 // "Post...()" methods when necessary; they need not call the superclass's
     43 // implementation (which is empty).
     44 //
     45 // Parts:
     46 //  - EarlyInitialization: things which should be done as soon as possible on
     47 //    program start (such as setting up signal handlers) and things to be done
     48 //    at some generic time before the start of the main message loop.
     49 //  - MainMessageLoopStart: things beginning with the start of the main message
     50 //    loop and ending with initialization of the main thread; platform-specific
     51 //    things which should be done immediately before the start of the main
     52 //    message loop should go in |PreMainMessageLoopStart()|.
     53 //  - (more to come)
     54 //
     55 // How to add stuff (to existing parts):
     56 //  - Figure out when your new code should be executed. What must happen
     57 //    before/after your code is executed? Are there performance reasons for
     58 //    running your code at a particular time? Document these things!
     59 //  - Split out any platform-specific bits. Please avoid #ifdefs it at all
     60 //    possible. You have two choices for platform-specific code: (1) Execute it
     61 //    from one of the platform-specific |Pre/Post...()| methods; do this if the
     62 //    code is unique to a platform type. Or (2) execute it from one of the
     63 //    "parts" (e.g., |EarlyInitialization()|) and provide platform-specific
     64 //    implementations of your code (in a virtual method); do this if you need to
     65 //    provide different implementations across most/all platforms.
     66 //  - Unless your new code is just one or two lines, put it into a separate
     67 //    method with a well-defined purpose. (Likewise, if you're adding to an
     68 //    existing chunk which makes it longer than one or two lines, please move
     69 //    the code out into a separate method.)
     70 class BrowserMainParts {
     71  public:
     72   // This static method is to be implemented by each platform and should
     73   // instantiate the appropriate subclass.
     74   static BrowserMainParts* CreateBrowserMainParts(
     75       const MainFunctionParams& parameters);
     76 
     77   virtual ~BrowserMainParts();
     78 
     79   // Parts to be called by |BrowserMain()|.
     80   void EarlyInitialization();
     81   void MainMessageLoopStart();
     82 
     83   void SetupFieldTrials();
     84 
     85  protected:
     86   explicit BrowserMainParts(const MainFunctionParams& parameters);
     87 
     88   // Accessors for data members (below) ----------------------------------------
     89   const MainFunctionParams& parameters() const {
     90     return parameters_;
     91   }
     92   const CommandLine& parsed_command_line() const {
     93     return parsed_command_line_;
     94   }
     95   MessageLoop& main_message_loop() const {
     96     return *main_message_loop_;
     97   }
     98 
     99   // Methods to be overridden to provide platform-specific code; these
    100   // correspond to the "parts" above.
    101   virtual void PreEarlyInitialization() {}
    102   virtual void PostEarlyInitialization() {}
    103   virtual void PreMainMessageLoopStart() {}
    104   virtual void PostMainMessageLoopStart() {}
    105 
    106  private:
    107   // Methods for |EarlyInitialization()| ---------------------------------------
    108 
    109   // A/B test for the maximum number of persistent connections per host.
    110   void ConnectionFieldTrial();
    111 
    112   // A/B test for determining a value for unused socket timeout.
    113   void SocketTimeoutFieldTrial();
    114 
    115   // A/B test for the maximum number of connections per proxy server.
    116   void ProxyConnectionsFieldTrial();
    117 
    118   // A/B test for spdy when --use-spdy not set.
    119   void SpdyFieldTrial();
    120 
    121   // A/B test for automatically establishing a backup TCP connection when a
    122   // specified timeout value is reached.
    123   void ConnectBackupJobsFieldTrial();
    124 
    125   // A/B test for SSL False Start.
    126   void SSLFalseStartFieldTrial();
    127 
    128   // Used to initialize NSPR where appropriate.
    129   virtual void InitializeSSL() = 0;
    130 
    131   // Methods for |MainMessageLoopStart()| --------------------------------------
    132 
    133   void InitializeMainThread();
    134 
    135   // Members initialized on construction ---------------------------------------
    136 
    137   const MainFunctionParams& parameters_;
    138   const CommandLine& parsed_command_line_;
    139 
    140 #if defined(TRACK_ALL_TASK_OBJECTS)
    141   // Creating this object starts tracking the creation and deletion of Task
    142   // instance. This MUST be done before main_message_loop, so that it is
    143   // destroyed after the main_message_loop.
    144   tracked_objects::AutoTracking tracking_objects_;
    145 #endif
    146 
    147   // Statistical testing infrastructure for the entire browser.
    148   base::FieldTrialList field_trial_;
    149 
    150   // Members initialized in |MainMessageLoopStart()| ---------------------------
    151   scoped_ptr<MessageLoop> main_message_loop_;
    152   scoped_ptr<ui::SystemMonitor> system_monitor_;
    153   scoped_ptr<HighResolutionTimerManager> hi_res_timer_manager_;
    154   scoped_ptr<net::NetworkChangeNotifier> network_change_notifier_;
    155   scoped_ptr<BrowserThread> main_thread_;
    156 
    157   DISALLOW_COPY_AND_ASSIGN(BrowserMainParts);
    158 };
    159 
    160 
    161 // Perform platform-specific work that needs to be done after the main event
    162 // loop has ended.
    163 void DidEndMainMessageLoop();
    164 
    165 // Records the conditions that can prevent Breakpad from generating and
    166 // sending crash reports.  The presence of a Breakpad handler (after
    167 // attempting to initialize crash reporting) and the presence of a debugger
    168 // are registered with the UMA metrics service.
    169 void RecordBreakpadStatusUMA(MetricsService* metrics);
    170 
    171 // Displays a warning message if some minimum level of OS support is not
    172 // present on the current platform.
    173 void WarnAboutMinimumSystemRequirements();
    174 
    175 // Records the time from our process' startup to the present time in
    176 // the UMA histogram |metric_name|.
    177 void RecordBrowserStartupTime();
    178 
    179 #endif  // CHROME_BROWSER_BROWSER_MAIN_H_
    180