Upstream version 7.36.149.0
[platform/framework/web/crosswalk.git] / src / chrome / test / base / testing_browser_process.cc
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/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"
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
47 TestingBrowserProcess* TestingBrowserProcess::GetGlobal() {
48   return static_cast<TestingBrowserProcess*>(g_browser_process);
49 }
50
51 // static
52 void TestingBrowserProcess::CreateInstance() {
53   DCHECK(!g_browser_process);
54   g_browser_process = new TestingBrowserProcess;
55 }
56
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;
63 }
64
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());
77 }
78
79 TestingBrowserProcess::~TestingBrowserProcess() {
80   EXPECT_FALSE(local_state_);
81 #if defined(ENABLE_CONFIGURATION_POLICY)
82   SetBrowserPolicyConnector(NULL);
83 #endif
84   extensions::ExtensionsBrowserClient::Set(NULL);
85
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);
89 }
90
91 void TestingBrowserProcess::ResourceDispatcherHostCreated() {
92 }
93
94 void TestingBrowserProcess::EndSession() {
95 }
96
97 MetricsService* TestingBrowserProcess::metrics_service() {
98   return NULL;
99 }
100
101 rappor::RapporService* TestingBrowserProcess::rappor_service() {
102   return NULL;
103 }
104
105 IOThread* TestingBrowserProcess::io_thread() {
106   return io_thread_;
107 }
108
109 WatchDogThread* TestingBrowserProcess::watchdog_thread() {
110   return NULL;
111 }
112
113 ProfileManager* TestingBrowserProcess::profile_manager() {
114 #if defined(OS_IOS)
115   NOTIMPLEMENTED();
116   return NULL;
117 #else
118   return profile_manager_.get();
119 #endif
120 }
121
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
133 }
134
135 PrefService* TestingBrowserProcess::local_state() {
136   return local_state_;
137 }
138
139 chrome_variations::VariationsService*
140     TestingBrowserProcess::variations_service() {
141   return NULL;
142 }
143
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
153 }
154
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
166 }
167
168 IconManager* TestingBrowserProcess::icon_manager() {
169   return NULL;
170 }
171
172 GLStringManager* TestingBrowserProcess::gl_string_manager() {
173   return NULL;
174 }
175
176 GpuModeManager* TestingBrowserProcess::gpu_mode_manager() {
177   return NULL;
178 }
179
180 BackgroundModeManager* TestingBrowserProcess::background_mode_manager() {
181   return NULL;
182 }
183
184 void TestingBrowserProcess::set_background_mode_manager_for_test(
185     scoped_ptr<BackgroundModeManager> manager) {
186   NOTREACHED();
187 }
188
189 StatusTray* TestingBrowserProcess::status_tray() {
190   return NULL;
191 }
192
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
200 }
201
202 safe_browsing::ClientSideDetectionService*
203 TestingBrowserProcess::safe_browsing_detection_service() {
204   return NULL;
205 }
206
207 net::URLRequestContextGetter* TestingBrowserProcess::system_request_context() {
208   return system_request_context_;
209 }
210
211 BrowserProcessPlatformPart* TestingBrowserProcess::platform_part() {
212   return platform_part_.get();
213 }
214
215 extensions::EventRouterForwarder*
216 TestingBrowserProcess::extension_event_router_forwarder() {
217   return NULL;
218 }
219
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
230 }
231
232 message_center::MessageCenter* TestingBrowserProcess::message_center() {
233   return message_center::MessageCenter::Get();
234 }
235
236 IntranetRedirectDetector* TestingBrowserProcess::intranet_redirect_detector() {
237   return NULL;
238 }
239 void TestingBrowserProcess::CreateDevToolsHttpProtocolHandler(
240     chrome::HostDesktopType host_desktop_type,
241     const std::string& ip,
242     int port) {
243 }
244
245 unsigned int TestingBrowserProcess::AddRefModule() {
246   return ++module_ref_count_;
247 }
248
249 unsigned int TestingBrowserProcess::ReleaseModule() {
250   DCHECK_GT(module_ref_count_, 0U);
251   return --module_ref_count_;
252 }
253
254 bool TestingBrowserProcess::IsShuttingDown() {
255   return false;
256 }
257
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
267 }
268
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
280 }
281
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());
288   }
289   return background_printing_manager_.get();
290 #else
291   NOTIMPLEMENTED();
292   return NULL;
293 #endif
294 }
295
296 const std::string& TestingBrowserProcess::GetApplicationLocale() {
297   return app_locale_;
298 }
299
300 void TestingBrowserProcess::SetApplicationLocale(
301     const std::string& app_locale) {
302   app_locale_ = app_locale;
303 }
304
305 DownloadStatusUpdater* TestingBrowserProcess::download_status_updater() {
306   return NULL;
307 }
308
309 DownloadRequestLimiter* TestingBrowserProcess::download_request_limiter() {
310   return NULL;
311 }
312
313 ChromeNetLog* TestingBrowserProcess::net_log() {
314   return NULL;
315 }
316
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
326 }
327
328 component_updater::ComponentUpdateService*
329 TestingBrowserProcess::component_updater() {
330   return NULL;
331 }
332
333 CRLSetFetcher* TestingBrowserProcess::crl_set_fetcher() {
334   return NULL;
335 }
336
337 component_updater::PnaclComponentInstaller*
338 TestingBrowserProcess::pnacl_component_installer() {
339   return NULL;
340 }
341
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
351 }
352
353 bool TestingBrowserProcess::created_local_state() const {
354     return (local_state_ != NULL);
355 }
356
357 #if defined(ENABLE_WEBRTC)
358 WebRtcLogUploader* TestingBrowserProcess::webrtc_log_uploader() {
359   return NULL;
360 }
361 #endif
362
363 void TestingBrowserProcess::SetSystemRequestContext(
364     net::URLRequestContextGetter* context_getter) {
365   system_request_context_ = context_getter;
366 }
367
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
385   }
386   local_state_ = local_state;
387 }
388
389 void TestingBrowserProcess::SetIOThread(IOThread* io_thread) {
390   io_thread_ = io_thread;
391 }
392
393 void TestingBrowserProcess::SetBrowserPolicyConnector(
394     policy::BrowserPolicyConnector* connector) {
395 #if defined(ENABLE_CONFIGURATION_POLICY)
396   if (browser_policy_connector_) {
397     browser_policy_connector_->Shutdown();
398   }
399   browser_policy_connector_.reset(connector);
400 #else
401   CHECK(false);
402 #endif
403 }
404
405 void TestingBrowserProcess::SetSafeBrowsingService(
406     SafeBrowsingService* sb_service) {
407 #if !defined(OS_IOS)
408   NOTIMPLEMENTED();
409   sb_service_ = sb_service;
410 #endif
411 }
412
413 ///////////////////////////////////////////////////////////////////////////////
414
415 TestingBrowserProcessInitializer::TestingBrowserProcessInitializer() {
416   TestingBrowserProcess::CreateInstance();
417 }
418
419 TestingBrowserProcessInitializer::~TestingBrowserProcessInitializer() {
420   TestingBrowserProcess::DeleteInstance();
421 }