arc_browser_context_keyed_service_factory_base.h 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. // Copyright 2017 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 ASH_COMPONENTS_ARC_ARC_BROWSER_CONTEXT_KEYED_SERVICE_FACTORY_BASE_H_
  5. #define ASH_COMPONENTS_ARC_ARC_BROWSER_CONTEXT_KEYED_SERVICE_FACTORY_BASE_H_
  6. #include <memory>
  7. #include "ash/components/arc/session/arc_service_manager.h"
  8. #include "base/bind.h"
  9. #include "base/logging.h"
  10. #include "components/keyed_service/content/browser_context_dependency_manager.h"
  11. #include "components/keyed_service/content/browser_context_keyed_service_factory.h"
  12. namespace content {
  13. class BrowserContext;
  14. } // namespace content
  15. namespace arc {
  16. namespace internal {
  17. // Implementation of BrowserContextKeyedServiceFactory for ARC related
  18. // services. The ARC related BrowserContextKeyedService can make its factory
  19. // class derived from this class.
  20. //
  21. // How to use:
  22. // In .h:
  23. // #include "components/keyed_service/core/keyed_service.h"
  24. //
  25. // namespace content {
  26. // class BrowserContext;
  27. // } // namespace content
  28. //
  29. // class ArcFooService : public KeyedService, ... {
  30. // public:
  31. // static ArcFooService* GetForBrowserContext(
  32. // content::BrowserContext* context);
  33. //
  34. // ArcFooService(content::BrowserContext* context,
  35. // ArcBridgeService* arc_bridge_service);
  36. // };
  37. //
  38. // In .cc:
  39. //
  40. // namespace {
  41. // class ArcFooServiceFactory
  42. // : public internal::ArcBrowserContextKeyedServiceFactoryBase<
  43. // ArcFooService,
  44. // ArcFooServiceFactory> {
  45. // public:
  46. // static constexpr const char* kName = "ArcFooServiceFactory";
  47. //
  48. // static ArcFooServiceFactory* GetInstance() {
  49. // return base::Singleton<ArcFooServiceFactory>::get();
  50. // }
  51. //
  52. // private:
  53. // friend struct base::DefaultSingletonTraits<ArcFooServiceFactory>;
  54. // ArcFooServiceFactory() = default;
  55. // ~ArcFooServiceFactory() override = default;
  56. // };
  57. // } // namespace
  58. //
  59. // ArcFooService* ArcFooService::GetForBrowserContext(
  60. // content::BrowserContext* context) {
  61. // return ArcFooServiceFactory::GetForBrowserContext(context);
  62. // }
  63. //
  64. // If the service depends on other KeyedService, DependsOn() can be called in
  65. // the factory's ctor similar to other BrowserContextKeyedServiceFactory
  66. // subclasses.
  67. //
  68. // This header is intended to be included only from the .cc file directly.
  69. //
  70. // TODO(hidehiko): Make ArcFooService constructor (and maybe destructor)
  71. // private with declaring appropriate friend.
  72. template <typename Service, typename Factory>
  73. class ArcBrowserContextKeyedServiceFactoryBase
  74. : public BrowserContextKeyedServiceFactory {
  75. public:
  76. // Returns the instance of the service for the given |context|,
  77. // or nullptr if |context| is not allowed to use ARC.
  78. // If the instance is not yet created, this creates a new service instance.
  79. static Service* GetForBrowserContext(content::BrowserContext* context) {
  80. return static_cast<Service*>(
  81. Factory::GetInstance()->GetServiceForBrowserContext(context,
  82. true /* create */));
  83. }
  84. // Does the same as GetForBrowserContext() but for testing. This should be
  85. // called from the |Service|'s unit test's fixture to instantiate the service.
  86. // Note that this function does not check whether or not |context| is for the
  87. // primary user. Also, ArcServiceManager has to be instantiated before calling
  88. // this function.
  89. static Service* GetForBrowserContextForTesting(
  90. content::BrowserContext* context) {
  91. Factory::GetInstance()->SetTestingFactoryAndUse(
  92. context, base::BindRepeating([](content::BrowserContext* context)
  93. -> std::unique_ptr<KeyedService> {
  94. return std::make_unique<Service>(
  95. context, ArcServiceManager::Get()->arc_bridge_service());
  96. }));
  97. return GetForBrowserContext(context);
  98. }
  99. ArcBrowserContextKeyedServiceFactoryBase(
  100. const ArcBrowserContextKeyedServiceFactoryBase&) = delete;
  101. ArcBrowserContextKeyedServiceFactoryBase& operator=(
  102. const ArcBrowserContextKeyedServiceFactoryBase&) = delete;
  103. protected:
  104. ArcBrowserContextKeyedServiceFactoryBase()
  105. : BrowserContextKeyedServiceFactory(
  106. Factory::kName,
  107. BrowserContextDependencyManager::GetInstance()) {}
  108. ~ArcBrowserContextKeyedServiceFactoryBase() override = default;
  109. // BrowserContextKeyedServiceFactory override:
  110. KeyedService* BuildServiceInstanceFor(
  111. content::BrowserContext* context) const override {
  112. auto* arc_service_manager = arc::ArcServiceManager::Get();
  113. // Practically, this is in testing case.
  114. if (!arc_service_manager) {
  115. VLOG(2) << "ArcServiceManager is not available.";
  116. return nullptr;
  117. }
  118. if (arc_service_manager->browser_context() != context) {
  119. VLOG(2) << "Non ARC allowed browser context.";
  120. return nullptr;
  121. }
  122. return new Service(context, arc_service_manager->arc_bridge_service());
  123. }
  124. };
  125. } // namespace internal
  126. } // namespace arc
  127. #endif // ASH_COMPONENTS_ARC_ARC_BROWSER_CONTEXT_KEYED_SERVICE_FACTORY_BASE_H_