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

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

Powered by Google App Engine
This is Rietveld 408576698