Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(29)

Side by Side Diff: chrome/browser/profiles/profile_keyed_service_factory.h

Issue 9703038: Profiles: Really fix refcounted services. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Forgot to save a file. >_< Created 8 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
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/compiler_specific.h" 11 #include "base/compiler_specific.h"
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 supplies the instance of a ProfileKeyedService for a given
30 // Profile. This is used primarily for testing, where we want to feed a
31 // specific mock into the PKSF system.
32 typedef ProfileKeyedService* (*FactoryFunction)(Profile* profile);
33
34 // Associates |factory| with |profile| so that |factory| is used to create
35 // the ProfileKeyedService when requested. |factory| can be NULL to signal
36 // that ProfileKeyedService should be NULL. Multiple calls to
37 // SetTestingFactory() are allowed; previous services will be shut down.
38 void SetTestingFactory(Profile* profile, FactoryFunction factory);
39
40 // Associates |factory| with |profile| and immediately returns the created
41 // ProfileKeyedService. Since the factory will be used immediately, it may
42 // not be NULL.
43 ProfileKeyedService* SetTestingFactoryAndUse(Profile* profile,
44 FactoryFunction factory);
45
27 protected: 46 protected:
28 // ProfileKeyedServiceFactories must communicate with a 47 // ProfileKeyedServiceFactories must communicate with a
29 // ProfileDependencyManager. For all non-test code, write your subclass 48 // ProfileDependencyManager. For all non-test code, write your subclass
30 // constructors like this: 49 // constructors like this:
31 // 50 //
32 // MyServiceFactory::MyServiceFactory() 51 // MyServiceFactory::MyServiceFactory()
33 // : ProfileKeyedServiceFactory( 52 // : ProfileKeyedServiceFactory(
34 // "MyService", 53 // "MyService",
35 // ProfileDependencyManager::GetInstance()) 54 // ProfileDependencyManager::GetInstance())
36 // {} 55 // {}
37 ProfileKeyedServiceFactory(const char* name, 56 ProfileKeyedServiceFactory(const char* name,
38 ProfileDependencyManager* manager); 57 ProfileDependencyManager* manager);
39 virtual ~ProfileKeyedServiceFactory(); 58 virtual ~ProfileKeyedServiceFactory();
40 59
41 // Common implementation that maps |profile| to some service object. Deals 60 // Common implementation that maps |profile| to some service object. Deals
42 // with incognito profiles per subclass instructions with 61 // with incognito profiles per subclass instructions with
43 // ServiceRedirectedInIncognito() and ServiceHasOwnInstanceInIncognito(). 62 // ServiceRedirectedInIncognito() and ServiceHasOwnInstanceInIncognito()
44 // If |create| is true, the service will be created using 63 // through the GetProfileToUse() method on the base. If |create| is true,
45 // BuildServiceInstanceFor() if it doesn't already exist. 64 // the service will be created using BuildServiceInstanceFor() if it doesn't
65 // already exist.
46 ProfileKeyedService* GetServiceForProfile(Profile* profile, bool create); 66 ProfileKeyedService* GetServiceForProfile(Profile* profile, bool create);
47 67
68 // Maps |profile| to |service| with debug checks to prevent duplication.
69 void Associate(Profile* profile, ProfileKeyedService* service);
70
48 // All subclasses of ProfileKeyedServiceFactory must return a 71 // All subclasses of ProfileKeyedServiceFactory must return a
49 // ProfileKeyedService instead of just a ProfileKeyedBase. 72 // ProfileKeyedService instead of just a ProfileKeyedBase.
50 virtual ProfileKeyedService* BuildServiceInstanceFor( 73 virtual ProfileKeyedService* BuildServiceInstanceFor(
51 Profile* profile) const = 0; 74 Profile* profile) const = 0;
52 75
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 76 // A helper object actually listens for notifications about Profile
61 // destruction, calculates the order in which things are destroyed and then 77 // destruction, calculates the order in which things are destroyed and then
62 // does a two pass shutdown. 78 // does a two pass shutdown.
63 // 79 //
64 // First, ProfileShutdown() is called on every ServiceFactory and will 80 // First, ProfileShutdown() is called on every ServiceFactory and will
65 // usually call ProfileKeyedService::Shutdown(), which gives each 81 // usually call ProfileKeyedService::Shutdown(), which gives each
66 // ProfileKeyedService a chance to remove dependencies on other services that 82 // ProfileKeyedService a chance to remove dependencies on other services that
67 // it may be holding. 83 // it may be holding.
68 // 84 //
69 // Secondly, ProfileDestroyed() is called on every ServiceFactory and the 85 // Secondly, ProfileDestroyed() is called on every ServiceFactory and the
70 // default implementation removes it from |mapping_| and deletes the pointer. 86 // default implementation removes it from |mapping_| and deletes the pointer.
71 virtual void ProfileShutdown(Profile* profile) OVERRIDE; 87 virtual void ProfileShutdown(Profile* profile) OVERRIDE;
72 virtual void ProfileDestroyed(Profile* profile) OVERRIDE; 88 virtual void ProfileDestroyed(Profile* profile) OVERRIDE;
73 89
90 virtual void SetEmptyTestingFactory(Profile* profile) OVERRIDE;
91 virtual void CreateServiceNow(Profile* profile) OVERRIDE;
92
74 private: 93 private:
75 friend class ProfileDependencyManager; 94 friend class ProfileDependencyManager;
76 friend class ProfileDependencyManagerUnittests; 95 friend class ProfileDependencyManagerUnittests;
77 96
78 // The mapping between a Profile and its service. 97 // The mapping between a Profile and its service.
79 std::map<Profile*, ProfileKeyedService*> mapping_; 98 std::map<Profile*, ProfileKeyedService*> mapping_;
99
100 // The mapping between a Profile and its overridden FactoryFunction.
101 std::map<Profile*, FactoryFunction> factories_;
102
103 DISALLOW_COPY_AND_ASSIGN(ProfileKeyedServiceFactory);
80 }; 104 };
81 105
82 #endif // CHROME_BROWSER_PROFILES_PROFILE_KEYED_SERVICE_FACTORY_H_ 106 #endif // CHROME_BROWSER_PROFILES_PROFILE_KEYED_SERVICE_FACTORY_H_
OLDNEW
« no previous file with comments | « chrome/browser/profiles/profile_keyed_service.h ('k') | chrome/browser/profiles/profile_keyed_service_factory.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698