OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "chrome/test/base/testing_profile.h" | 5 #include "chrome/test/base/testing_profile.h" |
6 | 6 |
7 #include "build/build_config.h" | 7 #include "build/build_config.h" |
8 | 8 |
9 #include "base/base_paths.h" | 9 #include "base/base_paths.h" |
10 #include "base/command_line.h" | 10 #include "base/command_line.h" |
11 #include "base/file_util.h" | 11 #include "base/file_util.h" |
12 #include "base/message_loop_proxy.h" | 12 #include "base/message_loop_proxy.h" |
13 #include "base/path_service.h" | 13 #include "base/path_service.h" |
14 #include "base/string_number_conversions.h" | 14 #include "base/string_number_conversions.h" |
15 #include "chrome/browser/autocomplete/autocomplete_classifier.h" | 15 #include "chrome/browser/autocomplete/autocomplete_classifier.h" |
16 #include "chrome/browser/bookmarks/bookmark_model.h" | 16 #include "chrome/browser/bookmarks/bookmark_model.h" |
| 17 #include "chrome/browser/bookmarks/bookmark_model_factory.h" |
17 #include "chrome/browser/browser_process.h" | 18 #include "chrome/browser/browser_process.h" |
18 #include "chrome/browser/content_settings/host_content_settings_map.h" | 19 #include "chrome/browser/content_settings/host_content_settings_map.h" |
19 #include "chrome/browser/custom_handlers/protocol_handler_registry.h" | 20 #include "chrome/browser/custom_handlers/protocol_handler_registry.h" |
20 #include "chrome/browser/extensions/extension_service.h" | 21 #include "chrome/browser/extensions/extension_service.h" |
21 #include "chrome/browser/extensions/extension_special_storage_policy.h" | 22 #include "chrome/browser/extensions/extension_special_storage_policy.h" |
22 #include "chrome/browser/extensions/extension_system.h" | 23 #include "chrome/browser/extensions/extension_system.h" |
23 #include "chrome/browser/extensions/extension_system_factory.h" | 24 #include "chrome/browser/extensions/extension_system_factory.h" |
24 #include "chrome/browser/extensions/test_extension_system.h" | 25 #include "chrome/browser/extensions/test_extension_system.h" |
25 #include "chrome/browser/favicon/favicon_service.h" | 26 #include "chrome/browser/favicon/favicon_service.h" |
26 #include "chrome/browser/geolocation/chrome_geolocation_permission_context.h" | 27 #include "chrome/browser/geolocation/chrome_geolocation_permission_context.h" |
27 #include "chrome/browser/history/history.h" | 28 #include "chrome/browser/history/history.h" |
28 #include "chrome/browser/history/history_backend.h" | 29 #include "chrome/browser/history/history_backend.h" |
| 30 #include "chrome/browser/history/history_service_factory.h" |
29 #include "chrome/browser/history/top_sites.h" | 31 #include "chrome/browser/history/top_sites.h" |
30 #include "chrome/browser/net/proxy_service_factory.h" | 32 #include "chrome/browser/net/proxy_service_factory.h" |
31 #include "chrome/browser/notifications/desktop_notification_service.h" | 33 #include "chrome/browser/notifications/desktop_notification_service.h" |
32 #include "chrome/browser/notifications/desktop_notification_service_factory.h" | 34 #include "chrome/browser/notifications/desktop_notification_service_factory.h" |
33 #include "chrome/browser/prefs/browser_prefs.h" | 35 #include "chrome/browser/prefs/browser_prefs.h" |
34 #include "chrome/browser/prefs/testing_pref_store.h" | 36 #include "chrome/browser/prefs/testing_pref_store.h" |
35 #include "chrome/browser/prerender/prerender_manager.h" | 37 #include "chrome/browser/prerender/prerender_manager.h" |
36 #include "chrome/browser/profiles/profile_dependency_manager.h" | 38 #include "chrome/browser/profiles/profile_dependency_manager.h" |
37 #include "chrome/browser/protector/protector_service_factory.h" | 39 #include "chrome/browser/protector/protector_service_factory.h" |
38 #include "chrome/browser/search_engines/template_url_fetcher_factory.h" | 40 #include "chrome/browser/search_engines/template_url_fetcher_factory.h" |
(...skipping 189 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
228 chrome::NOTIFICATION_PROFILE_DESTROYED, | 230 chrome::NOTIFICATION_PROFILE_DESTROYED, |
229 content::Source<Profile>(static_cast<Profile*>(this)), | 231 content::Source<Profile>(static_cast<Profile*>(this)), |
230 content::NotificationService::NoDetails()); | 232 content::NotificationService::NoDetails()); |
231 | 233 |
232 profile_dependency_manager_->DestroyProfileServices(this); | 234 profile_dependency_manager_->DestroyProfileServices(this); |
233 | 235 |
234 if (host_content_settings_map_) | 236 if (host_content_settings_map_) |
235 host_content_settings_map_->ShutdownOnUIThread(); | 237 host_content_settings_map_->ShutdownOnUIThread(); |
236 | 238 |
237 DestroyTopSites(); | 239 DestroyTopSites(); |
238 DestroyHistoryService(); | |
239 // FaviconService depends on HistoryServce so destroying it later. | |
240 DestroyFaviconService(); | 240 DestroyFaviconService(); |
241 | 241 |
242 if (pref_proxy_config_tracker_.get()) | 242 if (pref_proxy_config_tracker_.get()) |
243 pref_proxy_config_tracker_->DetachFromPrefService(); | 243 pref_proxy_config_tracker_->DetachFromPrefService(); |
244 } | 244 } |
245 | 245 |
246 void TestingProfile::CreateFaviconService() { | 246 void TestingProfile::CreateFaviconService() { |
247 favicon_service_.reset(new FaviconService(this)); | 247 favicon_service_.reset(new FaviconService(this)); |
248 } | 248 } |
249 | 249 |
| 250 static scoped_refptr<RefcountedProfileKeyedService> BuildHistoryService( |
| 251 Profile* profile) { |
| 252 return new HistoryService(profile); |
| 253 } |
| 254 |
250 void TestingProfile::CreateHistoryService(bool delete_file, bool no_db) { | 255 void TestingProfile::CreateHistoryService(bool delete_file, bool no_db) { |
251 DestroyHistoryService(); | 256 DestroyHistoryService(); |
252 if (delete_file) { | 257 if (delete_file) { |
253 FilePath path = GetPath(); | 258 FilePath path = GetPath(); |
254 path = path.Append(chrome::kHistoryFilename); | 259 path = path.Append(chrome::kHistoryFilename); |
255 file_util::Delete(path, false); | 260 file_util::Delete(path, false); |
256 } | 261 } |
257 history_service_ = new HistoryService(this); | 262 // This will create and init the history service. |
258 history_service_->Init(GetPath(), bookmark_bar_model_.get(), no_db); | 263 HistoryService* history_service = static_cast<HistoryService*>( |
| 264 HistoryServiceFactory::GetInstance()->SetTestingFactoryAndUse( |
| 265 this, BuildHistoryService).get()); |
| 266 if (!history_service->Init(this->GetPath(), |
| 267 reinterpret_cast<BookmarkService*>( |
| 268 BookmarkModelFactory::GetForProfile(this)), |
| 269 no_db)) { |
| 270 HistoryServiceFactory::GetInstance()->SetTestingFactoryAndUse(this, NULL); |
| 271 } |
259 } | 272 } |
260 | 273 |
261 void TestingProfile::DestroyHistoryService() { | 274 void TestingProfile::DestroyHistoryService() { |
262 if (!history_service_.get()) | 275 scoped_refptr<HistoryService> history_service = |
| 276 HistoryServiceFactory::GetForProfileIfExists(this); |
| 277 if (!history_service.get()) |
263 return; | 278 return; |
264 | 279 |
265 history_service_->NotifyRenderProcessHostDestruction(0); | 280 history_service->NotifyRenderProcessHostDestruction(0); |
266 history_service_->SetOnBackendDestroyTask(MessageLoop::QuitClosure()); | 281 history_service->SetOnBackendDestroyTask(MessageLoop::QuitClosure()); |
267 history_service_->Cleanup(); | 282 history_service->Cleanup(); |
268 history_service_ = NULL; | 283 HistoryServiceFactory::ShutdownForProfile(this); |
269 | 284 |
270 // Wait for the backend class to terminate before deleting the files and | 285 // Wait for the backend class to terminate before deleting the files and |
271 // moving to the next test. Note: if this never terminates, somebody is | 286 // moving to the next test. Note: if this never terminates, somebody is |
272 // probably leaking a reference to the history backend, so it never calls | 287 // probably leaking a reference to the history backend, so it never calls |
273 // our destroy task. | 288 // our destroy task. |
274 MessageLoop::current()->Run(); | 289 MessageLoop::current()->Run(); |
275 | 290 |
276 // Make sure we don't have any event pending that could disrupt the next | 291 // Make sure we don't have any event pending that could disrupt the next |
277 // test. | 292 // test. |
278 MessageLoop::current()->PostTask(FROM_HERE, MessageLoop::QuitClosure()); | 293 MessageLoop::current()->PostTask(FROM_HERE, MessageLoop::QuitClosure()); |
(...skipping 16 matching lines...) Expand all Loading... |
295 // normally handled by browser_process shutdown. | 310 // normally handled by browser_process shutdown. |
296 if (MessageLoop::current()) | 311 if (MessageLoop::current()) |
297 MessageLoop::current()->RunAllPending(); | 312 MessageLoop::current()->RunAllPending(); |
298 } | 313 } |
299 } | 314 } |
300 | 315 |
301 void TestingProfile::DestroyFaviconService() { | 316 void TestingProfile::DestroyFaviconService() { |
302 favicon_service_.reset(); | 317 favicon_service_.reset(); |
303 } | 318 } |
304 | 319 |
| 320 static ProfileKeyedService* BuildBookmarkModel(Profile* profile) { |
| 321 BookmarkModel* bookmark_model = new BookmarkModel(profile); |
| 322 bookmark_model->Load(); |
| 323 return bookmark_model; |
| 324 } |
| 325 |
| 326 |
305 void TestingProfile::CreateBookmarkModel(bool delete_file) { | 327 void TestingProfile::CreateBookmarkModel(bool delete_file) { |
306 // Nuke the model first, that way we're sure it's done writing to disk. | |
307 bookmark_bar_model_.reset(NULL); | |
308 | 328 |
309 if (delete_file) { | 329 if (delete_file) { |
310 FilePath path = GetPath(); | 330 FilePath path = GetPath(); |
311 path = path.Append(chrome::kBookmarksFileName); | 331 path = path.Append(chrome::kBookmarksFileName); |
312 file_util::Delete(path, false); | 332 file_util::Delete(path, false); |
313 } | 333 } |
314 bookmark_bar_model_.reset(new BookmarkModel(this)); | 334 // This will create a bookmark model. |
315 if (history_service_.get()) { | 335 BookmarkModel* bookmark_service = |
316 history_service_->history_backend_->bookmark_service_ = | 336 static_cast<BookmarkModel*>( |
317 bookmark_bar_model_.get(); | 337 BookmarkModelFactory::GetInstance()->SetTestingFactoryAndUse( |
318 history_service_->history_backend_->expirer_.bookmark_service_ = | 338 this, BuildBookmarkModel)); |
319 bookmark_bar_model_.get(); | 339 |
| 340 HistoryService* history_service = |
| 341 HistoryServiceFactory::GetForProfileIfExists(this).get(); |
| 342 if (history_service) { |
| 343 history_service->history_backend_->bookmark_service_ = |
| 344 bookmark_service; |
| 345 history_service->history_backend_->expirer_.bookmark_service_ = |
| 346 bookmark_service; |
320 } | 347 } |
321 bookmark_bar_model_->Load(); | |
322 } | 348 } |
323 | 349 |
324 void TestingProfile::CreateAutocompleteClassifier() { | 350 void TestingProfile::CreateAutocompleteClassifier() { |
325 autocomplete_classifier_.reset(new AutocompleteClassifier(this)); | 351 autocomplete_classifier_.reset(new AutocompleteClassifier(this)); |
326 } | 352 } |
327 | 353 |
328 void TestingProfile::CreateProtocolHandlerRegistry() { | 354 void TestingProfile::CreateProtocolHandlerRegistry() { |
329 protocol_handler_registry_ = new ProtocolHandlerRegistry(this, | 355 protocol_handler_registry_ = new ProtocolHandlerRegistry(this, |
330 new ProtocolHandlerRegistry::Delegate()); | 356 new ProtocolHandlerRegistry::Delegate()); |
331 } | 357 } |
332 | 358 |
333 static scoped_refptr<RefcountedProfileKeyedService> BuildWebDataService( | 359 static scoped_refptr<RefcountedProfileKeyedService> BuildWebDataService( |
334 Profile* profile) { | 360 Profile* profile) { |
335 WebDataService* web_data_service = new WebDataService(); | 361 WebDataService* web_data_service = new WebDataService(); |
336 if (web_data_service) | 362 if (web_data_service) |
337 web_data_service->Init(profile->GetPath()); | 363 web_data_service->Init(profile->GetPath()); |
338 return scoped_refptr<WebDataService>(web_data_service); | 364 return scoped_refptr<WebDataService>(web_data_service); |
339 } | 365 } |
340 | 366 |
341 void TestingProfile::CreateWebDataService() { | 367 void TestingProfile::CreateWebDataService() { |
342 WebDataServiceFactory::GetInstance()->SetTestingFactoryAndUse( | 368 WebDataServiceFactory::GetInstance()->SetTestingFactoryAndUse( |
343 this, BuildWebDataService); | 369 this, BuildWebDataService); |
344 } | 370 } |
345 | 371 |
346 void TestingProfile::BlockUntilBookmarkModelLoaded() { | 372 void TestingProfile::BlockUntilBookmarkModelLoaded() { |
347 DCHECK(bookmark_bar_model_.get()); | 373 DCHECK(GetBookmarkModel()); |
348 if (bookmark_bar_model_->IsLoaded()) | 374 if (GetBookmarkModel()->IsLoaded()) |
349 return; | 375 return; |
350 BookmarkLoadObserver observer; | 376 BookmarkLoadObserver observer; |
351 bookmark_bar_model_->AddObserver(&observer); | 377 GetBookmarkModel()->AddObserver(&observer); |
352 MessageLoop::current()->Run(); | 378 MessageLoop::current()->Run(); |
353 bookmark_bar_model_->RemoveObserver(&observer); | 379 GetBookmarkModel()->RemoveObserver(&observer); |
354 DCHECK(bookmark_bar_model_->IsLoaded()); | 380 DCHECK(GetBookmarkModel()->IsLoaded()); |
355 } | 381 } |
356 | 382 |
357 // TODO(phajdan.jr): Doesn't this hang if Top Sites are already loaded? | 383 // TODO(phajdan.jr): Doesn't this hang if Top Sites are already loaded? |
358 void TestingProfile::BlockUntilTopSitesLoaded() { | 384 void TestingProfile::BlockUntilTopSitesLoaded() { |
359 ui_test_utils::WindowedNotificationObserver top_sites_loaded_observer( | 385 ui_test_utils::WindowedNotificationObserver top_sites_loaded_observer( |
360 chrome::NOTIFICATION_TOP_SITES_LOADED, | 386 chrome::NOTIFICATION_TOP_SITES_LOADED, |
361 content::NotificationService::AllSources()); | 387 content::NotificationService::AllSources()); |
362 if (!GetHistoryService(Profile::EXPLICIT_ACCESS)) | 388 if (!GetHistoryService(Profile::EXPLICIT_ACCESS)) |
363 GetTopSites()->HistoryLoaded(); | 389 GetTopSites()->HistoryLoaded(); |
364 top_sites_loaded_observer.Wait(); | 390 top_sites_loaded_observer.Wait(); |
(...skipping 94 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
459 if (!extension_special_storage_policy_.get()) | 485 if (!extension_special_storage_policy_.get()) |
460 extension_special_storage_policy_ = new ExtensionSpecialStoragePolicy(NULL); | 486 extension_special_storage_policy_ = new ExtensionSpecialStoragePolicy(NULL); |
461 return extension_special_storage_policy_.get(); | 487 return extension_special_storage_policy_.get(); |
462 } | 488 } |
463 | 489 |
464 FaviconService* TestingProfile::GetFaviconService(ServiceAccessType access) { | 490 FaviconService* TestingProfile::GetFaviconService(ServiceAccessType access) { |
465 return favicon_service_.get(); | 491 return favicon_service_.get(); |
466 } | 492 } |
467 | 493 |
468 HistoryService* TestingProfile::GetHistoryService(ServiceAccessType access) { | 494 HistoryService* TestingProfile::GetHistoryService(ServiceAccessType access) { |
469 return history_service_.get(); | 495 return HistoryServiceFactory::GetForProfileIfExists(this); |
470 } | 496 } |
471 | 497 |
472 HistoryService* TestingProfile::GetHistoryServiceWithoutCreating() { | 498 HistoryService* TestingProfile::GetHistoryServiceWithoutCreating() { |
473 return history_service_.get(); | 499 return HistoryServiceFactory::GetForProfileIfExists(this); |
474 } | 500 } |
475 | 501 |
476 net::CookieMonster* TestingProfile::GetCookieMonster() { | 502 net::CookieMonster* TestingProfile::GetCookieMonster() { |
477 if (!GetRequestContext()) | 503 if (!GetRequestContext()) |
478 return NULL; | 504 return NULL; |
479 return GetRequestContext()->GetURLRequestContext()->cookie_store()-> | 505 return GetRequestContext()->GetURLRequestContext()->cookie_store()-> |
480 GetCookieMonster(); | 506 GetCookieMonster(); |
481 } | 507 } |
482 | 508 |
483 AutocompleteClassifier* TestingProfile::GetAutocompleteClassifier() { | 509 AutocompleteClassifier* TestingProfile::GetAutocompleteClassifier() { |
(...skipping 142 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
626 | 652 |
627 void TestingProfile::SetID(const std::wstring& id) { | 653 void TestingProfile::SetID(const std::wstring& id) { |
628 id_ = id; | 654 id_ = id; |
629 } | 655 } |
630 | 656 |
631 bool TestingProfile::DidLastSessionExitCleanly() { | 657 bool TestingProfile::DidLastSessionExitCleanly() { |
632 return last_session_exited_cleanly_; | 658 return last_session_exited_cleanly_; |
633 } | 659 } |
634 | 660 |
635 BookmarkModel* TestingProfile::GetBookmarkModel() { | 661 BookmarkModel* TestingProfile::GetBookmarkModel() { |
636 return bookmark_bar_model_.get(); | 662 return BookmarkModelFactory::GetForProfileIfExists(this); |
637 } | 663 } |
638 | 664 |
639 bool TestingProfile::IsSameProfile(Profile *p) { | 665 bool TestingProfile::IsSameProfile(Profile *p) { |
640 return this == p; | 666 return this == p; |
641 } | 667 } |
642 | 668 |
643 base::Time TestingProfile::GetStartTime() const { | 669 base::Time TestingProfile::GetStartTime() const { |
644 return start_time_; | 670 return start_time_; |
645 } | 671 } |
646 | 672 |
(...skipping 11 matching lines...) Expand all Loading... |
658 | 684 |
659 PrefProxyConfigTracker* TestingProfile::GetProxyConfigTracker() { | 685 PrefProxyConfigTracker* TestingProfile::GetProxyConfigTracker() { |
660 if (!pref_proxy_config_tracker_.get()) { | 686 if (!pref_proxy_config_tracker_.get()) { |
661 pref_proxy_config_tracker_.reset( | 687 pref_proxy_config_tracker_.reset( |
662 ProxyServiceFactory::CreatePrefProxyConfigTracker(GetPrefs())); | 688 ProxyServiceFactory::CreatePrefProxyConfigTracker(GetPrefs())); |
663 } | 689 } |
664 return pref_proxy_config_tracker_.get(); | 690 return pref_proxy_config_tracker_.get(); |
665 } | 691 } |
666 | 692 |
667 void TestingProfile::BlockUntilHistoryProcessesPendingRequests() { | 693 void TestingProfile::BlockUntilHistoryProcessesPendingRequests() { |
668 DCHECK(history_service_.get()); | 694 scoped_refptr<HistoryService> history_service = |
| 695 HistoryServiceFactory::GetForProfileIfExists(this); |
| 696 DCHECK(history_service.get()); |
669 DCHECK(MessageLoop::current()); | 697 DCHECK(MessageLoop::current()); |
670 | 698 |
671 CancelableRequestConsumer consumer; | 699 CancelableRequestConsumer consumer; |
672 history_service_->ScheduleDBTask(new QuittingHistoryDBTask(), &consumer); | 700 history_service->ScheduleDBTask(new QuittingHistoryDBTask(), &consumer); |
673 MessageLoop::current()->Run(); | 701 MessageLoop::current()->Run(); |
674 } | 702 } |
675 | 703 |
676 chrome_browser_net::Predictor* TestingProfile::GetNetworkPredictor() { | 704 chrome_browser_net::Predictor* TestingProfile::GetNetworkPredictor() { |
677 return NULL; | 705 return NULL; |
678 } | 706 } |
679 | 707 |
680 void TestingProfile::ClearNetworkingHistorySince(base::Time time) { | 708 void TestingProfile::ClearNetworkingHistorySince(base::Time time) { |
681 NOTIMPLEMENTED(); | 709 NOTIMPLEMENTED(); |
682 } | 710 } |
(...skipping 11 matching lines...) Expand all Loading... |
694 } | 722 } |
695 | 723 |
696 bool TestingProfile::WasCreatedByVersionOrLater(const std::string& version) { | 724 bool TestingProfile::WasCreatedByVersionOrLater(const std::string& version) { |
697 return true; | 725 return true; |
698 } | 726 } |
699 | 727 |
700 base::Callback<ChromeURLDataManagerBackend*(void)> | 728 base::Callback<ChromeURLDataManagerBackend*(void)> |
701 TestingProfile::GetChromeURLDataManagerBackendGetter() const { | 729 TestingProfile::GetChromeURLDataManagerBackendGetter() const { |
702 return base::Callback<ChromeURLDataManagerBackend*(void)>(); | 730 return base::Callback<ChromeURLDataManagerBackend*(void)>(); |
703 } | 731 } |
OLD | NEW |