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