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/browser/ui/bookmarks/bookmark_prompt_controller.h"
18 #include "chrome/test/base/testing_browser_process_platform_part.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(OS_IOS) && !defined(OS_ANDROID)
31 #include "chrome/browser/media_galleries/media_file_system_registry.h"
32 #include "components/storage_monitor/storage_monitor.h"
33 #include "components/storage_monitor/test_storage_monitor.h"
36 #if defined(ENABLE_CONFIGURATION_POLICY)
37 #include "components/policy/core/browser/browser_policy_connector.h"
39 #include "components/policy/core/common/policy_service_stub.h"
40 #endif // defined(ENABLE_CONFIGURATION_POLICY)
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"
48 TestingBrowserProcess
* TestingBrowserProcess::GetGlobal() {
49 return static_cast<TestingBrowserProcess
*>(g_browser_process
);
53 void TestingBrowserProcess::CreateInstance() {
54 DCHECK(!g_browser_process
);
55 g_browser_process
= new TestingBrowserProcess
;
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
;
66 TestingBrowserProcess::TestingBrowserProcess()
67 : notification_service_(content::NotificationService::Create()),
72 system_request_context_(NULL
),
73 platform_part_(new TestingBrowserProcessPlatformPart()),
74 extensions_browser_client_(
75 new extensions::ChromeExtensionsBrowserClient
) {
76 extensions::ExtensionsBrowserClient::Set(extensions_browser_client_
.get());
77 apps::AppsClient::Set(ChromeAppsClient::GetInstance());
80 TestingBrowserProcess::~TestingBrowserProcess() {
81 EXPECT_FALSE(local_state_
);
82 #if defined(ENABLE_CONFIGURATION_POLICY)
83 SetBrowserPolicyConnector(NULL
);
85 extensions::ExtensionsBrowserClient::Set(NULL
);
87 // Destructors for some objects owned by TestingBrowserProcess will use
88 // g_browser_process if it is not NULL, so it must be NULL before proceeding.
89 DCHECK_EQ(static_cast<BrowserProcess
*>(NULL
), g_browser_process
);
92 void TestingBrowserProcess::ResourceDispatcherHostCreated() {
95 void TestingBrowserProcess::EndSession() {
98 MetricsService
* TestingBrowserProcess::metrics_service() {
102 rappor::RapporService
* TestingBrowserProcess::rappor_service() {
106 IOThread
* TestingBrowserProcess::io_thread() {
110 WatchDogThread
* TestingBrowserProcess::watchdog_thread() {
114 ProfileManager
* TestingBrowserProcess::profile_manager() {
119 return profile_manager_
.get();
123 void TestingBrowserProcess::SetProfileManager(ProfileManager
* profile_manager
) {
125 // NotificationUIManager can contain references to elements in the current
126 // ProfileManager (for example, the MessageCenterSettingsController maintains
127 // a pointer to the ProfileInfoCache). So when we change the ProfileManager
128 // (typically during test shutdown) make sure to reset any objects that might
129 // maintain references to it. See SetLocalState() for a description of a
130 // similar situation.
131 notification_ui_manager_
.reset();
132 profile_manager_
.reset(profile_manager
);
136 PrefService
* TestingBrowserProcess::local_state() {
140 chrome_variations::VariationsService
*
141 TestingBrowserProcess::variations_service() {
145 policy::BrowserPolicyConnector
*
146 TestingBrowserProcess::browser_policy_connector() {
147 #if defined(ENABLE_CONFIGURATION_POLICY)
148 if (!browser_policy_connector_
)
149 browser_policy_connector_
= platform_part_
->CreateBrowserPolicyConnector();
150 return browser_policy_connector_
.get();
156 policy::PolicyService
* TestingBrowserProcess::policy_service() {
160 #elif defined(ENABLE_CONFIGURATION_POLICY)
161 return browser_policy_connector()->GetPolicyService();
163 if (!policy_service_
)
164 policy_service_
.reset(new policy::PolicyServiceStub());
165 return policy_service_
.get();
169 IconManager
* TestingBrowserProcess::icon_manager() {
173 GLStringManager
* TestingBrowserProcess::gl_string_manager() {
177 GpuModeManager
* TestingBrowserProcess::gpu_mode_manager() {
181 BackgroundModeManager
* TestingBrowserProcess::background_mode_manager() {
185 void TestingBrowserProcess::set_background_mode_manager_for_test(
186 scoped_ptr
<BackgroundModeManager
> manager
) {
190 StatusTray
* TestingBrowserProcess::status_tray() {
194 SafeBrowsingService
* TestingBrowserProcess::safe_browsing_service() {
199 return sb_service_
.get();
203 safe_browsing::ClientSideDetectionService
*
204 TestingBrowserProcess::safe_browsing_detection_service() {
208 net::URLRequestContextGetter
* TestingBrowserProcess::system_request_context() {
209 return system_request_context_
;
212 BrowserProcessPlatformPart
* TestingBrowserProcess::platform_part() {
213 return platform_part_
.get();
216 extensions::EventRouterForwarder
*
217 TestingBrowserProcess::extension_event_router_forwarder() {
221 NotificationUIManager
* TestingBrowserProcess::notification_ui_manager() {
222 #if defined(ENABLE_NOTIFICATIONS)
223 if (!notification_ui_manager_
.get())
224 notification_ui_manager_
.reset(
225 NotificationUIManager::Create(local_state()));
226 return notification_ui_manager_
.get();
233 message_center::MessageCenter
* TestingBrowserProcess::message_center() {
234 return message_center::MessageCenter::Get();
237 IntranetRedirectDetector
* TestingBrowserProcess::intranet_redirect_detector() {
241 AutomationProviderList
* TestingBrowserProcess::GetAutomationProviderList() {
245 void TestingBrowserProcess::CreateDevToolsHttpProtocolHandler(
246 chrome::HostDesktopType host_desktop_type
,
247 const std::string
& ip
,
249 const std::string
& frontend_url
) {
252 unsigned int TestingBrowserProcess::AddRefModule() {
253 return ++module_ref_count_
;
256 unsigned int TestingBrowserProcess::ReleaseModule() {
257 DCHECK_GT(module_ref_count_
, 0U);
258 return --module_ref_count_
;
261 bool TestingBrowserProcess::IsShuttingDown() {
265 printing::PrintJobManager
* TestingBrowserProcess::print_job_manager() {
266 #if defined(ENABLE_FULL_PRINTING)
267 if (!print_job_manager_
.get())
268 print_job_manager_
.reset(new printing::PrintJobManager());
269 return print_job_manager_
.get();
276 printing::PrintPreviewDialogController
*
277 TestingBrowserProcess::print_preview_dialog_controller() {
278 #if defined(ENABLE_FULL_PRINTING)
279 if (!print_preview_dialog_controller_
.get())
280 print_preview_dialog_controller_
=
281 new printing::PrintPreviewDialogController();
282 return print_preview_dialog_controller_
.get();
289 printing::BackgroundPrintingManager
*
290 TestingBrowserProcess::background_printing_manager() {
291 #if defined(ENABLE_FULL_PRINTING)
292 if (!background_printing_manager_
.get()) {
293 background_printing_manager_
.reset(
294 new printing::BackgroundPrintingManager());
296 return background_printing_manager_
.get();
303 const std::string
& TestingBrowserProcess::GetApplicationLocale() {
307 void TestingBrowserProcess::SetApplicationLocale(
308 const std::string
& app_locale
) {
309 app_locale_
= app_locale
;
312 DownloadStatusUpdater
* TestingBrowserProcess::download_status_updater() {
316 DownloadRequestLimiter
* TestingBrowserProcess::download_request_limiter() {
320 ChromeNetLog
* TestingBrowserProcess::net_log() {
324 prerender::PrerenderTracker
* TestingBrowserProcess::prerender_tracker() {
329 if (!prerender_tracker_
.get())
330 prerender_tracker_
.reset(new prerender::PrerenderTracker());
331 return prerender_tracker_
.get();
335 component_updater::ComponentUpdateService
*
336 TestingBrowserProcess::component_updater() {
340 CRLSetFetcher
* TestingBrowserProcess::crl_set_fetcher() {
344 component_updater::PnaclComponentInstaller
*
345 TestingBrowserProcess::pnacl_component_installer() {
349 BookmarkPromptController
* TestingBrowserProcess::bookmark_prompt_controller() {
354 return bookmark_prompt_controller_
.get();
358 MediaFileSystemRegistry
* TestingBrowserProcess::media_file_system_registry() {
359 #if defined(OS_IOS) || defined(OS_ANDROID)
363 if (!media_file_system_registry_
)
364 media_file_system_registry_
.reset(new MediaFileSystemRegistry());
365 return media_file_system_registry_
.get();
369 bool TestingBrowserProcess::created_local_state() const {
370 return (local_state_
!= NULL
);
373 #if defined(ENABLE_WEBRTC)
374 WebRtcLogUploader
* TestingBrowserProcess::webrtc_log_uploader() {
379 void TestingBrowserProcess::SetBookmarkPromptController(
380 BookmarkPromptController
* controller
) {
382 bookmark_prompt_controller_
.reset(controller
);
386 void TestingBrowserProcess::SetSystemRequestContext(
387 net::URLRequestContextGetter
* context_getter
) {
388 system_request_context_
= context_getter
;
391 void TestingBrowserProcess::SetLocalState(PrefService
* local_state
) {
393 // The local_state_ PrefService is owned outside of TestingBrowserProcess,
394 // but some of the members of TestingBrowserProcess hold references to it
395 // (for example, via PrefNotifier members). But given our test
396 // infrastructure which tears down individual tests before freeing the
397 // TestingBrowserProcess, there's not a good way to make local_state outlive
398 // these dependencies. As a workaround, whenever local_state_ is cleared
399 // (assumedly as part of exiting the test and freeing TestingBrowserProcess)
400 // any components owned by TestingBrowserProcess that depend on local_state
403 notification_ui_manager_
.reset();
405 #if defined(ENABLE_CONFIGURATION_POLICY)
406 SetBrowserPolicyConnector(NULL
);
409 local_state_
= local_state
;
412 void TestingBrowserProcess::SetIOThread(IOThread
* io_thread
) {
413 io_thread_
= io_thread
;
416 void TestingBrowserProcess::SetBrowserPolicyConnector(
417 policy::BrowserPolicyConnector
* connector
) {
418 #if defined(ENABLE_CONFIGURATION_POLICY)
419 if (browser_policy_connector_
) {
420 browser_policy_connector_
->Shutdown();
422 browser_policy_connector_
.reset(connector
);
428 void TestingBrowserProcess::SetSafeBrowsingService(
429 SafeBrowsingService
* sb_service
) {
432 sb_service_
= sb_service
;
436 ///////////////////////////////////////////////////////////////////////////////
438 TestingBrowserProcessInitializer::TestingBrowserProcessInitializer() {
439 TestingBrowserProcess::CreateInstance();
442 TestingBrowserProcessInitializer::~TestingBrowserProcessInitializer() {
443 TestingBrowserProcess::DeleteInstance();