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.
4
5 #include "chrome/test/base/testing_browser_process.h"
6
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/apps/chrome_apps_client.h"
12 #include "chrome/browser/background/background_mode_manager.h"
13 #include "chrome/browser/browser_process.h"
14 #include "chrome/browser/browser_process_impl.h"
15 #include "chrome/browser/extensions/chrome_extensions_browser_client.h"
16 #include "chrome/browser/printing/print_job_manager.h"
17 #include "chrome/browser/profiles/profile_manager.h"
18 #include "chrome/test/base/testing_browser_process_platform_part.h"
19 #include "components/network_time/network_time_tracker.h"
20 #include "content/public/browser/notification_service.h"
21 #include "net/url_request/url_request_context_getter.h"
22 #include "testing/gtest/include/gtest/gtest.h"
23 #include "ui/message_center/message_center.h"
24
25 #if !defined(OS_IOS)
26 #include "chrome/browser/notifications/notification_ui_manager.h"
27 #include "chrome/browser/prerender/prerender_tracker.h"
28 #include "chrome/browser/safe_browsing/safe_browsing_service.h"
29 #endif
30
31 #if !defined(OS_IOS) && !defined(OS_ANDROID)
32 #include "chrome/browser/media_galleries/media_file_system_registry.h"
33 #include "components/storage_monitor/storage_monitor.h"
34 #include "components/storage_monitor/test_storage_monitor.h"
35 #endif
36
37 #if defined(ENABLE_CONFIGURATION_POLICY)
38 #include "components/policy/core/browser/browser_policy_connector.h"
39 #else
40 #include "components/policy/core/common/policy_service_stub.h"
41 #endif // defined(ENABLE_CONFIGURATION_POLICY)
42
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"
46 #endif
47
48 // static
GetGlobal()49 TestingBrowserProcess* TestingBrowserProcess::GetGlobal() {
50 return static_cast<TestingBrowserProcess*>(g_browser_process);
51 }
52
53 // static
CreateInstance()54 void TestingBrowserProcess::CreateInstance() {
55 DCHECK(!g_browser_process);
56 g_browser_process = new TestingBrowserProcess;
57 }
58
59 // static
DeleteInstance()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;
65 }
66
TestingBrowserProcess()67 TestingBrowserProcess::TestingBrowserProcess()
68 : notification_service_(content::NotificationService::Create()),
69 module_ref_count_(0),
70 app_locale_("en"),
71 local_state_(NULL),
72 io_thread_(NULL),
73 system_request_context_(NULL),
74 platform_part_(new TestingBrowserProcessPlatformPart()),
75 extensions_browser_client_(
76 new extensions::ChromeExtensionsBrowserClient) {
77 #if defined(ENABLE_EXTENSIONS)
78 apps::AppsClient::Set(ChromeAppsClient::GetInstance());
79 #endif
80 extensions::ExtensionsBrowserClient::Set(extensions_browser_client_.get());
81 }
82
~TestingBrowserProcess()83 TestingBrowserProcess::~TestingBrowserProcess() {
84 EXPECT_FALSE(local_state_);
85 #if defined(ENABLE_CONFIGURATION_POLICY)
86 SetBrowserPolicyConnector(NULL);
87 #endif
88 extensions::ExtensionsBrowserClient::Set(NULL);
89
90 // Destructors for some objects owned by TestingBrowserProcess will use
91 // g_browser_process if it is not NULL, so it must be NULL before proceeding.
92 DCHECK_EQ(static_cast<BrowserProcess*>(NULL), g_browser_process);
93 }
94
ResourceDispatcherHostCreated()95 void TestingBrowserProcess::ResourceDispatcherHostCreated() {
96 }
97
EndSession()98 void TestingBrowserProcess::EndSession() {
99 }
100
GetMetricsServicesManager()101 MetricsServicesManager* TestingBrowserProcess::GetMetricsServicesManager() {
102 return NULL;
103 }
104
metrics_service()105 MetricsService* TestingBrowserProcess::metrics_service() {
106 return NULL;
107 }
108
rappor_service()109 rappor::RapporService* TestingBrowserProcess::rappor_service() {
110 return NULL;
111 }
112
io_thread()113 IOThread* TestingBrowserProcess::io_thread() {
114 return io_thread_;
115 }
116
watchdog_thread()117 WatchDogThread* TestingBrowserProcess::watchdog_thread() {
118 return NULL;
119 }
120
profile_manager()121 ProfileManager* TestingBrowserProcess::profile_manager() {
122 #if defined(OS_IOS)
123 NOTIMPLEMENTED();
124 return NULL;
125 #else
126 return profile_manager_.get();
127 #endif
128 }
129
SetProfileManager(ProfileManager * profile_manager)130 void TestingBrowserProcess::SetProfileManager(ProfileManager* profile_manager) {
131 #if !defined(OS_IOS)
132 // NotificationUIManager can contain references to elements in the current
133 // ProfileManager (for example, the MessageCenterSettingsController maintains
134 // a pointer to the ProfileInfoCache). So when we change the ProfileManager
135 // (typically during test shutdown) make sure to reset any objects that might
136 // maintain references to it. See SetLocalState() for a description of a
137 // similar situation.
138 notification_ui_manager_.reset();
139 profile_manager_.reset(profile_manager);
140 #endif
141 }
142
local_state()143 PrefService* TestingBrowserProcess::local_state() {
144 return local_state_;
145 }
146
147 chrome_variations::VariationsService*
variations_service()148 TestingBrowserProcess::variations_service() {
149 return NULL;
150 }
151
152 policy::BrowserPolicyConnector*
browser_policy_connector()153 TestingBrowserProcess::browser_policy_connector() {
154 #if defined(ENABLE_CONFIGURATION_POLICY)
155 if (!browser_policy_connector_)
156 browser_policy_connector_ = platform_part_->CreateBrowserPolicyConnector();
157 return browser_policy_connector_.get();
158 #else
159 return NULL;
160 #endif
161 }
162
policy_service()163 policy::PolicyService* TestingBrowserProcess::policy_service() {
164 #if defined(OS_IOS)
165 NOTIMPLEMENTED();
166 return NULL;
167 #elif defined(ENABLE_CONFIGURATION_POLICY)
168 return browser_policy_connector()->GetPolicyService();
169 #else
170 if (!policy_service_)
171 policy_service_.reset(new policy::PolicyServiceStub());
172 return policy_service_.get();
173 #endif
174 }
175
icon_manager()176 IconManager* TestingBrowserProcess::icon_manager() {
177 return NULL;
178 }
179
gl_string_manager()180 GLStringManager* TestingBrowserProcess::gl_string_manager() {
181 return NULL;
182 }
183
gpu_mode_manager()184 GpuModeManager* TestingBrowserProcess::gpu_mode_manager() {
185 return NULL;
186 }
187
background_mode_manager()188 BackgroundModeManager* TestingBrowserProcess::background_mode_manager() {
189 return NULL;
190 }
191
set_background_mode_manager_for_test(scoped_ptr<BackgroundModeManager> manager)192 void TestingBrowserProcess::set_background_mode_manager_for_test(
193 scoped_ptr<BackgroundModeManager> manager) {
194 NOTREACHED();
195 }
196
status_tray()197 StatusTray* TestingBrowserProcess::status_tray() {
198 return NULL;
199 }
200
safe_browsing_service()201 SafeBrowsingService* TestingBrowserProcess::safe_browsing_service() {
202 #if defined(OS_IOS)
203 NOTIMPLEMENTED();
204 return NULL;
205 #else
206 return sb_service_.get();
207 #endif
208 }
209
210 safe_browsing::ClientSideDetectionService*
safe_browsing_detection_service()211 TestingBrowserProcess::safe_browsing_detection_service() {
212 return NULL;
213 }
214
system_request_context()215 net::URLRequestContextGetter* TestingBrowserProcess::system_request_context() {
216 return system_request_context_;
217 }
218
platform_part()219 BrowserProcessPlatformPart* TestingBrowserProcess::platform_part() {
220 return platform_part_.get();
221 }
222
223 extensions::EventRouterForwarder*
extension_event_router_forwarder()224 TestingBrowserProcess::extension_event_router_forwarder() {
225 return NULL;
226 }
227
notification_ui_manager()228 NotificationUIManager* TestingBrowserProcess::notification_ui_manager() {
229 #if defined(ENABLE_NOTIFICATIONS)
230 if (!notification_ui_manager_.get())
231 notification_ui_manager_.reset(
232 NotificationUIManager::Create(local_state()));
233 return notification_ui_manager_.get();
234 #else
235 NOTIMPLEMENTED();
236 return NULL;
237 #endif
238 }
239
message_center()240 message_center::MessageCenter* TestingBrowserProcess::message_center() {
241 return message_center::MessageCenter::Get();
242 }
243
intranet_redirect_detector()244 IntranetRedirectDetector* TestingBrowserProcess::intranet_redirect_detector() {
245 return NULL;
246 }
CreateDevToolsHttpProtocolHandler(chrome::HostDesktopType host_desktop_type,const std::string & ip,int port)247 void TestingBrowserProcess::CreateDevToolsHttpProtocolHandler(
248 chrome::HostDesktopType host_desktop_type,
249 const std::string& ip,
250 int port) {
251 }
252
AddRefModule()253 unsigned int TestingBrowserProcess::AddRefModule() {
254 return ++module_ref_count_;
255 }
256
ReleaseModule()257 unsigned int TestingBrowserProcess::ReleaseModule() {
258 DCHECK_GT(module_ref_count_, 0U);
259 return --module_ref_count_;
260 }
261
IsShuttingDown()262 bool TestingBrowserProcess::IsShuttingDown() {
263 return false;
264 }
265
print_job_manager()266 printing::PrintJobManager* TestingBrowserProcess::print_job_manager() {
267 #if defined(ENABLE_FULL_PRINTING)
268 if (!print_job_manager_.get())
269 print_job_manager_.reset(new printing::PrintJobManager());
270 return print_job_manager_.get();
271 #else
272 NOTIMPLEMENTED();
273 return NULL;
274 #endif
275 }
276
277 printing::PrintPreviewDialogController*
print_preview_dialog_controller()278 TestingBrowserProcess::print_preview_dialog_controller() {
279 #if defined(ENABLE_FULL_PRINTING)
280 if (!print_preview_dialog_controller_.get())
281 print_preview_dialog_controller_ =
282 new printing::PrintPreviewDialogController();
283 return print_preview_dialog_controller_.get();
284 #else
285 NOTIMPLEMENTED();
286 return NULL;
287 #endif
288 }
289
290 printing::BackgroundPrintingManager*
background_printing_manager()291 TestingBrowserProcess::background_printing_manager() {
292 #if defined(ENABLE_FULL_PRINTING)
293 if (!background_printing_manager_.get()) {
294 background_printing_manager_.reset(
295 new printing::BackgroundPrintingManager());
296 }
297 return background_printing_manager_.get();
298 #else
299 NOTIMPLEMENTED();
300 return NULL;
301 #endif
302 }
303
GetApplicationLocale()304 const std::string& TestingBrowserProcess::GetApplicationLocale() {
305 return app_locale_;
306 }
307
SetApplicationLocale(const std::string & app_locale)308 void TestingBrowserProcess::SetApplicationLocale(
309 const std::string& app_locale) {
310 app_locale_ = app_locale;
311 }
312
download_status_updater()313 DownloadStatusUpdater* TestingBrowserProcess::download_status_updater() {
314 return NULL;
315 }
316
download_request_limiter()317 DownloadRequestLimiter* TestingBrowserProcess::download_request_limiter() {
318 return NULL;
319 }
320
net_log()321 ChromeNetLog* TestingBrowserProcess::net_log() {
322 return NULL;
323 }
324
prerender_tracker()325 prerender::PrerenderTracker* TestingBrowserProcess::prerender_tracker() {
326 #if defined(OS_IOS)
327 NOTIMPLEMENTED();
328 return NULL;
329 #else
330 if (!prerender_tracker_.get())
331 prerender_tracker_.reset(new prerender::PrerenderTracker());
332 return prerender_tracker_.get();
333 #endif
334 }
335
336 component_updater::ComponentUpdateService*
component_updater()337 TestingBrowserProcess::component_updater() {
338 return NULL;
339 }
340
crl_set_fetcher()341 CRLSetFetcher* TestingBrowserProcess::crl_set_fetcher() {
342 return NULL;
343 }
344
345 component_updater::PnaclComponentInstaller*
pnacl_component_installer()346 TestingBrowserProcess::pnacl_component_installer() {
347 return NULL;
348 }
349
media_file_system_registry()350 MediaFileSystemRegistry* TestingBrowserProcess::media_file_system_registry() {
351 #if defined(OS_IOS) || defined(OS_ANDROID)
352 NOTIMPLEMENTED();
353 return NULL;
354 #else
355 if (!media_file_system_registry_)
356 media_file_system_registry_.reset(new MediaFileSystemRegistry());
357 return media_file_system_registry_.get();
358 #endif
359 }
360
created_local_state() const361 bool TestingBrowserProcess::created_local_state() const {
362 return (local_state_ != NULL);
363 }
364
365 #if defined(ENABLE_WEBRTC)
webrtc_log_uploader()366 WebRtcLogUploader* TestingBrowserProcess::webrtc_log_uploader() {
367 return NULL;
368 }
369 #endif
370
371 network_time::NetworkTimeTracker*
network_time_tracker()372 TestingBrowserProcess::network_time_tracker() {
373 if (!network_time_tracker_) {
374 DCHECK(local_state_);
375 network_time_tracker_.reset(new network_time::NetworkTimeTracker(
376 scoped_ptr<base::TickClock>(new base::DefaultTickClock()),
377 local_state_));
378 }
379 return network_time_tracker_.get();
380 }
381
gcm_driver()382 gcm::GCMDriver* TestingBrowserProcess::gcm_driver() {
383 return NULL;
384 }
385
SetSystemRequestContext(net::URLRequestContextGetter * context_getter)386 void TestingBrowserProcess::SetSystemRequestContext(
387 net::URLRequestContextGetter* context_getter) {
388 system_request_context_ = context_getter;
389 }
390
SetLocalState(PrefService * local_state)391 void TestingBrowserProcess::SetLocalState(PrefService* local_state) {
392 if (!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
401 // are also freed.
402 network_time_tracker_.reset();
403 #if !defined(OS_IOS)
404 notification_ui_manager_.reset();
405 #endif
406 #if defined(ENABLE_CONFIGURATION_POLICY)
407 SetBrowserPolicyConnector(NULL);
408 #endif
409 }
410 local_state_ = local_state;
411 }
412
SetIOThread(IOThread * io_thread)413 void TestingBrowserProcess::SetIOThread(IOThread* io_thread) {
414 io_thread_ = io_thread;
415 }
416
SetBrowserPolicyConnector(policy::BrowserPolicyConnector * connector)417 void TestingBrowserProcess::SetBrowserPolicyConnector(
418 policy::BrowserPolicyConnector* connector) {
419 #if defined(ENABLE_CONFIGURATION_POLICY)
420 if (browser_policy_connector_) {
421 browser_policy_connector_->Shutdown();
422 }
423 browser_policy_connector_.reset(connector);
424 #else
425 CHECK(false);
426 #endif
427 }
428
SetSafeBrowsingService(SafeBrowsingService * sb_service)429 void TestingBrowserProcess::SetSafeBrowsingService(
430 SafeBrowsingService* sb_service) {
431 #if !defined(OS_IOS)
432 NOTIMPLEMENTED();
433 sb_service_ = sb_service;
434 #endif
435 }
436
437 ///////////////////////////////////////////////////////////////////////////////
438
TestingBrowserProcessInitializer()439 TestingBrowserProcessInitializer::TestingBrowserProcessInitializer() {
440 TestingBrowserProcess::CreateInstance();
441 }
442
~TestingBrowserProcessInitializer()443 TestingBrowserProcessInitializer::~TestingBrowserProcessInitializer() {
444 TestingBrowserProcess::DeleteInstance();
445 }
446