testing_browser_process.cc revision 5d1f7b1de12d16ceb2c938c56701a3e8bfa558f7
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 "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"
22
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
28
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
34
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)
40
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
45
46// static
47TestingBrowserProcess* TestingBrowserProcess::GetGlobal() {
48  return static_cast<TestingBrowserProcess*>(g_browser_process);
49}
50
51// static
52void TestingBrowserProcess::CreateInstance() {
53  DCHECK(!g_browser_process);
54  g_browser_process = new TestingBrowserProcess;
55}
56
57// static
58void 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;
63}
64
65TestingBrowserProcess::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}
77
78TestingBrowserProcess::~TestingBrowserProcess() {
79  EXPECT_FALSE(local_state_);
80#if defined(ENABLE_CONFIGURATION_POLICY)
81  SetBrowserPolicyConnector(NULL);
82#endif
83  extensions::ExtensionsBrowserClient::Set(NULL);
84
85  // Destructors for some objects owned by TestingBrowserProcess will use
86  // g_browser_process if it is not NULL, so it must be NULL before proceeding.
87  DCHECK_EQ(static_cast<BrowserProcess*>(NULL), g_browser_process);
88}
89
90void TestingBrowserProcess::ResourceDispatcherHostCreated() {
91}
92
93void TestingBrowserProcess::EndSession() {
94}
95
96MetricsService* TestingBrowserProcess::metrics_service() {
97  return NULL;
98}
99
100IOThread* TestingBrowserProcess::io_thread() {
101  return io_thread_;
102}
103
104WatchDogThread* TestingBrowserProcess::watchdog_thread() {
105  return NULL;
106}
107
108ProfileManager* TestingBrowserProcess::profile_manager() {
109#if defined(OS_IOS)
110  NOTIMPLEMENTED();
111  return NULL;
112#else
113  return profile_manager_.get();
114#endif
115}
116
117void TestingBrowserProcess::SetProfileManager(ProfileManager* profile_manager) {
118#if !defined(OS_IOS)
119  // NotificationUIManager can contain references to elements in the current
120  // ProfileManager (for example, the MessageCenterSettingsController maintains
121  // a pointer to the ProfileInfoCache). So when we change the ProfileManager
122  // (typically during test shutdown) make sure to reset any objects that might
123  // maintain references to it. See SetLocalState() for a description of a
124  // similar situation.
125  notification_ui_manager_.reset();
126  profile_manager_.reset(profile_manager);
127#endif
128}
129
130PrefService* TestingBrowserProcess::local_state() {
131  return local_state_;
132}
133
134chrome_variations::VariationsService*
135    TestingBrowserProcess::variations_service() {
136  return NULL;
137}
138
139policy::BrowserPolicyConnector*
140    TestingBrowserProcess::browser_policy_connector() {
141#if defined(ENABLE_CONFIGURATION_POLICY)
142  if (!browser_policy_connector_)
143    browser_policy_connector_ = platform_part_->CreateBrowserPolicyConnector();
144  return browser_policy_connector_.get();
145#else
146  return NULL;
147#endif
148}
149
150policy::PolicyService* TestingBrowserProcess::policy_service() {
151#if defined(OS_IOS)
152  NOTIMPLEMENTED();
153  return NULL;
154#elif defined(ENABLE_CONFIGURATION_POLICY)
155  return browser_policy_connector()->GetPolicyService();
156#else
157  if (!policy_service_)
158    policy_service_.reset(new policy::PolicyServiceStub());
159  return policy_service_.get();
160#endif
161}
162
163IconManager* TestingBrowserProcess::icon_manager() {
164  return NULL;
165}
166
167GLStringManager* TestingBrowserProcess::gl_string_manager() {
168  return NULL;
169}
170
171GpuModeManager* TestingBrowserProcess::gpu_mode_manager() {
172  return NULL;
173}
174
175BackgroundModeManager* TestingBrowserProcess::background_mode_manager() {
176  return NULL;
177}
178
179void TestingBrowserProcess::set_background_mode_manager_for_test(
180    scoped_ptr<BackgroundModeManager> manager) {
181  NOTREACHED();
182}
183
184StatusTray* TestingBrowserProcess::status_tray() {
185  return NULL;
186}
187
188SafeBrowsingService* TestingBrowserProcess::safe_browsing_service() {
189#if defined(OS_IOS)
190  NOTIMPLEMENTED();
191  return NULL;
192#else
193  return sb_service_.get();
194#endif
195}
196
197safe_browsing::ClientSideDetectionService*
198TestingBrowserProcess::safe_browsing_detection_service() {
199  return NULL;
200}
201
202net::URLRequestContextGetter* TestingBrowserProcess::system_request_context() {
203  return system_request_context_;
204}
205
206BrowserProcessPlatformPart* TestingBrowserProcess::platform_part() {
207  return platform_part_.get();
208}
209
210extensions::EventRouterForwarder*
211TestingBrowserProcess::extension_event_router_forwarder() {
212  return NULL;
213}
214
215NotificationUIManager* TestingBrowserProcess::notification_ui_manager() {
216#if defined(ENABLE_NOTIFICATIONS)
217  if (!notification_ui_manager_.get())
218    notification_ui_manager_.reset(
219        NotificationUIManager::Create(local_state()));
220  return notification_ui_manager_.get();
221#else
222  NOTIMPLEMENTED();
223  return NULL;
224#endif
225}
226
227message_center::MessageCenter* TestingBrowserProcess::message_center() {
228  return message_center::MessageCenter::Get();
229}
230
231IntranetRedirectDetector* TestingBrowserProcess::intranet_redirect_detector() {
232  return NULL;
233}
234
235AutomationProviderList* TestingBrowserProcess::GetAutomationProviderList() {
236  return NULL;
237}
238
239void TestingBrowserProcess::CreateDevToolsHttpProtocolHandler(
240    chrome::HostDesktopType host_desktop_type,
241    const std::string& ip,
242    int port,
243    const std::string& frontend_url) {
244}
245
246unsigned int TestingBrowserProcess::AddRefModule() {
247  return ++module_ref_count_;
248}
249
250unsigned int TestingBrowserProcess::ReleaseModule() {
251  DCHECK_GT(module_ref_count_, 0U);
252  return --module_ref_count_;
253}
254
255bool TestingBrowserProcess::IsShuttingDown() {
256  return false;
257}
258
259printing::PrintJobManager* TestingBrowserProcess::print_job_manager() {
260#if defined(ENABLE_FULL_PRINTING)
261  if (!print_job_manager_.get())
262    print_job_manager_.reset(new printing::PrintJobManager());
263  return print_job_manager_.get();
264#else
265  NOTIMPLEMENTED();
266  return NULL;
267#endif
268}
269
270printing::PrintPreviewDialogController*
271TestingBrowserProcess::print_preview_dialog_controller() {
272#if defined(ENABLE_FULL_PRINTING)
273  if (!print_preview_dialog_controller_.get())
274    print_preview_dialog_controller_ =
275        new printing::PrintPreviewDialogController();
276  return print_preview_dialog_controller_.get();
277#else
278  NOTIMPLEMENTED();
279  return NULL;
280#endif
281}
282
283printing::BackgroundPrintingManager*
284TestingBrowserProcess::background_printing_manager() {
285#if defined(ENABLE_FULL_PRINTING)
286  if (!background_printing_manager_.get()) {
287    background_printing_manager_.reset(
288        new printing::BackgroundPrintingManager());
289  }
290  return background_printing_manager_.get();
291#else
292  NOTIMPLEMENTED();
293  return NULL;
294#endif
295}
296
297const std::string& TestingBrowserProcess::GetApplicationLocale() {
298  return app_locale_;
299}
300
301void TestingBrowserProcess::SetApplicationLocale(
302    const std::string& app_locale) {
303  app_locale_ = app_locale;
304}
305
306DownloadStatusUpdater* TestingBrowserProcess::download_status_updater() {
307  return NULL;
308}
309
310DownloadRequestLimiter* TestingBrowserProcess::download_request_limiter() {
311  return NULL;
312}
313
314ChromeNetLog* TestingBrowserProcess::net_log() {
315  return NULL;
316}
317
318prerender::PrerenderTracker* TestingBrowserProcess::prerender_tracker() {
319#if defined(OS_IOS)
320  NOTIMPLEMENTED();
321  return NULL;
322#else
323  if (!prerender_tracker_.get())
324    prerender_tracker_.reset(new prerender::PrerenderTracker());
325  return prerender_tracker_.get();
326#endif
327}
328
329component_updater::ComponentUpdateService*
330TestingBrowserProcess::component_updater() {
331  return NULL;
332}
333
334CRLSetFetcher* TestingBrowserProcess::crl_set_fetcher() {
335  return NULL;
336}
337
338component_updater::PnaclComponentInstaller*
339TestingBrowserProcess::pnacl_component_installer() {
340  return NULL;
341}
342
343BookmarkPromptController* TestingBrowserProcess::bookmark_prompt_controller() {
344#if defined(OS_IOS)
345  NOTIMPLEMENTED();
346  return NULL;
347#else
348  return bookmark_prompt_controller_.get();
349#endif
350}
351
352MediaFileSystemRegistry* TestingBrowserProcess::media_file_system_registry() {
353#if defined(OS_IOS) || defined(OS_ANDROID)
354  NOTIMPLEMENTED();
355  return NULL;
356#else
357  if (!media_file_system_registry_)
358    media_file_system_registry_.reset(new MediaFileSystemRegistry());
359  return media_file_system_registry_.get();
360#endif
361}
362
363bool TestingBrowserProcess::created_local_state() const {
364    return (local_state_ != NULL);
365}
366
367#if defined(ENABLE_WEBRTC)
368WebRtcLogUploader* TestingBrowserProcess::webrtc_log_uploader() {
369  return NULL;
370}
371#endif
372
373void TestingBrowserProcess::SetBookmarkPromptController(
374    BookmarkPromptController* controller) {
375#if !defined(OS_IOS)
376  bookmark_prompt_controller_.reset(controller);
377#endif
378}
379
380void TestingBrowserProcess::SetSystemRequestContext(
381    net::URLRequestContextGetter* context_getter) {
382  system_request_context_ = context_getter;
383}
384
385void TestingBrowserProcess::SetLocalState(PrefService* local_state) {
386  if (!local_state) {
387    // The local_state_ PrefService is owned outside of TestingBrowserProcess,
388    // but some of the members of TestingBrowserProcess hold references to it
389    // (for example, via PrefNotifier members). But given our test
390    // infrastructure which tears down individual tests before freeing the
391    // TestingBrowserProcess, there's not a good way to make local_state outlive
392    // these dependencies. As a workaround, whenever local_state_ is cleared
393    // (assumedly as part of exiting the test and freeing TestingBrowserProcess)
394    // any components owned by TestingBrowserProcess that depend on local_state
395    // are also freed.
396#if !defined(OS_IOS)
397    notification_ui_manager_.reset();
398#endif
399#if defined(ENABLE_CONFIGURATION_POLICY)
400    SetBrowserPolicyConnector(NULL);
401#endif
402  }
403  local_state_ = local_state;
404}
405
406void TestingBrowserProcess::SetIOThread(IOThread* io_thread) {
407  io_thread_ = io_thread;
408}
409
410void TestingBrowserProcess::SetBrowserPolicyConnector(
411    policy::BrowserPolicyConnector* connector) {
412#if defined(ENABLE_CONFIGURATION_POLICY)
413  if (browser_policy_connector_) {
414    browser_policy_connector_->Shutdown();
415  }
416  browser_policy_connector_.reset(connector);
417#else
418  CHECK(false);
419#endif
420}
421
422void TestingBrowserProcess::SetSafeBrowsingService(
423    SafeBrowsingService* sb_service) {
424#if !defined(OS_IOS)
425  NOTIMPLEMENTED();
426  sb_service_ = sb_service;
427#endif
428}
429
430///////////////////////////////////////////////////////////////////////////////
431
432TestingBrowserProcessInitializer::TestingBrowserProcessInitializer() {
433  TestingBrowserProcess::CreateInstance();
434}
435
436TestingBrowserProcessInitializer::~TestingBrowserProcessInitializer() {
437  TestingBrowserProcess::DeleteInstance();
438}
439