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