Home | History | Annotate | Download | only in browser
      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/pref_change_registrar.h"
     20 #include "base/threading/non_thread_safe.h"
     21 #include "base/timer/timer.h"
     22 #include "chrome/browser/browser_process.h"
     23 
     24 class ChromeNetLog;
     25 class ChromeResourceDispatcherHostDelegate;
     26 class CommandLine;
     27 class RemoteDebuggingServer;
     28 class PrefRegistrySimple;
     29 class PromoResourceService;
     30 
     31 #if defined(ENABLE_PLUGIN_INSTALLATION)
     32 class PluginsResourceService;
     33 #endif
     34 
     35 namespace base {
     36 class SequencedTaskRunner;
     37 }
     38 
     39 namespace policy {
     40 class BrowserPolicyConnector;
     41 class PolicyService;
     42 };
     43 
     44 // Real implementation of BrowserProcess that creates and returns the services.
     45 class BrowserProcessImpl : public BrowserProcess,
     46                            public base::NonThreadSafe {
     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   virtual BrowserProcessPlatformPart* platform_part() OVERRIDE;
     79   virtual extensions::EventRouterForwarder*
     80         extension_event_router_forwarder() OVERRIDE;
     81   virtual NotificationUIManager* notification_ui_manager() OVERRIDE;
     82   virtual message_center::MessageCenter* message_center() OVERRIDE;
     83   virtual policy::BrowserPolicyConnector* browser_policy_connector() OVERRIDE;
     84   virtual policy::PolicyService* policy_service() OVERRIDE;
     85   virtual IconManager* icon_manager() OVERRIDE;
     86   virtual GLStringManager* gl_string_manager() OVERRIDE;
     87   virtual GpuModeManager* gpu_mode_manager() OVERRIDE;
     88   virtual RenderWidgetSnapshotTaker* GetRenderWidgetSnapshotTaker() OVERRIDE;
     89   virtual AutomationProviderList* GetAutomationProviderList() OVERRIDE;
     90   virtual void CreateDevToolsHttpProtocolHandler(
     91       chrome::HostDesktopType host_desktop_type,
     92       const std::string& ip,
     93       int port,
     94       const std::string& frontend_url) OVERRIDE;
     95   virtual unsigned int AddRefModule() OVERRIDE;
     96   virtual unsigned int ReleaseModule() OVERRIDE;
     97   virtual bool IsShuttingDown() OVERRIDE;
     98   virtual printing::PrintJobManager* print_job_manager() OVERRIDE;
     99   virtual printing::PrintPreviewDialogController*
    100       print_preview_dialog_controller() OVERRIDE;
    101   virtual printing::BackgroundPrintingManager*
    102       background_printing_manager() OVERRIDE;
    103   virtual IntranetRedirectDetector* intranet_redirect_detector() OVERRIDE;
    104   virtual const std::string& GetApplicationLocale() OVERRIDE;
    105   virtual void SetApplicationLocale(const std::string& locale) OVERRIDE;
    106   virtual DownloadStatusUpdater* download_status_updater() OVERRIDE;
    107   virtual DownloadRequestLimiter* download_request_limiter() OVERRIDE;
    108   virtual BackgroundModeManager* background_mode_manager() OVERRIDE;
    109   virtual void set_background_mode_manager_for_test(
    110       scoped_ptr<BackgroundModeManager> manager) OVERRIDE;
    111   virtual StatusTray* status_tray() OVERRIDE;
    112   virtual SafeBrowsingService* safe_browsing_service() OVERRIDE;
    113   virtual safe_browsing::ClientSideDetectionService*
    114       safe_browsing_detection_service() OVERRIDE;
    115 
    116 #if (defined(OS_WIN) || defined(OS_LINUX)) && !defined(OS_CHROMEOS)
    117   virtual void StartAutoupdateTimer() OVERRIDE;
    118 #endif
    119 
    120   virtual ChromeNetLog* net_log() OVERRIDE;
    121   virtual prerender::PrerenderTracker* prerender_tracker() OVERRIDE;
    122   virtual ComponentUpdateService* component_updater() OVERRIDE;
    123   virtual CRLSetFetcher* crl_set_fetcher() OVERRIDE;
    124   virtual PnaclComponentInstaller* pnacl_component_installer() OVERRIDE;
    125   virtual BookmarkPromptController* bookmark_prompt_controller() OVERRIDE;
    126   virtual chrome::StorageMonitor* storage_monitor() OVERRIDE;
    127   void set_storage_monitor_for_test(scoped_ptr<chrome::StorageMonitor> monitor);
    128   virtual chrome::MediaFileSystemRegistry*
    129       media_file_system_registry() OVERRIDE;
    130   virtual bool created_local_state() const OVERRIDE;
    131 #if defined(ENABLE_WEBRTC)
    132   virtual WebRtcLogUploader* webrtc_log_uploader() OVERRIDE;
    133 #endif
    134 
    135   static void RegisterPrefs(PrefRegistrySimple* registry);
    136 
    137  private:
    138   void CreateMetricsService();
    139   void CreateWatchdogThread();
    140   void CreateProfileManager();
    141   void CreateLocalState();
    142   void CreateViewedPageTracker();
    143   void CreateIconManager();
    144   void CreateIntranetRedirectDetector();
    145   void CreateNotificationUIManager();
    146   void CreateStatusTrayManager();
    147   void CreatePrintPreviewDialogController();
    148   void CreateBackgroundPrintingManager();
    149   void CreateSafeBrowsingService();
    150   void CreateSafeBrowsingDetectionService();
    151   void CreateStatusTray();
    152   void CreateBackgroundModeManager();
    153 
    154   void ApplyAllowCrossOriginAuthPromptPolicy();
    155   void ApplyDefaultBrowserPolicy();
    156 
    157   bool created_metrics_service_;
    158   scoped_ptr<MetricsService> metrics_service_;
    159 
    160   scoped_ptr<IOThread> io_thread_;
    161 
    162   bool created_watchdog_thread_;
    163   scoped_ptr<WatchDogThread> watchdog_thread_;
    164 
    165   bool created_browser_policy_connector_;
    166 #if defined(ENABLE_CONFIGURATION_POLICY)
    167   // Must be destroyed after |local_state_|.
    168   scoped_ptr<policy::BrowserPolicyConnector> browser_policy_connector_;
    169 #endif
    170 
    171   // Must be destroyed after |local_state_|.
    172   // This is a stub when policy is not enabled. Otherwise, the PolicyService
    173   // is owned by the |browser_policy_connector_| and this is not used.
    174   scoped_ptr<policy::PolicyService> policy_service_;
    175 
    176   bool created_profile_manager_;
    177   scoped_ptr<ProfileManager> profile_manager_;
    178 
    179   bool created_local_state_;
    180   scoped_ptr<PrefService> local_state_;
    181 
    182   bool created_icon_manager_;
    183   scoped_ptr<IconManager> icon_manager_;
    184 
    185   scoped_ptr<GLStringManager> gl_string_manager_;
    186 
    187   scoped_ptr<GpuModeManager> gpu_mode_manager_;
    188 
    189   scoped_refptr<extensions::EventRouterForwarder>
    190       extension_event_router_forwarder_;
    191 
    192 #if !defined(OS_ANDROID)
    193   scoped_ptr<RemoteDebuggingServer> remote_debugging_server_;
    194 
    195   // Bookmark prompt controller displays the prompt for frequently visited URL.
    196   scoped_ptr<BookmarkPromptController> bookmark_prompt_controller_;
    197 #endif
    198 
    199 #if !defined(OS_ANDROID) && !defined(OS_IOS)
    200   scoped_ptr<chrome::StorageMonitor> storage_monitor_;
    201 
    202   scoped_ptr<chrome::MediaFileSystemRegistry> media_file_system_registry_;
    203 #endif
    204 
    205   scoped_refptr<printing::PrintPreviewDialogController>
    206       print_preview_dialog_controller_;
    207 
    208   scoped_ptr<printing::BackgroundPrintingManager> background_printing_manager_;
    209 
    210   scoped_ptr<chrome_variations::VariationsService> variations_service_;
    211 
    212   // Manager for desktop notification UI.
    213   bool created_notification_ui_manager_;
    214   scoped_ptr<NotificationUIManager> notification_ui_manager_;
    215 
    216 #if defined(ENABLE_AUTOMATION)
    217   scoped_ptr<AutomationProviderList> automation_provider_list_;
    218 #endif
    219 
    220   scoped_ptr<IntranetRedirectDetector> intranet_redirect_detector_;
    221 
    222   scoped_ptr<StatusTray> status_tray_;
    223 
    224   scoped_ptr<BackgroundModeManager> background_mode_manager_;
    225 
    226   bool created_safe_browsing_service_;
    227   scoped_refptr<SafeBrowsingService> safe_browsing_service_;
    228 
    229   unsigned int module_ref_count_;
    230   bool did_start_;
    231 
    232   // Ensures that all the print jobs are finished before closing the browser.
    233   scoped_ptr<printing::PrintJobManager> print_job_manager_;
    234 
    235   std::string locale_;
    236 
    237   bool checked_for_new_frames_;
    238   bool using_new_frames_;
    239 
    240   // This service just sits around and makes snapshots for renderers. It does
    241   // nothing in the constructor so we don't have to worry about lazy init.
    242   scoped_ptr<RenderWidgetSnapshotTaker> render_widget_snapshot_taker_;
    243 
    244   // Download status updates (like a changing application icon on dock/taskbar)
    245   // are global per-application. DownloadStatusUpdater does no work in the ctor
    246   // so we don't have to worry about lazy initialization.
    247   scoped_ptr<DownloadStatusUpdater> download_status_updater_;
    248 
    249   scoped_refptr<DownloadRequestLimiter> download_request_limiter_;
    250 
    251   // Sequenced task runner for local state related I/O tasks.
    252   const scoped_refptr<base::SequencedTaskRunner> local_state_task_runner_;
    253 
    254   // Ensures that the observers of plugin/print disable/enable state
    255   // notifications are properly added and removed.
    256   PrefChangeRegistrar pref_change_registrar_;
    257 
    258   // Lives here so can safely log events on shutdown.
    259   scoped_ptr<ChromeNetLog> net_log_;
    260 
    261   // Ordered before resource_dispatcher_host_delegate_ due to destruction
    262   // ordering.
    263   scoped_ptr<prerender::PrerenderTracker> prerender_tracker_;
    264 
    265   scoped_ptr<ChromeResourceDispatcherHostDelegate>
    266       resource_dispatcher_host_delegate_;
    267 
    268   scoped_refptr<PromoResourceService> promo_resource_service_;
    269 
    270 #if (defined(OS_WIN) || defined(OS_LINUX)) && !defined(OS_CHROMEOS)
    271   base::RepeatingTimer<BrowserProcessImpl> autoupdate_timer_;
    272 
    273   // Gets called by autoupdate timer to see if browser needs restart and can be
    274   // restarted, and if that's the case, restarts the browser.
    275   void OnAutoupdateTimer();
    276   bool CanAutorestartForUpdate() const;
    277   void RestartBackgroundInstance();
    278 #endif  // defined(OS_WIN) || defined(OS_LINUX) && !defined(OS_CHROMEOS)
    279 
    280   // component updater is normally not used under ChromeOS due
    281   // to concerns over integrity of data shared between profiles,
    282   // but some users of component updater only install per-user.
    283   scoped_ptr<ComponentUpdateService> component_updater_;
    284   scoped_refptr<CRLSetFetcher> crl_set_fetcher_;
    285   scoped_ptr<PnaclComponentInstaller> pnacl_component_installer_;
    286 
    287 #if defined(ENABLE_PLUGIN_INSTALLATION)
    288   scoped_refptr<PluginsResourceService> plugins_resource_service_;
    289 #endif
    290 
    291   scoped_ptr<BrowserProcessPlatformPart> platform_part_;
    292 
    293   // TODO(eroman): Remove this when done debugging 113031. This tracks
    294   // the callstack which released the final module reference count.
    295   base::debug::StackTrace release_last_reference_callstack_;
    296 
    297 #if defined(ENABLE_WEBRTC)
    298   // Lazily initialized.
    299   scoped_ptr<WebRtcLogUploader> webrtc_log_uploader_;
    300 #endif
    301 
    302   DISALLOW_COPY_AND_ASSIGN(BrowserProcessImpl);
    303 };
    304 
    305 #endif  // CHROME_BROWSER_BROWSER_PROCESS_IMPL_H_
    306