OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 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_BROWSER_CONTEXT_KEYED_SERVICE_BROWSER_CONTEXT_KEYED_SERVICE_F
ACTORY_H_ | 5 #ifndef COMPONENTS_BROWSER_CONTEXT_KEYED_SERVICE_BROWSER_CONTEXT_KEYED_SERVICE_F
ACTORY_H_ |
6 #define COMPONENTS_BROWSER_CONTEXT_KEYED_SERVICE_BROWSER_CONTEXT_KEYED_SERVICE_F
ACTORY_H_ | 6 #define COMPONENTS_BROWSER_CONTEXT_KEYED_SERVICE_BROWSER_CONTEXT_KEYED_SERVICE_F
ACTORY_H_ |
7 | 7 |
8 #include <map> | 8 #include <map> |
9 | 9 |
10 #include "base/basictypes.h" | 10 #include "base/basictypes.h" |
11 #include "base/compiler_specific.h" | 11 #include "base/compiler_specific.h" |
12 #include "components/browser_context_keyed_service/browser_context_keyed_base_fa
ctory.h" | 12 #include "components/browser_context_keyed_service/browser_context_keyed_base_fa
ctory.h" |
13 #include "components/browser_context_keyed_service/browser_context_keyed_service
.h" | 13 #include "components/browser_context_keyed_service/browser_context_keyed_service
.h" |
14 | 14 |
15 class ProfileDependencyManager; | 15 class BrowserContextDependencyManager; |
16 class ProfileKeyedService; | 16 class BrowserContextKeyedService; |
17 | 17 |
18 // Base class for Factories that take a Profile object and return some service | 18 // Base class for Factories that take a BrowserContext object and return some |
19 // on a one-to-one mapping. Each factory that derives from this class *must* | 19 // service on a one-to-one mapping. Each factory that derives from this class |
20 // be a Singleton (only unit tests don't do that). See ThemeServiceFactory as | 20 // *must* be a Singleton (only unit tests don't do that). |
21 // an example of how to derive from this class. | |
22 // | 21 // |
23 // We do this because services depend on each other and we need to control | 22 // We do this because services depend on each other and we need to control |
24 // shutdown/destruction order. In each derived classes' constructors, the | 23 // shutdown/destruction order. In each derived classes' constructors, the |
25 // implementors must explicitly state which services are depended on. | 24 // implementors must explicitly state which services are depended on. |
26 class ProfileKeyedServiceFactory : public ProfileKeyedBaseFactory { | 25 class BrowserContextKeyedServiceFactory |
| 26 : public BrowserContextKeyedBaseFactory { |
27 public: | 27 public: |
28 // A function that supplies the instance of a ProfileKeyedService for a given | 28 // A function that supplies the instance of a BrowserContextKeyedService |
29 // Profile. This is used primarily for testing, where we want to feed a | 29 // for a given BrowserContext. This is used primarily for testing, where |
30 // specific mock into the PKSF system. | 30 // we want to feed a specific mock into the BCKSF system. |
31 typedef ProfileKeyedService* | 31 typedef BrowserContextKeyedService* |
32 (*FactoryFunction)(content::BrowserContext* profile); | 32 (*FactoryFunction)(content::BrowserContext* context); |
33 | 33 |
34 // Associates |factory| with |profile| so that |factory| is used to create | 34 // Associates |factory| with |context| so that |factory| is used to create |
35 // the ProfileKeyedService when requested. |factory| can be NULL to signal | 35 // the BrowserContextKeyedService when requested. |factory| can be NULL |
36 // that ProfileKeyedService should be NULL. Multiple calls to | 36 // to signal that BrowserContextKeyedService should be NULL. Multiple calls to |
37 // SetTestingFactory() are allowed; previous services will be shut down. | 37 // SetTestingFactory() are allowed; previous services will be shut down. |
38 void SetTestingFactory(content::BrowserContext* profile, | 38 void SetTestingFactory(content::BrowserContext* context, |
39 FactoryFunction factory); | 39 FactoryFunction factory); |
40 | 40 |
41 // Associates |factory| with |profile| and immediately returns the created | 41 // Associates |factory| with |context| and immediately returns the created |
42 // ProfileKeyedService. Since the factory will be used immediately, it may | 42 // BrowserContextKeyedService. Since the factory will be used immediately, |
43 // not be NULL. | 43 // it may not be NULL. |
44 ProfileKeyedService* SetTestingFactoryAndUse(content::BrowserContext* profile, | 44 BrowserContextKeyedService* SetTestingFactoryAndUse( |
45 FactoryFunction factory); | 45 content::BrowserContext* context, |
| 46 FactoryFunction factory); |
46 | 47 |
47 protected: | 48 protected: |
48 // ProfileKeyedServiceFactories must communicate with a | 49 // BrowserContextKeyedServiceFactories must communicate with a |
49 // ProfileDependencyManager. For all non-test code, write your subclass | 50 // BrowserContextDependencyManager. For all non-test code, write your subclass |
50 // constructors like this: | 51 // constructors like this: |
51 // | 52 // |
52 // MyServiceFactory::MyServiceFactory() | 53 // MyServiceFactory::MyServiceFactory() |
53 // : ProfileKeyedServiceFactory( | 54 // : BrowserContextKeyedServiceFactory( |
54 // "MyService", | 55 // "MyService", |
55 // ProfileDependencyManager::GetInstance()) | 56 // BrowserContextDependencyManager::GetInstance()) |
56 // {} | 57 // {} |
57 ProfileKeyedServiceFactory(const char* name, | 58 BrowserContextKeyedServiceFactory(const char* name, |
58 ProfileDependencyManager* manager); | 59 BrowserContextDependencyManager* manager); |
59 virtual ~ProfileKeyedServiceFactory(); | 60 virtual ~BrowserContextKeyedServiceFactory(); |
60 | 61 |
61 // Common implementation that maps |profile| to some service object. Deals | 62 // Common implementation that maps |context| to some service object. Deals |
62 // with incognito profiles per subclass instructions with | 63 // with incognito contexts per subclass instructions with |
63 // ServiceRedirectedInIncognito() and ServiceHasOwnInstanceInIncognito() | 64 // ServiceRedirectedInIncognito() and ServiceHasOwnInstanceInIncognito() |
64 // through the GetProfileToUse() method on the base. If |create| is true, | 65 // through the GetBrowserContextToUse() method on the base. |
65 // the service will be created using BuildServiceInstanceFor() if it doesn't | 66 // If |create| is true, the service will be created using |
66 // already exist. | 67 // BuildServiceInstanceFor() if it doesn't already exist. |
67 ProfileKeyedService* GetServiceForProfile(content::BrowserContext* profile, | 68 BrowserContextKeyedService* GetServiceForBrowserContext( |
68 bool create); | 69 content::BrowserContext* context, |
| 70 bool create); |
69 | 71 |
70 // Maps |profile| to |service| with debug checks to prevent duplication. | 72 // Maps |context| to |service| with debug checks to prevent duplication. |
71 void Associate(content::BrowserContext* profile, | 73 void Associate(content::BrowserContext* context, |
72 ProfileKeyedService* service); | 74 BrowserContextKeyedService* service); |
73 | 75 |
74 // All subclasses of ProfileKeyedServiceFactory must return a | 76 // All subclasses of BrowserContextKeyedServiceFactory must return a |
75 // ProfileKeyedService instead of just a ProfileKeyedBase. | 77 // BrowserContextKeyedService instead of just a BrowserContextKeyedBase. |
76 virtual ProfileKeyedService* BuildServiceInstanceFor( | 78 virtual BrowserContextKeyedService* BuildServiceInstanceFor( |
77 content::BrowserContext* profile) const = 0; | 79 content::BrowserContext* context) const = 0; |
78 | 80 |
79 // A helper object actually listens for notifications about Profile | 81 // A helper object actually listens for notifications about BrowserContext |
80 // destruction, calculates the order in which things are destroyed and then | 82 // destruction, calculates the order in which things are destroyed and then |
81 // does a two pass shutdown. | 83 // does a two pass shutdown. |
82 // | 84 // |
83 // First, ProfileShutdown() is called on every ServiceFactory and will | 85 // First, BrowserContextShutdown() is called on every ServiceFactory and will |
84 // usually call ProfileKeyedService::Shutdown(), which gives each | 86 // usually call BrowserContextKeyedService::Shutdown(), which gives each |
85 // ProfileKeyedService a chance to remove dependencies on other services that | 87 // BrowserContextKeyedService a chance to remove dependencies on other |
86 // it may be holding. | 88 // services that it may be holding. |
87 // | 89 // |
88 // Secondly, ProfileDestroyed() is called on every ServiceFactory and the | 90 // Secondly, BrowserContextDestroyed() is called on every ServiceFactory |
89 // default implementation removes it from |mapping_| and deletes the pointer. | 91 // and the default implementation removes it from |mapping_| and deletes |
90 virtual void ProfileShutdown(content::BrowserContext* profile) OVERRIDE; | 92 // the pointer. |
91 virtual void ProfileDestroyed(content::BrowserContext* profile) OVERRIDE; | 93 virtual void BrowserContextShutdown( |
| 94 content::BrowserContext* context) OVERRIDE; |
| 95 virtual void BrowserContextDestroyed( |
| 96 content::BrowserContext* context) OVERRIDE; |
92 | 97 |
93 virtual void SetEmptyTestingFactory( | 98 virtual void SetEmptyTestingFactory( |
94 content::BrowserContext* profile) OVERRIDE; | 99 content::BrowserContext* context) OVERRIDE; |
95 virtual void CreateServiceNow(content::BrowserContext* profile) OVERRIDE; | 100 virtual void CreateServiceNow(content::BrowserContext* context) OVERRIDE; |
96 | 101 |
97 private: | 102 private: |
98 friend class ProfileDependencyManager; | 103 friend class BrowserContextDependencyManager; |
99 friend class ProfileDependencyManagerUnittests; | 104 friend class BrowserContextDependencyManagerUnittests; |
100 | 105 |
101 typedef std::map<content::BrowserContext*, ProfileKeyedService*> | 106 typedef std::map<content::BrowserContext*, BrowserContextKeyedService*> |
102 ProfileKeyedServices; | 107 BrowserContextKeyedServices; |
103 typedef std::map<content::BrowserContext*, FactoryFunction> | 108 typedef std::map<content::BrowserContext*, FactoryFunction> |
104 ProfileOverriddenFunctions; | 109 BrowserContextOverriddenFunctions; |
105 | 110 |
106 // The mapping between a Profile and its service. | 111 // The mapping between a BrowserContext and its service. |
107 std::map<content::BrowserContext*, ProfileKeyedService*> mapping_; | 112 std::map<content::BrowserContext*, BrowserContextKeyedService*> mapping_; |
108 | 113 |
109 // The mapping between a Profile and its overridden FactoryFunction. | 114 // The mapping between a BrowserContext and its overridden FactoryFunction. |
110 std::map<content::BrowserContext*, FactoryFunction> factories_; | 115 std::map<content::BrowserContext*, FactoryFunction> factories_; |
111 | 116 |
112 DISALLOW_COPY_AND_ASSIGN(ProfileKeyedServiceFactory); | 117 DISALLOW_COPY_AND_ASSIGN(BrowserContextKeyedServiceFactory); |
113 }; | 118 }; |
114 | 119 |
115 #endif // COMPONENTS_BROWSER_CONTEXT_KEYED_SERVICE_BROWSER_CONTEXT_KEYED_SERVIC
E_FACTORY_H_ | 120 #endif // COMPONENTS_BROWSER_CONTEXT_KEYED_SERVICE_BROWSER_CONTEXT_KEYED_SERVIC
E_FACTORY_H_ |
OLD | NEW |