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.
5 #ifndef CHROME_BROWSER_BROWSER_MAIN_H_
6 #define CHROME_BROWSER_BROWSER_MAIN_H_
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"
16 class FieldTrialSynchronizer
;
17 class HighResolutionTimerManager
;
18 struct MainFunctionParams
;
28 class NetworkChangeNotifier
;
32 // This class contains different "stages" to be executed in |BrowserMain()|,
33 // mostly initialization. This is made into a class rather than just functions
34 // so each stage can create and maintain state. Each part is represented by a
35 // single method (e.g., "EarlyInitialization()"), which does the following:
36 // - calls a method (e.g., "PreEarlyInitialization()") which individual
37 // platforms can override to provide platform-specific code which is to be
38 // executed before the common code;
39 // - calls various methods for things common to all platforms (for that given
41 // - calls a method (e.g., "PostEarlyInitialization()") for platform-specific
42 // code to be called after the common code.
43 // As indicated above, platforms should override the default "Pre...()" and
44 // "Post...()" methods when necessary; they need not call the superclass's
45 // implementation (which is empty).
48 // - EarlyInitialization: things which should be done as soon as possible on
49 // program start (such as setting up signal handlers) and things to be done
50 // at some generic time before the start of the main message loop.
51 // - MainMessageLoopStart: things beginning with the start of the main message
52 // loop and ending with initialization of the main thread; platform-specific
53 // things which should be done immediately before the start of the main
54 // message loop should go in |PreMainMessageLoopStart()|.
57 // How to add stuff (to existing parts):
58 // - Figure out when your new code should be executed. What must happen
59 // before/after your code is executed? Are there performance reasons for
60 // running your code at a particular time? Document these things!
61 // - Split out any platform-specific bits. Please avoid #ifdefs it at all
62 // possible. You have two choices for platform-specific code: (1) Execute it
63 // from one of the platform-specific |Pre/Post...()| methods; do this if the
64 // code is unique to a platform type. Or (2) execute it from one of the
65 // "parts" (e.g., |EarlyInitialization()|) and provide platform-specific
66 // implementations of your code (in a virtual method); do this if you need to
67 // provide different implementations across most/all platforms.
68 // - Unless your new code is just one or two lines, put it into a separate
69 // method with a well-defined purpose. (Likewise, if you're adding to an
70 // existing chunk which makes it longer than one or two lines, please move
71 // the code out into a separate method.)
72 class BrowserMainParts
{
74 // This static method is to be implemented by each platform and should
75 // instantiate the appropriate subclass.
76 static BrowserMainParts
* CreateBrowserMainParts(
77 const MainFunctionParams
& parameters
);
79 virtual ~BrowserMainParts();
81 // Parts to be called by |BrowserMain()|.
82 void EarlyInitialization();
83 void MainMessageLoopStart();
85 // Constructs metrics service and does related initialization, including
86 // creation of field trials. Call only after labs have been converted to
88 MetricsService
* SetupMetricsAndFieldTrials(
89 const CommandLine
& parsed_command_line
,
90 PrefService
* local_state
);
93 explicit BrowserMainParts(const MainFunctionParams
& parameters
);
95 // Accessors for data members (below) ----------------------------------------
96 const MainFunctionParams
& parameters() const {
99 const CommandLine
& parsed_command_line() const {
100 return parsed_command_line_
;
102 MessageLoop
& main_message_loop() const {
103 return *main_message_loop_
;
106 // Methods to be overridden to provide platform-specific code; these
107 // correspond to the "parts" above.
108 virtual void PreEarlyInitialization() {}
109 virtual void PostEarlyInitialization() {}
110 virtual void PreMainMessageLoopStart() {}
111 virtual void PostMainMessageLoopStart() {}
114 // Methods for |EarlyInitialization()| ---------------------------------------
116 // A/B test for the maximum number of persistent connections per host.
117 void ConnectionFieldTrial();
119 // A/B test for determining a value for unused socket timeout.
120 void SocketTimeoutFieldTrial();
122 // A/B test for the maximum number of connections per proxy server.
123 void ProxyConnectionsFieldTrial();
125 // A/B test for spdy when --use-spdy not set.
126 void SpdyFieldTrial();
128 // A/B test for automatically establishing a backup TCP connection when a
129 // specified timeout value is reached.
130 void ConnectBackupJobsFieldTrial();
132 // A/B test for disabling revocation checking for sites with pinned
134 void RevocationCheckingDisabledFieldTrial();
136 // Used to initialize NSPR where appropriate.
137 virtual void InitializeSSL() = 0;
139 // Methods for |MainMessageLoopStart()| --------------------------------------
141 void InitializeMainThread();
143 // Methods for |SetupMetricsAndFieldTrials()| --------------------------------
145 static MetricsService
* InitializeMetrics(
146 const CommandLine
& parsed_command_line
,
147 const PrefService
* local_state
);
149 // Add an invocation of your field trial init function to this method.
150 void SetupFieldTrials(bool metrics_recording_enabled
);
152 // Members initialized on construction ---------------------------------------
154 const MainFunctionParams
& parameters_
;
155 const CommandLine
& parsed_command_line_
;
157 #if defined(TRACK_ALL_TASK_OBJECTS)
158 // Creating this object starts tracking the creation and deletion of Task
159 // instance. This MUST be done before main_message_loop, so that it is
160 // destroyed after the main_message_loop.
161 tracked_objects::AutoTracking tracking_objects_
;
164 // Statistical testing infrastructure for the entire browser. NULL until
165 // SetupMetricsAndFieldTrials is called.
166 scoped_ptr
<base::FieldTrialList
> field_trial_list_
;
168 // Members initialized in |MainMessageLoopStart()| ---------------------------
169 scoped_ptr
<MessageLoop
> main_message_loop_
;
170 scoped_ptr
<base::SystemMonitor
> system_monitor_
;
171 scoped_ptr
<HighResolutionTimerManager
> hi_res_timer_manager_
;
172 scoped_ptr
<net::NetworkChangeNotifier
> network_change_notifier_
;
173 scoped_ptr
<BrowserThread
> main_thread_
;
175 // Initialized in SetupMetricsAndFieldTrials.
176 scoped_refptr
<FieldTrialSynchronizer
> field_trial_synchronizer_
;
178 DISALLOW_COPY_AND_ASSIGN(BrowserMainParts
);
182 // Perform platform-specific work that needs to be done after the main event
184 void DidEndMainMessageLoop();
186 // Records the conditions that can prevent Breakpad from generating and
187 // sending crash reports. The presence of a Breakpad handler (after
188 // attempting to initialize crash reporting) and the presence of a debugger
189 // are registered with the UMA metrics service.
190 void RecordBreakpadStatusUMA(MetricsService
* metrics
);
192 // Displays a warning message if some minimum level of OS support is not
193 // present on the current platform.
194 void WarnAboutMinimumSystemRequirements();
196 // Records the time from our process' startup to the present time in
197 // the UMA histogram |metric_name|.
198 void RecordBrowserStartupTime();
200 #endif // CHROME_BROWSER_BROWSER_MAIN_H_