| 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) { | 16 : dependency_manager_(manager), factory_(NULL) { |
| 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 |
| 38 std::map<Profile*, ProfileKeyedService*>::iterator it = | 40 std::map<Profile*, ProfileKeyedService*>::iterator it = |
| 39 mapping_.find(profile); | 41 mapping_.find(profile); |
| 40 if (it != mapping_.end()) | 42 if (it != mapping_.end()) { |
| 41 return it->second; | 43 service = it->second; |
| 44 if (service || !factory_) |
| 45 return service; |
| 42 | 46 |
| 43 ProfileKeyedService* service = BuildServiceInstanceFor(profile); | 47 // service is NULL but we have a mock factory function |
| 48 mapping_.erase(it); |
| 49 service = factory_(profile); |
| 50 } else { |
| 51 service = BuildServiceInstanceFor(profile); |
| 52 } |
| 53 |
| 44 Associate(profile, service); | 54 Associate(profile, service); |
| 45 return service; | 55 return service; |
| 46 } | 56 } |
| 47 | 57 |
| 48 void ProfileKeyedServiceFactory::DependsOn(ProfileKeyedServiceFactory* rhs) { | 58 void ProfileKeyedServiceFactory::DependsOn(ProfileKeyedServiceFactory* rhs) { |
| 49 dependency_manager_->AddEdge(rhs, this); | 59 dependency_manager_->AddEdge(rhs, this); |
| 50 } | 60 } |
| 51 | 61 |
| 52 void ProfileKeyedServiceFactory::Associate(Profile* profile, | 62 void ProfileKeyedServiceFactory::Associate(Profile* profile, |
| 53 ProfileKeyedService* service) { | 63 ProfileKeyedService* service) { |
| 54 DCHECK(mapping_.find(profile) == mapping_.end()); | 64 DCHECK(mapping_.find(profile) == mapping_.end()); |
| 55 mapping_.insert(std::make_pair(profile, service)); | 65 mapping_.insert(std::make_pair(profile, service)); |
| 56 } | 66 } |
| 57 | 67 |
| 58 bool ProfileKeyedServiceFactory::ServiceRedirectedInIncognito() { | 68 bool ProfileKeyedServiceFactory::ServiceRedirectedInIncognito() { |
| 59 return false; | 69 return false; |
| 60 } | 70 } |
| 61 | 71 |
| 62 bool ProfileKeyedServiceFactory::ServiceHasOwnInstanceInIncognito() { | 72 bool ProfileKeyedServiceFactory::ServiceHasOwnInstanceInIncognito() { |
| 63 return false; | 73 return false; |
| 64 } | 74 } |
| 65 | 75 |
| 66 void ProfileKeyedServiceFactory::ProfileShutdown(Profile* profile) { | 76 void ProfileKeyedServiceFactory::ProfileShutdown(Profile* profile) { |
| 67 std::map<Profile*, ProfileKeyedService*>::iterator it = | 77 std::map<Profile*, ProfileKeyedService*>::iterator it = |
| 68 mapping_.find(profile); | 78 mapping_.find(profile); |
| 69 if (it != mapping_.end()) | 79 if (it != mapping_.end() && it->second) |
| 70 it->second->Shutdown(); | 80 it->second->Shutdown(); |
| 71 } | 81 } |
| 72 | 82 |
| 73 void ProfileKeyedServiceFactory::ProfileDestroyed(Profile* profile) { | 83 void ProfileKeyedServiceFactory::ProfileDestroyed(Profile* profile) { |
| 74 std::map<Profile*, ProfileKeyedService*>::iterator it = | 84 std::map<Profile*, ProfileKeyedService*>::iterator it = |
| 75 mapping_.find(profile); | 85 mapping_.find(profile); |
| 76 if (it != mapping_.end()) { | 86 if (it != mapping_.end()) { |
| 77 delete it->second; | 87 delete it->second; |
| 78 mapping_.erase(it); | 88 mapping_.erase(it); |
| 79 } | 89 } |
| 80 } | 90 } |
| OLD | NEW |