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