OLD | NEW |
(Empty) | |
| 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 |
| 5 #ifndef COMPONENTS_KEYED_SERVICE_CORE_KEYED_SERVICE_FACTORY_H_ |
| 6 #define COMPONENTS_KEYED_SERVICE_CORE_KEYED_SERVICE_FACTORY_H_ |
| 7 |
| 8 #include <map> |
| 9 |
| 10 #include "base/basictypes.h" |
| 11 #include "base/callback_forward.h" |
| 12 #include "base/compiler_specific.h" |
| 13 #include "components/keyed_service/core/keyed_service_base_factory.h" |
| 14 #include "components/keyed_service/core/keyed_service_export.h" |
| 15 |
| 16 class DependencyManager; |
| 17 class KeyedService; |
| 18 |
| 19 // Base class for Factories that take a base::SupportsUserData object and return |
| 20 // some service on a one-to-one mapping. Each concrete factory that derives from |
| 21 // this class *must* be a Singleton (only unit tests don't do that). |
| 22 // |
| 23 // We do this because services depend on each other and we need to control |
| 24 // shutdown/destruction order. In each derived classes' constructors, the |
| 25 // implementors must explicitly state which services are depended on. |
| 26 class KEYED_SERVICE_EXPORT KeyedServiceFactory |
| 27 : public KeyedServiceBaseFactory { |
| 28 protected: |
| 29 KeyedServiceFactory(const char* name, DependencyManager* manager); |
| 30 ~KeyedServiceFactory() override; |
| 31 |
| 32 // A function that supplies the instance of a KeyedService for a given |
| 33 // |context|. This is used primarily for testing, where we want to feed |
| 34 // a specific mock into the KeyedServiceFactory system. |
| 35 typedef KeyedService* (*TestingFactoryFunction)( |
| 36 base::SupportsUserData* context); |
| 37 |
| 38 // Associates |factory| with |context| so that |factory| is used to create |
| 39 // the KeyedService when requested. |factory| can be NULL to signal that |
| 40 // KeyedService should be NULL. Multiple calls to SetTestingFactory() are |
| 41 // allowed; previous services will be shut down. |
| 42 void SetTestingFactory(base::SupportsUserData* context, |
| 43 TestingFactoryFunction factory); |
| 44 |
| 45 // Associates |factory| with |context| and immediately returns the created |
| 46 // KeyedService. Since the factory will be used immediately, it may not be |
| 47 // NULL. |
| 48 KeyedService* SetTestingFactoryAndUse(base::SupportsUserData* context, |
| 49 TestingFactoryFunction factory); |
| 50 |
| 51 // Common implementation that maps |context| to some service object. Deals |
| 52 // with incognito contexts per subclass instructions with GetContextToUse() |
| 53 // method on the base. If |create| is true, the service will be created |
| 54 // using BuildServiceInstanceFor() if it doesn't already exist. |
| 55 KeyedService* GetServiceForContext(base::SupportsUserData* context, |
| 56 bool create); |
| 57 |
| 58 // Maps |context| to |service| with debug checks to prevent duplication. |
| 59 void Associate(base::SupportsUserData* context, KeyedService* service); |
| 60 |
| 61 // Removes the mapping from |context| to a service. |
| 62 void Disassociate(base::SupportsUserData* context); |
| 63 |
| 64 // Returns a new KeyedService that will be associated with |context|. |
| 65 virtual KeyedService* BuildServiceInstanceFor( |
| 66 base::SupportsUserData* context) const = 0; |
| 67 |
| 68 // Returns whether the |context| is off-the-record or not. |
| 69 virtual bool IsOffTheRecord(base::SupportsUserData* context) const = 0; |
| 70 |
| 71 // KeyedServiceBaseFactory: |
| 72 void ContextShutdown(base::SupportsUserData* context) override; |
| 73 void ContextDestroyed(base::SupportsUserData* context) override; |
| 74 |
| 75 void SetEmptyTestingFactory(base::SupportsUserData* context) override; |
| 76 bool HasTestingFactory(base::SupportsUserData* context) override; |
| 77 void CreateServiceNow(base::SupportsUserData* context) override; |
| 78 |
| 79 private: |
| 80 friend class DependencyManager; |
| 81 friend class DependencyManagerUnittests; |
| 82 |
| 83 typedef std::map<base::SupportsUserData*, KeyedService*> KeyedServices; |
| 84 typedef std::map<base::SupportsUserData*, TestingFactoryFunction> |
| 85 OverriddenTestingFunctions; |
| 86 |
| 87 // The mapping between a context and its service. |
| 88 KeyedServices mapping_; |
| 89 |
| 90 // The mapping between a context and its overridden |
| 91 // TestingFactoryFunction. |
| 92 OverriddenTestingFunctions testing_factories_; |
| 93 |
| 94 DISALLOW_COPY_AND_ASSIGN(KeyedServiceFactory); |
| 95 }; |
| 96 |
| 97 #endif // COMPONENTS_KEYED_SERVICE_CORE_KEYED_SERVICE_FACTORY_H_ |
OLD | NEW |