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