Home | History | Annotate | Download | only in content
      1 // Copyright 2014 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 COMPONENTS_KEYED_SERVICE_CONTENT_BROWSER_CONTEXT_KEYED_BASE_FACTORY_H_
      6 #define COMPONENTS_KEYED_SERVICE_CONTENT_BROWSER_CONTEXT_KEYED_BASE_FACTORY_H_
      7 
      8 #include <set>
      9 
     10 #include "base/threading/non_thread_safe.h"
     11 #include "components/keyed_service/core/dependency_node.h"
     12 #include "components/keyed_service/core/keyed_service_export.h"
     13 
     14 class BrowserContextDependencyManager;
     15 class PrefService;
     16 
     17 namespace content {
     18 class BrowserContext;
     19 }
     20 
     21 namespace user_prefs {
     22 class PrefRegistrySyncable;
     23 }
     24 
     25 // Base class for Factories that take a BrowserContext object and return some
     26 // service.
     27 //
     28 // Unless you're trying to make a new type of Factory, you probably don't want
     29 // this class, but its subclasses: BrowserContextKeyedServiceFactory and
     30 // RefcountedBrowserContextKeyedServiceFactory. This object describes general
     31 // dependency management between Factories; subclasses react to lifecycle
     32 // events and implement memory management.
     33 class KEYED_SERVICE_EXPORT BrowserContextKeyedBaseFactory
     34     : public base::NonThreadSafe,
     35       NON_EXPORTED_BASE(public DependencyNode) {
     36  public:
     37   // Registers preferences used in this service on the pref service of
     38   // |context|. This is the public interface and is safe to be called multiple
     39   // times because testing code can have multiple services of the same type
     40   // attached to a single |context|. Only test code is allowed to call this
     41   // method.
     42   // TODO(gab): This method can be removed entirely when
     43   // PrefService::DeprecatedGetPrefRegistry() is phased out.
     44   void RegisterUserPrefsOnBrowserContextForTest(
     45       content::BrowserContext* context);
     46 
     47 #ifndef NDEBUG
     48   // Returns our name. We don't keep track of this in release mode.
     49   const char* name() const { return service_name_; }
     50 #endif
     51 
     52  protected:
     53   BrowserContextKeyedBaseFactory(const char* name,
     54                                  BrowserContextDependencyManager* manager);
     55   virtual ~BrowserContextKeyedBaseFactory();
     56 
     57   // The main public interface for declaring dependencies between services
     58   // created by factories.
     59   void DependsOn(BrowserContextKeyedBaseFactory* rhs);
     60 
     61   // Calls RegisterProfilePrefs() after doing house keeping required to work
     62   // alongside RegisterUserPrefsOnBrowserContextForTest().
     63   // TODO(gab): This method can be replaced by RegisterProfilePrefs() directly
     64   // once RegisterUserPrefsOnBrowserContextForTest() is phased out.
     65   void RegisterProfilePrefsIfNecessaryForContext(
     66       const content::BrowserContext* context,
     67       user_prefs::PrefRegistrySyncable* registry);
     68 
     69   // Interface for people building a concrete FooServiceFactory: --------------
     70 
     71   // Finds which browser context (if any) to use.
     72   virtual content::BrowserContext* GetBrowserContextToUse(
     73       content::BrowserContext* context) const;
     74 
     75   // By default, we create instances of a service lazily and wait until
     76   // GetForBrowserContext() is called on our subclass. Some services need to be
     77   // created as soon as the BrowserContext has been brought up.
     78   virtual bool ServiceIsCreatedWithBrowserContext() const;
     79 
     80   // By default, TestingBrowserContexts will be treated like normal contexts.
     81   // You can override this so that by default, the service associated with the
     82   // TestingBrowserContext is NULL. (This is just a shortcut around
     83   // SetTestingFactory() to make sure our contexts don't directly refer to the
     84   // services they use.)
     85   virtual bool ServiceIsNULLWhileTesting() const;
     86 
     87   // Interface for people building a type of BrowserContextKeyedFactory: -------
     88 
     89   // A helper object actually listens for notifications about BrowserContext
     90   // destruction, calculates the order in which things are destroyed and then
     91   // does a two pass shutdown.
     92   //
     93   // It is up to the individual factory types to determine what this two pass
     94   // shutdown means. The general framework guarantees the following:
     95   //
     96   // - Each BrowserContextShutdown() is called in dependency order (and you may
     97   //   reach out to other services during this phase).
     98   //
     99   // - Each BrowserContextDestroyed() is called in dependency order. We will
    100   //   NOTREACHED() if you attempt to GetForBrowserContext() any other service.
    101   //   You should delete/deref/do other final memory management things during
    102   //   this phase. You must also call the base class method as the last thing
    103   //   you do.
    104   virtual void BrowserContextShutdown(content::BrowserContext* context) = 0;
    105   virtual void BrowserContextDestroyed(content::BrowserContext* context);
    106 
    107   // Returns whether we've registered the preferences on this context.
    108   bool ArePreferencesSetOn(content::BrowserContext* context) const;
    109 
    110   // Mark context as Preferences set.
    111   void MarkPreferencesSetOn(content::BrowserContext* context);
    112 
    113   // Which BrowserContextDependencyManager we should communicate with.
    114   // In real code, this will always be
    115   // BrowserContextDependencyManager::GetInstance(), but unit tests will want
    116   // to use their own copy.
    117   BrowserContextDependencyManager* dependency_manager_;
    118 
    119  private:
    120   friend class BrowserContextDependencyManager;
    121   friend class BrowserContextDependencyManagerUnittests;
    122 
    123   // Registers any user preferences on this service. This is called by
    124   // RegisterProfilePrefsIfNecessary() and should be overriden by any service
    125   // that wants to register profile-specific preferences.
    126   virtual void RegisterProfilePrefs(
    127       user_prefs::PrefRegistrySyncable* registry) {}
    128 
    129   // These two methods are for tight integration with the
    130   // BrowserContextDependencyManager.
    131 
    132   // Because of ServiceIsNULLWhileTesting(), we need a way to tell different
    133   // subclasses that they should disable testing.
    134   virtual void SetEmptyTestingFactory(content::BrowserContext* context) = 0;
    135 
    136   // Returns true if a testing factory function has been set for |context|.
    137   virtual bool HasTestingFactory(content::BrowserContext* context) = 0;
    138 
    139   // We also need a generalized, non-returning method that generates the object
    140   // now for when we're creating the context.
    141   virtual void CreateServiceNow(content::BrowserContext* context) = 0;
    142 
    143   // BrowserContexts that have this service's preferences registered on them.
    144   std::set<const content::BrowserContext*> registered_preferences_;
    145 
    146 #if !defined(NDEBUG)
    147   // A static string passed in to our constructor. Should be unique across all
    148   // services. This is used only for debugging in debug mode. (We can print
    149   // pretty graphs with GraphViz with this information.)
    150   const char* service_name_;
    151 #endif
    152 };
    153 
    154 #endif  // COMPONENTS_KEYED_SERVICE_CONTENT_BROWSER_CONTEXT_KEYED_BASE_FACTORY_H_
    155