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

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

Issue 9703038: Profiles: Really fix refcounted services. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Fix compile 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_BASE_FACTORY_H_ 5 #ifndef CHROME_BROWSER_PROFILES_PROFILE_KEYED_BASE_FACTORY_H_
6 #define CHROME_BROWSER_PROFILES_PROFILE_KEYED_BASE_FACTORY_H_ 6 #define CHROME_BROWSER_PROFILES_PROFILE_KEYED_BASE_FACTORY_H_
7 7
8 #include <map> 8 #include <map>
9 #include <set> 9 #include <set>
10 10
11 #include "base/threading/non_thread_safe.h" 11 #include "base/threading/non_thread_safe.h"
12 12
13 class PrefService; 13 class PrefService;
14 class Profile; 14 class Profile;
15 class ProfileKeyedBase; 15 class ProfileKeyedBase;
Miranda Callahan 2012/03/15 16:55:35 remove
16 class ProfileDependencyManager; 16 class ProfileDependencyManager;
17 17
18 // Base class for Factories that take a Profile object and return some service. 18 // Base class for Factories that take a Profile object and return some service.
19 // 19 //
20 // Unless you're trying to make a new type of Factory, you probably don't want 20 // Unless you're trying to make a new type of Factory, you probably don't want
21 // this class, but its subclasses: ProfileKeyedServiceFactory and 21 // this class, but its subclasses: ProfileKeyedServiceFactory and
22 // RefcountedProfileKeyedServiceFactory. This object describes general profile 22 // RefcountedProfileKeyedServiceFactory. This object describes general
23 // dependency management; subclasses react to lifecycle events and implement 23 // dependency management between Factories; subclasses react to lifecycle
24 // memory management. 24 // events and implement memory management.
25 class ProfileKeyedBaseFactory : public base::NonThreadSafe { 25 class ProfileKeyedBaseFactory : public base::NonThreadSafe {
26 public: 26 public:
27 // A function that replaces the (possibly internal) object used by this
28 // factory. For the majority of cases, this is the object returned to users.
29 typedef ProfileKeyedBase* (*FactoryFunction)(Profile* profile);
30
31 // Associates |factory| with |profile| so that |factory| is used to create
32 // the ProfileKeyedService when requested.
33 //
34 // |factory| can be NULL to signal that ProfileKeyedService should be NULL. A
35 // second call to SetTestingFactory() is allowed. If the FactoryFunction is
36 // changed AND an instance of the PKSF already exists for |profile|, that
37 // service is destroyed.
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 ProfileKeyedBase* SetTestingFactoryAndUse(Profile* profile,
44 FactoryFunction factory);
45
46 // Registers preferences used in this service on the pref service of 27 // Registers preferences used in this service on the pref service of
47 // |profile|. This is the public interface and is safe to be called multiple 28 // |profile|. This is the public interface and is safe to be called multiple
48 // times because testing code can have multiple services of the same type 29 // times because testing code can have multiple services of the same type
49 // attached to a single |profile|. 30 // attached to a single |profile|.
50 void RegisterUserPrefsOnProfile(Profile* profile); 31 void RegisterUserPrefsOnProfile(Profile* profile);
51 32
52 // Some unit tests don't run with a Profile, or switch out PrefServices and 33 // Some unit tests don't run with a Profile, or switch out PrefServices and
53 // require reregistration. They should do neither of those things, but there 34 // require reregistration. They should do neither of those things, but there
54 // are too many of them to go fix. 35 // are too many of them to go fix.
55 // 36 //
56 // DO NOT USE THIS IN REAL CODE. 37 // DO NOT USE THIS IN REAL CODE.
57 void ForceRegisterPrefsForTest(PrefService* prefs); 38 void ForceRegisterPrefsForTest(PrefService* prefs);
58 39
59 #ifndef NDEBUG 40 #ifndef NDEBUG
60 // Returns our name. We don't keep track of this in release mode. 41 // Returns our name. We don't keep track of this in release mode.
61 const char* name() const { return service_name_; } 42 const char* name() const { return service_name_; }
62 #endif 43 #endif
63 44
64 protected: 45 protected:
65 ProfileKeyedBaseFactory(const char* name, 46 ProfileKeyedBaseFactory(const char* name,
66 ProfileDependencyManager* manager); 47 ProfileDependencyManager* manager);
67 virtual ~ProfileKeyedBaseFactory(); 48 virtual ~ProfileKeyedBaseFactory();
68 49
69 // The main public interface for declaring dependencies between services 50 // The main public interface for declaring dependencies between services
70 // created by factories. 51 // created by factories.
71 void DependsOn(ProfileKeyedBaseFactory* rhs); 52 void DependsOn(ProfileKeyedBaseFactory* rhs);
72 53
54 // Finds which profile (if any) to use using the Service.*Incognito methods.
55 Profile* GetProfileToUse(Profile* profile);
56
73 // Interface for people building a concrete FooServiceFactory: -------------- 57 // Interface for people building a concrete FooServiceFactory: --------------
74 58
75 // Register any user preferences on this service. This is called during 59 // Register any user preferences on this service. This is called during
76 // CreateProfileService() since preferences are registered on a per Profile 60 // CreateProfileService() since preferences are registered on a per Profile
77 // basis. 61 // basis.
78 virtual void RegisterUserPrefs(PrefService* user_prefs) {} 62 virtual void RegisterUserPrefs(PrefService* user_prefs) {}
79 63
80 // Returns a new instance of the service, casted to our void* equivalent for
81 // our storage.
82 virtual ProfileKeyedBase* BuildServiceInstanceFor(
83 Profile* profile) const = 0;
84
85 // By default, if we are asked for a service with an Incognito profile, we 64 // By default, if we are asked for a service with an Incognito profile, we
86 // pass back NULL. To redirect to the Incognito's original profile or to 65 // pass back NULL. To redirect to the Incognito's original profile or to
87 // create another instance, even for Incognito windows, override one of the 66 // create another instance, even for Incognito windows, override one of the
88 // following methods: 67 // following methods:
89 virtual bool ServiceRedirectedInIncognito(); 68 virtual bool ServiceRedirectedInIncognito();
90 virtual bool ServiceHasOwnInstanceInIncognito(); 69 virtual bool ServiceHasOwnInstanceInIncognito();
91 70
92 // By default, we create instances of a service lazily and wait until 71 // By default, we create instances of a service lazily and wait until
93 // GetForProfile() is called on our subclass. Some services need to be 72 // GetForProfile() is called on our subclass. Some services need to be
94 // created as soon as the Profile has been brought up. 73 // created as soon as the Profile has been brought up.
95 virtual bool ServiceIsCreatedWithProfile(); 74 virtual bool ServiceIsCreatedWithProfile();
96 75
97 // By default, TestingProfiles will be treated like normal profiles. You can 76 // By default, TestingProfiles will be treated like normal profiles. You can
98 // override this so that by default, the service associated with the 77 // override this so that by default, the service associated with the
99 // TestingProfile is NULL. (This is just a shortcut around 78 // TestingProfile is NULL. (This is just a shortcut around
100 // SetTestingFactory() to make sure our profiles don't directly refer to the 79 // SetTestingFactory() to make sure our profiles don't directly refer to the
101 // services they use.) 80 // services they use.)
102 virtual bool ServiceIsNULLWhileTesting(); 81 virtual bool ServiceIsNULLWhileTesting();
103 82
104 // Interface for people building a type of ProfileKeyedFactory: ------------- 83 // Interface for people building a type of ProfileKeyedFactory: -------------
105 84
106 // Common implementation that maps |profile| to some object. Deals with
107 // incognito profiles per subclass instructions with
108 // ServiceRedirectedInIncognito() and ServiceHasOwnInstanceInIncognito().
109 // If |create| is true, the service will be created using
110 // BuildServiceInstanceFor() if it doesn't already exist.
111 virtual ProfileKeyedBase* GetBaseForProfile(Profile* profile,
112 bool create);
113
114 // The base factory is not responsible for storage; the derived factory type
115 // maintains storage and reacts to lifecycle events.
116 virtual void Associate(Profile* profile, ProfileKeyedBase* base) = 0;
117
118 // Returns whether there is an object associated with |profile|. If |out| is
119 // non-NULL, returns said object.
120 virtual bool GetAssociation(Profile* profile,
121 ProfileKeyedBase** out) const = 0;
122
123 // A helper object actually listens for notifications about Profile 85 // A helper object actually listens for notifications about Profile
124 // destruction, calculates the order in which things are destroyed and then 86 // destruction, calculates the order in which things are destroyed and then
125 // does a two pass shutdown. 87 // does a two pass shutdown.
126 // 88 //
127 // It is up to the individual factory types to determine what this two pass 89 // It is up to the individual factory types to determine what this two pass
128 // shutdown means. The general framework guarantees the following: 90 // shutdown means. The general framework guarantees the following:
129 // 91 //
130 // - Each ProfileShutdown() is called in dependency order (and you may reach 92 // - Each ProfileShutdown() is called in dependency order (and you may reach
131 // out to other services during this phase). 93 // out to other services during this phase).
132 // 94 //
133 // - Each ProfileDestroyed() is called in dependency order. We will 95 // - Each ProfileDestroyed() is called in dependency order. We will
134 // NOTREACHED() if you attempt to GetForProfile() any other service. You 96 // NOTREACHED() if you attempt to GetForProfile() any other service. You
135 // should delete/deref/do other final memory management things during this 97 // should delete/deref/do other final memory management things during this
136 // phase. You must also call the base class method as the last thing you 98 // phase. You must also call the base class method as the last thing you
137 // do. 99 // do.
138 virtual void ProfileShutdown(Profile* profile) = 0; 100 virtual void ProfileShutdown(Profile* profile) = 0;
139 virtual void ProfileDestroyed(Profile* profile); 101 virtual void ProfileDestroyed(Profile* profile);
140 102
103 protected:
104 // Returns whether we've registered the preferences on this profile.
105 bool ArePreferencesSetOn(Profile* profile);
106
107 // Mark profile as Preferences set.
108 void MarkPreferencesSetOn(Profile* profile);
109
141 private: 110 private:
142 friend class ProfileDependencyManager; 111 friend class ProfileDependencyManager;
143 friend class ProfileDependencyManagerUnittests; 112 friend class ProfileDependencyManagerUnittests;
144 113
114 // These two methods are for tight integration with the
115 // ProfileDependencyManager.
116
117 // Because of ServiceIsNULLWhileTesting(), we need a way to tell different
118 // subclasses that they should disable testing.
119 virtual void SetEmptyTestingFactory(Profile* profile) = 0;
120
121 // We also need a generalized, non-returning method that generates the object
122 // now for when we're creating the profile.
123 virtual void CreateServiceNow(Profile* profile) = 0;
124
145 // Which ProfileDependencyManager we should communicate with. In real code, 125 // Which ProfileDependencyManager we should communicate with. In real code,
146 // this will always be ProfileDependencyManager::GetInstance(), but unit 126 // this will always be ProfileDependencyManager::GetInstance(), but unit
147 // tests will want to use their own copy. 127 // tests will want to use their own copy.
148 ProfileDependencyManager* dependency_manager_; 128 ProfileDependencyManager* dependency_manager_;
149 129
150 // Profiles that have this service's preferences registered on them. 130 // Profiles that have this service's preferences registered on them.
151 std::set<Profile*> registered_preferences_; 131 std::set<Profile*> registered_preferences_;
152 132
153 // The mapping between a Profile and its overridden FactoryFunction.
154 std::map<Profile*, FactoryFunction> factories_;
155
156 #if !defined(NDEBUG) 133 #if !defined(NDEBUG)
157 // A static string passed in to our constructor. Should be unique across all 134 // A static string passed in to our constructor. Should be unique across all
158 // services. This is used only for debugging in debug mode. (We can print 135 // services. This is used only for debugging in debug mode. (We can print
159 // pretty graphs with GraphViz with this information.) 136 // pretty graphs with GraphViz with this information.)
160 const char* service_name_; 137 const char* service_name_;
161 #endif 138 #endif
162 }; 139 };
163 140
164 #endif // CHROME_BROWSER_PROFILES_PROFILE_KEYED_BASE_FACTORY_H_ 141 #endif // CHROME_BROWSER_PROFILES_PROFILE_KEYED_BASE_FACTORY_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698