Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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 class Profile; | 10 class Profile; |
| 11 class ProfileDependencyManager; | 11 class ProfileDependencyManager; |
| 12 class ProfileKeyedService; | 12 class ProfileKeyedService; |
| 13 | 13 |
| 14 // Base class for Factories that take a Profile object and return some service | 14 // Base class for Factories that take a Profile object and return some service |
| 15 // on a one-to-one mapping. Each factory that derives from this class *must* | 15 // on a one-to-one mapping. Each factory that derives from this class *must* |
| 16 // be a Singleton (only unit tests don't do that). See ThemeServiceFactory as | 16 // be a Singleton (only unit tests don't do that). See ThemeServiceFactory as |
| 17 // an example of how to derive from this class. | 17 // an example of how to derive from this class. |
| 18 // | 18 // |
| 19 // We do this because services depend on each other and we need to control | 19 // We do this because services depend on each other and we need to control |
| 20 // shutdown/destruction order. In each derived classes' constructors, the | 20 // shutdown/destruction order. In each derived classes' constructors, the |
| 21 // implementors must explicitly state which services are depended on. | 21 // implementors must explicitly state which services are depended on. |
| 22 class ProfileKeyedServiceFactory { | 22 class ProfileKeyedServiceFactory { |
| 23 public: | |
| 24 typedef ProfileKeyedService* (*FactoryFunction)(Profile* profile); | |
| 25 | |
| 26 #if defined(UNIT_TEST) | |
| 27 // Sets the factory which will be used by |GetServiceFunction|. A value of | |
| 28 // NULL (the default) results in a call to |BuildServiceInstanceFor|. | |
| 29 void set_factory_function(FactoryFunction factory) { factory_ = factory; } | |
|
Elliot Glaysher
2011/04/08 17:43:32
This is fine, but I'm worried that there isn't an
Torne
2011/04/11 11:52:51
I was assuming that test code does not create norm
| |
| 30 #endif | |
| 31 | |
| 23 protected: | 32 protected: |
| 24 friend class ProfileDependencyManager; | |
| 25 friend class ProfileDependencyManagerUnittests; | |
| 26 | |
| 27 // ProfileKeyedServiceFactories must communicate with a | 33 // ProfileKeyedServiceFactories must communicate with a |
| 28 // ProfileDependencyManager. For all non-test code, write your subclass | 34 // ProfileDependencyManager. For all non-test code, write your subclass |
| 29 // constructors like this: | 35 // constructors like this: |
| 30 // | 36 // |
| 31 // MyServiceFactory::MyServiceFactory() | 37 // MyServiceFactory::MyServiceFactory() |
| 32 // : ProfileKeyedServiceFactory( | 38 // : ProfileKeyedServiceFactory( |
| 33 // ProfileDependencyManager::GetInstance()) | 39 // ProfileDependencyManager::GetInstance()) |
| 34 // {} | 40 // {} |
| 35 explicit ProfileKeyedServiceFactory(ProfileDependencyManager* manager); | 41 explicit ProfileKeyedServiceFactory(ProfileDependencyManager* manager); |
| 36 virtual ~ProfileKeyedServiceFactory(); | 42 virtual ~ProfileKeyedServiceFactory(); |
| (...skipping 30 matching lines...) Expand all Loading... | |
| 67 // usually call ProfileKeyedService::Shutdown(), which gives each | 73 // usually call ProfileKeyedService::Shutdown(), which gives each |
| 68 // ProfileKeyedService a chance to remove dependencies on other services that | 74 // ProfileKeyedService a chance to remove dependencies on other services that |
| 69 // it may be holding. | 75 // it may be holding. |
| 70 // | 76 // |
| 71 // Secondly, ProfileDestroyed() is called on every ServiceFactory and the | 77 // Secondly, ProfileDestroyed() is called on every ServiceFactory and the |
| 72 // default implementation removes it from |mapping_| and deletes the pointer. | 78 // default implementation removes it from |mapping_| and deletes the pointer. |
| 73 virtual void ProfileShutdown(Profile* profile); | 79 virtual void ProfileShutdown(Profile* profile); |
| 74 virtual void ProfileDestroyed(Profile* profile); | 80 virtual void ProfileDestroyed(Profile* profile); |
| 75 | 81 |
| 76 private: | 82 private: |
| 83 friend class ProfileDependencyManager; | |
| 84 friend class ProfileDependencyManagerUnittests; | |
| 85 | |
| 77 // The mapping between a Profile and its service. | 86 // The mapping between a Profile and its service. |
| 78 std::map<Profile*, ProfileKeyedService*> mapping_; | 87 std::map<Profile*, ProfileKeyedService*> mapping_; |
| 79 | 88 |
| 80 // Which ProfileDependencyManager we should communicate with. In real code, | 89 // Which ProfileDependencyManager we should communicate with. In real code, |
| 81 // this will always be ProfileDependencyManager::GetInstance(), but unit | 90 // this will always be ProfileDependencyManager::GetInstance(), but unit |
| 82 // tests will want to use their own copy. | 91 // tests will want to use their own copy. |
| 83 ProfileDependencyManager* dependency_manager_; | 92 ProfileDependencyManager* dependency_manager_; |
| 93 | |
| 94 // An alternative function to use to create the service, used by tests. | |
| 95 FactoryFunction factory_; | |
| 84 }; | 96 }; |
| 85 | 97 |
| 86 #endif // CHROME_BROWSER_PROFILES_PROFILE_KEYED_SERVICE_FACTORY_H_ | 98 #endif // CHROME_BROWSER_PROFILES_PROFILE_KEYED_SERVICE_FACTORY_H_ |
| OLD | NEW |