| OLD | NEW |
| (Empty) | |
| 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 |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #ifndef CHROME_BROWSER_PROFILES_PROFILE_KEYED_SERVICE_FACTORY_H_ |
| 6 #define CHROME_BROWSER_PROFILES_PROFILE_KEYED_SERVICE_FACTORY_H_ |
| 7 |
| 8 #include <map> |
| 9 |
| 10 class Profile; |
| 11 class ProfileDependencyManager; |
| 12 class ProfileKeyedService; |
| 13 |
| 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* |
| 16 // be a Singleton (only unit tests don't do that). See ThemeServiceFactory as |
| 17 // an example of how to derive from this class. |
| 18 // |
| 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 |
| 21 // implementors must explicitly state which services are depended on. |
| 22 class ProfileKeyedServiceFactory { |
| 23 protected: |
| 24 friend class ProfileDependencyManager; |
| 25 friend class ProfileDependencyManagerUnittests; |
| 26 |
| 27 // ProfileKeyedServiceFactories must communicate with a |
| 28 // ProfileDependencyManager. For all non-test code, write your subclass |
| 29 // constructors like this: |
| 30 // |
| 31 // MyServiceFactory::MyServiceFactory() |
| 32 // : ProfileKeyedServiceFactory( |
| 33 // ProfileDependencyManager::GetInstance()) |
| 34 // {} |
| 35 explicit ProfileKeyedServiceFactory(ProfileDependencyManager* manager); |
| 36 virtual ~ProfileKeyedServiceFactory(); |
| 37 |
| 38 // Common implementation that maps |profile| to some service object. Deals |
| 39 // with incognito profiles per subclass instructions with |
| 40 // ServiceActiveInIncognito(). |
| 41 ProfileKeyedService* GetServiceForProfile(Profile* profile); |
| 42 |
| 43 // The main public interface for declaring dependencies between services |
| 44 // created by factories. |
| 45 void DependsOn(ProfileKeyedServiceFactory* rhs); |
| 46 |
| 47 // Maps |profile| to |provider| with debug checks to prevent duplication. |
| 48 void Associate(Profile* profile, ProfileKeyedService* service); |
| 49 |
| 50 // Returns a new instance of the service, casted to void* for our common |
| 51 // storage. |
| 52 virtual ProfileKeyedService* BuildServiceInstanceFor( |
| 53 Profile* profile) const = 0; |
| 54 |
| 55 // By default, if we are asked for a service with an Incognito profile, we |
| 56 // pass back NULL. To redirect to the Incognito's original profile or to |
| 57 // create another instance, even for Incognito windows, override one of the |
| 58 // following methods: |
| 59 virtual bool ServiceRedirectedInIncognito(); |
| 60 virtual bool ServiceHasOwnInstanceInIncognito(); |
| 61 |
| 62 // A helper object actually listens for notifications about Profile |
| 63 // destruction, calculates the order in which things are destroyed and then |
| 64 // does a two pass shutdown. |
| 65 // |
| 66 // First, ProfileShutdown() is called on every ServiceFactory and will |
| 67 // usually call ProfileKeyedService::Shutdown(), which gives each |
| 68 // ProfileKeyedService a chance to remove dependencies on other services that |
| 69 // it may be holding. |
| 70 // |
| 71 // Secondly, ProfileDestroyed() is called on every ServiceFactory and the |
| 72 // default implementation removes it from |mapping_| and deletes the pointer. |
| 73 virtual void ProfileShutdown(Profile* profile); |
| 74 virtual void ProfileDestroyed(Profile* profile); |
| 75 |
| 76 private: |
| 77 // The mapping between a Profile and its service. |
| 78 std::map<Profile*, ProfileKeyedService*> mapping_; |
| 79 |
| 80 // Which ProfileDependencyManager we should communicate with. In real code, |
| 81 // this will always be ProfileDependencyManager::GetInstance(), but unit |
| 82 // tests will want to use their own copy. |
| 83 ProfileDependencyManager* dependency_manager_; |
| 84 }; |
| 85 |
| 86 #endif // CHROME_BROWSER_PROFILES_PROFILE_KEYED_SERVICE_FACTORY_H_ |
| OLD | NEW |