| 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 CHROME_BROWSER_PROFILES_PROFILE_KEYED_SERVICE_FACTORY_H_ | 5 #ifndef CHROME_BROWSER_PROFILES_PROFILE_KEYED_SERVICE_FACTORY_H_ |
| 6 #define CHROME_BROWSER_PROFILES_PROFILE_KEYED_SERVICE_FACTORY_H_ | 6 #define CHROME_BROWSER_PROFILES_PROFILE_KEYED_SERVICE_FACTORY_H_ |
| 7 | 7 |
| 8 #include <map> | 8 #include <map> |
| 9 | 9 |
| 10 #include "base/basictypes.h" | 10 #include "base/basictypes.h" |
| (...skipping 11 matching lines...) Expand all Loading... |
| 22 // an example of how to derive from this class. | 22 // an example of how to derive from this class. |
| 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 ProfileKeyedServiceFactory : public ProfileKeyedBaseFactory { | 27 class ProfileKeyedServiceFactory : public ProfileKeyedBaseFactory { |
| 28 public: | 28 public: |
| 29 // A function that supplies the instance of a ProfileKeyedService for a given | 29 // A function that supplies the instance of a ProfileKeyedService for a given |
| 30 // Profile. This is used primarily for testing, where we want to feed a | 30 // Profile. This is used primarily for testing, where we want to feed a |
| 31 // specific mock into the PKSF system. | 31 // specific mock into the PKSF system. |
| 32 typedef ProfileKeyedService* (*FactoryFunction)(Profile* profile); | 32 typedef ProfileKeyedService* |
| 33 (*FactoryFunction)(content::BrowserContext* profile); |
| 33 | 34 |
| 34 // Associates |factory| with |profile| so that |factory| is used to create | 35 // Associates |factory| with |profile| so that |factory| is used to create |
| 35 // the ProfileKeyedService when requested. |factory| can be NULL to signal | 36 // the ProfileKeyedService when requested. |factory| can be NULL to signal |
| 36 // that ProfileKeyedService should be NULL. Multiple calls to | 37 // that ProfileKeyedService should be NULL. Multiple calls to |
| 37 // SetTestingFactory() are allowed; previous services will be shut down. | 38 // SetTestingFactory() are allowed; previous services will be shut down. |
| 38 void SetTestingFactory(Profile* profile, FactoryFunction factory); | 39 void SetTestingFactory(content::BrowserContext* profile, |
| 40 FactoryFunction factory); |
| 39 | 41 |
| 40 // Associates |factory| with |profile| and immediately returns the created | 42 // Associates |factory| with |profile| and immediately returns the created |
| 41 // ProfileKeyedService. Since the factory will be used immediately, it may | 43 // ProfileKeyedService. Since the factory will be used immediately, it may |
| 42 // not be NULL. | 44 // not be NULL. |
| 43 ProfileKeyedService* SetTestingFactoryAndUse(Profile* profile, | 45 ProfileKeyedService* SetTestingFactoryAndUse(content::BrowserContext* profile, |
| 44 FactoryFunction factory); | 46 FactoryFunction factory); |
| 45 | 47 |
| 46 protected: | 48 protected: |
| 47 // ProfileKeyedServiceFactories must communicate with a | 49 // ProfileKeyedServiceFactories must communicate with a |
| 48 // ProfileDependencyManager. For all non-test code, write your subclass | 50 // ProfileDependencyManager. For all non-test code, write your subclass |
| 49 // constructors like this: | 51 // constructors like this: |
| 50 // | 52 // |
| 51 // MyServiceFactory::MyServiceFactory() | 53 // MyServiceFactory::MyServiceFactory() |
| 52 // : ProfileKeyedServiceFactory( | 54 // : ProfileKeyedServiceFactory( |
| 53 // "MyService", | 55 // "MyService", |
| 54 // ProfileDependencyManager::GetInstance()) | 56 // ProfileDependencyManager::GetInstance()) |
| 55 // {} | 57 // {} |
| 56 ProfileKeyedServiceFactory(const char* name, | 58 ProfileKeyedServiceFactory(const char* name, |
| 57 ProfileDependencyManager* manager); | 59 ProfileDependencyManager* manager); |
| 58 virtual ~ProfileKeyedServiceFactory(); | 60 virtual ~ProfileKeyedServiceFactory(); |
| 59 | 61 |
| 60 // Common implementation that maps |profile| to some service object. Deals | 62 // Common implementation that maps |profile| to some service object. Deals |
| 61 // with incognito profiles per subclass instructions with | 63 // with incognito profiles per subclass instructions with |
| 62 // ServiceRedirectedInIncognito() and ServiceHasOwnInstanceInIncognito() | 64 // ServiceRedirectedInIncognito() and ServiceHasOwnInstanceInIncognito() |
| 63 // through the GetProfileToUse() method on the base. If |create| is true, | 65 // through the GetProfileToUse() method on the base. If |create| is true, |
| 64 // the service will be created using BuildServiceInstanceFor() if it doesn't | 66 // the service will be created using BuildServiceInstanceFor() if it doesn't |
| 65 // already exist. | 67 // already exist. |
| 66 ProfileKeyedService* GetServiceForProfile(Profile* profile, bool create); | 68 ProfileKeyedService* GetServiceForProfile(content::BrowserContext* profile, |
| 69 bool create); |
| 67 | 70 |
| 68 // Maps |profile| to |service| with debug checks to prevent duplication. | 71 // Maps |profile| to |service| with debug checks to prevent duplication. |
| 69 void Associate(Profile* profile, ProfileKeyedService* service); | 72 void Associate(content::BrowserContext* profile, |
| 73 ProfileKeyedService* service); |
| 70 | 74 |
| 71 // All subclasses of ProfileKeyedServiceFactory must return a | 75 // All subclasses of ProfileKeyedServiceFactory must return a |
| 72 // ProfileKeyedService instead of just a ProfileKeyedBase. | 76 // ProfileKeyedService instead of just a ProfileKeyedBase. |
| 73 virtual ProfileKeyedService* BuildServiceInstanceFor( | 77 virtual ProfileKeyedService* BuildServiceInstanceFor( |
| 74 Profile* profile) const = 0; | 78 content::BrowserContext* profile) const = 0; |
| 75 | 79 |
| 76 // A helper object actually listens for notifications about Profile | 80 // A helper object actually listens for notifications about Profile |
| 77 // destruction, calculates the order in which things are destroyed and then | 81 // destruction, calculates the order in which things are destroyed and then |
| 78 // does a two pass shutdown. | 82 // does a two pass shutdown. |
| 79 // | 83 // |
| 80 // First, ProfileShutdown() is called on every ServiceFactory and will | 84 // First, ProfileShutdown() is called on every ServiceFactory and will |
| 81 // usually call ProfileKeyedService::Shutdown(), which gives each | 85 // usually call ProfileKeyedService::Shutdown(), which gives each |
| 82 // ProfileKeyedService a chance to remove dependencies on other services that | 86 // ProfileKeyedService a chance to remove dependencies on other services that |
| 83 // it may be holding. | 87 // it may be holding. |
| 84 // | 88 // |
| 85 // Secondly, ProfileDestroyed() is called on every ServiceFactory and the | 89 // Secondly, ProfileDestroyed() is called on every ServiceFactory and the |
| 86 // default implementation removes it from |mapping_| and deletes the pointer. | 90 // default implementation removes it from |mapping_| and deletes the pointer. |
| 87 virtual void ProfileShutdown(Profile* profile) OVERRIDE; | 91 virtual void ProfileShutdown(content::BrowserContext* profile) OVERRIDE; |
| 88 virtual void ProfileDestroyed(Profile* profile) OVERRIDE; | 92 virtual void ProfileDestroyed(content::BrowserContext* profile) OVERRIDE; |
| 89 | 93 |
| 90 virtual void SetEmptyTestingFactory(Profile* profile) OVERRIDE; | 94 virtual void SetEmptyTestingFactory( |
| 91 virtual void CreateServiceNow(Profile* profile) OVERRIDE; | 95 content::BrowserContext* profile) OVERRIDE; |
| 96 virtual void CreateServiceNow(content::BrowserContext* profile) OVERRIDE; |
| 92 | 97 |
| 93 private: | 98 private: |
| 94 friend class ProfileDependencyManager; | 99 friend class ProfileDependencyManager; |
| 95 friend class ProfileDependencyManagerUnittests; | 100 friend class ProfileDependencyManagerUnittests; |
| 96 | 101 |
| 97 typedef std::map<Profile*, ProfileKeyedService*> ProfileKeyedServices; | 102 typedef std::map<content::BrowserContext*, ProfileKeyedService*> |
| 98 typedef std::map<Profile*, FactoryFunction> ProfileOverriddenFunctions; | 103 ProfileKeyedServices; |
| 104 typedef std::map<content::BrowserContext*, FactoryFunction> |
| 105 ProfileOverriddenFunctions; |
| 99 | 106 |
| 100 // The mapping between a Profile and its service. | 107 // The mapping between a Profile and its service. |
| 101 std::map<Profile*, ProfileKeyedService*> mapping_; | 108 std::map<content::BrowserContext*, ProfileKeyedService*> mapping_; |
| 102 | 109 |
| 103 // The mapping between a Profile and its overridden FactoryFunction. | 110 // The mapping between a Profile and its overridden FactoryFunction. |
| 104 std::map<Profile*, FactoryFunction> factories_; | 111 std::map<content::BrowserContext*, FactoryFunction> factories_; |
| 105 | 112 |
| 106 DISALLOW_COPY_AND_ASSIGN(ProfileKeyedServiceFactory); | 113 DISALLOW_COPY_AND_ASSIGN(ProfileKeyedServiceFactory); |
| 107 }; | 114 }; |
| 108 | 115 |
| 109 #endif // CHROME_BROWSER_PROFILES_PROFILE_KEYED_SERVICE_FACTORY_H_ | 116 #endif // CHROME_BROWSER_PROFILES_PROFILE_KEYED_SERVICE_FACTORY_H_ |
| OLD | NEW |