Revert 168224 - Update V8 to version 3.15.4.
[chromium-blink-merge.git] / chrome / browser / profiles / profile_keyed_service_factory.cc
blobd3ee4a547822849247be406f702d6cff646e780f
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"
7 #include <map>
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
20 // destruction.
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);
29 if (add_profile)
30 MarkPreferencesSetOn(profile);
32 factories_[profile] = factory;
35 ProfileKeyedService* ProfileKeyedServiceFactory::SetTestingFactoryAndUse(
36 Profile* profile,
37 FactoryFunction factory) {
38 DCHECK(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(
53 Profile* profile,
54 bool create) {
55 profile = GetProfileToUse(profile);
56 if (!profile)
57 return NULL;
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())
63 return it->second;
65 // Object not found.
66 if (!create)
67 return NULL; // And we're forbidden from creating one.
69 // Create new object.
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()) {
75 if (jt->second) {
76 if (!profile->IsOffTheRecord())
77 RegisterUserPrefsOnProfile(profile);
78 service = jt->second(profile);
80 } else {
81 service = BuildServiceInstanceFor(profile);
84 Associate(profile, service);
85 return 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()) {
103 delete it->second;
104 mapping_.erase(it);
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
110 // in this file).
111 factories_.erase(profile);
113 ProfileKeyedBaseFactory::ProfileDestroyed(profile);
116 void ProfileKeyedServiceFactory::SetEmptyTestingFactory(
117 Profile* profile) {
118 SetTestingFactory(profile, NULL);
121 void ProfileKeyedServiceFactory::CreateServiceNow(Profile* profile) {
122 GetServiceForProfile(profile, true);