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