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