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

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

Issue 6813116: Revert 81277 - Profile shouldn't own DesktopNotificationService.DesktopNotificationService is now... (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src/
Patch Set: Created 9 years, 8 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) 2011 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2011 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 class Profile; 10 class Profile;
11 class ProfileDependencyManager; 11 class ProfileDependencyManager;
12 class ProfileKeyedService; 12 class ProfileKeyedService;
13 13
14 // Base class for Factories that take a Profile object and return some service 14 // Base class for Factories that take a Profile object and return some service
15 // on a one-to-one mapping. Each factory that derives from this class *must* 15 // on a one-to-one mapping. Each factory that derives from this class *must*
16 // be a Singleton (only unit tests don't do that). See ThemeServiceFactory as 16 // be a Singleton (only unit tests don't do that). See ThemeServiceFactory as
17 // an example of how to derive from this class. 17 // an example of how to derive from this class.
18 // 18 //
19 // We do this because services depend on each other and we need to control 19 // We do this because services depend on each other and we need to control
20 // shutdown/destruction order. In each derived classes' constructors, the 20 // shutdown/destruction order. In each derived classes' constructors, the
21 // implementors must explicitly state which services are depended on. 21 // implementors must explicitly state which services are depended on.
22 class ProfileKeyedServiceFactory { 22 class ProfileKeyedServiceFactory {
23 public: 23 protected:
24 typedef ProfileKeyedService* (*FactoryFunction)(Profile* profile); 24 friend class ProfileDependencyManager;
25 friend class ProfileDependencyManagerUnittests;
25 26
26 #if defined(UNIT_TEST)
27 // Associate an already-created |service| with |profile| for this factory.
28 // The service may be a mock, or may be NULL to inhibit automatic creation of
29 // the service by the default function. A mock factory set with
30 // |set_test_factory| will be called instead if the service is NULL.
31 void ForceAssociationBetween(Profile* profile, ProfileKeyedService* service) {
32 Associate(profile, service);
33 }
34
35 // Sets the factory function to use to create mock instances of this service.
36 // The factory function will only be called for profiles for which
37 // |ForceAssociationBetween| has been previously called with a NULL service
38 // pointer, and therefore does not affect normal non-test profiles.
39 void set_test_factory(FactoryFunction factory) { factory_ = factory; }
40 #endif
41
42 protected:
43 // ProfileKeyedServiceFactories must communicate with a 27 // ProfileKeyedServiceFactories must communicate with a
44 // ProfileDependencyManager. For all non-test code, write your subclass 28 // ProfileDependencyManager. For all non-test code, write your subclass
45 // constructors like this: 29 // constructors like this:
46 // 30 //
47 // MyServiceFactory::MyServiceFactory() 31 // MyServiceFactory::MyServiceFactory()
48 // : ProfileKeyedServiceFactory( 32 // : ProfileKeyedServiceFactory(
49 // ProfileDependencyManager::GetInstance()) 33 // ProfileDependencyManager::GetInstance())
50 // {} 34 // {}
51 explicit ProfileKeyedServiceFactory(ProfileDependencyManager* manager); 35 explicit ProfileKeyedServiceFactory(ProfileDependencyManager* manager);
52 virtual ~ProfileKeyedServiceFactory(); 36 virtual ~ProfileKeyedServiceFactory();
(...skipping 30 matching lines...) Expand all
83 // usually call ProfileKeyedService::Shutdown(), which gives each 67 // usually call ProfileKeyedService::Shutdown(), which gives each
84 // ProfileKeyedService a chance to remove dependencies on other services that 68 // ProfileKeyedService a chance to remove dependencies on other services that
85 // it may be holding. 69 // it may be holding.
86 // 70 //
87 // Secondly, ProfileDestroyed() is called on every ServiceFactory and the 71 // Secondly, ProfileDestroyed() is called on every ServiceFactory and the
88 // default implementation removes it from |mapping_| and deletes the pointer. 72 // default implementation removes it from |mapping_| and deletes the pointer.
89 virtual void ProfileShutdown(Profile* profile); 73 virtual void ProfileShutdown(Profile* profile);
90 virtual void ProfileDestroyed(Profile* profile); 74 virtual void ProfileDestroyed(Profile* profile);
91 75
92 private: 76 private:
93 friend class ProfileDependencyManager;
94 friend class ProfileDependencyManagerUnittests;
95
96 // The mapping between a Profile and its service. 77 // The mapping between a Profile and its service.
97 std::map<Profile*, ProfileKeyedService*> mapping_; 78 std::map<Profile*, ProfileKeyedService*> mapping_;
98 79
99 // Which ProfileDependencyManager we should communicate with. In real code, 80 // Which ProfileDependencyManager we should communicate with. In real code,
100 // this will always be ProfileDependencyManager::GetInstance(), but unit 81 // this will always be ProfileDependencyManager::GetInstance(), but unit
101 // tests will want to use their own copy. 82 // tests will want to use their own copy.
102 ProfileDependencyManager* dependency_manager_; 83 ProfileDependencyManager* dependency_manager_;
103
104 // A mock factory function to use to create the service, used by tests.
105 FactoryFunction factory_;
106 }; 84 };
107 85
108 #endif // CHROME_BROWSER_PROFILES_PROFILE_KEYED_SERVICE_FACTORY_H_ 86 #endif // CHROME_BROWSER_PROFILES_PROFILE_KEYED_SERVICE_FACTORY_H_
OLDNEW
« no previous file with comments | « chrome/browser/profiles/profile_impl.cc ('k') | chrome/browser/profiles/profile_keyed_service_factory.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698