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 ChromeDeviceClient;
     25 class ChromeNetLog;
     26 class ChromeResourceDispatcherHostDelegate;
     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 CommandLine;
     37 class SequencedTaskRunner;
     38 }
     39 
     40 namespace extensions {
     41 class ExtensionsBrowserClient;
     42 }
     43 
     44 namespace gcm {
     45 class GCMDriver;
     46 }
     47 
     48 namespace policy {
     49 class BrowserPolicyConnector;
     50 class PolicyService;
     51 };
     52 
     53 // Real implementation of BrowserProcess that creates and returns the services.
     54 class BrowserProcessImpl : public BrowserProcess,
     55                            public base::NonThreadSafe {
     56  public:
     57   // |local_state_task_runner| must be a shutdown-blocking task runner.
     58   BrowserProcessImpl(base::SequencedTaskRunner* local_state_task_runner,
     59                      const base::CommandLine& command_line);
     60   virtual ~BrowserProcessImpl();
     61 
     62   // Called before the browser threads are created.
     63   void PreCreateThreads();
     64 
     65   // Called after the threads have been created but before the message loops
     66   // starts running. Allows the browser process to do any initialization that
     67   // requires all threads running.
     68   void PreMainMessageLoopRun();
     69 
     70   // Most cleanup is done by these functions, driven from
     71   // ChromeBrowserMain based on notifications from the content
     72   // framework, rather than in the destructor, so that we can
     73   // interleave cleanup with threads being stopped.
     74   void StartTearDown();
     75   void PostDestroyThreads();
     76 
     77   // BrowserProcess implementation.
     78   virtual void ResourceDispatcherHostCreated() OVERRIDE;
     79   virtual void EndSession() OVERRIDE;
     80   virtual MetricsServicesManager* GetMetricsServicesManager() OVERRIDE;
     81   virtual metrics::MetricsService* metrics_service() OVERRIDE;
     82   virtual rappor::RapporService* rappor_service() OVERRIDE;
     83   virtual IOThread* io_thread() OVERRIDE;
     84   virtual WatchDogThread* watchdog_thread() OVERRIDE;
     85   virtual ProfileManager* profile_manager() OVERRIDE;
     86   virtual PrefService* local_state() OVERRIDE;
     87   virtual net::URLRequestContextGetter* system_request_context() OVERRIDE;
     88   virtual chrome_variations::VariationsService* variations_service() OVERRIDE;
     89   virtual BrowserProcessPlatformPart* platform_part() OVERRIDE;
     90   virtual extensions::EventRouterForwarder*
     91         extension_event_router_forwarder() OVERRIDE;
     92   virtual NotificationUIManager* notification_ui_manager() OVERRIDE;
     93   virtual message_center::MessageCenter* message_center() OVERRIDE;
     94   virtual policy::BrowserPolicyConnector* browser_policy_connector() OVERRIDE;
     95   virtual policy::PolicyService* policy_service() OVERRIDE;
     96   virtual IconManager* icon_manager() OVERRIDE;
     97   virtual GLStringManager* gl_string_manager() OVERRIDE;
     98   virtual GpuModeManager* gpu_mode_manager() OVERRIDE;
     99   virtual void CreateDevToolsHttpProtocolHandler(
    100       chrome::HostDesktopType host_desktop_type,
    101       const std::string& ip,
    102       int port) OVERRIDE;
    103   virtual unsigned int AddRefModule() OVERRIDE;
    104   virtual unsigned int ReleaseModule() OVERRIDE;
    105   virtual bool IsShuttingDown() OVERRIDE;
    106   virtual printing::PrintJobManager* print_job_manager() OVERRIDE;
    107   virtual printing::PrintPreviewDialogController*
    108       print_preview_dialog_controller() OVERRIDE;
    109   virtual printing::BackgroundPrintingManager*
    110       background_printing_manager() OVERRIDE;
    111   virtual IntranetRedirectDetector* intranet_redirect_detector() OVERRIDE;
    112   virtual const std::string& GetApplicationLocale() OVERRIDE;
    113   virtual void SetApplicationLocale(const std::string& locale) OVERRIDE;
    114   virtual DownloadStatusUpdater* download_status_updater() OVERRIDE;
    115   virtual DownloadRequestLimiter* download_request_limiter() OVERRIDE;
    116   virtual BackgroundModeManager* background_mode_manager() OVERRIDE;
    117   virtual void set_background_mode_manager_for_test(
    118       scoped_ptr<BackgroundModeManager> manager) OVERRIDE;
    119   virtual StatusTray* status_tray() OVERRIDE;
    120   virtual SafeBrowsingService* safe_browsing_service() OVERRIDE;
    121   virtual safe_browsing::ClientSideDetectionService*
    122       safe_browsing_detection_service() OVERRIDE;
    123 
    124 #if (defined(OS_WIN) || defined(OS_LINUX)) && !defined(OS_CHROMEOS)
    125   virtual void StartAutoupdateTimer() OVERRIDE;
    126 #endif
    127 
    128   virtual ChromeNetLog* net_log() OVERRIDE;
    129   virtual prerender::PrerenderTracker* prerender_tracker() OVERRIDE;
    130   virtual component_updater::ComponentUpdateService*
    131       component_updater() OVERRIDE;
    132   virtual CRLSetFetcher* crl_set_fetcher() OVERRIDE;
    133   virtual component_updater::PnaclComponentInstaller*
    134       pnacl_component_installer() OVERRIDE;
    135   virtual MediaFileSystemRegistry* media_file_system_registry() OVERRIDE;
    136   virtual bool created_local_state() const OVERRIDE;
    137 #if defined(ENABLE_WEBRTC)
    138   virtual WebRtcLogUploader* webrtc_log_uploader() OVERRIDE;
    139 #endif
    140   virtual network_time::NetworkTimeTracker* network_time_tracker() OVERRIDE;
    141   virtual gcm::GCMDriver* gcm_driver() OVERRIDE;
    142 
    143   static void RegisterPrefs(PrefRegistrySimple* registry);
    144 
    145  private:
    146   void CreateWatchdogThread();
    147   void CreateProfileManager();
    148   void CreateLocalState();
    149   void CreateViewedPageTracker();
    150   void CreateIconManager();
    151   void CreateIntranetRedirectDetector();
    152   void CreateNotificationUIManager();
    153   void CreateStatusTrayManager();
    154   void CreatePrintPreviewDialogController();
    155   void CreateBackgroundPrintingManager();
    156   void CreateSafeBrowsingService();
    157   void CreateSafeBrowsingDetectionService();
    158   void CreateStatusTray();
    159   void CreateBackgroundModeManager();
    160   void CreateGCMDriver();
    161 
    162   void ApplyAllowCrossOriginAuthPromptPolicy();
    163   void ApplyDefaultBrowserPolicy();
    164   void ApplyMetricsReportingPolicy();
    165 
    166   scoped_ptr<MetricsServicesManager> metrics_services_manager_;
    167 
    168   scoped_ptr<IOThread> io_thread_;
    169 
    170   bool created_watchdog_thread_;
    171   scoped_ptr<WatchDogThread> watchdog_thread_;
    172 
    173   bool created_browser_policy_connector_;
    174 #if defined(ENABLE_CONFIGURATION_POLICY)
    175   // Must be destroyed after |local_state_|.
    176   scoped_ptr<policy::BrowserPolicyConnector> browser_policy_connector_;
    177 #else
    178   // Must be destroyed after |local_state_|.
    179   // This is a stub when policy is not enabled. Otherwise, the PolicyService
    180   // is owned by the |browser_policy_connector_| and this is not used.
    181   scoped_ptr<policy::PolicyService> policy_service_;
    182 #endif
    183 
    184   bool created_profile_manager_;
    185   scoped_ptr<ProfileManager> profile_manager_;
    186 
    187   bool created_local_state_;
    188   scoped_ptr<PrefService> local_state_;
    189 
    190   bool created_icon_manager_;
    191   scoped_ptr<IconManager> icon_manager_;
    192 
    193   scoped_ptr<GLStringManager> gl_string_manager_;
    194 
    195   scoped_ptr<GpuModeManager> gpu_mode_manager_;
    196 
    197 #if defined(ENABLE_EXTENSIONS)
    198   scoped_ptr<extensions::ExtensionsBrowserClient> extensions_browser_client_;
    199 
    200   scoped_refptr<extensions::EventRouterForwarder>
    201       extension_event_router_forwarder_;
    202 
    203   scoped_ptr<MediaFileSystemRegistry> media_file_system_registry_;
    204 #endif
    205 
    206 #if !defined(OS_ANDROID)
    207   scoped_ptr<RemoteDebuggingServer> remote_debugging_server_;
    208 #endif
    209 
    210 #if defined(ENABLE_FULL_PRINTING)
    211   scoped_refptr<printing::PrintPreviewDialogController>
    212       print_preview_dialog_controller_;
    213 
    214   scoped_ptr<printing::BackgroundPrintingManager> background_printing_manager_;
    215 #endif
    216 
    217   // Manager for desktop notification UI.
    218   bool created_notification_ui_manager_;
    219   scoped_ptr<NotificationUIManager> notification_ui_manager_;
    220 
    221   scoped_ptr<IntranetRedirectDetector> intranet_redirect_detector_;
    222 
    223   scoped_ptr<StatusTray> status_tray_;
    224 
    225   scoped_ptr<BackgroundModeManager> background_mode_manager_;
    226 
    227   bool created_safe_browsing_service_;
    228   scoped_refptr<SafeBrowsingService> safe_browsing_service_;
    229 
    230   unsigned int module_ref_count_;
    231   bool did_start_;
    232 
    233   // Ensures that all the print jobs are finished before closing the browser.
    234   scoped_ptr<printing::PrintJobManager> print_job_manager_;
    235 
    236   std::string locale_;
    237 
    238   // Download status updates (like a changing application icon on dock/taskbar)
    239   // are global per-application. DownloadStatusUpdater does no work in the ctor
    240   // so we don't have to worry about lazy initialization.
    241   scoped_ptr<DownloadStatusUpdater> download_status_updater_;
    242 
    243   scoped_refptr<DownloadRequestLimiter> download_request_limiter_;
    244 
    245   // Sequenced task runner for local state related I/O tasks.
    246   const scoped_refptr<base::SequencedTaskRunner> local_state_task_runner_;
    247 
    248   // Ensures that the observers of plugin/print disable/enable state
    249   // notifications are properly added and removed.
    250   PrefChangeRegistrar pref_change_registrar_;
    251 
    252   // Lives here so can safely log events on shutdown.
    253   scoped_ptr<ChromeNetLog> net_log_;
    254 
    255   // Ordered before resource_dispatcher_host_delegate_ due to destruction
    256   // ordering.
    257   scoped_ptr<prerender::PrerenderTracker> prerender_tracker_;
    258 
    259   scoped_ptr<ChromeResourceDispatcherHostDelegate>
    260       resource_dispatcher_host_delegate_;
    261 
    262   scoped_refptr<PromoResourceService> promo_resource_service_;
    263 
    264 #if (defined(OS_WIN) || defined(OS_LINUX)) && !defined(OS_CHROMEOS)
    265   base::RepeatingTimer<BrowserProcessImpl> autoupdate_timer_;
    266 
    267   // Gets called by autoupdate timer to see if browser needs restart and can be
    268   // restarted, and if that's the case, restarts the browser.
    269   void OnAutoupdateTimer();
    270   bool CanAutorestartForUpdate() const;
    271   void RestartBackgroundInstance();
    272 #endif  // defined(OS_WIN) || defined(OS_LINUX) && !defined(OS_CHROMEOS)
    273 
    274   // component updater is normally not used under ChromeOS due
    275   // to concerns over integrity of data shared between profiles,
    276   // but some users of component updater only install per-user.
    277   scoped_ptr<component_updater::ComponentUpdateService> component_updater_;
    278   scoped_refptr<CRLSetFetcher> crl_set_fetcher_;
    279 
    280 #if !defined(DISABLE_NACL)
    281   scoped_ptr<component_updater::PnaclComponentInstaller>
    282       pnacl_component_installer_;
    283 #endif
    284 
    285 #if defined(ENABLE_PLUGIN_INSTALLATION)
    286   scoped_refptr<PluginsResourceService> plugins_resource_service_;
    287 #endif
    288 
    289   scoped_ptr<BrowserProcessPlatformPart> platform_part_;
    290 
    291   // TODO(eroman): Remove this when done debugging 113031. This tracks
    292   // the callstack which released the final module reference count.
    293   base::debug::StackTrace release_last_reference_callstack_;
    294 
    295 #if defined(ENABLE_WEBRTC)
    296   // Lazily initialized.
    297   scoped_ptr<WebRtcLogUploader> webrtc_log_uploader_;
    298 #endif
    299 
    300   scoped_ptr<network_time::NetworkTimeTracker> network_time_tracker_;
    301 
    302   scoped_ptr<gcm::GCMDriver> gcm_driver_;
    303 
    304 #if !defined(OS_ANDROID)
    305   scoped_ptr<ChromeDeviceClient> device_client_;
    306 #endif
    307 
    308   DISALLOW_COPY_AND_ASSIGN(BrowserProcessImpl);
    309 };
    310 
    311 #endif  // CHROME_BROWSER_BROWSER_PROCESS_IMPL_H_
    312