| 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), 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 bool create) { |
| 27 // Possibly handle Incognito mode. | 28 // Possibly handle Incognito mode. |
| 28 if (profile->IsOffTheRecord()) { | 29 if (profile->IsOffTheRecord()) { |
| 29 if (ServiceRedirectedInIncognito()) { | 30 if (ServiceRedirectedInIncognito()) { |
| 30 profile = profile->GetOriginalProfile(); | 31 profile = profile->GetOriginalProfile(); |
| 31 } else if (ServiceHasOwnInstanceInIncognito()) { | 32 } else if (ServiceHasOwnInstanceInIncognito()) { |
| 32 // No-op; the pointers are already set correctly. | 33 // No-op; the pointers are already set correctly. |
| 33 } else { | 34 } else { |
| 34 return NULL; | 35 return NULL; |
| 35 } | 36 } |
| 36 } | 37 } |
| 37 | 38 |
| 38 ProfileKeyedService* service; | 39 ProfileKeyedService* service; |
| 39 | 40 |
| 40 std::map<Profile*, ProfileKeyedService*>::iterator it = | 41 std::map<Profile*, ProfileKeyedService*>::iterator it = |
| 41 mapping_.find(profile); | 42 mapping_.find(profile); |
| 42 if (it != mapping_.end()) { | 43 if (it != mapping_.end()) { |
| 43 service = it->second; | 44 service = it->second; |
| 44 if (service || !factory_) | 45 if (service || !factory_ || !create) |
| 45 return service; | 46 return service; |
| 46 | 47 |
| 47 // service is NULL but we have a mock factory function | 48 // service is NULL but we have a mock factory function |
| 48 mapping_.erase(it); | 49 mapping_.erase(it); |
| 49 service = factory_(profile); | 50 service = factory_(profile); |
| 51 } else if (create) { |
| 52 // not found but creation allowed |
| 53 service = BuildServiceInstanceFor(profile); |
| 50 } else { | 54 } else { |
| 51 service = BuildServiceInstanceFor(profile); | 55 // not found, creation forbidden |
| 56 return NULL; |
| 52 } | 57 } |
| 53 | 58 |
| 54 Associate(profile, service); | 59 Associate(profile, service); |
| 55 return service; | 60 return service; |
| 56 } | 61 } |
| 57 | 62 |
| 58 void ProfileKeyedServiceFactory::DependsOn(ProfileKeyedServiceFactory* rhs) { | 63 void ProfileKeyedServiceFactory::DependsOn(ProfileKeyedServiceFactory* rhs) { |
| 59 dependency_manager_->AddEdge(rhs, this); | 64 dependency_manager_->AddEdge(rhs, this); |
| 60 } | 65 } |
| 61 | 66 |
| (...skipping 19 matching lines...) Expand all Loading... |
| 81 } | 86 } |
| 82 | 87 |
| 83 void ProfileKeyedServiceFactory::ProfileDestroyed(Profile* profile) { | 88 void ProfileKeyedServiceFactory::ProfileDestroyed(Profile* profile) { |
| 84 std::map<Profile*, ProfileKeyedService*>::iterator it = | 89 std::map<Profile*, ProfileKeyedService*>::iterator it = |
| 85 mapping_.find(profile); | 90 mapping_.find(profile); |
| 86 if (it != mapping_.end()) { | 91 if (it != mapping_.end()) { |
| 87 delete it->second; | 92 delete it->second; |
| 88 mapping_.erase(it); | 93 mapping_.erase(it); |
| 89 } | 94 } |
| 90 } | 95 } |
| OLD | NEW |