Chromium Code Reviews| 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/compiler_specific.h" | 10 #include "base/compiler_specific.h" |
| 11 #include "base/basictypes.h" | |
|
Miranda Callahan
2012/03/15 15:31:07
nit: b before c except after e. actually, also aft
| |
| 11 #include "chrome/browser/profiles/profile_keyed_base_factory.h" | 12 #include "chrome/browser/profiles/profile_keyed_base_factory.h" |
| 12 #include "chrome/browser/profiles/profile_keyed_service.h" | 13 #include "chrome/browser/profiles/profile_keyed_service.h" |
| 13 | 14 |
| 14 class Profile; | 15 class Profile; |
| 15 class ProfileDependencyManager; | 16 class ProfileDependencyManager; |
| 16 class ProfileKeyedService; | 17 class ProfileKeyedService; |
| 17 | 18 |
| 18 // Base class for Factories that take a Profile object and return some service | 19 // Base class for Factories that take a Profile object and return some service |
| 19 // on a one-to-one mapping. Each factory that derives from this class *must* | 20 // on a one-to-one mapping. Each factory that derives from this class *must* |
| 20 // be a Singleton (only unit tests don't do that). See ThemeServiceFactory as | 21 // be a Singleton (only unit tests don't do that). See ThemeServiceFactory as |
| 21 // an example of how to derive from this class. | 22 // an example of how to derive from this class. |
| 22 // | 23 // |
| 23 // 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 |
| 24 // shutdown/destruction order. In each derived classes' constructors, the | 25 // shutdown/destruction order. In each derived classes' constructors, the |
| 25 // implementors must explicitly state which services are depended on. | 26 // implementors must explicitly state which services are depended on. |
| 26 class ProfileKeyedServiceFactory : public ProfileKeyedBaseFactory { | 27 class ProfileKeyedServiceFactory : public ProfileKeyedBaseFactory { |
| 28 public: | |
| 29 // A function that replaces the (possibly internal) object used by this | |
| 30 // factory. For the majority of cases, this is the object returned to users. | |
| 31 typedef ProfileKeyedService* (*FactoryFunction)(Profile* profile); | |
|
Miranda Callahan
2012/03/15 15:31:07
Shouldn't FactoryFunction be described as "A funct
| |
| 32 | |
| 33 // Associates |factory| with |profile| so that |factory| is used to create | |
| 34 // the ProfileKeyedService when requested. | |
| 35 // | |
|
Miranda Callahan
2012/03/15 15:31:07
nit: remove blank line.
| |
| 36 // |factory| can be NULL to signal that ProfileKeyedService should be NULL. A | |
| 37 // second call to SetTestingFactory() is allowed. If the FactoryFunction is | |
|
Miranda Callahan
2012/03/15 15:31:07
instead of "a second call", maybe "multiple calls
| |
| 38 // changed AND an instance of the PKSF already exists for |profile|, that | |
| 39 // service is destroyed. | |
| 40 void SetTestingFactory(Profile* profile, FactoryFunction factory); | |
| 41 | |
| 42 // Associates |factory| with |profile| and immediately returns the created | |
| 43 // ProfileKeyedService. Since the factory will be used immediately, it may | |
| 44 // not be NULL; | |
|
Miranda Callahan
2012/03/15 15:31:07
nit: s/;/.
| |
| 45 ProfileKeyedService* SetTestingFactoryAndUse(Profile* profile, | |
| 46 FactoryFunction factory); | |
| 47 | |
| 27 protected: | 48 protected: |
| 28 // ProfileKeyedServiceFactories must communicate with a | 49 // ProfileKeyedServiceFactories must communicate with a |
| 29 // ProfileDependencyManager. For all non-test code, write your subclass | 50 // ProfileDependencyManager. For all non-test code, write your subclass |
| 30 // constructors like this: | 51 // constructors like this: |
| 31 // | 52 // |
| 32 // MyServiceFactory::MyServiceFactory() | 53 // MyServiceFactory::MyServiceFactory() |
| 33 // : ProfileKeyedServiceFactory( | 54 // : ProfileKeyedServiceFactory( |
| 34 // "MyService", | 55 // "MyService", |
| 35 // ProfileDependencyManager::GetInstance()) | 56 // ProfileDependencyManager::GetInstance()) |
| 36 // {} | 57 // {} |
| 37 ProfileKeyedServiceFactory(const char* name, | 58 ProfileKeyedServiceFactory(const char* name, |
| 38 ProfileDependencyManager* manager); | 59 ProfileDependencyManager* manager); |
| 39 virtual ~ProfileKeyedServiceFactory(); | 60 virtual ~ProfileKeyedServiceFactory(); |
| 40 | 61 |
| 41 // Common implementation that maps |profile| to some service object. Deals | 62 // Common implementation that maps |profile| to some service object. Deals |
| 42 // with incognito profiles per subclass instructions with | 63 // with incognito profiles per subclass instructions with |
| 43 // ServiceRedirectedInIncognito() and ServiceHasOwnInstanceInIncognito(). | 64 // ServiceRedirectedInIncognito() and ServiceHasOwnInstanceInIncognito(). |
|
Miranda Callahan
2012/03/15 15:31:07
Change comment to reflect the fact that we're usin
Elliot Glaysher
2012/03/15 18:02:41
Done.
| |
| 44 // If |create| is true, the service will be created using | 65 // If |create| is true, the service will be created using |
| 45 // BuildServiceInstanceFor() if it doesn't already exist. | 66 // BuildServiceInstanceFor() if it doesn't already exist. |
| 46 ProfileKeyedService* GetServiceForProfile(Profile* profile, bool create); | 67 ProfileKeyedService* GetServiceForProfile(Profile* profile, bool create); |
| 47 | 68 |
| 69 // Maps |profile| to |provider| with debug checks to prevent duplication. | |
|
Miranda Callahan
2012/03/15 15:31:07
s/|provider|/|service|/
| |
| 70 void Associate(Profile* profile, ProfileKeyedService* service); | |
| 71 | |
| 48 // All subclasses of ProfileKeyedServiceFactory must return a | 72 // All subclasses of ProfileKeyedServiceFactory must return a |
| 49 // ProfileKeyedService instead of just a ProfileKeyedBase. | 73 // ProfileKeyedService instead of just a ProfileKeyedBase. |
| 50 virtual ProfileKeyedService* BuildServiceInstanceFor( | 74 virtual ProfileKeyedService* BuildServiceInstanceFor( |
| 51 Profile* profile) const = 0; | 75 Profile* profile) const = 0; |
| 52 | 76 |
| 53 // Maps |profile| to |provider| with debug checks to prevent duplication. | |
| 54 virtual void Associate(Profile* profile, ProfileKeyedBase* base) OVERRIDE; | |
| 55 | |
| 56 // Returns the previously associated |base| for |profile|, or NULL. | |
| 57 virtual bool GetAssociation(Profile* profile, | |
| 58 ProfileKeyedBase** out) const OVERRIDE; | |
| 59 | |
| 60 // A helper object actually listens for notifications about Profile | 77 // A helper object actually listens for notifications about Profile |
| 61 // destruction, calculates the order in which things are destroyed and then | 78 // destruction, calculates the order in which things are destroyed and then |
| 62 // does a two pass shutdown. | 79 // does a two pass shutdown. |
| 63 // | 80 // |
| 64 // First, ProfileShutdown() is called on every ServiceFactory and will | 81 // First, ProfileShutdown() is called on every ServiceFactory and will |
| 65 // usually call ProfileKeyedService::Shutdown(), which gives each | 82 // usually call ProfileKeyedService::Shutdown(), which gives each |
| 66 // ProfileKeyedService a chance to remove dependencies on other services that | 83 // ProfileKeyedService a chance to remove dependencies on other services that |
| 67 // it may be holding. | 84 // it may be holding. |
| 68 // | 85 // |
| 69 // Secondly, ProfileDestroyed() is called on every ServiceFactory and the | 86 // Secondly, ProfileDestroyed() is called on every ServiceFactory and the |
| 70 // default implementation removes it from |mapping_| and deletes the pointer. | 87 // default implementation removes it from |mapping_| and deletes the pointer. |
|
Miranda Callahan
2012/03/15 15:31:07
Every time I see "ProfileShutdown" and "ProfileDes
Elliot Glaysher
2012/03/15 18:02:41
Can I do that as a separate patch? Adding another
Miranda Callahan
2012/03/15 18:11:21
Oh yes, definitely.
| |
| 71 virtual void ProfileShutdown(Profile* profile) OVERRIDE; | 88 virtual void ProfileShutdown(Profile* profile) OVERRIDE; |
| 72 virtual void ProfileDestroyed(Profile* profile) OVERRIDE; | 89 virtual void ProfileDestroyed(Profile* profile) OVERRIDE; |
| 73 | 90 |
| 91 virtual void SetEmptyTestingFactory(Profile* profile) OVERRIDE; | |
| 92 virtual void CreateServiceNow(Profile* profile) OVERRIDE; | |
| 93 | |
| 74 private: | 94 private: |
| 75 friend class ProfileDependencyManager; | 95 friend class ProfileDependencyManager; |
| 76 friend class ProfileDependencyManagerUnittests; | 96 friend class ProfileDependencyManagerUnittests; |
| 77 | 97 |
| 78 // The mapping between a Profile and its service. | 98 // The mapping between a Profile and its service. |
| 79 std::map<Profile*, ProfileKeyedService*> mapping_; | 99 std::map<Profile*, ProfileKeyedService*> mapping_; |
| 100 | |
| 101 // The mapping between a Profile and its overridden FactoryFunction. | |
| 102 std::map<Profile*, FactoryFunction> factories_; | |
| 103 | |
| 104 DISALLOW_COPY_AND_ASSIGN(ProfileKeyedServiceFactory); | |
| 80 }; | 105 }; |
| 81 | 106 |
| 82 #endif // CHROME_BROWSER_PROFILES_PROFILE_KEYED_SERVICE_FACTORY_H_ | 107 #endif // CHROME_BROWSER_PROFILES_PROFILE_KEYED_SERVICE_FACTORY_H_ |
| OLD | NEW |