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_SERVICE_FACTORY_H_
6#define COMPONENTS_KEYED_SERVICE_CONTENT_BROWSER_CONTEXT_KEYED_SERVICE_FACTORY_H_
7
8#include <map>
9
10#include "base/basictypes.h"
11#include "base/compiler_specific.h"
12#include "components/keyed_service/content/browser_context_keyed_base_factory.h"
13#include "components/keyed_service/core/keyed_service.h"
14#include "components/keyed_service/core/keyed_service_export.h"
15
16class BrowserContextDependencyManager;
17class KeyedService;
18
19// Base class for Factories that take a BrowserContext object and return some
20// service on a one-to-one mapping. Each factory that derives from this class
21// *must* be a Singleton (only unit tests don't do that).
22//
23// We do this because services depend on each other and we need to control
24// shutdown/destruction order. In each derived classes' constructors, the
25// implementors must explicitly state which services are depended on.
26class KEYED_SERVICE_EXPORT BrowserContextKeyedServiceFactory
27    : public BrowserContextKeyedBaseFactory {
28 public:
29  // A function that supplies the instance of a KeyedService for a given
30  // BrowserContext. This is used primarily for testing, where we want to feed
31  // a specific mock into the BCKSF system.
32  typedef KeyedService* (*TestingFactoryFunction)(
33      content::BrowserContext* context);
34
35  // Associates |factory| with |context| so that |factory| is used to create
36  // the KeyedService when requested.  |factory| can be NULL to signal that
37  // KeyedService should be NULL. Multiple calls to SetTestingFactory() are
38  // allowed; previous services will be shut down.
39  void SetTestingFactory(content::BrowserContext* context,
40                         TestingFactoryFunction factory);
41
42  // Associates |factory| with |context| and immediately returns the created
43  // KeyedService. Since the factory will be used immediately, it may not be
44  // NULL.
45  KeyedService* SetTestingFactoryAndUse(content::BrowserContext* context,
46                                        TestingFactoryFunction factory);
47
48 protected:
49  // BrowserContextKeyedServiceFactories must communicate with a
50  // BrowserContextDependencyManager. For all non-test code, write your subclass
51  // constructors like this:
52  //
53  //   MyServiceFactory::MyServiceFactory()
54  //     : BrowserContextKeyedServiceFactory(
55  //         "MyService",
56  //         BrowserContextDependencyManager::GetInstance())
57  //   {}
58  BrowserContextKeyedServiceFactory(const char* name,
59                                    BrowserContextDependencyManager* manager);
60  virtual ~BrowserContextKeyedServiceFactory();
61
62  // Common implementation that maps |context| to some service object. Deals
63  // with incognito contexts per subclass instructions with
64  // GetBrowserContextRedirectedInIncognito() and
65  // GetBrowserContextOwnInstanceInIncognito() through the
66  // GetBrowserContextToUse() method on the base.  If |create| is true, the
67  // service will be created using BuildServiceInstanceFor() if it doesn't
68  // already exist.
69  KeyedService* GetServiceForBrowserContext(content::BrowserContext* context,
70                                            bool create);
71
72  // Maps |context| to |service| with debug checks to prevent duplication.
73  void Associate(content::BrowserContext* context, KeyedService* service);
74
75  // Removes the mapping from |context| to a service.
76  void Disassociate(content::BrowserContext* context);
77
78  // All subclasses of BrowserContextKeyedServiceFactory must return a
79  // KeyedService instead of just a BrowserContextKeyedBase.
80  virtual KeyedService* BuildServiceInstanceFor(
81      content::BrowserContext* context) const = 0;
82
83  // A helper object actually listens for notifications about BrowserContext
84  // destruction, calculates the order in which things are destroyed and then
85  // does a two pass shutdown.
86  //
87  // First, BrowserContextShutdown() is called on every ServiceFactory and will
88  // usually call KeyedService::Shutdown(), which gives each
89  // KeyedService a chance to remove dependencies on other
90  // services that it may be holding.
91  //
92  // Secondly, BrowserContextDestroyed() is called on every ServiceFactory
93  // and the default implementation removes it from |mapping_| and deletes
94  // the pointer.
95  virtual void BrowserContextShutdown(content::BrowserContext* context)
96      OVERRIDE;
97  virtual void BrowserContextDestroyed(content::BrowserContext* context)
98      OVERRIDE;
99
100  virtual void SetEmptyTestingFactory(content::BrowserContext* context)
101      OVERRIDE;
102  virtual bool HasTestingFactory(content::BrowserContext* context) OVERRIDE;
103  virtual void CreateServiceNow(content::BrowserContext* context) OVERRIDE;
104
105 private:
106  friend class BrowserContextDependencyManager;
107  friend class BrowserContextDependencyManagerUnittests;
108
109  typedef std::map<content::BrowserContext*, KeyedService*>
110      BrowserContextKeyedServices;
111  typedef std::map<content::BrowserContext*, TestingFactoryFunction>
112      BrowserContextOverriddenTestingFunctions;
113
114  // The mapping between a BrowserContext and its service.
115  BrowserContextKeyedServices mapping_;
116
117  // The mapping between a BrowserContext and its overridden
118  // TestingFactoryFunction.
119  BrowserContextOverriddenTestingFunctions testing_factories_;
120
121  DISALLOW_COPY_AND_ASSIGN(BrowserContextKeyedServiceFactory);
122};
123
124#endif  // COMPONENTS_KEYED_SERVICE_CONTENT_BROWSER_CONTEXT_KEYED_SERVICE_FACTORY_H_
125