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/printing/print_job_manager.h"
14 #include "chrome/browser/profiles/profile_manager.h"
15 #include "chrome/browser/ui/bookmarks/bookmark_prompt_controller.h"
16 #include "chrome/test/base/testing_browser_process_platform_part.h"
17 #include "content/public/browser/notification_service.h"
18 #include "net/url_request/url_request_context_getter.h"
19 #include "testing/gtest/include/gtest/gtest.h"
20 #include "ui/message_center/message_center.h"
23 #include "chrome/browser/notifications/notification_ui_manager.h"
24 #include "chrome/browser/prerender/prerender_tracker.h"
25 #include "chrome/browser/safe_browsing/safe_browsing_service.h"
26 #include "chrome/browser/thumbnails/render_widget_snapshot_taker.h"
29 #if !defined(OS_IOS) && !defined(OS_ANDROID)
30 #include "chrome/browser/media_galleries/media_file_system_registry.h"
31 #include "chrome/browser/storage_monitor/storage_monitor.h"
32 #include "chrome/browser/storage_monitor/test_storage_monitor.h"
35 #if defined(ENABLE_CONFIGURATION_POLICY)
36 #include "chrome/browser/policy/browser_policy_connector.h"
38 #include "chrome/browser/policy/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);
51 TestingBrowserProcess::TestingBrowserProcess()
52 : notification_service_(content::NotificationService::Create()),
56 render_widget_snapshot_taker_(new RenderWidgetSnapshotTaker),
60 system_request_context_(NULL),
61 platform_part_(new TestingBrowserProcessPlatformPart()) {
64 TestingBrowserProcess::~TestingBrowserProcess() {
65 EXPECT_FALSE(local_state_);
66 #if defined(ENABLE_CONFIGURATION_POLICY)
67 SetBrowserPolicyConnector(NULL);
70 // Destructors for some objects owned by TestingBrowserProcess will use
71 // g_browser_process if it is not NULL, so it must be NULL before proceeding.
72 DCHECK_EQ(static_cast<BrowserProcess*>(NULL), g_browser_process);
75 void TestingBrowserProcess::ResourceDispatcherHostCreated() {
78 void TestingBrowserProcess::EndSession() {
81 MetricsService* TestingBrowserProcess::metrics_service() {
85 IOThread* TestingBrowserProcess::io_thread() {
89 WatchDogThread* TestingBrowserProcess::watchdog_thread() {
93 ProfileManager* TestingBrowserProcess::profile_manager() {
98 return profile_manager_.get();
102 void TestingBrowserProcess::SetProfileManager(ProfileManager* profile_manager) {
104 profile_manager_.reset(profile_manager);
108 PrefService* TestingBrowserProcess::local_state() {
112 chrome_variations::VariationsService*
113 TestingBrowserProcess::variations_service() {
117 policy::BrowserPolicyConnector*
118 TestingBrowserProcess::browser_policy_connector() {
119 #if defined(ENABLE_CONFIGURATION_POLICY)
120 if (!browser_policy_connector_)
121 browser_policy_connector_.reset(new policy::BrowserPolicyConnector());
122 return browser_policy_connector_.get();
128 policy::PolicyService* TestingBrowserProcess::policy_service() {
132 #elif defined(ENABLE_CONFIGURATION_POLICY)
133 return browser_policy_connector()->GetPolicyService();
135 if (!policy_service_)
136 policy_service_.reset(new policy::PolicyServiceStub());
137 return policy_service_.get();
141 IconManager* TestingBrowserProcess::icon_manager() {
145 GLStringManager* TestingBrowserProcess::gl_string_manager() {
149 GpuModeManager* TestingBrowserProcess::gpu_mode_manager() {
153 RenderWidgetSnapshotTaker*
154 TestingBrowserProcess::GetRenderWidgetSnapshotTaker() {
159 return render_widget_snapshot_taker_.get();
163 BackgroundModeManager* TestingBrowserProcess::background_mode_manager() {
167 void TestingBrowserProcess::set_background_mode_manager_for_test(
168 scoped_ptr<BackgroundModeManager> manager) {
172 StatusTray* TestingBrowserProcess::status_tray() {
176 SafeBrowsingService* TestingBrowserProcess::safe_browsing_service() {
181 return sb_service_.get();
185 safe_browsing::ClientSideDetectionService*
186 TestingBrowserProcess::safe_browsing_detection_service() {
190 net::URLRequestContextGetter* TestingBrowserProcess::system_request_context() {
191 return system_request_context_;
194 BrowserProcessPlatformPart* TestingBrowserProcess::platform_part() {
195 return platform_part_.get();
198 extensions::EventRouterForwarder*
199 TestingBrowserProcess::extension_event_router_forwarder() {
203 NotificationUIManager* TestingBrowserProcess::notification_ui_manager() {
204 #if defined(ENABLE_NOTIFICATIONS)
205 if (!notification_ui_manager_.get())
206 notification_ui_manager_.reset(
207 NotificationUIManager::Create(local_state()));
208 return notification_ui_manager_.get();
215 message_center::MessageCenter* TestingBrowserProcess::message_center() {
216 return message_center::MessageCenter::Get();
219 IntranetRedirectDetector* TestingBrowserProcess::intranet_redirect_detector() {
223 AutomationProviderList* TestingBrowserProcess::GetAutomationProviderList() {
227 void TestingBrowserProcess::CreateDevToolsHttpProtocolHandler(
228 chrome::HostDesktopType host_desktop_type,
229 const std::string& ip,
231 const std::string& frontend_url) {
234 unsigned int TestingBrowserProcess::AddRefModule() {
235 return ++module_ref_count_;
238 unsigned int TestingBrowserProcess::ReleaseModule() {
239 DCHECK_GT(module_ref_count_, 0U);
240 return --module_ref_count_;
243 bool TestingBrowserProcess::IsShuttingDown() {
247 printing::PrintJobManager* TestingBrowserProcess::print_job_manager() {
248 #if defined(ENABLE_FULL_PRINTING)
249 if (!print_job_manager_.get())
250 print_job_manager_.reset(new printing::PrintJobManager());
251 return print_job_manager_.get();
258 printing::PrintPreviewDialogController*
259 TestingBrowserProcess::print_preview_dialog_controller() {
260 #if defined(ENABLE_FULL_PRINTING)
261 if (!print_preview_dialog_controller_.get())
262 print_preview_dialog_controller_ =
263 new printing::PrintPreviewDialogController();
264 return print_preview_dialog_controller_.get();
271 printing::BackgroundPrintingManager*
272 TestingBrowserProcess::background_printing_manager() {
273 #if defined(ENABLE_FULL_PRINTING)
274 if (!background_printing_manager_.get()) {
275 background_printing_manager_.reset(
276 new printing::BackgroundPrintingManager());
278 return background_printing_manager_.get();
285 const std::string& TestingBrowserProcess::GetApplicationLocale() {
289 void TestingBrowserProcess::SetApplicationLocale(
290 const std::string& app_locale) {
291 app_locale_ = app_locale;
294 DownloadStatusUpdater* TestingBrowserProcess::download_status_updater() {
298 DownloadRequestLimiter* TestingBrowserProcess::download_request_limiter() {
302 ChromeNetLog* TestingBrowserProcess::net_log() {
306 prerender::PrerenderTracker* TestingBrowserProcess::prerender_tracker() {
311 if (!prerender_tracker_.get())
312 prerender_tracker_.reset(new prerender::PrerenderTracker());
313 return prerender_tracker_.get();
317 ComponentUpdateService* TestingBrowserProcess::component_updater() {
321 CRLSetFetcher* TestingBrowserProcess::crl_set_fetcher() {
325 PnaclComponentInstaller* TestingBrowserProcess::pnacl_component_installer() {
329 BookmarkPromptController* TestingBrowserProcess::bookmark_prompt_controller() {
334 return bookmark_prompt_controller_.get();
338 StorageMonitor* TestingBrowserProcess::storage_monitor() {
339 #if defined(OS_IOS) || defined(OS_ANDROID)
343 return storage_monitor_.get();
347 MediaFileSystemRegistry* TestingBrowserProcess::media_file_system_registry() {
348 #if defined(OS_IOS) || defined(OS_ANDROID)
352 if (!media_file_system_registry_)
353 media_file_system_registry_.reset(new MediaFileSystemRegistry());
354 return media_file_system_registry_.get();
358 bool TestingBrowserProcess::created_local_state() const {
359 return (local_state_ != NULL);
362 #if defined(ENABLE_WEBRTC)
363 WebRtcLogUploader* TestingBrowserProcess::webrtc_log_uploader() {
368 void TestingBrowserProcess::SetBookmarkPromptController(
369 BookmarkPromptController* controller) {
371 bookmark_prompt_controller_.reset(controller);
375 void TestingBrowserProcess::SetSystemRequestContext(
376 net::URLRequestContextGetter* context_getter) {
377 system_request_context_ = context_getter;
380 void TestingBrowserProcess::SetLocalState(PrefService* local_state) {
382 // The local_state_ PrefService is owned outside of TestingBrowserProcess,
383 // but some of the members of TestingBrowserProcess hold references to it
384 // (for example, via PrefNotifier members). But given our test
385 // infrastructure which tears down individual tests before freeing the
386 // TestingBrowserProcess, there's not a good way to make local_state outlive
387 // these dependencies. As a workaround, whenever local_state_ is cleared
388 // (assumedly as part of exiting the test and freeing TestingBrowserProcess)
389 // any components owned by TestingBrowserProcess that depend on local_state
392 notification_ui_manager_.reset();
394 #if defined(ENABLE_CONFIGURATION_POLICY)
395 SetBrowserPolicyConnector(NULL);
398 local_state_ = local_state;
401 void TestingBrowserProcess::SetIOThread(IOThread* io_thread) {
402 io_thread_ = io_thread;
405 void TestingBrowserProcess::SetBrowserPolicyConnector(
406 policy::BrowserPolicyConnector* connector) {
407 #if defined(ENABLE_CONFIGURATION_POLICY)
408 if (browser_policy_connector_) {
409 browser_policy_connector_->Shutdown();
411 browser_policy_connector_.reset(connector);
417 void TestingBrowserProcess::SetSafeBrowsingService(
418 SafeBrowsingService* sb_service) {
421 sb_service_ = sb_service;
425 void TestingBrowserProcess::SetStorageMonitor(
426 scoped_ptr<StorageMonitor> storage_monitor) {
427 #if !defined(OS_IOS) && !defined(OS_ANDROID)
428 storage_monitor_ = storage_monitor.Pass();