Move render_view_context_menu.* and related files out of tab_contents.
[chromium-blink-merge.git] / chrome / test / base / testing_browser_process.cc
blobdc570aaacad72f1e667d3f0feb4df320d8676c50
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/browser/ui/bookmarks/bookmark_prompt_controller.h"
18 #include "chrome/test/base/testing_browser_process_platform_part.h"
19 #include "content/public/browser/notification_service.h"
20 #include "net/url_request/url_request_context_getter.h"
21 #include "testing/gtest/include/gtest/gtest.h"
22 #include "ui/message_center/message_center.h"
24 #if !defined(OS_IOS)
25 #include "chrome/browser/notifications/notification_ui_manager.h"
26 #include "chrome/browser/prerender/prerender_tracker.h"
27 #include "chrome/browser/safe_browsing/safe_browsing_service.h"
28 #endif
30 #if !defined(OS_IOS) && !defined(OS_ANDROID)
31 #include "chrome/browser/media_galleries/media_file_system_registry.h"
32 #include "components/storage_monitor/storage_monitor.h"
33 #include "components/storage_monitor/test_storage_monitor.h"
34 #endif
36 #if defined(ENABLE_CONFIGURATION_POLICY)
37 #include "components/policy/core/browser/browser_policy_connector.h"
38 #else
39 #include "components/policy/core/common/policy_service_stub.h"
40 #endif // defined(ENABLE_CONFIGURATION_POLICY)
42 #if defined(ENABLE_FULL_PRINTING)
43 #include "chrome/browser/printing/background_printing_manager.h"
44 #include "chrome/browser/printing/print_preview_dialog_controller.h"
45 #endif
47 // static
48 TestingBrowserProcess* TestingBrowserProcess::GetGlobal() {
49 return static_cast<TestingBrowserProcess*>(g_browser_process);
52 // static
53 void TestingBrowserProcess::CreateInstance() {
54 DCHECK(!g_browser_process);
55 g_browser_process = new TestingBrowserProcess;
58 // static
59 void TestingBrowserProcess::DeleteInstance() {
60 // g_browser_process must be NULL during its own destruction.
61 BrowserProcess* browser_process = g_browser_process;
62 g_browser_process = NULL;
63 delete browser_process;
66 TestingBrowserProcess::TestingBrowserProcess()
67 : notification_service_(content::NotificationService::Create()),
68 module_ref_count_(0),
69 app_locale_("en"),
70 local_state_(NULL),
71 io_thread_(NULL),
72 system_request_context_(NULL),
73 platform_part_(new TestingBrowserProcessPlatformPart()),
74 extensions_browser_client_(
75 new extensions::ChromeExtensionsBrowserClient) {
76 extensions::ExtensionsBrowserClient::Set(extensions_browser_client_.get());
77 apps::AppsClient::Set(ChromeAppsClient::GetInstance());
80 TestingBrowserProcess::~TestingBrowserProcess() {
81 EXPECT_FALSE(local_state_);
82 #if defined(ENABLE_CONFIGURATION_POLICY)
83 SetBrowserPolicyConnector(NULL);
84 #endif
85 extensions::ExtensionsBrowserClient::Set(NULL);
87 // Destructors for some objects owned by TestingBrowserProcess will use
88 // g_browser_process if it is not NULL, so it must be NULL before proceeding.
89 DCHECK_EQ(static_cast<BrowserProcess*>(NULL), g_browser_process);
92 void TestingBrowserProcess::ResourceDispatcherHostCreated() {
95 void TestingBrowserProcess::EndSession() {
98 MetricsService* TestingBrowserProcess::metrics_service() {
99 return NULL;
102 rappor::RapporService* TestingBrowserProcess::rappor_service() {
103 return NULL;
106 IOThread* TestingBrowserProcess::io_thread() {
107 return io_thread_;
110 WatchDogThread* TestingBrowserProcess::watchdog_thread() {
111 return NULL;
114 ProfileManager* TestingBrowserProcess::profile_manager() {
115 #if defined(OS_IOS)
116 NOTIMPLEMENTED();
117 return NULL;
118 #else
119 return profile_manager_.get();
120 #endif
123 void TestingBrowserProcess::SetProfileManager(ProfileManager* profile_manager) {
124 #if !defined(OS_IOS)
125 // NotificationUIManager can contain references to elements in the current
126 // ProfileManager (for example, the MessageCenterSettingsController maintains
127 // a pointer to the ProfileInfoCache). So when we change the ProfileManager
128 // (typically during test shutdown) make sure to reset any objects that might
129 // maintain references to it. See SetLocalState() for a description of a
130 // similar situation.
131 notification_ui_manager_.reset();
132 profile_manager_.reset(profile_manager);
133 #endif
136 PrefService* TestingBrowserProcess::local_state() {
137 return local_state_;
140 chrome_variations::VariationsService*
141 TestingBrowserProcess::variations_service() {
142 return NULL;
145 policy::BrowserPolicyConnector*
146 TestingBrowserProcess::browser_policy_connector() {
147 #if defined(ENABLE_CONFIGURATION_POLICY)
148 if (!browser_policy_connector_)
149 browser_policy_connector_ = platform_part_->CreateBrowserPolicyConnector();
150 return browser_policy_connector_.get();
151 #else
152 return NULL;
153 #endif
156 policy::PolicyService* TestingBrowserProcess::policy_service() {
157 #if defined(OS_IOS)
158 NOTIMPLEMENTED();
159 return NULL;
160 #elif defined(ENABLE_CONFIGURATION_POLICY)
161 return browser_policy_connector()->GetPolicyService();
162 #else
163 if (!policy_service_)
164 policy_service_.reset(new policy::PolicyServiceStub());
165 return policy_service_.get();
166 #endif
169 IconManager* TestingBrowserProcess::icon_manager() {
170 return NULL;
173 GLStringManager* TestingBrowserProcess::gl_string_manager() {
174 return NULL;
177 GpuModeManager* TestingBrowserProcess::gpu_mode_manager() {
178 return NULL;
181 BackgroundModeManager* TestingBrowserProcess::background_mode_manager() {
182 return NULL;
185 void TestingBrowserProcess::set_background_mode_manager_for_test(
186 scoped_ptr<BackgroundModeManager> manager) {
187 NOTREACHED();
190 StatusTray* TestingBrowserProcess::status_tray() {
191 return NULL;
194 SafeBrowsingService* TestingBrowserProcess::safe_browsing_service() {
195 #if defined(OS_IOS)
196 NOTIMPLEMENTED();
197 return NULL;
198 #else
199 return sb_service_.get();
200 #endif
203 safe_browsing::ClientSideDetectionService*
204 TestingBrowserProcess::safe_browsing_detection_service() {
205 return NULL;
208 net::URLRequestContextGetter* TestingBrowserProcess::system_request_context() {
209 return system_request_context_;
212 BrowserProcessPlatformPart* TestingBrowserProcess::platform_part() {
213 return platform_part_.get();
216 extensions::EventRouterForwarder*
217 TestingBrowserProcess::extension_event_router_forwarder() {
218 return NULL;
221 NotificationUIManager* TestingBrowserProcess::notification_ui_manager() {
222 #if defined(ENABLE_NOTIFICATIONS)
223 if (!notification_ui_manager_.get())
224 notification_ui_manager_.reset(
225 NotificationUIManager::Create(local_state()));
226 return notification_ui_manager_.get();
227 #else
228 NOTIMPLEMENTED();
229 return NULL;
230 #endif
233 message_center::MessageCenter* TestingBrowserProcess::message_center() {
234 return message_center::MessageCenter::Get();
237 IntranetRedirectDetector* TestingBrowserProcess::intranet_redirect_detector() {
238 return NULL;
241 AutomationProviderList* TestingBrowserProcess::GetAutomationProviderList() {
242 return NULL;
245 void TestingBrowserProcess::CreateDevToolsHttpProtocolHandler(
246 chrome::HostDesktopType host_desktop_type,
247 const std::string& ip,
248 int port,
249 const std::string& frontend_url) {
252 unsigned int TestingBrowserProcess::AddRefModule() {
253 return ++module_ref_count_;
256 unsigned int TestingBrowserProcess::ReleaseModule() {
257 DCHECK_GT(module_ref_count_, 0U);
258 return --module_ref_count_;
261 bool TestingBrowserProcess::IsShuttingDown() {
262 return false;
265 printing::PrintJobManager* TestingBrowserProcess::print_job_manager() {
266 #if defined(ENABLE_FULL_PRINTING)
267 if (!print_job_manager_.get())
268 print_job_manager_.reset(new printing::PrintJobManager());
269 return print_job_manager_.get();
270 #else
271 NOTIMPLEMENTED();
272 return NULL;
273 #endif
276 printing::PrintPreviewDialogController*
277 TestingBrowserProcess::print_preview_dialog_controller() {
278 #if defined(ENABLE_FULL_PRINTING)
279 if (!print_preview_dialog_controller_.get())
280 print_preview_dialog_controller_ =
281 new printing::PrintPreviewDialogController();
282 return print_preview_dialog_controller_.get();
283 #else
284 NOTIMPLEMENTED();
285 return NULL;
286 #endif
289 printing::BackgroundPrintingManager*
290 TestingBrowserProcess::background_printing_manager() {
291 #if defined(ENABLE_FULL_PRINTING)
292 if (!background_printing_manager_.get()) {
293 background_printing_manager_.reset(
294 new printing::BackgroundPrintingManager());
296 return background_printing_manager_.get();
297 #else
298 NOTIMPLEMENTED();
299 return NULL;
300 #endif
303 const std::string& TestingBrowserProcess::GetApplicationLocale() {
304 return app_locale_;
307 void TestingBrowserProcess::SetApplicationLocale(
308 const std::string& app_locale) {
309 app_locale_ = app_locale;
312 DownloadStatusUpdater* TestingBrowserProcess::download_status_updater() {
313 return NULL;
316 DownloadRequestLimiter* TestingBrowserProcess::download_request_limiter() {
317 return NULL;
320 ChromeNetLog* TestingBrowserProcess::net_log() {
321 return NULL;
324 prerender::PrerenderTracker* TestingBrowserProcess::prerender_tracker() {
325 #if defined(OS_IOS)
326 NOTIMPLEMENTED();
327 return NULL;
328 #else
329 if (!prerender_tracker_.get())
330 prerender_tracker_.reset(new prerender::PrerenderTracker());
331 return prerender_tracker_.get();
332 #endif
335 component_updater::ComponentUpdateService*
336 TestingBrowserProcess::component_updater() {
337 return NULL;
340 CRLSetFetcher* TestingBrowserProcess::crl_set_fetcher() {
341 return NULL;
344 component_updater::PnaclComponentInstaller*
345 TestingBrowserProcess::pnacl_component_installer() {
346 return NULL;
349 BookmarkPromptController* TestingBrowserProcess::bookmark_prompt_controller() {
350 #if defined(OS_IOS)
351 NOTIMPLEMENTED();
352 return NULL;
353 #else
354 return bookmark_prompt_controller_.get();
355 #endif
358 MediaFileSystemRegistry* TestingBrowserProcess::media_file_system_registry() {
359 #if defined(OS_IOS) || defined(OS_ANDROID)
360 NOTIMPLEMENTED();
361 return NULL;
362 #else
363 if (!media_file_system_registry_)
364 media_file_system_registry_.reset(new MediaFileSystemRegistry());
365 return media_file_system_registry_.get();
366 #endif
369 bool TestingBrowserProcess::created_local_state() const {
370 return (local_state_ != NULL);
373 #if defined(ENABLE_WEBRTC)
374 WebRtcLogUploader* TestingBrowserProcess::webrtc_log_uploader() {
375 return NULL;
377 #endif
379 void TestingBrowserProcess::SetBookmarkPromptController(
380 BookmarkPromptController* controller) {
381 #if !defined(OS_IOS)
382 bookmark_prompt_controller_.reset(controller);
383 #endif
386 void TestingBrowserProcess::SetSystemRequestContext(
387 net::URLRequestContextGetter* context_getter) {
388 system_request_context_ = context_getter;
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 #if !defined(OS_IOS)
403 notification_ui_manager_.reset();
404 #endif
405 #if defined(ENABLE_CONFIGURATION_POLICY)
406 SetBrowserPolicyConnector(NULL);
407 #endif
409 local_state_ = local_state;
412 void TestingBrowserProcess::SetIOThread(IOThread* io_thread) {
413 io_thread_ = io_thread;
416 void TestingBrowserProcess::SetBrowserPolicyConnector(
417 policy::BrowserPolicyConnector* connector) {
418 #if defined(ENABLE_CONFIGURATION_POLICY)
419 if (browser_policy_connector_) {
420 browser_policy_connector_->Shutdown();
422 browser_policy_connector_.reset(connector);
423 #else
424 CHECK(false);
425 #endif
428 void TestingBrowserProcess::SetSafeBrowsingService(
429 SafeBrowsingService* sb_service) {
430 #if !defined(OS_IOS)
431 NOTIMPLEMENTED();
432 sb_service_ = sb_service;
433 #endif
436 ///////////////////////////////////////////////////////////////////////////////
438 TestingBrowserProcessInitializer::TestingBrowserProcessInitializer() {
439 TestingBrowserProcess::CreateInstance();
442 TestingBrowserProcessInitializer::~TestingBrowserProcessInitializer() {
443 TestingBrowserProcess::DeleteInstance();