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
3 // found in the LICENSE file.
5 #ifndef CHROME_BROWSER_PROFILES_PROFILE_KEYED_BASE_FACTORY_H_
6 #define CHROME_BROWSER_PROFILES_PROFILE_KEYED_BASE_FACTORY_H_
10 #include "base/threading/non_thread_safe.h"
11 #include "chrome/browser/profiles/dependency_node.h"
13 class PrefRegistrySyncable
;
16 class ProfileDependencyManager
;
18 // Base class for Factories that take a Profile object and return some service.
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
22 // RefcountedProfileKeyedServiceFactory. This object describes general
23 // dependency management between Factories; subclasses react to lifecycle
24 // events and implement memory management.
25 class ProfileKeyedBaseFactory
: public base::NonThreadSafe
,
26 public DependencyNode
{
28 // Registers preferences used in this service on the pref service of
29 // |profile|. This is the public interface and is safe to be called multiple
30 // times because testing code can have multiple services of the same type
31 // attached to a single |profile|.
32 void RegisterUserPrefsOnProfile(Profile
* profile
);
35 // Returns our name. We don't keep track of this in release mode.
36 const char* name() const { return service_name_
; }
40 ProfileKeyedBaseFactory(const char* name
,
41 ProfileDependencyManager
* manager
);
42 virtual ~ProfileKeyedBaseFactory();
44 // The main public interface for declaring dependencies between services
45 // created by factories.
46 void DependsOn(ProfileKeyedBaseFactory
* rhs
);
48 // Finds which profile (if any) to use using the Service.*Incognito methods.
49 Profile
* GetProfileToUse(Profile
* profile
);
51 // Interface for people building a concrete FooServiceFactory: --------------
53 // Register any user preferences on this service. This is called during
54 // CreateProfileService() since preferences are registered on a per Profile
56 virtual void RegisterUserPrefs(PrefRegistrySyncable
* registry
) {}
58 // By default, if we are asked for a service with an Incognito profile, we
59 // pass back NULL. To redirect to the Incognito's original profile or to
60 // create another instance, even for Incognito windows, override one of the
62 virtual bool ServiceRedirectedInIncognito() const;
63 virtual bool ServiceHasOwnInstanceInIncognito() const;
65 // By default, we create instances of a service lazily and wait until
66 // GetForProfile() is called on our subclass. Some services need to be
67 // created as soon as the Profile has been brought up.
68 virtual bool ServiceIsCreatedWithProfile() const;
70 // By default, TestingProfiles will be treated like normal profiles. You can
71 // override this so that by default, the service associated with the
72 // TestingProfile is NULL. (This is just a shortcut around
73 // SetTestingFactory() to make sure our profiles don't directly refer to the
74 // services they use.)
75 virtual bool ServiceIsNULLWhileTesting() const;
77 // Interface for people building a type of ProfileKeyedFactory: -------------
79 // A helper object actually listens for notifications about Profile
80 // destruction, calculates the order in which things are destroyed and then
81 // does a two pass shutdown.
83 // It is up to the individual factory types to determine what this two pass
84 // shutdown means. The general framework guarantees the following:
86 // - Each ProfileShutdown() is called in dependency order (and you may reach
87 // out to other services during this phase).
89 // - Each ProfileDestroyed() is called in dependency order. We will
90 // NOTREACHED() if you attempt to GetForProfile() any other service. You
91 // should delete/deref/do other final memory management things during this
92 // phase. You must also call the base class method as the last thing you
94 virtual void ProfileShutdown(Profile
* profile
) = 0;
95 virtual void ProfileDestroyed(Profile
* profile
);
97 // Returns whether we've registered the preferences on this profile.
98 bool ArePreferencesSetOn(Profile
* profile
) const;
100 // Mark profile as Preferences set.
101 void MarkPreferencesSetOn(Profile
* profile
);
104 friend class ProfileDependencyManager
;
105 friend class ProfileDependencyManagerUnittests
;
107 // These two methods are for tight integration with the
108 // ProfileDependencyManager.
110 // Because of ServiceIsNULLWhileTesting(), we need a way to tell different
111 // subclasses that they should disable testing.
112 virtual void SetEmptyTestingFactory(Profile
* profile
) = 0;
114 // We also need a generalized, non-returning method that generates the object
115 // now for when we're creating the profile.
116 virtual void CreateServiceNow(Profile
* profile
) = 0;
118 // Which ProfileDependencyManager we should communicate with. In real code,
119 // this will always be ProfileDependencyManager::GetInstance(), but unit
120 // tests will want to use their own copy.
121 ProfileDependencyManager
* dependency_manager_
;
123 // Profiles that have this service's preferences registered on them.
124 std::set
<Profile
*> registered_preferences_
;
127 // A static string passed in to our constructor. Should be unique across all
128 // services. This is used only for debugging in debug mode. (We can print
129 // pretty graphs with GraphViz with this information.)
130 const char* service_name_
;
134 #endif // CHROME_BROWSER_PROFILES_PROFILE_KEYED_BASE_FACTORY_H_