| 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 // Associate an already-created |service| with |profile| for this factory. |
| 28 // The service may be a mock, or may be NULL to inhibit automatic creation of |
| 29 // the service by the default function. A mock factory set with |
| 30 // |set_test_factory| will be called instead if the service is NULL. |
| 31 void ForceAssociationBetween(Profile* profile, ProfileKeyedService* service) { |
| 32 Associate(profile, service); |
| 33 } |
| 34 |
| 35 // Sets the factory function to use to create mock instances of this service. |
| 36 // The factory function will only be called for profiles for which |
| 37 // |ForceAssociationBetween| has been previously called with a NULL service |
| 38 // pointer, and therefore does not affect normal non-test profiles. |
| 39 void set_test_factory(FactoryFunction factory) { factory_ = factory; } |
| 40 #endif |
| 41 |
| 23 protected: | 42 protected: |
| 24 friend class ProfileDependencyManager; | |
| 25 friend class ProfileDependencyManagerUnittests; | |
| 26 | |
| 27 // ProfileKeyedServiceFactories must communicate with a | 43 // ProfileKeyedServiceFactories must communicate with a |
| 28 // ProfileDependencyManager. For all non-test code, write your subclass | 44 // ProfileDependencyManager. For all non-test code, write your subclass |
| 29 // constructors like this: | 45 // constructors like this: |
| 30 // | 46 // |
| 31 // MyServiceFactory::MyServiceFactory() | 47 // MyServiceFactory::MyServiceFactory() |
| 32 // : ProfileKeyedServiceFactory( | 48 // : ProfileKeyedServiceFactory( |
| 33 // ProfileDependencyManager::GetInstance()) | 49 // ProfileDependencyManager::GetInstance()) |
| 34 // {} | 50 // {} |
| 35 explicit ProfileKeyedServiceFactory(ProfileDependencyManager* manager); | 51 explicit ProfileKeyedServiceFactory(ProfileDependencyManager* manager); |
| 36 virtual ~ProfileKeyedServiceFactory(); | 52 virtual ~ProfileKeyedServiceFactory(); |
| (...skipping 30 matching lines...) Expand all Loading... |
| 67 // usually call ProfileKeyedService::Shutdown(), which gives each | 83 // usually call ProfileKeyedService::Shutdown(), which gives each |
| 68 // ProfileKeyedService a chance to remove dependencies on other services that | 84 // ProfileKeyedService a chance to remove dependencies on other services that |
| 69 // it may be holding. | 85 // it may be holding. |
| 70 // | 86 // |
| 71 // Secondly, ProfileDestroyed() is called on every ServiceFactory and the | 87 // Secondly, ProfileDestroyed() is called on every ServiceFactory and the |
| 72 // default implementation removes it from |mapping_| and deletes the pointer. | 88 // default implementation removes it from |mapping_| and deletes the pointer. |
| 73 virtual void ProfileShutdown(Profile* profile); | 89 virtual void ProfileShutdown(Profile* profile); |
| 74 virtual void ProfileDestroyed(Profile* profile); | 90 virtual void ProfileDestroyed(Profile* profile); |
| 75 | 91 |
| 76 private: | 92 private: |
| 93 friend class ProfileDependencyManager; |
| 94 friend class ProfileDependencyManagerUnittests; |
| 95 |
| 77 // The mapping between a Profile and its service. | 96 // The mapping between a Profile and its service. |
| 78 std::map<Profile*, ProfileKeyedService*> mapping_; | 97 std::map<Profile*, ProfileKeyedService*> mapping_; |
| 79 | 98 |
| 80 // Which ProfileDependencyManager we should communicate with. In real code, | 99 // Which ProfileDependencyManager we should communicate with. In real code, |
| 81 // this will always be ProfileDependencyManager::GetInstance(), but unit | 100 // this will always be ProfileDependencyManager::GetInstance(), but unit |
| 82 // tests will want to use their own copy. | 101 // tests will want to use their own copy. |
| 83 ProfileDependencyManager* dependency_manager_; | 102 ProfileDependencyManager* dependency_manager_; |
| 103 |
| 104 // A mock factory function to use to create the service, used by tests. |
| 105 FactoryFunction factory_; |
| 84 }; | 106 }; |
| 85 | 107 |
| 86 #endif // CHROME_BROWSER_PROFILES_PROFILE_KEYED_SERVICE_FACTORY_H_ | 108 #endif // CHROME_BROWSER_PROFILES_PROFILE_KEYED_SERVICE_FACTORY_H_ |
| OLD | NEW |