/extensions/browser/browser_context_keyed_api_factory.h

https://gitlab.com/0072016/Facebook-SDK- · C Header · 142 lines · 62 code · 23 blank · 57 comment · 2 complexity · 7ba0e4bf084330d2bc520b3ad718094b MD5 · raw file

  1. // Copyright 2014 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. #ifndef EXTENSIONS_BROWSER_BROWSER_CONTEXT_KEYED_API_FACTORY_H_
  5. #define EXTENSIONS_BROWSER_BROWSER_CONTEXT_KEYED_API_FACTORY_H_
  6. #include "base/macros.h"
  7. #include "components/keyed_service/content/browser_context_dependency_manager.h"
  8. #include "components/keyed_service/content/browser_context_keyed_service_factory.h"
  9. #include "components/keyed_service/core/keyed_service.h"
  10. #include "extensions/browser/extension_system_provider.h"
  11. #include "extensions/browser/extensions_browser_client.h"
  12. namespace extensions {
  13. template <typename T>
  14. class BrowserContextKeyedAPIFactory;
  15. // Instantiations of BrowserContextKeyedAPIFactory should use this base class
  16. // and also define a static const char* service_name() function (used in the
  17. // BrowserContextKeyedBaseFactory constructor). These fields should
  18. // be accessible to the BrowserContextKeyedAPIFactory for the service.
  19. class BrowserContextKeyedAPI : public KeyedService {
  20. protected:
  21. // Defaults for flags that control BrowserContextKeyedAPIFactory behavior.
  22. // These can be overridden by subclasses to change that behavior.
  23. // See BrowserContextKeyedBaseFactory for usage.
  24. // These flags affect what instance is returned when Get() is called
  25. // on an incognito profile. By default, it returns NULL. If
  26. // kServiceRedirectedInIncognito is true, it returns the instance for the
  27. // corresponding regular profile. If kServiceHasOwnInstanceInIncognito
  28. // is true, it returns a separate instance.
  29. static const bool kServiceRedirectedInIncognito = false;
  30. static const bool kServiceHasOwnInstanceInIncognito = false;
  31. // If set to false, don't start the service at BrowserContext creation time.
  32. // (The default differs from the BrowserContextKeyedBaseFactory default,
  33. // because historically, BrowserContextKeyedAPIs often do tasks at startup.)
  34. static const bool kServiceIsCreatedWithBrowserContext = true;
  35. // If set to true, GetForProfile returns NULL for TestingBrowserContexts.
  36. static const bool kServiceIsNULLWhileTesting = false;
  37. // Users of this factory template must define a GetFactoryInstance()
  38. // and manage their own instances (using LazyInstance), because those cannot
  39. // be included in more than one translation unit (and thus cannot be
  40. // initialized in a header file).
  41. //
  42. // In the header file, declare GetFactoryInstance(), e.g.:
  43. // class HistoryAPI {
  44. // ...
  45. // public:
  46. // static BrowserContextKeyedAPIFactory<HistoryAPI>* GetFactoryInstance();
  47. // };
  48. //
  49. // In the cc file, provide the implementation, e.g.:
  50. // static base::LazyInstance<BrowserContextKeyedAPIFactory<HistoryAPI> >
  51. // g_factory = LAZY_INSTANCE_INITIALIZER;
  52. //
  53. // // static
  54. // BrowserContextKeyedAPIFactory<HistoryAPI>*
  55. // HistoryAPI::GetFactoryInstance() {
  56. // return g_factory.Pointer();
  57. // }
  58. };
  59. // A template for factories for KeyedServices that manage extension APIs. T is
  60. // a KeyedService that uses this factory template instead of its own separate
  61. // factory definition to manage its per-profile instances.
  62. template <typename T>
  63. class BrowserContextKeyedAPIFactory : public BrowserContextKeyedServiceFactory {
  64. public:
  65. static T* Get(content::BrowserContext* context) {
  66. return static_cast<T*>(
  67. T::GetFactoryInstance()->GetServiceForBrowserContext(context, true));
  68. }
  69. static T* GetIfExists(content::BrowserContext* context) {
  70. return static_cast<T*>(
  71. T::GetFactoryInstance()->GetServiceForBrowserContext(context, false));
  72. }
  73. // Declare dependencies on other factories.
  74. // By default, ExtensionSystemFactory is the only dependency; however,
  75. // specializations can override this. Declare your specialization in
  76. // your header file after the BrowserContextKeyedAPI class definition.
  77. // Then in the cc file (or inline in the header), define it, e.g.:
  78. // template <>
  79. // void BrowserContextKeyedAPIFactory<
  80. // PushMessagingAPI>::DeclareFactoryDependencies() {
  81. // DependsOn(ExtensionsBrowserClient::Get()->GetExtensionSystemFactory());
  82. // DependsOn(ProfileSyncServiceFactory::GetInstance());
  83. // }
  84. void DeclareFactoryDependencies() {
  85. DependsOn(ExtensionsBrowserClient::Get()->GetExtensionSystemFactory());
  86. }
  87. BrowserContextKeyedAPIFactory()
  88. : BrowserContextKeyedServiceFactory(
  89. T::service_name(),
  90. BrowserContextDependencyManager::GetInstance()) {
  91. DeclareFactoryDependencies();
  92. }
  93. ~BrowserContextKeyedAPIFactory() override {}
  94. private:
  95. // BrowserContextKeyedServiceFactory implementation.
  96. KeyedService* BuildServiceInstanceFor(
  97. content::BrowserContext* context) const override {
  98. return new T(context);
  99. }
  100. // BrowserContextKeyedBaseFactory implementation.
  101. // These can be effectively overridden with template specializations.
  102. content::BrowserContext* GetBrowserContextToUse(
  103. content::BrowserContext* context) const override {
  104. if (T::kServiceRedirectedInIncognito)
  105. return ExtensionsBrowserClient::Get()->GetOriginalContext(context);
  106. if (T::kServiceHasOwnInstanceInIncognito)
  107. return context;
  108. return BrowserContextKeyedServiceFactory::GetBrowserContextToUse(context);
  109. }
  110. bool ServiceIsCreatedWithBrowserContext() const override {
  111. return T::kServiceIsCreatedWithBrowserContext;
  112. }
  113. bool ServiceIsNULLWhileTesting() const override {
  114. return T::kServiceIsNULLWhileTesting;
  115. }
  116. DISALLOW_COPY_AND_ASSIGN(BrowserContextKeyedAPIFactory);
  117. };
  118. } // namespace extensions
  119. #endif // EXTENSIONS_BROWSER_BROWSER_CONTEXT_KEYED_API_FACTORY_H_