Cast: Stop logging kVideoFrameSentToEncoder and rename a couple events.
[chromium-blink-merge.git] / chrome / test / base / testing_browser_process.cc
blob390d9ef21ea44de4fef69d572f899fadd9fd1908
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"
23 #if !defined(OS_IOS)
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 #endif
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"
33 #endif
35 #if defined(ENABLE_CONFIGURATION_POLICY)
36 #include "components/policy/core/browser/browser_policy_connector.h"
37 #else
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"
44 #endif
46 // static
47 TestingBrowserProcess* TestingBrowserProcess::GetGlobal() {
48 return static_cast<TestingBrowserProcess*>(g_browser_process);
51 // static
52 void TestingBrowserProcess::CreateInstance() {
53 DCHECK(!g_browser_process);
54 g_browser_process = new TestingBrowserProcess;
57 // static
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()),
67 module_ref_count_(0),
68 app_locale_("en"),
69 local_state_(NULL),
70 io_thread_(NULL),
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);
83 #endif
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() {
98 return NULL;
101 rappor::RapporService* TestingBrowserProcess::rappor_service() {
102 return NULL;
105 IOThread* TestingBrowserProcess::io_thread() {
106 return io_thread_;
109 WatchDogThread* TestingBrowserProcess::watchdog_thread() {
110 return NULL;
113 ProfileManager* TestingBrowserProcess::profile_manager() {
114 #if defined(OS_IOS)
115 NOTIMPLEMENTED();
116 return NULL;
117 #else
118 return profile_manager_.get();
119 #endif
122 void TestingBrowserProcess::SetProfileManager(ProfileManager* profile_manager) {
123 #if !defined(OS_IOS)
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);
132 #endif
135 PrefService* TestingBrowserProcess::local_state() {
136 return local_state_;
139 chrome_variations::VariationsService*
140 TestingBrowserProcess::variations_service() {
141 return NULL;
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();
150 #else
151 return NULL;
152 #endif
155 policy::PolicyService* TestingBrowserProcess::policy_service() {
156 #if defined(OS_IOS)
157 NOTIMPLEMENTED();
158 return NULL;
159 #elif defined(ENABLE_CONFIGURATION_POLICY)
160 return browser_policy_connector()->GetPolicyService();
161 #else
162 if (!policy_service_)
163 policy_service_.reset(new policy::PolicyServiceStub());
164 return policy_service_.get();
165 #endif
168 IconManager* TestingBrowserProcess::icon_manager() {
169 return NULL;
172 GLStringManager* TestingBrowserProcess::gl_string_manager() {
173 return NULL;
176 GpuModeManager* TestingBrowserProcess::gpu_mode_manager() {
177 return NULL;
180 BackgroundModeManager* TestingBrowserProcess::background_mode_manager() {
181 return NULL;
184 void TestingBrowserProcess::set_background_mode_manager_for_test(
185 scoped_ptr<BackgroundModeManager> manager) {
186 NOTREACHED();
189 StatusTray* TestingBrowserProcess::status_tray() {
190 return NULL;
193 SafeBrowsingService* TestingBrowserProcess::safe_browsing_service() {
194 #if defined(OS_IOS)
195 NOTIMPLEMENTED();
196 return NULL;
197 #else
198 return sb_service_.get();
199 #endif
202 safe_browsing::ClientSideDetectionService*
203 TestingBrowserProcess::safe_browsing_detection_service() {
204 return NULL;
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() {
217 return NULL;
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();
226 #else
227 NOTIMPLEMENTED();
228 return NULL;
229 #endif
232 message_center::MessageCenter* TestingBrowserProcess::message_center() {
233 return message_center::MessageCenter::Get();
236 IntranetRedirectDetector* TestingBrowserProcess::intranet_redirect_detector() {
237 return NULL;
239 void TestingBrowserProcess::CreateDevToolsHttpProtocolHandler(
240 chrome::HostDesktopType host_desktop_type,
241 const std::string& ip,
242 int port) {
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() {
255 return false;
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();
263 #else
264 NOTIMPLEMENTED();
265 return NULL;
266 #endif
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();
276 #else
277 NOTIMPLEMENTED();
278 return NULL;
279 #endif
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();
290 #else
291 NOTIMPLEMENTED();
292 return NULL;
293 #endif
296 const std::string& TestingBrowserProcess::GetApplicationLocale() {
297 return app_locale_;
300 void TestingBrowserProcess::SetApplicationLocale(
301 const std::string& app_locale) {
302 app_locale_ = app_locale;
305 DownloadStatusUpdater* TestingBrowserProcess::download_status_updater() {
306 return NULL;
309 DownloadRequestLimiter* TestingBrowserProcess::download_request_limiter() {
310 return NULL;
313 ChromeNetLog* TestingBrowserProcess::net_log() {
314 return NULL;
317 prerender::PrerenderTracker* TestingBrowserProcess::prerender_tracker() {
318 #if defined(OS_IOS)
319 NOTIMPLEMENTED();
320 return NULL;
321 #else
322 if (!prerender_tracker_.get())
323 prerender_tracker_.reset(new prerender::PrerenderTracker());
324 return prerender_tracker_.get();
325 #endif
328 component_updater::ComponentUpdateService*
329 TestingBrowserProcess::component_updater() {
330 return NULL;
333 CRLSetFetcher* TestingBrowserProcess::crl_set_fetcher() {
334 return NULL;
337 component_updater::PnaclComponentInstaller*
338 TestingBrowserProcess::pnacl_component_installer() {
339 return NULL;
342 MediaFileSystemRegistry* TestingBrowserProcess::media_file_system_registry() {
343 #if defined(OS_IOS) || defined(OS_ANDROID)
344 NOTIMPLEMENTED();
345 return NULL;
346 #else
347 if (!media_file_system_registry_)
348 media_file_system_registry_.reset(new MediaFileSystemRegistry());
349 return media_file_system_registry_.get();
350 #endif
353 bool TestingBrowserProcess::created_local_state() const {
354 return (local_state_ != NULL);
357 #if defined(ENABLE_WEBRTC)
358 WebRtcLogUploader* TestingBrowserProcess::webrtc_log_uploader() {
359 return NULL;
361 #endif
363 void TestingBrowserProcess::SetSystemRequestContext(
364 net::URLRequestContextGetter* context_getter) {
365 system_request_context_ = context_getter;
368 void TestingBrowserProcess::SetLocalState(PrefService* local_state) {
369 if (!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
378 // are also freed.
379 #if !defined(OS_IOS)
380 notification_ui_manager_.reset();
381 #endif
382 #if defined(ENABLE_CONFIGURATION_POLICY)
383 SetBrowserPolicyConnector(NULL);
384 #endif
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);
400 #else
401 CHECK(false);
402 #endif
405 void TestingBrowserProcess::SetSafeBrowsingService(
406 SafeBrowsingService* sb_service) {
407 #if !defined(OS_IOS)
408 NOTIMPLEMENTED();
409 sb_service_ = sb_service;
410 #endif
413 ///////////////////////////////////////////////////////////////////////////////
415 TestingBrowserProcessInitializer::TestingBrowserProcessInitializer() {
416 TestingBrowserProcess::CreateInstance();
419 TestingBrowserProcessInitializer::~TestingBrowserProcessInitializer() {
420 TestingBrowserProcess::DeleteInstance();