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 #ifndef CHROME_BROWSER_PROFILES_PROFILE_KEYED_SERVICE_FACTORY_H_
      6 #define CHROME_BROWSER_PROFILES_PROFILE_KEYED_SERVICE_FACTORY_H_
      7 
      8 #include <map>
      9 
     10 class Profile;
     11 class ProfileDependencyManager;
     12 class ProfileKeyedService;
     13 
     14 // Base class for Factories that take a Profile object and return some service
     15 // on a one-to-one mapping. Each factory that derives from this class *must*
     16 // be a Singleton (only unit tests don't do that). See ThemeServiceFactory as
     17 // an example of how to derive from this class.
     18 //
     19 // We do this because services depend on each other and we need to control
     20 // shutdown/destruction order. In each derived classes' constructors, the
     21 // implementors must explicitly state which services are depended on.
     22 class ProfileKeyedServiceFactory {
     23  public:
     24   typedef ProfileKeyedService* (*FactoryFunction)(Profile* profile);
     25 
     26 #if defined(UNIT_TEST)
     27   // Associate an already-created |service| with |profile| for this factory.
     28   // The service may be a mock, or may be NULL to inhibit automatic creation of
     29   // the service by the default function. A mock factory set with
     30   // |set_test_factory| will be called instead if the service is NULL.
     31   void ForceAssociationBetween(Profile* profile, ProfileKeyedService* service) {
     32     Associate(profile, service);
     33   }
     34 
     35   // Sets the factory function to use to create mock instances of this service.
     36   // The factory function will only be called for profiles for which
     37   // |ForceAssociationBetween| has been previously called with a NULL service
     38   // pointer, and therefore does not affect normal non-test profiles.
     39   void set_test_factory(FactoryFunction factory) { factory_ = factory; }
     40 #endif
     41 
     42  protected:
     43   // ProfileKeyedServiceFactories must communicate with a
     44   // ProfileDependencyManager. For all non-test code, write your subclass
     45   // constructors like this:
     46   //
     47   //   MyServiceFactory::MyServiceFactory()
     48   //     : ProfileKeyedServiceFactory(
     49   //         ProfileDependencyManager::GetInstance())
     50   //   {}
     51   explicit ProfileKeyedServiceFactory(ProfileDependencyManager* manager);
     52   virtual ~ProfileKeyedServiceFactory();
     53 
     54   // Common implementation that maps |profile| to some service object. Deals
     55   // with incognito profiles per subclass instructions with
     56   // ServiceActiveInIncognito().
     57   ProfileKeyedService* GetServiceForProfile(Profile* profile);
     58 
     59   // The main public interface for declaring dependencies between services
     60   // created by factories.
     61   void DependsOn(ProfileKeyedServiceFactory* rhs);
     62 
     63   // Maps |profile| to |provider| with debug checks to prevent duplication.
     64   void Associate(Profile* profile, ProfileKeyedService* service);
     65 
     66   // Returns a new instance of the service, casted to void* for our common
     67   // storage.
     68   virtual ProfileKeyedService* BuildServiceInstanceFor(
     69       Profile* profile) const = 0;
     70 
     71   // By default, if we are asked for a service with an Incognito profile, we
     72   // pass back NULL. To redirect to the Incognito's original profile or to
     73   // create another instance, even for Incognito windows, override one of the
     74   // following methods:
     75   virtual bool ServiceRedirectedInIncognito();
     76   virtual bool ServiceHasOwnInstanceInIncognito();
     77 
     78   // A helper object actually listens for notifications about Profile
     79   // destruction, calculates the order in which things are destroyed and then
     80   // does a two pass shutdown.
     81   //
     82   // First, ProfileShutdown() is called on every ServiceFactory and will
     83   // usually call ProfileKeyedService::Shutdown(), which gives each
     84   // ProfileKeyedService a chance to remove dependencies on other services that
     85   // it may be holding.
     86   //
     87   // Secondly, ProfileDestroyed() is called on every ServiceFactory and the
     88   // default implementation removes it from |mapping_| and deletes the pointer.
     89   virtual void ProfileShutdown(Profile* profile);
     90   virtual void ProfileDestroyed(Profile* profile);
     91 
     92  private:
     93   friend class ProfileDependencyManager;
     94   friend class ProfileDependencyManagerUnittests;
     95 
     96   // The mapping between a Profile and its service.
     97   std::map<Profile*, ProfileKeyedService*> mapping_;
     98 
     99   // Which ProfileDependencyManager we should communicate with. In real code,
    100   // this will always be ProfileDependencyManager::GetInstance(), but unit
    101   // tests will want to use their own copy.
    102   ProfileDependencyManager* dependency_manager_;
    103 
    104   // A mock factory function to use to create the service, used by tests.
    105   FactoryFunction factory_;
    106 };
    107 
    108 #endif  // CHROME_BROWSER_PROFILES_PROFILE_KEYED_SERVICE_FACTORY_H_
    109