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