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.
5 #include "chrome/test/base/testing_browser_process.h"
7 #include "base/prefs/pref_service.h"
8 #include "base/strings/string_util.h"
9 #include "build/build_config.h"
10 #include "chrome/browser/apps/chrome_apps_client.h"
11 #include "chrome/browser/background/background_mode_manager.h"
12 #include "chrome/browser/browser_process.h"
13 #include "chrome/browser/browser_process_impl.h"
14 #include "chrome/browser/extensions/chrome_extensions_browser_client.h"
15 #include "chrome/browser/printing/print_job_manager.h"
16 #include "chrome/browser/profiles/profile_manager.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"
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"
29 #if !defined(OS_IOS) && !defined(OS_ANDROID)
30 #include "chrome/browser/media_galleries/media_file_system_registry.h"
31 #include "components/storage_monitor/storage_monitor.h"
32 #include "components/storage_monitor/test_storage_monitor.h"
35 #if defined(ENABLE_CONFIGURATION_POLICY)
36 #include "components/policy/core/browser/browser_policy_connector.h"
38 #include "components/policy/core/common/policy_service_stub.h"
39 #endif // defined(ENABLE_CONFIGURATION_POLICY)
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"
47 TestingBrowserProcess
* TestingBrowserProcess::GetGlobal() {
48 return static_cast<TestingBrowserProcess
*>(g_browser_process
);
52 void TestingBrowserProcess::CreateInstance() {
53 DCHECK(!g_browser_process
);
54 g_browser_process
= new TestingBrowserProcess
;
58 void TestingBrowserProcess::DeleteInstance() {
59 // g_browser_process must be NULL during its own destruction.
60 BrowserProcess
* browser_process
= g_browser_process
;
61 g_browser_process
= NULL
;
62 delete browser_process
;
65 TestingBrowserProcess::TestingBrowserProcess()
66 : notification_service_(content::NotificationService::Create()),
71 system_request_context_(NULL
),
72 platform_part_(new TestingBrowserProcessPlatformPart()),
73 extensions_browser_client_(
74 new extensions::ChromeExtensionsBrowserClient
) {
75 extensions::ExtensionsBrowserClient::Set(extensions_browser_client_
.get());
76 apps::AppsClient::Set(ChromeAppsClient::GetInstance());
79 TestingBrowserProcess::~TestingBrowserProcess() {
80 EXPECT_FALSE(local_state_
);
81 #if defined(ENABLE_CONFIGURATION_POLICY)
82 SetBrowserPolicyConnector(NULL
);
84 extensions::ExtensionsBrowserClient::Set(NULL
);
86 // Destructors for some objects owned by TestingBrowserProcess will use
87 // g_browser_process if it is not NULL, so it must be NULL before proceeding.
88 DCHECK_EQ(static_cast<BrowserProcess
*>(NULL
), g_browser_process
);
91 void TestingBrowserProcess::ResourceDispatcherHostCreated() {
94 void TestingBrowserProcess::EndSession() {
97 MetricsService
* TestingBrowserProcess::metrics_service() {
101 rappor::RapporService
* TestingBrowserProcess::rappor_service() {
105 IOThread
* TestingBrowserProcess::io_thread() {
109 WatchDogThread
* TestingBrowserProcess::watchdog_thread() {
113 ProfileManager
* TestingBrowserProcess::profile_manager() {
118 return profile_manager_
.get();
122 void TestingBrowserProcess::SetProfileManager(ProfileManager
* profile_manager
) {
124 // NotificationUIManager can contain references to elements in the current
125 // ProfileManager (for example, the MessageCenterSettingsController maintains
126 // a pointer to the ProfileInfoCache). So when we change the ProfileManager
127 // (typically during test shutdown) make sure to reset any objects that might
128 // maintain references to it. See SetLocalState() for a description of a
129 // similar situation.
130 notification_ui_manager_
.reset();
131 profile_manager_
.reset(profile_manager
);
135 PrefService
* TestingBrowserProcess::local_state() {
139 chrome_variations::VariationsService
*
140 TestingBrowserProcess::variations_service() {
144 policy::BrowserPolicyConnector
*
145 TestingBrowserProcess::browser_policy_connector() {
146 #if defined(ENABLE_CONFIGURATION_POLICY)
147 if (!browser_policy_connector_
)
148 browser_policy_connector_
= platform_part_
->CreateBrowserPolicyConnector();
149 return browser_policy_connector_
.get();
155 policy::PolicyService
* TestingBrowserProcess::policy_service() {
159 #elif defined(ENABLE_CONFIGURATION_POLICY)
160 return browser_policy_connector()->GetPolicyService();
162 if (!policy_service_
)
163 policy_service_
.reset(new policy::PolicyServiceStub());
164 return policy_service_
.get();
168 IconManager
* TestingBrowserProcess::icon_manager() {
172 GLStringManager
* TestingBrowserProcess::gl_string_manager() {
176 GpuModeManager
* TestingBrowserProcess::gpu_mode_manager() {
180 BackgroundModeManager
* TestingBrowserProcess::background_mode_manager() {
184 void TestingBrowserProcess::set_background_mode_manager_for_test(
185 scoped_ptr
<BackgroundModeManager
> manager
) {
189 StatusTray
* TestingBrowserProcess::status_tray() {
193 SafeBrowsingService
* TestingBrowserProcess::safe_browsing_service() {
198 return sb_service_
.get();
202 safe_browsing::ClientSideDetectionService
*
203 TestingBrowserProcess::safe_browsing_detection_service() {
207 net::URLRequestContextGetter
* TestingBrowserProcess::system_request_context() {
208 return system_request_context_
;
211 BrowserProcessPlatformPart
* TestingBrowserProcess::platform_part() {
212 return platform_part_
.get();
215 extensions::EventRouterForwarder
*
216 TestingBrowserProcess::extension_event_router_forwarder() {
220 NotificationUIManager
* TestingBrowserProcess::notification_ui_manager() {
221 #if defined(ENABLE_NOTIFICATIONS)
222 if (!notification_ui_manager_
.get())
223 notification_ui_manager_
.reset(
224 NotificationUIManager::Create(local_state()));
225 return notification_ui_manager_
.get();
232 message_center::MessageCenter
* TestingBrowserProcess::message_center() {
233 return message_center::MessageCenter::Get();
236 IntranetRedirectDetector
* TestingBrowserProcess::intranet_redirect_detector() {
239 void TestingBrowserProcess::CreateDevToolsHttpProtocolHandler(
240 chrome::HostDesktopType host_desktop_type
,
241 const std::string
& ip
,
245 unsigned int TestingBrowserProcess::AddRefModule() {
246 return ++module_ref_count_
;
249 unsigned int TestingBrowserProcess::ReleaseModule() {
250 DCHECK_GT(module_ref_count_
, 0U);
251 return --module_ref_count_
;
254 bool TestingBrowserProcess::IsShuttingDown() {
258 printing::PrintJobManager
* TestingBrowserProcess::print_job_manager() {
259 #if defined(ENABLE_FULL_PRINTING)
260 if (!print_job_manager_
.get())
261 print_job_manager_
.reset(new printing::PrintJobManager());
262 return print_job_manager_
.get();
269 printing::PrintPreviewDialogController
*
270 TestingBrowserProcess::print_preview_dialog_controller() {
271 #if defined(ENABLE_FULL_PRINTING)
272 if (!print_preview_dialog_controller_
.get())
273 print_preview_dialog_controller_
=
274 new printing::PrintPreviewDialogController();
275 return print_preview_dialog_controller_
.get();
282 printing::BackgroundPrintingManager
*
283 TestingBrowserProcess::background_printing_manager() {
284 #if defined(ENABLE_FULL_PRINTING)
285 if (!background_printing_manager_
.get()) {
286 background_printing_manager_
.reset(
287 new printing::BackgroundPrintingManager());
289 return background_printing_manager_
.get();
296 const std::string
& TestingBrowserProcess::GetApplicationLocale() {
300 void TestingBrowserProcess::SetApplicationLocale(
301 const std::string
& app_locale
) {
302 app_locale_
= app_locale
;
305 DownloadStatusUpdater
* TestingBrowserProcess::download_status_updater() {
309 DownloadRequestLimiter
* TestingBrowserProcess::download_request_limiter() {
313 ChromeNetLog
* TestingBrowserProcess::net_log() {
317 prerender::PrerenderTracker
* TestingBrowserProcess::prerender_tracker() {
322 if (!prerender_tracker_
.get())
323 prerender_tracker_
.reset(new prerender::PrerenderTracker());
324 return prerender_tracker_
.get();
328 component_updater::ComponentUpdateService
*
329 TestingBrowserProcess::component_updater() {
333 CRLSetFetcher
* TestingBrowserProcess::crl_set_fetcher() {
337 component_updater::PnaclComponentInstaller
*
338 TestingBrowserProcess::pnacl_component_installer() {
342 MediaFileSystemRegistry
* TestingBrowserProcess::media_file_system_registry() {
343 #if defined(OS_IOS) || defined(OS_ANDROID)
347 if (!media_file_system_registry_
)
348 media_file_system_registry_
.reset(new MediaFileSystemRegistry());
349 return media_file_system_registry_
.get();
353 bool TestingBrowserProcess::created_local_state() const {
354 return (local_state_
!= NULL
);
357 #if defined(ENABLE_WEBRTC)
358 WebRtcLogUploader
* TestingBrowserProcess::webrtc_log_uploader() {
363 void TestingBrowserProcess::SetSystemRequestContext(
364 net::URLRequestContextGetter
* context_getter
) {
365 system_request_context_
= context_getter
;
368 void TestingBrowserProcess::SetLocalState(PrefService
* local_state
) {
370 // The local_state_ PrefService is owned outside of TestingBrowserProcess,
371 // but some of the members of TestingBrowserProcess hold references to it
372 // (for example, via PrefNotifier members). But given our test
373 // infrastructure which tears down individual tests before freeing the
374 // TestingBrowserProcess, there's not a good way to make local_state outlive
375 // these dependencies. As a workaround, whenever local_state_ is cleared
376 // (assumedly as part of exiting the test and freeing TestingBrowserProcess)
377 // any components owned by TestingBrowserProcess that depend on local_state
380 notification_ui_manager_
.reset();
382 #if defined(ENABLE_CONFIGURATION_POLICY)
383 SetBrowserPolicyConnector(NULL
);
386 local_state_
= local_state
;
389 void TestingBrowserProcess::SetIOThread(IOThread
* io_thread
) {
390 io_thread_
= io_thread
;
393 void TestingBrowserProcess::SetBrowserPolicyConnector(
394 policy::BrowserPolicyConnector
* connector
) {
395 #if defined(ENABLE_CONFIGURATION_POLICY)
396 if (browser_policy_connector_
) {
397 browser_policy_connector_
->Shutdown();
399 browser_policy_connector_
.reset(connector
);
405 void TestingBrowserProcess::SetSafeBrowsingService(
406 SafeBrowsingService
* sb_service
) {
409 sb_service_
= sb_service
;
413 ///////////////////////////////////////////////////////////////////////////////
415 TestingBrowserProcessInitializer::TestingBrowserProcessInitializer() {
416 TestingBrowserProcess::CreateInstance();
419 TestingBrowserProcessInitializer::~TestingBrowserProcessInitializer() {
420 TestingBrowserProcess::DeleteInstance();