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