| 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 std::map<Profile*, ProfileKeyedService*>::iterator it = | 38 std::map<Profile*, ProfileKeyedService*>::iterator it = |
| 39 mapping_.find(profile); | 39 mapping_.find(profile); |
| 40 if (it != mapping_.end()) | 40 if (it != mapping_.end()) |
| 41 return it->second; | 41 return it->second; |
| 42 | 42 |
| 43 ProfileKeyedService* service = BuildServiceInstanceFor(profile); | 43 ProfileKeyedService* service; |
| 44 if (factory_) |
| 45 service = factory_(profile); |
| 46 else |
| 47 service = BuildServiceInstanceFor(profile); |
| 44 Associate(profile, service); | 48 Associate(profile, service); |
| 45 return service; | 49 return service; |
| 46 } | 50 } |
| 47 | 51 |
| 48 void ProfileKeyedServiceFactory::DependsOn(ProfileKeyedServiceFactory* rhs) { | 52 void ProfileKeyedServiceFactory::DependsOn(ProfileKeyedServiceFactory* rhs) { |
| 49 dependency_manager_->AddEdge(rhs, this); | 53 dependency_manager_->AddEdge(rhs, this); |
| 50 } | 54 } |
| 51 | 55 |
| 52 void ProfileKeyedServiceFactory::Associate(Profile* profile, | 56 void ProfileKeyedServiceFactory::Associate(Profile* profile, |
| 53 ProfileKeyedService* service) { | 57 ProfileKeyedService* service) { |
| (...skipping 17 matching lines...) Expand all Loading... |
| 71 } | 75 } |
| 72 | 76 |
| 73 void ProfileKeyedServiceFactory::ProfileDestroyed(Profile* profile) { | 77 void ProfileKeyedServiceFactory::ProfileDestroyed(Profile* profile) { |
| 74 std::map<Profile*, ProfileKeyedService*>::iterator it = | 78 std::map<Profile*, ProfileKeyedService*>::iterator it = |
| 75 mapping_.find(profile); | 79 mapping_.find(profile); |
| 76 if (it != mapping_.end()) { | 80 if (it != mapping_.end()) { |
| 77 delete it->second; | 81 delete it->second; |
| 78 mapping_.erase(it); | 82 mapping_.erase(it); |
| 79 } | 83 } |
| 80 } | 84 } |
| OLD | NEW |