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 CHROME_BROWSER_CHROME_BROWSER_MAIN_H_
6#define CHROME_BROWSER_CHROME_BROWSER_MAIN_H_
7
8#include "base/basictypes.h"
9#include "base/memory/scoped_ptr.h"
10#include "base/metrics/field_trial.h"
11#include "base/tracked_objects.h"
12#include "chrome/browser/chrome_browser_field_trials.h"
13#include "chrome/browser/chrome_process_singleton.h"
14#include "chrome/browser/first_run/first_run.h"
15#include "chrome/browser/process_singleton.h"
16#include "chrome/browser/task_profiler/auto_tracking.h"
17#include "chrome/browser/ui/startup/startup_browser_creator.h"
18#include "content/public/browser/browser_main_parts.h"
19#include "content/public/common/main_function_params.h"
20
21class BrowserProcessImpl;
22class ChromeBrowserMainExtraParts;
23class FieldTrialSynchronizer;
24class MetricsService;
25class PrefService;
26class ProcessPowerCollector;
27class Profile;
28class StartupBrowserCreator;
29class StartupTimeBomb;
30class ShutdownWatcherHelper;
31class ThreeDAPIObserver;
32
33namespace chrome_browser {
34// For use by ShowMissingLocaleMessageBox.
35#if defined(OS_WIN)
36extern const char kMissingLocaleDataTitle[];
37#endif
38
39#if defined(OS_WIN)
40extern const char kMissingLocaleDataMessage[];
41#endif
42}
43
44namespace metrics {
45class TrackingSynchronizer;
46}
47
48class ChromeBrowserMainParts : public content::BrowserMainParts {
49 public:
50  virtual ~ChromeBrowserMainParts();
51
52  // Add additional ChromeBrowserMainExtraParts.
53  virtual void AddParts(ChromeBrowserMainExtraParts* parts);
54
55 protected:
56  explicit ChromeBrowserMainParts(
57      const content::MainFunctionParams& parameters);
58
59  // content::BrowserMainParts overrides.
60  // These are called in-order by content::BrowserMainLoop.
61  // Each stage calls the same stages in any ChromeBrowserMainExtraParts added
62  // with AddParts() from ChromeContentBrowserClient::CreateBrowserMainParts.
63  virtual void PreEarlyInitialization() OVERRIDE;
64  virtual void PostEarlyInitialization() OVERRIDE;
65  virtual void ToolkitInitialized() OVERRIDE;
66  virtual void PreMainMessageLoopStart() OVERRIDE;
67  virtual void PostMainMessageLoopStart() OVERRIDE;
68  virtual int PreCreateThreads() OVERRIDE;
69  virtual void PreMainMessageLoopRun() OVERRIDE;
70  virtual bool MainMessageLoopRun(int* result_code) OVERRIDE;
71  virtual void PostMainMessageLoopRun() OVERRIDE;
72  virtual void PostDestroyThreads() OVERRIDE;
73
74  // Additional stages for ChromeBrowserMainExtraParts. These stages are called
75  // in order from PreMainMessageLoopRun(). See implementation for details.
76  virtual void PreProfileInit();
77  virtual void PostProfileInit();
78  virtual void PreBrowserStart();
79  virtual void PostBrowserStart();
80
81  // Displays a warning message that we can't find any locale data files.
82  virtual void ShowMissingLocaleMessageBox() = 0;
83
84  const content::MainFunctionParams& parameters() const {
85    return parameters_;
86  }
87  const base::CommandLine& parsed_command_line() const {
88    return parsed_command_line_;
89  }
90
91  Profile* profile() { return profile_; }
92
93  const PrefService* local_state() const { return local_state_; }
94
95 private:
96  // Methods for |SetupMetricsAndFieldTrials()| --------------------------------
97
98  // Constructs metrics service and does related initialization, including
99  // creation of field trials. Call only after labs have been converted to
100  // switches.
101  void SetupMetricsAndFieldTrials();
102
103  // Starts recording of metrics. This can only be called after we have a file
104  // thread.
105  void StartMetricsRecording();
106
107  // Record time from process startup to present time in an UMA histogram.
108  void RecordBrowserStartupTime();
109
110  // Records a time value to an UMA histogram in the context of the
111  // PreReadExperiment field-trial. This also reports to the appropriate
112  // sub-histogram (_PreRead(Enabled|Disabled)).
113  void RecordPreReadExperimentTime(const char* name, base::TimeDelta time);
114
115  // Methods for Main Message Loop -------------------------------------------
116
117  int PreCreateThreadsImpl();
118  int PreMainMessageLoopRunImpl();
119
120  // Members initialized on construction ---------------------------------------
121
122  const content::MainFunctionParams parameters_;
123  const base::CommandLine& parsed_command_line_;
124  int result_code_;
125
126  // Create StartupTimeBomb object for watching jank during startup.
127  scoped_ptr<StartupTimeBomb> startup_watcher_;
128
129  // Create ShutdownWatcherHelper object for watching jank during shutdown.
130  // Please keep |shutdown_watcher| as the first object constructed, and hence
131  // it is destroyed last.
132  scoped_ptr<ShutdownWatcherHelper> shutdown_watcher_;
133
134  // Creating this object starts tracking the creation and deletion of Task
135  // instance. This MUST be done before main_message_loop, so that it is
136  // destroyed after the main_message_loop.
137  task_profiler::AutoTracking tracking_objects_;
138
139  // Statistical testing infrastructure for the entire browser. NULL until
140  // SetupMetricsAndFieldTrials is called.
141  scoped_ptr<base::FieldTrialList> field_trial_list_;
142
143  ChromeBrowserFieldTrials browser_field_trials_;
144
145#if !defined(OS_ANDROID) && !defined(OS_IOS)
146  // A monitor for attributing power consumption to origins.
147  scoped_ptr<ProcessPowerCollector> process_power_collector_;
148#endif
149
150  // Vector of additional ChromeBrowserMainExtraParts.
151  // Parts are deleted in the inverse order they are added.
152  std::vector<ChromeBrowserMainExtraParts*> chrome_extra_parts_;
153
154  // Members initialized after / released before main_message_loop_ ------------
155
156  scoped_ptr<BrowserProcessImpl> browser_process_;
157  scoped_refptr<metrics::TrackingSynchronizer> tracking_synchronizer_;
158#if !defined(OS_ANDROID)
159  // Browser creation happens on the Java side in Android.
160  scoped_ptr<StartupBrowserCreator> browser_creator_;
161
162  // Android doesn't support multiple browser processes, so it doesn't implement
163  // ProcessSingleton.
164  scoped_ptr<ChromeProcessSingleton> process_singleton_;
165
166  // Android's first run is done in Java instead of native.
167  scoped_ptr<first_run::MasterPrefs> master_prefs_;
168#endif
169  Profile* profile_;
170  bool run_message_loop_;
171  ProcessSingleton::NotifyResult notify_result_;
172  scoped_ptr<ThreeDAPIObserver> three_d_observer_;
173
174  // Initialized in SetupMetricsAndFieldTrials.
175  scoped_refptr<FieldTrialSynchronizer> field_trial_synchronizer_;
176
177  // Members initialized in PreMainMessageLoopRun, needed in
178  // PreMainMessageLoopRunThreadsCreated.
179  PrefService* local_state_;
180  base::FilePath user_data_dir_;
181
182  // Members needed across shutdown methods.
183  bool restart_last_session_;
184
185  // Tests can set this to true to disable restricting cookie access in the
186  // network stack, as this can only be done once.
187  static bool disable_enforcing_cookie_policies_for_tests_;
188
189  DISALLOW_COPY_AND_ASSIGN(ChromeBrowserMainParts);
190};
191
192#endif  // CHROME_BROWSER_CHROME_BROWSER_MAIN_H_
193