/chrome/browser/autocomplete/shortcuts_backend_factory.cc

https://gitlab.com/jonnialva90/iridium-browser · C++ · 105 lines · 81 code · 15 blank · 9 comment · 0 complexity · 77dc18d3fb0bfec09600f1acf4b115ca 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/autocomplete/shortcuts_backend_factory.h"
  5. #include "base/memory/scoped_ptr.h"
  6. #include "base/prefs/pref_service.h"
  7. #include "chrome/browser/autocomplete/shortcuts_extensions_manager.h"
  8. #include "chrome/browser/history/history_service_factory.h"
  9. #include "chrome/browser/profiles/profile.h"
  10. #include "chrome/browser/search_engines/template_url_service_factory.h"
  11. #include "chrome/browser/search_engines/ui_thread_search_terms_data.h"
  12. #include "components/keyed_service/content/browser_context_dependency_manager.h"
  13. #include "components/omnibox/browser/shortcuts_backend.h"
  14. #include "components/omnibox/browser/shortcuts_constants.h"
  15. #include "content/public/browser/browser_thread.h"
  16. namespace {
  17. #if defined(ENABLE_EXTENSIONS)
  18. const char kShortcutsExtensionsManagerKey[] = "ShortcutsExtensionsManager";
  19. #endif
  20. }
  21. // static
  22. scoped_refptr<ShortcutsBackend> ShortcutsBackendFactory::GetForProfile(
  23. Profile* profile) {
  24. return static_cast<ShortcutsBackend*>(
  25. GetInstance()->GetServiceForBrowserContext(profile, true).get());
  26. }
  27. // static
  28. scoped_refptr<ShortcutsBackend> ShortcutsBackendFactory::GetForProfileIfExists(
  29. Profile* profile) {
  30. return static_cast<ShortcutsBackend*>(
  31. GetInstance()->GetServiceForBrowserContext(profile, false).get());
  32. }
  33. // static
  34. ShortcutsBackendFactory* ShortcutsBackendFactory::GetInstance() {
  35. return base::Singleton<ShortcutsBackendFactory>::get();
  36. }
  37. // static
  38. scoped_refptr<RefcountedKeyedService>
  39. ShortcutsBackendFactory::BuildProfileForTesting(
  40. content::BrowserContext* profile) {
  41. return CreateShortcutsBackend(Profile::FromBrowserContext(profile), false);
  42. }
  43. // static
  44. scoped_refptr<RefcountedKeyedService>
  45. ShortcutsBackendFactory::BuildProfileNoDatabaseForTesting(
  46. content::BrowserContext* profile) {
  47. return CreateShortcutsBackend(Profile::FromBrowserContext(profile), true);
  48. }
  49. ShortcutsBackendFactory::ShortcutsBackendFactory()
  50. : RefcountedBrowserContextKeyedServiceFactory(
  51. "ShortcutsBackend",
  52. BrowserContextDependencyManager::GetInstance()) {
  53. DependsOn(HistoryServiceFactory::GetInstance());
  54. DependsOn(TemplateURLServiceFactory::GetInstance());
  55. }
  56. ShortcutsBackendFactory::~ShortcutsBackendFactory() {}
  57. scoped_refptr<RefcountedKeyedService>
  58. ShortcutsBackendFactory::BuildServiceInstanceFor(
  59. content::BrowserContext* profile) const {
  60. return CreateShortcutsBackend(Profile::FromBrowserContext(profile), false);
  61. }
  62. bool ShortcutsBackendFactory::ServiceIsNULLWhileTesting() const {
  63. return true;
  64. }
  65. void ShortcutsBackendFactory::BrowserContextShutdown(
  66. content::BrowserContext* context) {
  67. #if defined(ENABLE_EXTENSIONS)
  68. context->RemoveUserData(kShortcutsExtensionsManagerKey);
  69. #endif
  70. RefcountedBrowserContextKeyedServiceFactory::BrowserContextShutdown(context);
  71. }
  72. // static
  73. scoped_refptr<ShortcutsBackend> ShortcutsBackendFactory::CreateShortcutsBackend(
  74. Profile* profile,
  75. bool suppress_db) {
  76. scoped_refptr<ShortcutsBackend> backend(new ShortcutsBackend(
  77. TemplateURLServiceFactory::GetForProfile(profile),
  78. make_scoped_ptr(new UIThreadSearchTermsData(profile)),
  79. HistoryServiceFactory::GetForProfile(profile,
  80. ServiceAccessType::EXPLICIT_ACCESS),
  81. content::BrowserThread::GetMessageLoopProxyForThread(
  82. content::BrowserThread::DB),
  83. profile->GetPath().Append(kShortcutsDatabaseName), suppress_db));
  84. #if defined(ENABLE_EXTENSIONS)
  85. ShortcutsExtensionsManager* extensions_manager =
  86. new ShortcutsExtensionsManager(profile);
  87. profile->SetUserData(kShortcutsExtensionsManagerKey, extensions_manager);
  88. #endif
  89. return backend->Init() ? backend : nullptr;
  90. }