Fix build break
[chromium-blink-merge.git] / chrome / browser / profiles / profile_keyed_service_factory.h
blob0cfa841e1a572610208003fb201551f18c852291
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_SERVICE_FACTORY_H_
6 #define CHROME_BROWSER_PROFILES_PROFILE_KEYED_SERVICE_FACTORY_H_
8 #include <map>
10 #include "base/basictypes.h"
11 #include "base/compiler_specific.h"
12 #include "chrome/browser/profiles/profile_keyed_base_factory.h"
13 #include "chrome/browser/profiles/profile_keyed_service.h"
15 class Profile;
16 class ProfileDependencyManager;
17 class ProfileKeyedService;
19 // Base class for Factories that take a Profile object and return some service
20 // on a one-to-one mapping. Each factory that derives from this class *must*
21 // be a Singleton (only unit tests don't do that). See ThemeServiceFactory as
22 // an example of how to derive from this class.
24 // We do this because services depend on each other and we need to control
25 // shutdown/destruction order. In each derived classes' constructors, the
26 // implementors must explicitly state which services are depended on.
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);
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);
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);
46 protected:
47 // ProfileKeyedServiceFactories must communicate with a
48 // ProfileDependencyManager. For all non-test code, write your subclass
49 // constructors like this:
51 // MyServiceFactory::MyServiceFactory()
52 // : ProfileKeyedServiceFactory(
53 // "MyService",
54 // ProfileDependencyManager::GetInstance())
55 // {}
56 ProfileKeyedServiceFactory(const char* name,
57 ProfileDependencyManager* manager);
58 virtual ~ProfileKeyedServiceFactory();
60 // Common implementation that maps |profile| to some service object. Deals
61 // with incognito profiles per subclass instructions with
62 // ServiceRedirectedInIncognito() and ServiceHasOwnInstanceInIncognito()
63 // through the GetProfileToUse() method on the base. If |create| is true,
64 // the service will be created using BuildServiceInstanceFor() if it doesn't
65 // already exist.
66 ProfileKeyedService* GetServiceForProfile(Profile* profile, bool create);
68 // Maps |profile| to |service| with debug checks to prevent duplication.
69 void Associate(Profile* profile, ProfileKeyedService* service);
71 // All subclasses of ProfileKeyedServiceFactory must return a
72 // ProfileKeyedService instead of just a ProfileKeyedBase.
73 virtual ProfileKeyedService* BuildServiceInstanceFor(
74 Profile* profile) const = 0;
76 // A helper object actually listens for notifications about Profile
77 // destruction, calculates the order in which things are destroyed and then
78 // does a two pass shutdown.
80 // First, ProfileShutdown() is called on every ServiceFactory and will
81 // usually call ProfileKeyedService::Shutdown(), which gives each
82 // ProfileKeyedService a chance to remove dependencies on other services that
83 // it may be holding.
85 // Secondly, ProfileDestroyed() is called on every ServiceFactory and the
86 // default implementation removes it from |mapping_| and deletes the pointer.
87 virtual void ProfileShutdown(Profile* profile) OVERRIDE;
88 virtual void ProfileDestroyed(Profile* profile) OVERRIDE;
90 virtual void SetEmptyTestingFactory(Profile* profile) OVERRIDE;
91 virtual void CreateServiceNow(Profile* profile) OVERRIDE;
93 private:
94 friend class ProfileDependencyManager;
95 friend class ProfileDependencyManagerUnittests;
97 typedef std::map<Profile*, ProfileKeyedService*> ProfileKeyedServices;
98 typedef std::map<Profile*, FactoryFunction> ProfileOverriddenFunctions;
100 // The mapping between a Profile and its service.
101 std::map<Profile*, ProfileKeyedService*> mapping_;
103 // The mapping between a Profile and its overridden FactoryFunction.
104 std::map<Profile*, FactoryFunction> factories_;
106 DISALLOW_COPY_AND_ASSIGN(ProfileKeyedServiceFactory);
109 #endif // CHROME_BROWSER_PROFILES_PROFILE_KEYED_SERVICE_FACTORY_H_