/chrome/browser/history/history_service_factory.cc

https://gitlab.com/jonnialva90/iridium-browser · C++ · 100 lines · 76 code · 14 blank · 10 comment · 5 complexity · d8945b1e259c8bde5ef32c07b8186492 MD5 · raw file

  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. #include "chrome/browser/history/history_service_factory.h"
  5. #include "base/prefs/pref_service.h"
  6. #include "chrome/browser/bookmarks/bookmark_model_factory.h"
  7. #include "chrome/browser/history/chrome_history_client.h"
  8. #include "chrome/browser/profiles/incognito_helpers.h"
  9. #include "chrome/browser/profiles/profile.h"
  10. #include "chrome/common/pref_names.h"
  11. #include "components/bookmarks/browser/bookmark_model.h"
  12. #include "components/history/content/browser/content_visit_delegate.h"
  13. #include "components/history/content/browser/history_database_helper.h"
  14. #include "components/history/core/browser/history_database_params.h"
  15. #include "components/history/core/browser/history_service.h"
  16. #include "components/keyed_service/content/browser_context_dependency_manager.h"
  17. #include "components/keyed_service/core/service_access_type.h"
  18. // static
  19. history::HistoryService* HistoryServiceFactory::GetForProfile(
  20. Profile* profile,
  21. ServiceAccessType sat) {
  22. // If saving history is disabled, only allow explicit access.
  23. if (sat != ServiceAccessType::EXPLICIT_ACCESS &&
  24. profile->GetPrefs()->GetBoolean(prefs::kSavingBrowserHistoryDisabled)) {
  25. return nullptr;
  26. }
  27. return static_cast<history::HistoryService*>(
  28. GetInstance()->GetServiceForBrowserContext(profile, true));
  29. }
  30. // static
  31. history::HistoryService* HistoryServiceFactory::GetForProfileIfExists(
  32. Profile* profile,
  33. ServiceAccessType sat) {
  34. // If saving history is disabled, only allow explicit access.
  35. if (sat != ServiceAccessType::EXPLICIT_ACCESS &&
  36. profile->GetPrefs()->GetBoolean(prefs::kSavingBrowserHistoryDisabled)) {
  37. return nullptr;
  38. }
  39. return static_cast<history::HistoryService*>(
  40. GetInstance()->GetServiceForBrowserContext(profile, false));
  41. }
  42. // static
  43. history::HistoryService* HistoryServiceFactory::GetForProfileWithoutCreating(
  44. Profile* profile) {
  45. return static_cast<history::HistoryService*>(
  46. GetInstance()->GetServiceForBrowserContext(profile, false));
  47. }
  48. // static
  49. HistoryServiceFactory* HistoryServiceFactory::GetInstance() {
  50. return base::Singleton<HistoryServiceFactory>::get();
  51. }
  52. // static
  53. void HistoryServiceFactory::ShutdownForProfile(Profile* profile) {
  54. HistoryServiceFactory* factory = GetInstance();
  55. factory->BrowserContextDestroyed(profile);
  56. }
  57. HistoryServiceFactory::HistoryServiceFactory()
  58. : BrowserContextKeyedServiceFactory(
  59. "HistoryService",
  60. BrowserContextDependencyManager::GetInstance()) {
  61. DependsOn(BookmarkModelFactory::GetInstance());
  62. }
  63. HistoryServiceFactory::~HistoryServiceFactory() {
  64. }
  65. KeyedService* HistoryServiceFactory::BuildServiceInstanceFor(
  66. content::BrowserContext* context) const {
  67. Profile* profile = Profile::FromBrowserContext(context);
  68. scoped_ptr<history::HistoryService> history_service(
  69. new history::HistoryService(
  70. make_scoped_ptr(new ChromeHistoryClient(
  71. BookmarkModelFactory::GetForProfile(profile))),
  72. make_scoped_ptr(new history::ContentVisitDelegate(profile))));
  73. if (!history_service->Init(
  74. profile->GetPrefs()->GetString(prefs::kAcceptLanguages),
  75. history::HistoryDatabaseParamsForPath(profile->GetPath()))) {
  76. return nullptr;
  77. }
  78. return history_service.release();
  79. }
  80. content::BrowserContext* HistoryServiceFactory::GetBrowserContextToUse(
  81. content::BrowserContext* context) const {
  82. return chrome::GetBrowserContextRedirectedInIncognito(context);
  83. }
  84. bool HistoryServiceFactory::ServiceIsNULLWhileTesting() const {
  85. return true;
  86. }