Home | History | Annotate | Download | only in base
      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 #include "chrome/test/base/testing_browser_process.h"
      6 
      7 #include "base/prefs/pref_service.h"
      8 #include "base/strings/string_util.h"
      9 #include "build/build_config.h"
     10 #include "chrome/browser/background/background_mode_manager.h"
     11 #include "chrome/browser/browser_process.h"
     12 #include "chrome/browser/browser_process_impl.h"
     13 #include "chrome/browser/profiles/profile_manager.h"
     14 #include "chrome/browser/ui/bookmarks/bookmark_prompt_controller.h"
     15 #include "chrome/test/base/testing_browser_process_platform_part.h"
     16 #include "content/public/browser/notification_service.h"
     17 #include "net/url_request/url_request_context_getter.h"
     18 #include "testing/gtest/include/gtest/gtest.h"
     19 #include "ui/message_center/message_center.h"
     20 
     21 #if !defined(OS_IOS)
     22 #include "chrome/browser/notifications/notification_ui_manager.h"
     23 #include "chrome/browser/prerender/prerender_tracker.h"
     24 #include "chrome/browser/safe_browsing/safe_browsing_service.h"
     25 #include "chrome/browser/thumbnails/render_widget_snapshot_taker.h"
     26 #endif
     27 
     28 #if !defined(OS_IOS) && !defined(OS_ANDROID)
     29 #include "chrome/browser/media_galleries/media_file_system_registry.h"
     30 #include "chrome/browser/storage_monitor/storage_monitor.h"
     31 #include "chrome/browser/storage_monitor/test_storage_monitor.h"
     32 #endif
     33 
     34 #if defined(ENABLE_CONFIGURATION_POLICY)
     35 #include "chrome/browser/policy/browser_policy_connector.h"
     36 #else
     37 #include "chrome/browser/policy/policy_service_stub.h"
     38 #endif  // defined(ENABLE_CONFIGURATION_POLICY)
     39 
     40 #if defined(ENABLE_FULL_PRINTING)
     41 #include "chrome/browser/printing/background_printing_manager.h"
     42 #include "chrome/browser/printing/print_preview_dialog_controller.h"
     43 #endif
     44 
     45 // static
     46 TestingBrowserProcess* TestingBrowserProcess::GetGlobal() {
     47   return static_cast<TestingBrowserProcess*>(g_browser_process);
     48 }
     49 
     50 TestingBrowserProcess::TestingBrowserProcess()
     51     : notification_service_(content::NotificationService::Create()),
     52       module_ref_count_(0),
     53       app_locale_("en"),
     54 #if !defined(OS_IOS)
     55       render_widget_snapshot_taker_(new RenderWidgetSnapshotTaker),
     56 #endif
     57       local_state_(NULL),
     58       io_thread_(NULL),
     59       system_request_context_(NULL),
     60       platform_part_(new TestingBrowserProcessPlatformPart()) {
     61 }
     62 
     63 TestingBrowserProcess::~TestingBrowserProcess() {
     64   EXPECT_FALSE(local_state_);
     65 #if defined(ENABLE_CONFIGURATION_POLICY)
     66   SetBrowserPolicyConnector(NULL);
     67 #endif
     68 
     69   // Destructors for some objects owned by TestingBrowserProcess will use
     70   // g_browser_process if it is not NULL, so it must be NULL before proceeding.
     71   DCHECK_EQ(static_cast<BrowserProcess*>(NULL), g_browser_process);
     72 }
     73 
     74 void TestingBrowserProcess::ResourceDispatcherHostCreated() {
     75 }
     76 
     77 void TestingBrowserProcess::EndSession() {
     78 }
     79 
     80 MetricsService* TestingBrowserProcess::metrics_service() {
     81   return NULL;
     82 }
     83 
     84 IOThread* TestingBrowserProcess::io_thread() {
     85   return io_thread_;
     86 }
     87 
     88 WatchDogThread* TestingBrowserProcess::watchdog_thread() {
     89   return NULL;
     90 }
     91 
     92 ProfileManager* TestingBrowserProcess::profile_manager() {
     93 #if defined(OS_IOS)
     94   NOTIMPLEMENTED();
     95   return NULL;
     96 #else
     97   return profile_manager_.get();
     98 #endif
     99 }
    100 
    101 void TestingBrowserProcess::SetProfileManager(ProfileManager* profile_manager) {
    102 #if !defined(OS_IOS)
    103   profile_manager_.reset(profile_manager);
    104 #endif
    105 }
    106 
    107 PrefService* TestingBrowserProcess::local_state() {
    108   return local_state_;
    109 }
    110 
    111 chrome_variations::VariationsService*
    112     TestingBrowserProcess::variations_service() {
    113   return NULL;
    114 }
    115 
    116 policy::BrowserPolicyConnector*
    117     TestingBrowserProcess::browser_policy_connector() {
    118 #if defined(ENABLE_CONFIGURATION_POLICY)
    119   if (!browser_policy_connector_)
    120     browser_policy_connector_.reset(new policy::BrowserPolicyConnector());
    121   return browser_policy_connector_.get();
    122 #else
    123   return NULL;
    124 #endif
    125 }
    126 
    127 policy::PolicyService* TestingBrowserProcess::policy_service() {
    128 #if defined(OS_IOS)
    129   NOTIMPLEMENTED();
    130   return NULL;
    131 #elif defined(ENABLE_CONFIGURATION_POLICY)
    132   return browser_policy_connector()->GetPolicyService();
    133 #else
    134   if (!policy_service_)
    135     policy_service_.reset(new policy::PolicyServiceStub());
    136   return policy_service_.get();
    137 #endif
    138 }
    139 
    140 IconManager* TestingBrowserProcess::icon_manager() {
    141   return NULL;
    142 }
    143 
    144 GLStringManager* TestingBrowserProcess::gl_string_manager() {
    145   return NULL;
    146 }
    147 
    148 GpuModeManager* TestingBrowserProcess::gpu_mode_manager() {
    149   return NULL;
    150 }
    151 
    152 RenderWidgetSnapshotTaker*
    153 TestingBrowserProcess::GetRenderWidgetSnapshotTaker() {
    154 #if defined(OS_IOS)
    155   NOTREACHED();
    156   return NULL;
    157 #else
    158   return render_widget_snapshot_taker_.get();
    159 #endif
    160 }
    161 
    162 BackgroundModeManager* TestingBrowserProcess::background_mode_manager() {
    163   return NULL;
    164 }
    165 
    166 void TestingBrowserProcess::set_background_mode_manager_for_test(
    167     scoped_ptr<BackgroundModeManager> manager) {
    168   NOTREACHED();
    169 }
    170 
    171 StatusTray* TestingBrowserProcess::status_tray() {
    172   return NULL;
    173 }
    174 
    175 SafeBrowsingService* TestingBrowserProcess::safe_browsing_service() {
    176 #if defined(OS_IOS)
    177   NOTIMPLEMENTED();
    178   return NULL;
    179 #else
    180   return sb_service_.get();
    181 #endif
    182 }
    183 
    184 safe_browsing::ClientSideDetectionService*
    185 TestingBrowserProcess::safe_browsing_detection_service() {
    186   return NULL;
    187 }
    188 
    189 net::URLRequestContextGetter* TestingBrowserProcess::system_request_context() {
    190   return system_request_context_;
    191 }
    192 
    193 BrowserProcessPlatformPart* TestingBrowserProcess::platform_part() {
    194   return platform_part_.get();
    195 }
    196 
    197 extensions::EventRouterForwarder*
    198 TestingBrowserProcess::extension_event_router_forwarder() {
    199   return NULL;
    200 }
    201 
    202 NotificationUIManager* TestingBrowserProcess::notification_ui_manager() {
    203 #if defined(ENABLE_NOTIFICATIONS)
    204   if (!notification_ui_manager_.get())
    205     notification_ui_manager_.reset(
    206         NotificationUIManager::Create(local_state()));
    207   return notification_ui_manager_.get();
    208 #else
    209   NOTIMPLEMENTED();
    210   return NULL;
    211 #endif
    212 }
    213 
    214 message_center::MessageCenter* TestingBrowserProcess::message_center() {
    215   return message_center::MessageCenter::Get();
    216 }
    217 
    218 IntranetRedirectDetector* TestingBrowserProcess::intranet_redirect_detector() {
    219   return NULL;
    220 }
    221 
    222 AutomationProviderList* TestingBrowserProcess::GetAutomationProviderList() {
    223   return NULL;
    224 }
    225 
    226 void TestingBrowserProcess::CreateDevToolsHttpProtocolHandler(
    227     chrome::HostDesktopType host_desktop_type,
    228     const std::string& ip,
    229     int port,
    230     const std::string& frontend_url) {
    231 }
    232 
    233 unsigned int TestingBrowserProcess::AddRefModule() {
    234   return ++module_ref_count_;
    235 }
    236 
    237 unsigned int TestingBrowserProcess::ReleaseModule() {
    238   DCHECK_GT(module_ref_count_, 0U);
    239   return --module_ref_count_;
    240 }
    241 
    242 bool TestingBrowserProcess::IsShuttingDown() {
    243   return false;
    244 }
    245 
    246 printing::PrintJobManager* TestingBrowserProcess::print_job_manager() {
    247   return NULL;
    248 }
    249 
    250 printing::PrintPreviewDialogController*
    251 TestingBrowserProcess::print_preview_dialog_controller() {
    252 #if defined(ENABLE_FULL_PRINTING)
    253   if (!print_preview_dialog_controller_.get())
    254     print_preview_dialog_controller_ =
    255         new printing::PrintPreviewDialogController();
    256   return print_preview_dialog_controller_.get();
    257 #else
    258   NOTIMPLEMENTED();
    259   return NULL;
    260 #endif
    261 }
    262 
    263 printing::BackgroundPrintingManager*
    264 TestingBrowserProcess::background_printing_manager() {
    265 #if defined(ENABLE_FULL_PRINTING)
    266   if (!background_printing_manager_.get()) {
    267     background_printing_manager_.reset(
    268         new printing::BackgroundPrintingManager());
    269   }
    270   return background_printing_manager_.get();
    271 #else
    272   NOTIMPLEMENTED();
    273   return NULL;
    274 #endif
    275 }
    276 
    277 const std::string& TestingBrowserProcess::GetApplicationLocale() {
    278   return app_locale_;
    279 }
    280 
    281 void TestingBrowserProcess::SetApplicationLocale(
    282     const std::string& app_locale) {
    283   app_locale_ = app_locale;
    284 }
    285 
    286 DownloadStatusUpdater* TestingBrowserProcess::download_status_updater() {
    287   return NULL;
    288 }
    289 
    290 DownloadRequestLimiter* TestingBrowserProcess::download_request_limiter() {
    291   return NULL;
    292 }
    293 
    294 ChromeNetLog* TestingBrowserProcess::net_log() {
    295   return NULL;
    296 }
    297 
    298 prerender::PrerenderTracker* TestingBrowserProcess::prerender_tracker() {
    299 #if defined(OS_IOS)
    300   NOTIMPLEMENTED();
    301   return NULL;
    302 #else
    303   if (!prerender_tracker_.get())
    304     prerender_tracker_.reset(new prerender::PrerenderTracker());
    305   return prerender_tracker_.get();
    306 #endif
    307 }
    308 
    309 ComponentUpdateService* TestingBrowserProcess::component_updater() {
    310   return NULL;
    311 }
    312 
    313 CRLSetFetcher* TestingBrowserProcess::crl_set_fetcher() {
    314   return NULL;
    315 }
    316 
    317 PnaclComponentInstaller* TestingBrowserProcess::pnacl_component_installer() {
    318   return NULL;
    319 }
    320 
    321 BookmarkPromptController* TestingBrowserProcess::bookmark_prompt_controller() {
    322 #if defined(OS_IOS)
    323   NOTIMPLEMENTED();
    324   return NULL;
    325 #else
    326   return bookmark_prompt_controller_.get();
    327 #endif
    328 }
    329 
    330 chrome::StorageMonitor* TestingBrowserProcess::storage_monitor() {
    331 #if defined(OS_IOS) || defined(OS_ANDROID)
    332   NOTIMPLEMENTED();
    333   return NULL;
    334 #else
    335   return storage_monitor_.get();
    336 #endif
    337 }
    338 
    339 chrome::MediaFileSystemRegistry*
    340 TestingBrowserProcess::media_file_system_registry() {
    341 #if defined(OS_IOS) || defined(OS_ANDROID)
    342   NOTIMPLEMENTED();
    343   return NULL;
    344 #else
    345   if (!media_file_system_registry_)
    346     media_file_system_registry_.reset(new chrome::MediaFileSystemRegistry());
    347   return media_file_system_registry_.get();
    348 #endif
    349 }
    350 
    351 bool TestingBrowserProcess::created_local_state() const {
    352     return (local_state_ != NULL);
    353 }
    354 
    355 #if defined(ENABLE_WEBRTC)
    356 WebRtcLogUploader* TestingBrowserProcess::webrtc_log_uploader() {
    357   return NULL;
    358 }
    359 #endif
    360 
    361 void TestingBrowserProcess::SetBookmarkPromptController(
    362     BookmarkPromptController* controller) {
    363 #if !defined(OS_IOS)
    364   bookmark_prompt_controller_.reset(controller);
    365 #endif
    366 }
    367 
    368 void TestingBrowserProcess::SetSystemRequestContext(
    369     net::URLRequestContextGetter* context_getter) {
    370   system_request_context_ = context_getter;
    371 }
    372 
    373 void TestingBrowserProcess::SetLocalState(PrefService* local_state) {
    374   if (!local_state) {
    375     // The local_state_ PrefService is owned outside of TestingBrowserProcess,
    376     // but some of the members of TestingBrowserProcess hold references to it
    377     // (for example, via PrefNotifier members). But given our test
    378     // infrastructure which tears down individual tests before freeing the
    379     // TestingBrowserProcess, there's not a good way to make local_state outlive
    380     // these dependencies. As a workaround, whenever local_state_ is cleared
    381     // (assumedly as part of exiting the test and freeing TestingBrowserProcess)
    382     // any components owned by TestingBrowserProcess that depend on local_state
    383     // are also freed.
    384 #if !defined(OS_IOS)
    385     notification_ui_manager_.reset();
    386 #endif
    387 #if defined(ENABLE_CONFIGURATION_POLICY)
    388     SetBrowserPolicyConnector(NULL);
    389 #endif
    390   }
    391   local_state_ = local_state;
    392 }
    393 
    394 void TestingBrowserProcess::SetIOThread(IOThread* io_thread) {
    395   io_thread_ = io_thread;
    396 }
    397 
    398 void TestingBrowserProcess::SetBrowserPolicyConnector(
    399     policy::BrowserPolicyConnector* connector) {
    400 #if defined(ENABLE_CONFIGURATION_POLICY)
    401   if (browser_policy_connector_) {
    402     browser_policy_connector_->Shutdown();
    403   }
    404   browser_policy_connector_.reset(connector);
    405 #else
    406   CHECK(false);
    407 #endif
    408 }
    409 
    410 void TestingBrowserProcess::SetSafeBrowsingService(
    411     SafeBrowsingService* sb_service) {
    412 #if !defined(OS_IOS)
    413   NOTIMPLEMENTED();
    414   sb_service_ = sb_service;
    415 #endif
    416 }
    417 
    418 void TestingBrowserProcess::SetStorageMonitor(
    419     scoped_ptr<chrome::StorageMonitor> storage_monitor) {
    420 #if !defined(OS_IOS) && !defined(OS_ANDROID)
    421   storage_monitor_ = storage_monitor.Pass();
    422 #endif
    423 }
    424