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

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

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 #include "chrome/browser/profiles/profile_keyed_service_factory.h" 5 #include "chrome/browser/profiles/profile_keyed_service_factory.h"
6 6
7 #include <map> 7 #include <map>
8 8
9 #include "base/memory/singleton.h" 9 #include "base/memory/singleton.h"
10 #include "chrome/browser/profiles/profile.h" 10 #include "chrome/browser/profiles/profile.h"
11 #include "chrome/browser/profiles/profile_dependency_manager.h" 11 #include "chrome/browser/profiles/profile_dependency_manager.h"
12 #include "chrome/browser/profiles/profile_keyed_service.h" 12 #include "chrome/browser/profiles/profile_keyed_service.h"
13 13
14 void ProfileKeyedServiceFactory::SetTestingFactory(Profile* profile,
15 FactoryFunction factory) {
16 // Destroying the profile may cause us to lose data about whether |profile|
17 // has our preferences registered on it (since the profile object itself
18 // isn't dead). See if we need to readd it once we've gone through normal
19 // destruction.
20 bool add_profile = ArePreferencesSetOn(profile);
21
22 // We have to go through the shutdown and destroy mechanisms because there
23 // are unit tests that create a service on a profile and then change the
24 // testing service mid-test.
25 ProfileShutdown(profile);
26 ProfileDestroyed(profile);
27
28 if (add_profile)
29 MarkPreferencesSetOn(profile);
30
31 factories_[profile] = factory;
32 }
33
34 ProfileKeyedService* ProfileKeyedServiceFactory::SetTestingFactoryAndUse(
35 Profile* profile,
36 FactoryFunction factory) {
37 DCHECK(factory);
38 SetTestingFactory(profile, factory);
39 return GetServiceForProfile(profile, true);
40 }
41
14 ProfileKeyedServiceFactory::ProfileKeyedServiceFactory( 42 ProfileKeyedServiceFactory::ProfileKeyedServiceFactory(
15 const char* name, ProfileDependencyManager* manager) 43 const char* name, ProfileDependencyManager* manager)
16 : ProfileKeyedBaseFactory(name, manager) { 44 : ProfileKeyedBaseFactory(name, manager) {
17 } 45 }
18 46
19 ProfileKeyedServiceFactory::~ProfileKeyedServiceFactory() { 47 ProfileKeyedServiceFactory::~ProfileKeyedServiceFactory() {
20 DCHECK(mapping_.empty()); 48 DCHECK(mapping_.empty());
21 } 49 }
22 50
23 ProfileKeyedService* ProfileKeyedServiceFactory::GetServiceForProfile( 51 ProfileKeyedService* ProfileKeyedServiceFactory::GetServiceForProfile(
24 Profile* profile, 52 Profile* profile,
25 bool create) { 53 bool create) {
26 return static_cast<ProfileKeyedService*>(GetBaseForProfile(profile, create)); 54 profile = GetProfileToUse(profile);
55 if (!profile)
56 return NULL;
57
58 // NOTE: If you modify any of the logic below, make sure to update the
59 // refcounted version in refcounted_profile_keyed_service_factory.cc!
60 ProfileKeyedService* service = NULL;
61 std::map<Profile*, ProfileKeyedService*>::const_iterator it =
62 mapping_.find(profile);
63 if (it != mapping_.end()) {
64 return it->second;
65 } else if (create) {
66 // Object not found, and we must create it.
67 //
68 // Check to see if we have a per-Profile testing factory that we should use
69 // instead of default behavior.
70 std::map<Profile*, FactoryFunction>::iterator jt = factories_.find(profile);
71 if (jt != factories_.end()) {
72 if (jt->second) {
73 if (!profile->IsOffTheRecord())
74 RegisterUserPrefsOnProfile(profile);
75 service = jt->second(profile);
76 } else {
77 service = NULL;
78 }
79 } else {
80 service = BuildServiceInstanceFor(profile);
81 }
82 } else {
83 // Object not found, and we're forbidden from creating one.
84 return NULL;
85 }
86
87 Associate(profile, service);
88 return service;
27 } 89 }
28 90
29 void ProfileKeyedServiceFactory::Associate(Profile* profile, 91 void ProfileKeyedServiceFactory::Associate(Profile* profile,
30 ProfileKeyedBase* service) { 92 ProfileKeyedService* service) {
31 DCHECK(mapping_.find(profile) == mapping_.end()); 93 DCHECK(mapping_.find(profile) == mapping_.end());
32 mapping_.insert(std::make_pair( 94 mapping_.insert(std::make_pair(profile, service));
33 profile, static_cast<ProfileKeyedService*>(service)));
34 }
35
36 bool ProfileKeyedServiceFactory::GetAssociation(
37 Profile* profile,
38 ProfileKeyedBase** out) const {
39 bool found = false;
40 std::map<Profile*, ProfileKeyedService*>::const_iterator it =
41 mapping_.find(profile);
42 if (it != mapping_.end()) {
43 found = true;
44 if (out)
45 *out = it->second;
46 }
47 return found;
48 } 95 }
49 96
50 void ProfileKeyedServiceFactory::ProfileShutdown(Profile* profile) { 97 void ProfileKeyedServiceFactory::ProfileShutdown(Profile* profile) {
51 std::map<Profile*, ProfileKeyedService*>::iterator it = 98 std::map<Profile*, ProfileKeyedService*>::iterator it =
52 mapping_.find(profile); 99 mapping_.find(profile);
53 if (it != mapping_.end() && it->second) 100 if (it != mapping_.end() && it->second)
54 it->second->Shutdown(); 101 it->second->Shutdown();
55 } 102 }
56 103
57 void ProfileKeyedServiceFactory::ProfileDestroyed(Profile* profile) { 104 void ProfileKeyedServiceFactory::ProfileDestroyed(Profile* profile) {
58 std::map<Profile*, ProfileKeyedService*>::iterator it = 105 std::map<Profile*, ProfileKeyedService*>::iterator it =
59 mapping_.find(profile); 106 mapping_.find(profile);
60 if (it != mapping_.end()) { 107 if (it != mapping_.end()) {
61 delete it->second; 108 delete it->second;
62 mapping_.erase(it); 109 mapping_.erase(it);
63 } 110 }
64 111
112 // For unit tests, we also remove the factory function both so we don't
113 // maintain a big map of dead pointers, but also since we may have a second
114 // object that lives at the same address (see other comments about unit tests
115 // in this file).
116 factories_.erase(profile);
117
65 ProfileKeyedBaseFactory::ProfileDestroyed(profile); 118 ProfileKeyedBaseFactory::ProfileDestroyed(profile);
66 } 119 }
120
121 void ProfileKeyedServiceFactory::SetEmptyTestingFactory(
122 Profile* profile) {
123 SetTestingFactory(profile, NULL);
124 }
125
126 void ProfileKeyedServiceFactory::CreateServiceNow(Profile* profile) {
127 GetServiceForProfile(profile, true);
128 }
OLDNEW
« no previous file with comments | « chrome/browser/profiles/profile_keyed_service_factory.h ('k') | chrome/browser/profiles/refcounted_profile_keyed_service.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698