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 "base/time/default_tick_clock.h"
10 #include "build/build_config.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/printing/print_job_manager.h"
15 #include "chrome/browser/profiles/profile_manager.h"
16 #include "chrome/browser/ui/apps/chrome_app_window_client.h"
17 #include "chrome/test/base/testing_browser_process_platform_part.h"
18 #include "components/network_time/network_time_tracker.h"
19 #include "content/public/browser/notification_service.h"
20 #include "net/url_request/url_request_context_getter.h"
21 #include "testing/gtest/include/gtest/gtest.h"
22 #include "ui/message_center/message_center.h"
25 #include "chrome/browser/notifications/notification_ui_manager.h"
26 #include "chrome/browser/prerender/prerender_tracker.h"
27 #include "chrome/browser/safe_browsing/safe_browsing_service.h"
30 #if defined(ENABLE_CONFIGURATION_POLICY)
31 #include "components/policy/core/browser/browser_policy_connector.h"
33 #include "components/policy/core/common/policy_service_stub.h"
34 #endif // defined(ENABLE_CONFIGURATION_POLICY)
36 #if defined(ENABLE_EXTENSIONS)
37 #include "chrome/browser/extensions/chrome_extensions_browser_client.h"
38 #include "chrome/browser/media_galleries/media_file_system_registry.h"
39 #include "components/storage_monitor/storage_monitor.h"
40 #include "components/storage_monitor/test_storage_monitor.h"
43 #if defined(ENABLE_FULL_PRINTING)
44 #include "chrome/browser/printing/background_printing_manager.h"
45 #include "chrome/browser/printing/print_preview_dialog_controller.h"
49 TestingBrowserProcess
* TestingBrowserProcess::GetGlobal() {
50 return static_cast<TestingBrowserProcess
*>(g_browser_process
);
54 void TestingBrowserProcess::CreateInstance() {
55 DCHECK(!g_browser_process
);
56 g_browser_process
= new TestingBrowserProcess
;
60 void TestingBrowserProcess::DeleteInstance() {
61 // g_browser_process must be NULL during its own destruction.
62 BrowserProcess
* browser_process
= g_browser_process
;
63 g_browser_process
= NULL
;
64 delete browser_process
;
67 TestingBrowserProcess::TestingBrowserProcess()
68 : notification_service_(content::NotificationService::Create()),
73 system_request_context_(NULL
),
74 platform_part_(new TestingBrowserProcessPlatformPart()) {
75 #if defined(ENABLE_EXTENSIONS)
76 extensions_browser_client_
.reset(
77 new extensions::ChromeExtensionsBrowserClient
);
78 extensions::AppWindowClient::Set(ChromeAppWindowClient::GetInstance());
79 extensions::ExtensionsBrowserClient::Set(extensions_browser_client_
.get());
83 TestingBrowserProcess::~TestingBrowserProcess() {
84 EXPECT_FALSE(local_state_
);
85 #if defined(ENABLE_CONFIGURATION_POLICY)
86 SetBrowserPolicyConnector(NULL
);
88 #if defined(ENABLE_EXTENSIONS)
89 extensions::ExtensionsBrowserClient::Set(NULL
);
92 // Destructors for some objects owned by TestingBrowserProcess will use
93 // g_browser_process if it is not NULL, so it must be NULL before proceeding.
94 DCHECK_EQ(static_cast<BrowserProcess
*>(NULL
), g_browser_process
);
97 void TestingBrowserProcess::ResourceDispatcherHostCreated() {
100 void TestingBrowserProcess::EndSession() {
103 MetricsServicesManager
* TestingBrowserProcess::GetMetricsServicesManager() {
107 metrics::MetricsService
* TestingBrowserProcess::metrics_service() {
111 rappor::RapporService
* TestingBrowserProcess::rappor_service() {
115 IOThread
* TestingBrowserProcess::io_thread() {
119 WatchDogThread
* TestingBrowserProcess::watchdog_thread() {
123 ProfileManager
* TestingBrowserProcess::profile_manager() {
128 return profile_manager_
.get();
132 void TestingBrowserProcess::SetProfileManager(ProfileManager
* profile_manager
) {
134 // NotificationUIManager can contain references to elements in the current
135 // ProfileManager (for example, the MessageCenterSettingsController maintains
136 // a pointer to the ProfileInfoCache). So when we change the ProfileManager
137 // (typically during test shutdown) make sure to reset any objects that might
138 // maintain references to it. See SetLocalState() for a description of a
139 // similar situation.
140 notification_ui_manager_
.reset();
141 profile_manager_
.reset(profile_manager
);
145 PrefService
* TestingBrowserProcess::local_state() {
149 chrome_variations::VariationsService
*
150 TestingBrowserProcess::variations_service() {
154 policy::BrowserPolicyConnector
*
155 TestingBrowserProcess::browser_policy_connector() {
156 #if defined(ENABLE_CONFIGURATION_POLICY)
157 if (!browser_policy_connector_
)
158 browser_policy_connector_
= platform_part_
->CreateBrowserPolicyConnector();
159 return browser_policy_connector_
.get();
165 policy::PolicyService
* TestingBrowserProcess::policy_service() {
169 #elif defined(ENABLE_CONFIGURATION_POLICY)
170 return browser_policy_connector()->GetPolicyService();
172 if (!policy_service_
)
173 policy_service_
.reset(new policy::PolicyServiceStub());
174 return policy_service_
.get();
178 IconManager
* TestingBrowserProcess::icon_manager() {
182 GLStringManager
* TestingBrowserProcess::gl_string_manager() {
186 GpuModeManager
* TestingBrowserProcess::gpu_mode_manager() {
190 BackgroundModeManager
* TestingBrowserProcess::background_mode_manager() {
194 void TestingBrowserProcess::set_background_mode_manager_for_test(
195 scoped_ptr
<BackgroundModeManager
> manager
) {
199 StatusTray
* TestingBrowserProcess::status_tray() {
203 SafeBrowsingService
* TestingBrowserProcess::safe_browsing_service() {
208 return sb_service_
.get();
212 safe_browsing::ClientSideDetectionService
*
213 TestingBrowserProcess::safe_browsing_detection_service() {
217 net::URLRequestContextGetter
* TestingBrowserProcess::system_request_context() {
218 return system_request_context_
;
221 BrowserProcessPlatformPart
* TestingBrowserProcess::platform_part() {
222 return platform_part_
.get();
225 extensions::EventRouterForwarder
*
226 TestingBrowserProcess::extension_event_router_forwarder() {
230 NotificationUIManager
* TestingBrowserProcess::notification_ui_manager() {
231 #if defined(ENABLE_NOTIFICATIONS)
232 if (!notification_ui_manager_
.get())
233 notification_ui_manager_
.reset(
234 NotificationUIManager::Create(local_state()));
235 return notification_ui_manager_
.get();
242 message_center::MessageCenter
* TestingBrowserProcess::message_center() {
243 return message_center::MessageCenter::Get();
246 IntranetRedirectDetector
* TestingBrowserProcess::intranet_redirect_detector() {
249 void TestingBrowserProcess::CreateDevToolsHttpProtocolHandler(
250 chrome::HostDesktopType host_desktop_type
,
251 const std::string
& ip
,
255 unsigned int TestingBrowserProcess::AddRefModule() {
256 return ++module_ref_count_
;
259 unsigned int TestingBrowserProcess::ReleaseModule() {
260 DCHECK_GT(module_ref_count_
, 0U);
261 return --module_ref_count_
;
264 bool TestingBrowserProcess::IsShuttingDown() {
268 printing::PrintJobManager
* TestingBrowserProcess::print_job_manager() {
269 #if defined(ENABLE_PRINTING)
270 if (!print_job_manager_
.get())
271 print_job_manager_
.reset(new printing::PrintJobManager());
272 return print_job_manager_
.get();
279 printing::PrintPreviewDialogController
*
280 TestingBrowserProcess::print_preview_dialog_controller() {
281 #if defined(ENABLE_FULL_PRINTING)
282 if (!print_preview_dialog_controller_
.get())
283 print_preview_dialog_controller_
=
284 new printing::PrintPreviewDialogController();
285 return print_preview_dialog_controller_
.get();
292 printing::BackgroundPrintingManager
*
293 TestingBrowserProcess::background_printing_manager() {
294 #if defined(ENABLE_FULL_PRINTING)
295 if (!background_printing_manager_
.get()) {
296 background_printing_manager_
.reset(
297 new printing::BackgroundPrintingManager());
299 return background_printing_manager_
.get();
306 const std::string
& TestingBrowserProcess::GetApplicationLocale() {
310 void TestingBrowserProcess::SetApplicationLocale(
311 const std::string
& app_locale
) {
312 app_locale_
= app_locale
;
315 DownloadStatusUpdater
* TestingBrowserProcess::download_status_updater() {
319 DownloadRequestLimiter
* TestingBrowserProcess::download_request_limiter() {
323 ChromeNetLog
* TestingBrowserProcess::net_log() {
327 prerender::PrerenderTracker
* TestingBrowserProcess::prerender_tracker() {
332 if (!prerender_tracker_
.get())
333 prerender_tracker_
.reset(new prerender::PrerenderTracker());
334 return prerender_tracker_
.get();
338 component_updater::ComponentUpdateService
*
339 TestingBrowserProcess::component_updater() {
343 CRLSetFetcher
* TestingBrowserProcess::crl_set_fetcher() {
347 component_updater::PnaclComponentInstaller
*
348 TestingBrowserProcess::pnacl_component_installer() {
352 MediaFileSystemRegistry
* TestingBrowserProcess::media_file_system_registry() {
353 #if defined(OS_IOS) || defined(OS_ANDROID)
357 if (!media_file_system_registry_
)
358 media_file_system_registry_
.reset(new MediaFileSystemRegistry());
359 return media_file_system_registry_
.get();
363 bool TestingBrowserProcess::created_local_state() const {
364 return (local_state_
!= NULL
);
367 #if defined(ENABLE_WEBRTC)
368 WebRtcLogUploader
* TestingBrowserProcess::webrtc_log_uploader() {
373 network_time::NetworkTimeTracker
*
374 TestingBrowserProcess::network_time_tracker() {
375 if (!network_time_tracker_
) {
376 DCHECK(local_state_
);
377 network_time_tracker_
.reset(new network_time::NetworkTimeTracker(
378 scoped_ptr
<base::TickClock
>(new base::DefaultTickClock()),
381 return network_time_tracker_
.get();
384 gcm::GCMDriver
* TestingBrowserProcess::gcm_driver() {
388 void TestingBrowserProcess::SetSystemRequestContext(
389 net::URLRequestContextGetter
* context_getter
) {
390 system_request_context_
= context_getter
;
393 void TestingBrowserProcess::SetLocalState(PrefService
* local_state
) {
395 // The local_state_ PrefService is owned outside of TestingBrowserProcess,
396 // but some of the members of TestingBrowserProcess hold references to it
397 // (for example, via PrefNotifier members). But given our test
398 // infrastructure which tears down individual tests before freeing the
399 // TestingBrowserProcess, there's not a good way to make local_state outlive
400 // these dependencies. As a workaround, whenever local_state_ is cleared
401 // (assumedly as part of exiting the test and freeing TestingBrowserProcess)
402 // any components owned by TestingBrowserProcess that depend on local_state
404 network_time_tracker_
.reset();
406 notification_ui_manager_
.reset();
408 #if defined(ENABLE_CONFIGURATION_POLICY)
409 SetBrowserPolicyConnector(NULL
);
412 local_state_
= local_state
;
415 void TestingBrowserProcess::SetIOThread(IOThread
* io_thread
) {
416 io_thread_
= io_thread
;
419 void TestingBrowserProcess::SetBrowserPolicyConnector(
420 policy::BrowserPolicyConnector
* connector
) {
421 #if defined(ENABLE_CONFIGURATION_POLICY)
422 if (browser_policy_connector_
) {
423 browser_policy_connector_
->Shutdown();
425 browser_policy_connector_
.reset(connector
);
431 void TestingBrowserProcess::SetSafeBrowsingService(
432 SafeBrowsingService
* sb_service
) {
435 sb_service_
= sb_service
;
439 ///////////////////////////////////////////////////////////////////////////////
441 TestingBrowserProcessInitializer::TestingBrowserProcessInitializer() {
442 TestingBrowserProcess::CreateInstance();
445 TestingBrowserProcessInitializer::~TestingBrowserProcessInitializer() {
446 TestingBrowserProcess::DeleteInstance();