Home | History | Annotate | Download | only in profiles
      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), factory_(NULL) {
     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   ProfileKeyedService* service;
     39 
     40   std::map<Profile*, ProfileKeyedService*>::iterator it =
     41       mapping_.find(profile);
     42   if (it != mapping_.end()) {
     43     service = it->second;
     44     if (service || !factory_)
     45       return service;
     46 
     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 
     54   Associate(profile, service);
     55   return service;
     56 }
     57 
     58 void ProfileKeyedServiceFactory::DependsOn(ProfileKeyedServiceFactory* rhs) {
     59   dependency_manager_->AddEdge(rhs, this);
     60 }
     61 
     62 void ProfileKeyedServiceFactory::Associate(Profile* profile,
     63                                            ProfileKeyedService* service) {
     64   DCHECK(mapping_.find(profile) == mapping_.end());
     65   mapping_.insert(std::make_pair(profile, service));
     66 }
     67 
     68 bool ProfileKeyedServiceFactory::ServiceRedirectedInIncognito() {
     69   return false;
     70 }
     71 
     72 bool ProfileKeyedServiceFactory::ServiceHasOwnInstanceInIncognito() {
     73   return false;
     74 }
     75 
     76 void ProfileKeyedServiceFactory::ProfileShutdown(Profile* profile) {
     77   std::map<Profile*, ProfileKeyedService*>::iterator it =
     78       mapping_.find(profile);
     79   if (it != mapping_.end() && it->second)
     80     it->second->Shutdown();
     81 }
     82 
     83 void ProfileKeyedServiceFactory::ProfileDestroyed(Profile* profile) {
     84   std::map<Profile*, ProfileKeyedService*>::iterator it =
     85       mapping_.find(profile);
     86   if (it != mapping_.end()) {
     87     delete it->second;
     88     mapping_.erase(it);
     89   }
     90 }
     91