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/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"
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"
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"
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()),
71 render_widget_snapshot_taker_(new RenderWidgetSnapshotTaker
),
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());
82 TestingBrowserProcess::~TestingBrowserProcess() {
83 EXPECT_FALSE(local_state_
);
84 #if defined(ENABLE_CONFIGURATION_POLICY)
85 SetBrowserPolicyConnector(NULL
);
87 extensions::ExtensionsBrowserClient::Set(NULL
);
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
);
94 void TestingBrowserProcess::ResourceDispatcherHostCreated() {
97 void TestingBrowserProcess::EndSession() {
100 MetricsService
* TestingBrowserProcess::metrics_service() {
104 IOThread
* TestingBrowserProcess::io_thread() {
108 WatchDogThread
* TestingBrowserProcess::watchdog_thread() {
112 ProfileManager
* TestingBrowserProcess::profile_manager() {
117 return profile_manager_
.get();
121 void TestingBrowserProcess::SetProfileManager(ProfileManager
* profile_manager
) {
123 // NotificationUIManager can contain references to elements in the current
124 // ProfileManager (for example, the MessageCenterSettingsController maintains
125 // a pointer to the ProfileInfoCache). So when we change the ProfileManager
126 // (typically during test shutdown) make sure to reset any objects that might
127 // maintain references to it. See SetLocalState() for a description of a
128 // similar situation.
129 notification_ui_manager_
.reset();
130 profile_manager_
.reset(profile_manager
);
134 PrefService
* TestingBrowserProcess::local_state() {
138 chrome_variations::VariationsService
*
139 TestingBrowserProcess::variations_service() {
143 policy::BrowserPolicyConnector
*
144 TestingBrowserProcess::browser_policy_connector() {
145 #if defined(ENABLE_CONFIGURATION_POLICY)
146 if (!browser_policy_connector_
)
147 browser_policy_connector_
= platform_part_
->CreateBrowserPolicyConnector();
148 return browser_policy_connector_
.get();
154 policy::PolicyService
* TestingBrowserProcess::policy_service() {
158 #elif defined(ENABLE_CONFIGURATION_POLICY)
159 return browser_policy_connector()->GetPolicyService();
161 if (!policy_service_
)
162 policy_service_
.reset(new policy::PolicyServiceStub());
163 return policy_service_
.get();
167 IconManager
* TestingBrowserProcess::icon_manager() {
171 GLStringManager
* TestingBrowserProcess::gl_string_manager() {
175 GpuModeManager
* TestingBrowserProcess::gpu_mode_manager() {
179 RenderWidgetSnapshotTaker
*
180 TestingBrowserProcess::GetRenderWidgetSnapshotTaker() {
185 return render_widget_snapshot_taker_
.get();
189 BackgroundModeManager
* TestingBrowserProcess::background_mode_manager() {
193 void TestingBrowserProcess::set_background_mode_manager_for_test(
194 scoped_ptr
<BackgroundModeManager
> manager
) {
198 StatusTray
* TestingBrowserProcess::status_tray() {
202 SafeBrowsingService
* TestingBrowserProcess::safe_browsing_service() {
207 return sb_service_
.get();
211 safe_browsing::ClientSideDetectionService
*
212 TestingBrowserProcess::safe_browsing_detection_service() {
216 net::URLRequestContextGetter
* TestingBrowserProcess::system_request_context() {
217 return system_request_context_
;
220 BrowserProcessPlatformPart
* TestingBrowserProcess::platform_part() {
221 return platform_part_
.get();
224 extensions::EventRouterForwarder
*
225 TestingBrowserProcess::extension_event_router_forwarder() {
229 NotificationUIManager
* TestingBrowserProcess::notification_ui_manager() {
230 #if defined(ENABLE_NOTIFICATIONS)
231 if (!notification_ui_manager_
.get())
232 notification_ui_manager_
.reset(
233 NotificationUIManager::Create(local_state()));
234 return notification_ui_manager_
.get();
241 message_center::MessageCenter
* TestingBrowserProcess::message_center() {
242 return message_center::MessageCenter::Get();
245 IntranetRedirectDetector
* TestingBrowserProcess::intranet_redirect_detector() {
249 AutomationProviderList
* TestingBrowserProcess::GetAutomationProviderList() {
253 void TestingBrowserProcess::CreateDevToolsHttpProtocolHandler(
254 chrome::HostDesktopType host_desktop_type
,
255 const std::string
& ip
,
257 const std::string
& frontend_url
) {
260 unsigned int TestingBrowserProcess::AddRefModule() {
261 return ++module_ref_count_
;
264 unsigned int TestingBrowserProcess::ReleaseModule() {
265 DCHECK_GT(module_ref_count_
, 0U);
266 return --module_ref_count_
;
269 bool TestingBrowserProcess::IsShuttingDown() {
273 printing::PrintJobManager
* TestingBrowserProcess::print_job_manager() {
274 #if defined(ENABLE_FULL_PRINTING)
275 if (!print_job_manager_
.get())
276 print_job_manager_
.reset(new printing::PrintJobManager());
277 return print_job_manager_
.get();
284 printing::PrintPreviewDialogController
*
285 TestingBrowserProcess::print_preview_dialog_controller() {
286 #if defined(ENABLE_FULL_PRINTING)
287 if (!print_preview_dialog_controller_
.get())
288 print_preview_dialog_controller_
=
289 new printing::PrintPreviewDialogController();
290 return print_preview_dialog_controller_
.get();
297 printing::BackgroundPrintingManager
*
298 TestingBrowserProcess::background_printing_manager() {
299 #if defined(ENABLE_FULL_PRINTING)
300 if (!background_printing_manager_
.get()) {
301 background_printing_manager_
.reset(
302 new printing::BackgroundPrintingManager());
304 return background_printing_manager_
.get();
311 const std::string
& TestingBrowserProcess::GetApplicationLocale() {
315 void TestingBrowserProcess::SetApplicationLocale(
316 const std::string
& app_locale
) {
317 app_locale_
= app_locale
;
320 DownloadStatusUpdater
* TestingBrowserProcess::download_status_updater() {
324 DownloadRequestLimiter
* TestingBrowserProcess::download_request_limiter() {
328 ChromeNetLog
* TestingBrowserProcess::net_log() {
332 prerender::PrerenderTracker
* TestingBrowserProcess::prerender_tracker() {
337 if (!prerender_tracker_
.get())
338 prerender_tracker_
.reset(new prerender::PrerenderTracker());
339 return prerender_tracker_
.get();
343 component_updater::ComponentUpdateService
*
344 TestingBrowserProcess::component_updater() {
348 CRLSetFetcher
* TestingBrowserProcess::crl_set_fetcher() {
352 component_updater::PnaclComponentInstaller
*
353 TestingBrowserProcess::pnacl_component_installer() {
357 BookmarkPromptController
* TestingBrowserProcess::bookmark_prompt_controller() {
362 return bookmark_prompt_controller_
.get();
366 StorageMonitor
* TestingBrowserProcess::storage_monitor() {
367 #if defined(OS_IOS) || defined(OS_ANDROID)
371 return storage_monitor_
.get();
375 MediaFileSystemRegistry
* TestingBrowserProcess::media_file_system_registry() {
376 #if defined(OS_IOS) || defined(OS_ANDROID)
380 if (!media_file_system_registry_
)
381 media_file_system_registry_
.reset(new MediaFileSystemRegistry());
382 return media_file_system_registry_
.get();
386 bool TestingBrowserProcess::created_local_state() const {
387 return (local_state_
!= NULL
);
390 #if defined(ENABLE_WEBRTC)
391 WebRtcLogUploader
* TestingBrowserProcess::webrtc_log_uploader() {
396 void TestingBrowserProcess::SetBookmarkPromptController(
397 BookmarkPromptController
* controller
) {
399 bookmark_prompt_controller_
.reset(controller
);
403 void TestingBrowserProcess::SetSystemRequestContext(
404 net::URLRequestContextGetter
* context_getter
) {
405 system_request_context_
= context_getter
;
408 void TestingBrowserProcess::SetLocalState(PrefService
* local_state
) {
410 // The local_state_ PrefService is owned outside of TestingBrowserProcess,
411 // but some of the members of TestingBrowserProcess hold references to it
412 // (for example, via PrefNotifier members). But given our test
413 // infrastructure which tears down individual tests before freeing the
414 // TestingBrowserProcess, there's not a good way to make local_state outlive
415 // these dependencies. As a workaround, whenever local_state_ is cleared
416 // (assumedly as part of exiting the test and freeing TestingBrowserProcess)
417 // any components owned by TestingBrowserProcess that depend on local_state
420 notification_ui_manager_
.reset();
422 #if defined(ENABLE_CONFIGURATION_POLICY)
423 SetBrowserPolicyConnector(NULL
);
426 local_state_
= local_state
;
429 void TestingBrowserProcess::SetIOThread(IOThread
* io_thread
) {
430 io_thread_
= io_thread
;
433 void TestingBrowserProcess::SetBrowserPolicyConnector(
434 policy::BrowserPolicyConnector
* connector
) {
435 #if defined(ENABLE_CONFIGURATION_POLICY)
436 if (browser_policy_connector_
) {
437 browser_policy_connector_
->Shutdown();
439 browser_policy_connector_
.reset(connector
);
445 void TestingBrowserProcess::SetSafeBrowsingService(
446 SafeBrowsingService
* sb_service
) {
449 sb_service_
= sb_service
;
453 void TestingBrowserProcess::SetStorageMonitor(
454 scoped_ptr
<StorageMonitor
> storage_monitor
) {
455 #if !defined(OS_IOS) && !defined(OS_ANDROID)
456 storage_monitor_
= storage_monitor
.Pass();
460 ///////////////////////////////////////////////////////////////////////////////
462 TestingBrowserProcessInitializer::TestingBrowserProcessInitializer() {
463 TestingBrowserProcess::CreateInstance();
466 TestingBrowserProcessInitializer::~TestingBrowserProcessInitializer() {
467 TestingBrowserProcess::DeleteInstance();