| OLD | NEW |
| 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 #include "chrome/browser/profiles/profile_keyed_service_factory.h" | 5 #include "chrome/browser/profiles/profile_keyed_service_factory.h" |
| 6 | 6 |
| 7 #include <vector> | 7 #include <vector> |
| 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 ProfileKeyedServiceFactory::ProfileKeyedServiceFactory( | 14 ProfileKeyedServiceFactory::ProfileKeyedServiceFactory( |
| 15 ProfileDependencyManager* manager) | 15 ProfileDependencyManager* manager) |
| 16 : dependency_manager_(manager), factory_(NULL) { | 16 : dependency_manager_(manager) { |
| 17 dependency_manager_->AddComponent(this); | 17 dependency_manager_->AddComponent(this); |
| 18 } | 18 } |
| 19 | 19 |
| 20 ProfileKeyedServiceFactory::~ProfileKeyedServiceFactory() { | 20 ProfileKeyedServiceFactory::~ProfileKeyedServiceFactory() { |
| 21 dependency_manager_->RemoveComponent(this); | 21 dependency_manager_->RemoveComponent(this); |
| 22 DCHECK(mapping_.empty()); | 22 DCHECK(mapping_.empty()); |
| 23 } | 23 } |
| 24 | 24 |
| 25 ProfileKeyedService* ProfileKeyedServiceFactory::GetServiceForProfile( | 25 ProfileKeyedService* ProfileKeyedServiceFactory::GetServiceForProfile( |
| 26 Profile* profile) { | 26 Profile* profile) { |
| 27 // Possibly handle Incognito mode. | 27 // Possibly handle Incognito mode. |
| 28 if (profile->IsOffTheRecord()) { | 28 if (profile->IsOffTheRecord()) { |
| 29 if (ServiceRedirectedInIncognito()) { | 29 if (ServiceRedirectedInIncognito()) { |
| 30 profile = profile->GetOriginalProfile(); | 30 profile = profile->GetOriginalProfile(); |
| 31 } else if (ServiceHasOwnInstanceInIncognito()) { | 31 } else if (ServiceHasOwnInstanceInIncognito()) { |
| 32 // No-op; the pointers are already set correctly. | 32 // No-op; the pointers are already set correctly. |
| 33 } else { | 33 } else { |
| 34 return NULL; | 34 return NULL; |
| 35 } | 35 } |
| 36 } | 36 } |
| 37 | 37 |
| 38 ProfileKeyedService* service; | |
| 39 | |
| 40 std::map<Profile*, ProfileKeyedService*>::iterator it = | 38 std::map<Profile*, ProfileKeyedService*>::iterator it = |
| 41 mapping_.find(profile); | 39 mapping_.find(profile); |
| 42 if (it != mapping_.end()) { | 40 if (it != mapping_.end()) |
| 43 service = it->second; | 41 return it->second; |
| 44 if (service || !factory_) | |
| 45 return service; | |
| 46 | 42 |
| 47 // service is NULL but we have a mock factory function | 43 ProfileKeyedService* service = BuildServiceInstanceFor(profile); |
| 48 mapping_.erase(it); | |
| 49 service = factory_(profile); | |
| 50 } else { | |
| 51 service = BuildServiceInstanceFor(profile); | |
| 52 } | |
| 53 | |
| 54 Associate(profile, service); | 44 Associate(profile, service); |
| 55 return service; | 45 return service; |
| 56 } | 46 } |
| 57 | 47 |
| 58 void ProfileKeyedServiceFactory::DependsOn(ProfileKeyedServiceFactory* rhs) { | 48 void ProfileKeyedServiceFactory::DependsOn(ProfileKeyedServiceFactory* rhs) { |
| 59 dependency_manager_->AddEdge(rhs, this); | 49 dependency_manager_->AddEdge(rhs, this); |
| 60 } | 50 } |
| 61 | 51 |
| 62 void ProfileKeyedServiceFactory::Associate(Profile* profile, | 52 void ProfileKeyedServiceFactory::Associate(Profile* profile, |
| 63 ProfileKeyedService* service) { | 53 ProfileKeyedService* service) { |
| 64 DCHECK(mapping_.find(profile) == mapping_.end()); | 54 DCHECK(mapping_.find(profile) == mapping_.end()); |
| 65 mapping_.insert(std::make_pair(profile, service)); | 55 mapping_.insert(std::make_pair(profile, service)); |
| 66 } | 56 } |
| 67 | 57 |
| 68 bool ProfileKeyedServiceFactory::ServiceRedirectedInIncognito() { | 58 bool ProfileKeyedServiceFactory::ServiceRedirectedInIncognito() { |
| 69 return false; | 59 return false; |
| 70 } | 60 } |
| 71 | 61 |
| 72 bool ProfileKeyedServiceFactory::ServiceHasOwnInstanceInIncognito() { | 62 bool ProfileKeyedServiceFactory::ServiceHasOwnInstanceInIncognito() { |
| 73 return false; | 63 return false; |
| 74 } | 64 } |
| 75 | 65 |
| 76 void ProfileKeyedServiceFactory::ProfileShutdown(Profile* profile) { | 66 void ProfileKeyedServiceFactory::ProfileShutdown(Profile* profile) { |
| 77 std::map<Profile*, ProfileKeyedService*>::iterator it = | 67 std::map<Profile*, ProfileKeyedService*>::iterator it = |
| 78 mapping_.find(profile); | 68 mapping_.find(profile); |
| 79 if (it != mapping_.end() && it->second) | 69 if (it != mapping_.end()) |
| 80 it->second->Shutdown(); | 70 it->second->Shutdown(); |
| 81 } | 71 } |
| 82 | 72 |
| 83 void ProfileKeyedServiceFactory::ProfileDestroyed(Profile* profile) { | 73 void ProfileKeyedServiceFactory::ProfileDestroyed(Profile* profile) { |
| 84 std::map<Profile*, ProfileKeyedService*>::iterator it = | 74 std::map<Profile*, ProfileKeyedService*>::iterator it = |
| 85 mapping_.find(profile); | 75 mapping_.find(profile); |
| 86 if (it != mapping_.end()) { | 76 if (it != mapping_.end()) { |
| 87 delete it->second; | 77 delete it->second; |
| 88 mapping_.erase(it); | 78 mapping_.erase(it); |
| 89 } | 79 } |
| 90 } | 80 } |
| OLD | NEW |