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 199 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
238 chrome::NOTIFICATION_PROFILE_DESTROYED, | 240 chrome::NOTIFICATION_PROFILE_DESTROYED, |
239 content::Source<Profile>(static_cast<Profile*>(this)), | 241 content::Source<Profile>(static_cast<Profile*>(this)), |
240 content::NotificationService::NoDetails()); | 242 content::NotificationService::NoDetails()); |
241 | 243 |
242 profile_dependency_manager_->DestroyProfileServices(this); | 244 profile_dependency_manager_->DestroyProfileServices(this); |
243 | 245 |
244 if (host_content_settings_map_) | 246 if (host_content_settings_map_) |
245 host_content_settings_map_->ShutdownOnUIThread(); | 247 host_content_settings_map_->ShutdownOnUIThread(); |
246 | 248 |
247 DestroyTopSites(); | 249 DestroyTopSites(); |
248 DestroyHistoryService(); | |
249 // FaviconService depends on HistoryServce so destroying it later. | |
250 DestroyFaviconService(); | 250 DestroyFaviconService(); |
251 | 251 |
252 if (pref_proxy_config_tracker_.get()) | 252 if (pref_proxy_config_tracker_.get()) |
253 pref_proxy_config_tracker_->DetachFromPrefService(); | 253 pref_proxy_config_tracker_->DetachFromPrefService(); |
254 } | 254 } |
255 | 255 |
256 void TestingProfile::CreateFaviconService() { | 256 void TestingProfile::CreateFaviconService() { |
257 favicon_service_.reset(new FaviconService(this)); | 257 favicon_service_.reset(new FaviconService(this)); |
258 } | 258 } |
259 | 259 |
| 260 static scoped_refptr<RefcountedProfileKeyedService> BuildHistoryService( |
| 261 Profile* profile) { |
| 262 return new HistoryService(profile); |
| 263 } |
| 264 |
260 void TestingProfile::CreateHistoryService(bool delete_file, bool no_db) { | 265 void TestingProfile::CreateHistoryService(bool delete_file, bool no_db) { |
261 DestroyHistoryService(); | 266 DestroyHistoryService(); |
262 if (delete_file) { | 267 if (delete_file) { |
263 FilePath path = GetPath(); | 268 FilePath path = GetPath(); |
264 path = path.Append(chrome::kHistoryFilename); | 269 path = path.Append(chrome::kHistoryFilename); |
265 file_util::Delete(path, false); | 270 file_util::Delete(path, false); |
266 } | 271 } |
267 history_service_ = new HistoryService(this); | 272 // This will create and init the history service. |
268 history_service_->Init(GetPath(), bookmark_bar_model_.get(), no_db); | 273 HistoryService* history_service = static_cast<HistoryService*>( |
| 274 HistoryServiceFactory::GetInstance()->SetTestingFactoryAndUse( |
| 275 this, BuildHistoryService).get()); |
| 276 if (!history_service->Init(this->GetPath(), |
| 277 reinterpret_cast<BookmarkService*>( |
| 278 BookmarkModelFactory::GetForProfile(this)), |
| 279 no_db)) { |
| 280 HistoryServiceFactory::GetInstance()->SetTestingFactoryAndUse(this, NULL); |
| 281 } |
269 } | 282 } |
270 | 283 |
271 void TestingProfile::DestroyHistoryService() { | 284 void TestingProfile::DestroyHistoryService() { |
272 if (!history_service_.get()) | 285 scoped_refptr<HistoryService> history_service = |
| 286 HistoryServiceFactory::GetForProfileIfExists(this); |
| 287 if (!history_service.get()) |
273 return; | 288 return; |
274 | 289 |
275 history_service_->NotifyRenderProcessHostDestruction(0); | 290 history_service->NotifyRenderProcessHostDestruction(0); |
276 history_service_->SetOnBackendDestroyTask(MessageLoop::QuitClosure()); | 291 history_service->SetOnBackendDestroyTask(MessageLoop::QuitClosure()); |
277 history_service_->Cleanup(); | 292 history_service->Cleanup(); |
278 history_service_ = NULL; | 293 HistoryServiceFactory::ShutdownForProfile(this); |
279 | 294 |
280 // Wait for the backend class to terminate before deleting the files and | 295 // Wait for the backend class to terminate before deleting the files and |
281 // moving to the next test. Note: if this never terminates, somebody is | 296 // moving to the next test. Note: if this never terminates, somebody is |
282 // probably leaking a reference to the history backend, so it never calls | 297 // probably leaking a reference to the history backend, so it never calls |
283 // our destroy task. | 298 // our destroy task. |
284 MessageLoop::current()->Run(); | 299 MessageLoop::current()->Run(); |
285 | 300 |
286 // Make sure we don't have any event pending that could disrupt the next | 301 // Make sure we don't have any event pending that could disrupt the next |
287 // test. | 302 // test. |
288 MessageLoop::current()->PostTask(FROM_HERE, MessageLoop::QuitClosure()); | 303 MessageLoop::current()->PostTask(FROM_HERE, MessageLoop::QuitClosure()); |
(...skipping 16 matching lines...) Expand all Loading... |
305 // normally handled by browser_process shutdown. | 320 // normally handled by browser_process shutdown. |
306 if (MessageLoop::current()) | 321 if (MessageLoop::current()) |
307 MessageLoop::current()->RunAllPending(); | 322 MessageLoop::current()->RunAllPending(); |
308 } | 323 } |
309 } | 324 } |
310 | 325 |
311 void TestingProfile::DestroyFaviconService() { | 326 void TestingProfile::DestroyFaviconService() { |
312 favicon_service_.reset(); | 327 favicon_service_.reset(); |
313 } | 328 } |
314 | 329 |
| 330 static ProfileKeyedService* BuildBookmarkModel(Profile* profile) { |
| 331 BookmarkModel* bookmark_model = new BookmarkModel(profile); |
| 332 bookmark_model->Load(); |
| 333 return bookmark_model; |
| 334 } |
| 335 |
| 336 |
315 void TestingProfile::CreateBookmarkModel(bool delete_file) { | 337 void TestingProfile::CreateBookmarkModel(bool delete_file) { |
316 // Nuke the model first, that way we're sure it's done writing to disk. | |
317 bookmark_bar_model_.reset(NULL); | |
318 | 338 |
319 if (delete_file) { | 339 if (delete_file) { |
320 FilePath path = GetPath(); | 340 FilePath path = GetPath(); |
321 path = path.Append(chrome::kBookmarksFileName); | 341 path = path.Append(chrome::kBookmarksFileName); |
322 file_util::Delete(path, false); | 342 file_util::Delete(path, false); |
323 } | 343 } |
324 bookmark_bar_model_.reset(new BookmarkModel(this)); | 344 // This will create a bookmark model. |
325 if (history_service_.get()) { | 345 BookmarkModel* bookmark_service = |
326 history_service_->history_backend_->bookmark_service_ = | 346 static_cast<BookmarkModel*>( |
327 bookmark_bar_model_.get(); | 347 BookmarkModelFactory::GetInstance()->SetTestingFactoryAndUse( |
328 history_service_->history_backend_->expirer_.bookmark_service_ = | 348 this, BuildBookmarkModel)); |
329 bookmark_bar_model_.get(); | 349 |
| 350 HistoryService* history_service = |
| 351 HistoryServiceFactory::GetForProfileIfExists(this).get(); |
| 352 if (history_service) { |
| 353 history_service->history_backend_->bookmark_service_ = |
| 354 bookmark_service; |
| 355 history_service->history_backend_->expirer_.bookmark_service_ = |
| 356 bookmark_service; |
330 } | 357 } |
331 bookmark_bar_model_->Load(); | |
332 } | 358 } |
333 | 359 |
334 void TestingProfile::CreateAutocompleteClassifier() { | 360 void TestingProfile::CreateAutocompleteClassifier() { |
335 autocomplete_classifier_.reset(new AutocompleteClassifier(this)); | 361 autocomplete_classifier_.reset(new AutocompleteClassifier(this)); |
336 } | 362 } |
337 | 363 |
338 void TestingProfile::CreateProtocolHandlerRegistry() { | 364 void TestingProfile::CreateProtocolHandlerRegistry() { |
339 protocol_handler_registry_ = new ProtocolHandlerRegistry(this, | 365 protocol_handler_registry_ = new ProtocolHandlerRegistry(this, |
340 new ProtocolHandlerRegistry::Delegate()); | 366 new ProtocolHandlerRegistry::Delegate()); |
341 } | 367 } |
342 | 368 |
343 static scoped_refptr<RefcountedProfileKeyedService> BuildWebDataService( | 369 static scoped_refptr<RefcountedProfileKeyedService> BuildWebDataService( |
344 Profile* profile) { | 370 Profile* profile) { |
345 WebDataService* web_data_service = new WebDataService(); | 371 WebDataService* web_data_service = new WebDataService(); |
346 if (web_data_service) | 372 if (web_data_service) |
347 web_data_service->Init(profile->GetPath()); | 373 web_data_service->Init(profile->GetPath()); |
348 return web_data_service; | 374 return web_data_service; |
349 } | 375 } |
350 | 376 |
351 void TestingProfile::CreateWebDataService() { | 377 void TestingProfile::CreateWebDataService() { |
352 WebDataServiceFactory::GetInstance()->SetTestingFactory( | 378 WebDataServiceFactory::GetInstance()->SetTestingFactory( |
353 this, BuildWebDataService); | 379 this, BuildWebDataService); |
354 } | 380 } |
355 | 381 |
356 void TestingProfile::BlockUntilBookmarkModelLoaded() { | 382 void TestingProfile::BlockUntilBookmarkModelLoaded() { |
357 DCHECK(bookmark_bar_model_.get()); | 383 DCHECK(GetBookmarkModel()); |
358 if (bookmark_bar_model_->IsLoaded()) | 384 if (GetBookmarkModel()->IsLoaded()) |
359 return; | 385 return; |
360 BookmarkLoadObserver observer; | 386 BookmarkLoadObserver observer; |
361 bookmark_bar_model_->AddObserver(&observer); | 387 GetBookmarkModel()->AddObserver(&observer); |
362 MessageLoop::current()->Run(); | 388 MessageLoop::current()->Run(); |
363 bookmark_bar_model_->RemoveObserver(&observer); | 389 GetBookmarkModel()->RemoveObserver(&observer); |
364 DCHECK(bookmark_bar_model_->IsLoaded()); | 390 DCHECK(GetBookmarkModel()->IsLoaded()); |
365 } | 391 } |
366 | 392 |
367 // TODO(phajdan.jr): Doesn't this hang if Top Sites are already loaded? | 393 // TODO(phajdan.jr): Doesn't this hang if Top Sites are already loaded? |
368 void TestingProfile::BlockUntilTopSitesLoaded() { | 394 void TestingProfile::BlockUntilTopSitesLoaded() { |
369 ui_test_utils::WindowedNotificationObserver top_sites_loaded_observer( | 395 ui_test_utils::WindowedNotificationObserver top_sites_loaded_observer( |
370 chrome::NOTIFICATION_TOP_SITES_LOADED, | 396 chrome::NOTIFICATION_TOP_SITES_LOADED, |
371 content::NotificationService::AllSources()); | 397 content::NotificationService::AllSources()); |
372 if (!GetHistoryService(Profile::EXPLICIT_ACCESS)) | 398 if (!GetHistoryService(Profile::EXPLICIT_ACCESS)) |
373 GetTopSites()->HistoryLoaded(); | 399 GetTopSites()->HistoryLoaded(); |
374 top_sites_loaded_observer.Wait(); | 400 top_sites_loaded_observer.Wait(); |
(...skipping 94 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
469 if (!extension_special_storage_policy_.get()) | 495 if (!extension_special_storage_policy_.get()) |
470 extension_special_storage_policy_ = new ExtensionSpecialStoragePolicy(NULL); | 496 extension_special_storage_policy_ = new ExtensionSpecialStoragePolicy(NULL); |
471 return extension_special_storage_policy_.get(); | 497 return extension_special_storage_policy_.get(); |
472 } | 498 } |
473 | 499 |
474 FaviconService* TestingProfile::GetFaviconService(ServiceAccessType access) { | 500 FaviconService* TestingProfile::GetFaviconService(ServiceAccessType access) { |
475 return favicon_service_.get(); | 501 return favicon_service_.get(); |
476 } | 502 } |
477 | 503 |
478 HistoryService* TestingProfile::GetHistoryService(ServiceAccessType access) { | 504 HistoryService* TestingProfile::GetHistoryService(ServiceAccessType access) { |
479 return history_service_.get(); | 505 return HistoryServiceFactory::GetForProfileIfExists(this); |
480 } | 506 } |
481 | 507 |
482 HistoryService* TestingProfile::GetHistoryServiceWithoutCreating() { | 508 HistoryService* TestingProfile::GetHistoryServiceWithoutCreating() { |
483 return history_service_.get(); | 509 return HistoryServiceFactory::GetForProfileIfExists(this); |
484 } | 510 } |
485 | 511 |
486 net::CookieMonster* TestingProfile::GetCookieMonster() { | 512 net::CookieMonster* TestingProfile::GetCookieMonster() { |
487 if (!GetRequestContext()) | 513 if (!GetRequestContext()) |
488 return NULL; | 514 return NULL; |
489 return GetRequestContext()->GetURLRequestContext()->cookie_store()-> | 515 return GetRequestContext()->GetURLRequestContext()->cookie_store()-> |
490 GetCookieMonster(); | 516 GetCookieMonster(); |
491 } | 517 } |
492 | 518 |
493 AutocompleteClassifier* TestingProfile::GetAutocompleteClassifier() { | 519 AutocompleteClassifier* TestingProfile::GetAutocompleteClassifier() { |
(...skipping 151 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
645 | 671 |
646 void TestingProfile::SetID(const std::wstring& id) { | 672 void TestingProfile::SetID(const std::wstring& id) { |
647 id_ = id; | 673 id_ = id; |
648 } | 674 } |
649 | 675 |
650 bool TestingProfile::DidLastSessionExitCleanly() { | 676 bool TestingProfile::DidLastSessionExitCleanly() { |
651 return last_session_exited_cleanly_; | 677 return last_session_exited_cleanly_; |
652 } | 678 } |
653 | 679 |
654 BookmarkModel* TestingProfile::GetBookmarkModel() { | 680 BookmarkModel* TestingProfile::GetBookmarkModel() { |
655 return bookmark_bar_model_.get(); | 681 return BookmarkModelFactory::GetForProfileIfExists(this); |
656 } | 682 } |
657 | 683 |
658 bool TestingProfile::IsSameProfile(Profile *p) { | 684 bool TestingProfile::IsSameProfile(Profile *p) { |
659 return this == p; | 685 return this == p; |
660 } | 686 } |
661 | 687 |
662 base::Time TestingProfile::GetStartTime() const { | 688 base::Time TestingProfile::GetStartTime() const { |
663 return start_time_; | 689 return start_time_; |
664 } | 690 } |
665 | 691 |
(...skipping 11 matching lines...) Expand all Loading... |
677 | 703 |
678 PrefProxyConfigTracker* TestingProfile::GetProxyConfigTracker() { | 704 PrefProxyConfigTracker* TestingProfile::GetProxyConfigTracker() { |
679 if (!pref_proxy_config_tracker_.get()) { | 705 if (!pref_proxy_config_tracker_.get()) { |
680 pref_proxy_config_tracker_.reset( | 706 pref_proxy_config_tracker_.reset( |
681 ProxyServiceFactory::CreatePrefProxyConfigTracker(GetPrefs())); | 707 ProxyServiceFactory::CreatePrefProxyConfigTracker(GetPrefs())); |
682 } | 708 } |
683 return pref_proxy_config_tracker_.get(); | 709 return pref_proxy_config_tracker_.get(); |
684 } | 710 } |
685 | 711 |
686 void TestingProfile::BlockUntilHistoryProcessesPendingRequests() { | 712 void TestingProfile::BlockUntilHistoryProcessesPendingRequests() { |
687 DCHECK(history_service_.get()); | 713 scoped_refptr<HistoryService> history_service = |
| 714 HistoryServiceFactory::GetForProfileIfExists(this); |
| 715 DCHECK(history_service.get()); |
688 DCHECK(MessageLoop::current()); | 716 DCHECK(MessageLoop::current()); |
689 | 717 |
690 CancelableRequestConsumer consumer; | 718 CancelableRequestConsumer consumer; |
691 history_service_->ScheduleDBTask(new QuittingHistoryDBTask(), &consumer); | 719 history_service->ScheduleDBTask(new QuittingHistoryDBTask(), &consumer); |
692 MessageLoop::current()->Run(); | 720 MessageLoop::current()->Run(); |
693 } | 721 } |
694 | 722 |
695 chrome_browser_net::Predictor* TestingProfile::GetNetworkPredictor() { | 723 chrome_browser_net::Predictor* TestingProfile::GetNetworkPredictor() { |
696 return NULL; | 724 return NULL; |
697 } | 725 } |
698 | 726 |
699 void TestingProfile::ClearNetworkingHistorySince(base::Time time) { | 727 void TestingProfile::ClearNetworkingHistorySince(base::Time time) { |
700 NOTIMPLEMENTED(); | 728 NOTIMPLEMENTED(); |
701 } | 729 } |
(...skipping 11 matching lines...) Expand all Loading... |
713 } | 741 } |
714 | 742 |
715 bool TestingProfile::WasCreatedByVersionOrLater(const std::string& version) { | 743 bool TestingProfile::WasCreatedByVersionOrLater(const std::string& version) { |
716 return true; | 744 return true; |
717 } | 745 } |
718 | 746 |
719 base::Callback<ChromeURLDataManagerBackend*(void)> | 747 base::Callback<ChromeURLDataManagerBackend*(void)> |
720 TestingProfile::GetChromeURLDataManagerBackendGetter() const { | 748 TestingProfile::GetChromeURLDataManagerBackendGetter() const { |
721 return base::Callback<ChromeURLDataManagerBackend*(void)>(); | 749 return base::Callback<ChromeURLDataManagerBackend*(void)>(); |
722 } | 750 } |
OLD | NEW |