browser_process_impl.h revision 5821806d5e7f356e8fa4b058a389a808ea183019
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// When each service is created, we set a flag indicating this. At this point,
6// the service initialization could fail or succeed. This allows us to remember
7// if we tried to create a service, and not try creating it over and over if
8// the creation failed.
9
10#ifndef CHROME_BROWSER_BROWSER_PROCESS_IMPL_H_
11#define CHROME_BROWSER_BROWSER_PROCESS_IMPL_H_
12
13#include <string>
14
15#include "base/basictypes.h"
16#include "base/debug/stack_trace.h"
17#include "base/memory/ref_counted.h"
18#include "base/memory/scoped_ptr.h"
19#include "base/prefs/public/pref_change_registrar.h"
20#include "base/prefs/public/pref_observer.h"
21#include "base/threading/non_thread_safe.h"
22#include "base/timer.h"
23#include "chrome/browser/browser_process.h"
24
25class ChromeNetLog;
26class ChromeResourceDispatcherHostDelegate;
27class CommandLine;
28class RemoteDebuggingServer;
29
30#if defined(ENABLE_PLUGIN_INSTALLATION)
31class PluginsResourceService;
32#endif
33
34namespace base {
35class SequencedTaskRunner;
36}
37
38namespace policy {
39class BrowserPolicyConnector;
40class PolicyService;
41};
42
43// Real implementation of BrowserProcess that creates and returns the services.
44class BrowserProcessImpl : public BrowserProcess,
45                           public base::NonThreadSafe,
46                           public PrefObserver {
47 public:
48  // |local_state_task_runner| must be a shutdown-blocking task runner.
49  BrowserProcessImpl(base::SequencedTaskRunner* local_state_task_runner,
50                     const CommandLine& command_line);
51  virtual ~BrowserProcessImpl();
52
53  // Called before the browser threads are created.
54  void PreCreateThreads();
55
56  // Called after the threads have been created but before the message loops
57  // starts running. Allows the browser process to do any initialization that
58  // requires all threads running.
59  void PreMainMessageLoopRun();
60
61  // Most cleanup is done by these functions, driven from
62  // ChromeBrowserMain based on notifications from the content
63  // framework, rather than in the destructor, so that we can
64  // interleave cleanup with threads being stopped.
65  void StartTearDown();
66  void PostDestroyThreads();
67
68  // BrowserProcess implementation.
69  virtual void ResourceDispatcherHostCreated() OVERRIDE;
70  virtual void EndSession() OVERRIDE;
71  virtual MetricsService* metrics_service() OVERRIDE;
72  virtual IOThread* io_thread() OVERRIDE;
73  virtual WatchDogThread* watchdog_thread() OVERRIDE;
74  virtual ProfileManager* profile_manager() OVERRIDE;
75  virtual PrefService* local_state() OVERRIDE;
76  virtual net::URLRequestContextGetter* system_request_context() OVERRIDE;
77  virtual chrome_variations::VariationsService* variations_service() OVERRIDE;
78#if defined(OS_CHROMEOS)
79  virtual chromeos::OomPriorityManager* oom_priority_manager() OVERRIDE;
80#endif  // defined(OS_CHROMEOS)
81  virtual extensions::EventRouterForwarder*
82        extension_event_router_forwarder() OVERRIDE;
83  virtual NotificationUIManager* notification_ui_manager() OVERRIDE;
84  virtual policy::BrowserPolicyConnector* browser_policy_connector() OVERRIDE;
85  virtual policy::PolicyService* policy_service() OVERRIDE;
86  virtual IconManager* icon_manager() OVERRIDE;
87  virtual RenderWidgetSnapshotTaker* GetRenderWidgetSnapshotTaker() OVERRIDE;
88  virtual AutomationProviderList* GetAutomationProviderList() OVERRIDE;
89  virtual void CreateDevToolsHttpProtocolHandler(
90      Profile* profile,
91      const std::string& ip,
92      int port,
93      const std::string& frontend_url) OVERRIDE;
94  virtual unsigned int AddRefModule() OVERRIDE;
95  virtual unsigned int ReleaseModule() OVERRIDE;
96  virtual bool IsShuttingDown() OVERRIDE;
97  virtual printing::PrintJobManager* print_job_manager() OVERRIDE;
98  virtual printing::PrintPreviewTabController*
99      print_preview_tab_controller() OVERRIDE;
100  virtual printing::BackgroundPrintingManager*
101      background_printing_manager() OVERRIDE;
102  virtual IntranetRedirectDetector* intranet_redirect_detector() OVERRIDE;
103  virtual const std::string& GetApplicationLocale() OVERRIDE;
104  virtual void SetApplicationLocale(const std::string& locale) OVERRIDE;
105  virtual DownloadStatusUpdater* download_status_updater() OVERRIDE;
106  virtual DownloadRequestLimiter* download_request_limiter() OVERRIDE;
107  virtual BackgroundModeManager* background_mode_manager() OVERRIDE;
108  virtual StatusTray* status_tray() OVERRIDE;
109  virtual SafeBrowsingService* safe_browsing_service() OVERRIDE;
110  virtual safe_browsing::ClientSideDetectionService*
111      safe_browsing_detection_service() OVERRIDE;
112
113#if (defined(OS_WIN) || defined(OS_LINUX)) && !defined(OS_CHROMEOS)
114  virtual void StartAutoupdateTimer() OVERRIDE;
115#endif
116
117  virtual ChromeNetLog* net_log() OVERRIDE;
118  virtual prerender::PrerenderTracker* prerender_tracker() OVERRIDE;
119  virtual ComponentUpdateService* component_updater() OVERRIDE;
120  virtual CRLSetFetcher* crl_set_fetcher() OVERRIDE;
121  virtual BookmarkPromptController* bookmark_prompt_controller() OVERRIDE;
122
123  // PrefObserver implementation.
124  virtual void OnPreferenceChanged(PrefServiceBase* service,
125                                   const std::string& pref_name) OVERRIDE;
126
127 private:
128  void CreateMetricsService();
129  void CreateWatchdogThread();
130#if defined(OS_CHROMEOS)
131  void InitializeWebSocketProxyThread();
132#endif
133  void CreateProfileManager();
134  void CreateLocalState();
135  void CreateViewedPageTracker();
136  void CreateIconManager();
137  void CreateIntranetRedirectDetector();
138  void CreateNotificationUIManager();
139  void CreateStatusTrayManager();
140  void CreatePrintPreviewTabController();
141  void CreateBackgroundPrintingManager();
142  void CreateSafeBrowsingService();
143  void CreateSafeBrowsingDetectionService();
144  void CreateStatusTray();
145  void CreateBackgroundModeManager();
146
147  void ApplyDisabledSchemesPolicy();
148  void ApplyAllowCrossOriginAuthPromptPolicy();
149  void ApplyDefaultBrowserPolicy();
150
151  bool created_metrics_service_;
152  scoped_ptr<MetricsService> metrics_service_;
153
154  scoped_ptr<IOThread> io_thread_;
155
156  bool created_watchdog_thread_;
157  scoped_ptr<WatchDogThread> watchdog_thread_;
158
159  bool created_browser_policy_connector_;
160#if defined(ENABLE_CONFIGURATION_POLICY)
161  // Must be destroyed after |local_state_|.
162  scoped_ptr<policy::BrowserPolicyConnector> browser_policy_connector_;
163#endif
164
165  // Must be destroyed after |local_state_|.
166  // This is a stub when policy is not enabled. Otherwise, the PolicyService
167  // is owned by the |browser_policy_connector_| and this is not used.
168  scoped_ptr<policy::PolicyService> policy_service_;
169
170  bool created_profile_manager_;
171  scoped_ptr<ProfileManager> profile_manager_;
172
173  bool created_local_state_;
174  scoped_ptr<PrefService> local_state_;
175
176  bool created_icon_manager_;
177  scoped_ptr<IconManager> icon_manager_;
178
179  scoped_refptr<extensions::EventRouterForwarder>
180      extension_event_router_forwarder_;
181
182#if !defined(OS_ANDROID)
183  scoped_ptr<RemoteDebuggingServer> remote_debugging_server_;
184
185  // Bookmark prompt controller displays the prompt for frequently visited URL.
186  scoped_ptr<BookmarkPromptController> bookmark_prompt_controller_;
187#endif
188
189  scoped_refptr<printing::PrintPreviewTabController>
190      print_preview_tab_controller_;
191
192  scoped_ptr<printing::BackgroundPrintingManager> background_printing_manager_;
193
194  scoped_ptr<chrome_variations::VariationsService> variations_service_;
195
196  // Manager for desktop notification UI.
197  bool created_notification_ui_manager_;
198  scoped_ptr<NotificationUIManager> notification_ui_manager_;
199
200#if defined(ENABLE_AUTOMATION)
201  scoped_ptr<AutomationProviderList> automation_provider_list_;
202#endif
203
204  scoped_ptr<IntranetRedirectDetector> intranet_redirect_detector_;
205
206  scoped_ptr<StatusTray> status_tray_;
207
208  scoped_ptr<BackgroundModeManager> background_mode_manager_;
209
210  bool created_safe_browsing_service_;
211  scoped_refptr<SafeBrowsingService> safe_browsing_service_;
212
213  unsigned int module_ref_count_;
214  bool did_start_;
215
216  // Ensures that all the print jobs are finished before closing the browser.
217  scoped_ptr<printing::PrintJobManager> print_job_manager_;
218
219  std::string locale_;
220
221  bool checked_for_new_frames_;
222  bool using_new_frames_;
223
224  // This service just sits around and makes snapshots for renderers. It does
225  // nothing in the constructor so we don't have to worry about lazy init.
226  scoped_ptr<RenderWidgetSnapshotTaker> render_widget_snapshot_taker_;
227
228  // Download status updates (like a changing application icon on dock/taskbar)
229  // are global per-application. DownloadStatusUpdater does no work in the ctor
230  // so we don't have to worry about lazy initialization.
231  scoped_ptr<DownloadStatusUpdater> download_status_updater_;
232
233  scoped_refptr<DownloadRequestLimiter> download_request_limiter_;
234
235  // Sequenced task runner for local state related I/O tasks.
236  const scoped_refptr<base::SequencedTaskRunner> local_state_task_runner_;
237
238  // Ensures that the observers of plugin/print disable/enable state
239  // notifications are properly added and removed.
240  PrefChangeRegistrar pref_change_registrar_;
241
242  // Lives here so can safely log events on shutdown.
243  scoped_ptr<ChromeNetLog> net_log_;
244
245  // Ordered before resource_dispatcher_host_delegate_ due to destruction
246  // ordering.
247  scoped_ptr<prerender::PrerenderTracker> prerender_tracker_;
248
249  scoped_ptr<ChromeResourceDispatcherHostDelegate>
250      resource_dispatcher_host_delegate_;
251
252#if (defined(OS_WIN) || defined(OS_LINUX)) && !defined(OS_CHROMEOS)
253  base::RepeatingTimer<BrowserProcessImpl> autoupdate_timer_;
254
255  // Gets called by autoupdate timer to see if browser needs restart and can be
256  // restarted, and if that's the case, restarts the browser.
257  void OnAutoupdateTimer();
258  bool CanAutorestartForUpdate() const;
259  void RestartBackgroundInstance();
260#endif  // defined(OS_WIN) || defined(OS_LINUX) && !defined(OS_CHROMEOS)
261
262#if defined(OS_CHROMEOS)
263  scoped_ptr<chromeos::OomPriorityManager> oom_priority_manager_;
264#else
265  scoped_ptr<ComponentUpdateService> component_updater_;
266
267  scoped_refptr<CRLSetFetcher> crl_set_fetcher_;
268#endif
269
270#if defined(ENABLE_PLUGIN_INSTALLATION)
271  scoped_refptr<PluginsResourceService> plugins_resource_service_;
272#endif
273  // TODO(eroman): Remove this when done debugging 113031. This tracks
274  // the callstack which released the final module reference count.
275  base::debug::StackTrace release_last_reference_callstack_;
276
277  DISALLOW_COPY_AND_ASSIGN(BrowserProcessImpl);
278};
279
280#endif  // CHROME_BROWSER_BROWSER_PROCESS_IMPL_H_
281