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
49TestingBrowserProcess* TestingBrowserProcess::GetGlobal() {
50  return static_cast<TestingBrowserProcess*>(g_browser_process);
51}
52
53// static
54void TestingBrowserProcess::CreateInstance() {
55  DCHECK(!g_browser_process);
56  g_browser_process = new TestingBrowserProcess;
57}
58
59// static
60void 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
67TestingBrowserProcess::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
83TestingBrowserProcess::~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
95void TestingBrowserProcess::ResourceDispatcherHostCreated() {
96}
97
98void TestingBrowserProcess::EndSession() {
99}
100
101MetricsServicesManager* TestingBrowserProcess::GetMetricsServicesManager() {
102  return NULL;
103}
104
105MetricsService* TestingBrowserProcess::metrics_service() {
106  return NULL;
107}
108
109rappor::RapporService* TestingBrowserProcess::rappor_service() {
110  return NULL;
111}
112
113IOThread* TestingBrowserProcess::io_thread() {
114  return io_thread_;
115}
116
117WatchDogThread* TestingBrowserProcess::watchdog_thread() {
118  return NULL;
119}
120
121ProfileManager* TestingBrowserProcess::profile_manager() {
122#if defined(OS_IOS)
123  NOTIMPLEMENTED();
124  return NULL;
125#else
126  return profile_manager_.get();
127#endif
128}
129
130void 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
143PrefService* TestingBrowserProcess::local_state() {
144  return local_state_;
145}
146
147chrome_variations::VariationsService*
148    TestingBrowserProcess::variations_service() {
149  return NULL;
150}
151
152policy::BrowserPolicyConnector*
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
163policy::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
176IconManager* TestingBrowserProcess::icon_manager() {
177  return NULL;
178}
179
180GLStringManager* TestingBrowserProcess::gl_string_manager() {
181  return NULL;
182}
183
184GpuModeManager* TestingBrowserProcess::gpu_mode_manager() {
185  return NULL;
186}
187
188BackgroundModeManager* TestingBrowserProcess::background_mode_manager() {
189  return NULL;
190}
191
192void TestingBrowserProcess::set_background_mode_manager_for_test(
193    scoped_ptr<BackgroundModeManager> manager) {
194  NOTREACHED();
195}
196
197StatusTray* TestingBrowserProcess::status_tray() {
198  return NULL;
199}
200
201SafeBrowsingService* 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
210safe_browsing::ClientSideDetectionService*
211TestingBrowserProcess::safe_browsing_detection_service() {
212  return NULL;
213}
214
215net::URLRequestContextGetter* TestingBrowserProcess::system_request_context() {
216  return system_request_context_;
217}
218
219BrowserProcessPlatformPart* TestingBrowserProcess::platform_part() {
220  return platform_part_.get();
221}
222
223extensions::EventRouterForwarder*
224TestingBrowserProcess::extension_event_router_forwarder() {
225  return NULL;
226}
227
228NotificationUIManager* 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
240message_center::MessageCenter* TestingBrowserProcess::message_center() {
241  return message_center::MessageCenter::Get();
242}
243
244IntranetRedirectDetector* TestingBrowserProcess::intranet_redirect_detector() {
245  return NULL;
246}
247void TestingBrowserProcess::CreateDevToolsHttpProtocolHandler(
248    chrome::HostDesktopType host_desktop_type,
249    const std::string& ip,
250    int port) {
251}
252
253unsigned int TestingBrowserProcess::AddRefModule() {
254  return ++module_ref_count_;
255}
256
257unsigned int TestingBrowserProcess::ReleaseModule() {
258  DCHECK_GT(module_ref_count_, 0U);
259  return --module_ref_count_;
260}
261
262bool TestingBrowserProcess::IsShuttingDown() {
263  return false;
264}
265
266printing::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
277printing::PrintPreviewDialogController*
278TestingBrowserProcess::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
290printing::BackgroundPrintingManager*
291TestingBrowserProcess::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
304const std::string& TestingBrowserProcess::GetApplicationLocale() {
305  return app_locale_;
306}
307
308void TestingBrowserProcess::SetApplicationLocale(
309    const std::string& app_locale) {
310  app_locale_ = app_locale;
311}
312
313DownloadStatusUpdater* TestingBrowserProcess::download_status_updater() {
314  return NULL;
315}
316
317DownloadRequestLimiter* TestingBrowserProcess::download_request_limiter() {
318  return NULL;
319}
320
321ChromeNetLog* TestingBrowserProcess::net_log() {
322  return NULL;
323}
324
325prerender::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
336component_updater::ComponentUpdateService*
337TestingBrowserProcess::component_updater() {
338  return NULL;
339}
340
341CRLSetFetcher* TestingBrowserProcess::crl_set_fetcher() {
342  return NULL;
343}
344
345component_updater::PnaclComponentInstaller*
346TestingBrowserProcess::pnacl_component_installer() {
347  return NULL;
348}
349
350MediaFileSystemRegistry* 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
361bool TestingBrowserProcess::created_local_state() const {
362    return (local_state_ != NULL);
363}
364
365#if defined(ENABLE_WEBRTC)
366WebRtcLogUploader* TestingBrowserProcess::webrtc_log_uploader() {
367  return NULL;
368}
369#endif
370
371network_time::NetworkTimeTracker*
372TestingBrowserProcess::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
382gcm::GCMDriver* TestingBrowserProcess::gcm_driver() {
383  return NULL;
384}
385
386void TestingBrowserProcess::SetSystemRequestContext(
387    net::URLRequestContextGetter* context_getter) {
388  system_request_context_ = context_getter;
389}
390
391void 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
413void TestingBrowserProcess::SetIOThread(IOThread* io_thread) {
414  io_thread_ = io_thread;
415}
416
417void 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
429void TestingBrowserProcess::SetSafeBrowsingService(
430    SafeBrowsingService* sb_service) {
431#if !defined(OS_IOS)
432  NOTIMPLEMENTED();
433  sb_service_ = sb_service;
434#endif
435}
436
437///////////////////////////////////////////////////////////////////////////////
438
439TestingBrowserProcessInitializer::TestingBrowserProcessInitializer() {
440  TestingBrowserProcess::CreateInstance();
441}
442
443TestingBrowserProcessInitializer::~TestingBrowserProcessInitializer() {
444  TestingBrowserProcess::DeleteInstance();
445}
446