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 #include "chrome/browser/profiles/profile_keyed_service_factory.h"
9 #include "base/logging.h"
10 #include "base/stl_util.h"
11 #include "chrome/browser/profiles/profile.h"
12 #include "chrome/browser/profiles/profile_dependency_manager.h"
13 #include "chrome/browser/profiles/profile_keyed_service.h"
15 void ProfileKeyedServiceFactory::SetTestingFactory(Profile
* profile
,
16 FactoryFunction factory
) {
17 // Destroying the profile may cause us to lose data about whether |profile|
18 // has our preferences registered on it (since the profile object itself
19 // isn't dead). See if we need to readd it once we've gone through normal
21 bool add_profile
= ArePreferencesSetOn(profile
);
23 // We have to go through the shutdown and destroy mechanisms because there
24 // are unit tests that create a service on a profile and then change the
25 // testing service mid-test.
26 ProfileShutdown(profile
);
27 ProfileDestroyed(profile
);
30 MarkPreferencesSetOn(profile
);
32 factories_
[profile
] = factory
;
35 ProfileKeyedService
* ProfileKeyedServiceFactory::SetTestingFactoryAndUse(
37 FactoryFunction factory
) {
39 SetTestingFactory(profile
, factory
);
40 return GetServiceForProfile(profile
, true);
43 ProfileKeyedServiceFactory::ProfileKeyedServiceFactory(
44 const char* name
, ProfileDependencyManager
* manager
)
45 : ProfileKeyedBaseFactory(name
, manager
) {
48 ProfileKeyedServiceFactory::~ProfileKeyedServiceFactory() {
49 DCHECK(mapping_
.empty());
52 ProfileKeyedService
* ProfileKeyedServiceFactory::GetServiceForProfile(
55 profile
= GetProfileToUse(profile
);
59 // NOTE: If you modify any of the logic below, make sure to update the
60 // refcounted version in refcounted_profile_keyed_service_factory.cc!
61 ProfileKeyedServices::const_iterator it
= mapping_
.find(profile
);
62 if (it
!= mapping_
.end())
67 return NULL
; // And we're forbidden from creating one.
70 // Check to see if we have a per-Profile testing factory that we should use
71 // instead of default behavior.
72 ProfileKeyedService
* service
= NULL
;
73 ProfileOverriddenFunctions::const_iterator jt
= factories_
.find(profile
);
74 if (jt
!= factories_
.end()) {
76 if (!profile
->IsOffTheRecord())
77 RegisterUserPrefsOnProfile(profile
);
78 service
= jt
->second(profile
);
81 service
= BuildServiceInstanceFor(profile
);
84 Associate(profile
, service
);
88 void ProfileKeyedServiceFactory::Associate(Profile
* profile
,
89 ProfileKeyedService
* service
) {
90 DCHECK(!ContainsKey(mapping_
, profile
));
91 mapping_
.insert(std::make_pair(profile
, service
));
94 void ProfileKeyedServiceFactory::ProfileShutdown(Profile
* profile
) {
95 ProfileKeyedServices::iterator it
= mapping_
.find(profile
);
96 if (it
!= mapping_
.end() && it
->second
)
97 it
->second
->Shutdown();
100 void ProfileKeyedServiceFactory::ProfileDestroyed(Profile
* profile
) {
101 ProfileKeyedServices::iterator it
= mapping_
.find(profile
);
102 if (it
!= mapping_
.end()) {
107 // For unit tests, we also remove the factory function both so we don't
108 // maintain a big map of dead pointers, but also since we may have a second
109 // object that lives at the same address (see other comments about unit tests
111 factories_
.erase(profile
);
113 ProfileKeyedBaseFactory::ProfileDestroyed(profile
);
116 void ProfileKeyedServiceFactory::SetEmptyTestingFactory(
118 SetTestingFactory(profile
, NULL
);
121 void ProfileKeyedServiceFactory::CreateServiceNow(Profile
* profile
) {
122 GetServiceForProfile(profile
, true);