1a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)// Copyright 2014 The Chromium Authors. All rights reserved.
290dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)// Use of this source code is governed by a BSD-style license that can be
390dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)// found in the LICENSE file.
490dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)
5a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)#ifndef COMPONENTS_KEYED_SERVICE_CONTENT_BROWSER_CONTEXT_KEYED_SERVICE_FACTORY_H_
6a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)#define COMPONENTS_KEYED_SERVICE_CONTENT_BROWSER_CONTEXT_KEYED_SERVICE_FACTORY_H_
790dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)
890dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)#include <map>
990dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)
1090dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)#include "base/basictypes.h"
1190dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)#include "base/compiler_specific.h"
12a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)#include "components/keyed_service/content/browser_context_keyed_base_factory.h"
13a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)#include "components/keyed_service/core/keyed_service.h"
14a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)#include "components/keyed_service/core/keyed_service_export.h"
1590dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)
1690dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)class BrowserContextDependencyManager;
17a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)class KeyedService;
1890dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)
1990dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)// Base class for Factories that take a BrowserContext object and return some
2090dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)// service on a one-to-one mapping. Each factory that derives from this class
2190dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)// *must* be a Singleton (only unit tests don't do that).
2290dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)//
2390dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)// We do this because services depend on each other and we need to control
2490dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)// shutdown/destruction order. In each derived classes' constructors, the
2590dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)// implementors must explicitly state which services are depended on.
26a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)class KEYED_SERVICE_EXPORT BrowserContextKeyedServiceFactory
2790dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)    : public BrowserContextKeyedBaseFactory {
2890dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) public:
29a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  // A function that supplies the instance of a KeyedService for a given
30a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  // BrowserContext. This is used primarily for testing, where we want to feed
31a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  // a specific mock into the BCKSF system.
32a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  typedef KeyedService* (*TestingFactoryFunction)(
33a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)      content::BrowserContext* context);
3490dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)
3590dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  // Associates |factory| with |context| so that |factory| is used to create
36a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  // the KeyedService when requested.  |factory| can be NULL to signal that
37a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  // KeyedService should be NULL. Multiple calls to SetTestingFactory() are
38a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  // allowed; previous services will be shut down.
3990dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  void SetTestingFactory(content::BrowserContext* context,
40a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)                         TestingFactoryFunction factory);
4190dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)
4290dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  // Associates |factory| with |context| and immediately returns the created
43a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  // KeyedService. Since the factory will be used immediately, it may not be
44a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  // NULL.
45a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  KeyedService* SetTestingFactoryAndUse(content::BrowserContext* context,
46a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)                                        TestingFactoryFunction factory);
4790dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)
4890dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) protected:
4990dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  // BrowserContextKeyedServiceFactories must communicate with a
5090dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  // BrowserContextDependencyManager. For all non-test code, write your subclass
5190dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  // constructors like this:
5290dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  //
5390dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  //   MyServiceFactory::MyServiceFactory()
5490dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  //     : BrowserContextKeyedServiceFactory(
5590dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  //         "MyService",
5690dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  //         BrowserContextDependencyManager::GetInstance())
5790dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  //   {}
5890dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  BrowserContextKeyedServiceFactory(const char* name,
5990dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)                                    BrowserContextDependencyManager* manager);
6090dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  virtual ~BrowserContextKeyedServiceFactory();
6190dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)
6290dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  // Common implementation that maps |context| to some service object. Deals
6390dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  // with incognito contexts per subclass instructions with
64eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch  // GetBrowserContextRedirectedInIncognito() and
65eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch  // GetBrowserContextOwnInstanceInIncognito() through the
66eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch  // GetBrowserContextToUse() method on the base.  If |create| is true, the
67eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch  // service will be created using BuildServiceInstanceFor() if it doesn't
68eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch  // already exist.
69a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  KeyedService* GetServiceForBrowserContext(content::BrowserContext* context,
70a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)                                            bool create);
7190dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)
7290dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  // Maps |context| to |service| with debug checks to prevent duplication.
73a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  void Associate(content::BrowserContext* context, KeyedService* service);
7490dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)
75f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  // Removes the mapping from |context| to a service.
76f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  void Disassociate(content::BrowserContext* context);
77f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)
7890dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  // All subclasses of BrowserContextKeyedServiceFactory must return a
79a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  // KeyedService instead of just a BrowserContextKeyedBase.
80a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  virtual KeyedService* BuildServiceInstanceFor(
8190dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)      content::BrowserContext* context) const = 0;
8290dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)
8390dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  // A helper object actually listens for notifications about BrowserContext
8490dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  // destruction, calculates the order in which things are destroyed and then
8590dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  // does a two pass shutdown.
8690dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  //
8790dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  // First, BrowserContextShutdown() is called on every ServiceFactory and will
88a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  // usually call KeyedService::Shutdown(), which gives each
89a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  // KeyedService a chance to remove dependencies on other
9090dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  // services that it may be holding.
9190dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  //
9290dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  // Secondly, BrowserContextDestroyed() is called on every ServiceFactory
9390dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  // and the default implementation removes it from |mapping_| and deletes
9490dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  // the pointer.
95a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  virtual void BrowserContextShutdown(content::BrowserContext* context)
96a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)      OVERRIDE;
97a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  virtual void BrowserContextDestroyed(content::BrowserContext* context)
98a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)      OVERRIDE;
9990dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)
100a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  virtual void SetEmptyTestingFactory(content::BrowserContext* context)
101a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)      OVERRIDE;
102116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  virtual bool HasTestingFactory(content::BrowserContext* context) OVERRIDE;
10390dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  virtual void CreateServiceNow(content::BrowserContext* context) OVERRIDE;
10490dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)
10590dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) private:
10690dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  friend class BrowserContextDependencyManager;
10790dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  friend class BrowserContextDependencyManagerUnittests;
10890dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)
109a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  typedef std::map<content::BrowserContext*, KeyedService*>
11090dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)      BrowserContextKeyedServices;
111a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)  typedef std::map<content::BrowserContext*, TestingFactoryFunction>
112a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)      BrowserContextOverriddenTestingFunctions;
11390dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)
11490dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  // The mapping between a BrowserContext and its service.
115a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)  BrowserContextKeyedServices mapping_;
11690dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)
117a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)  // The mapping between a BrowserContext and its overridden
118a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)  // TestingFactoryFunction.
119a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)  BrowserContextOverriddenTestingFunctions testing_factories_;
12090dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)
12190dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  DISALLOW_COPY_AND_ASSIGN(BrowserContextKeyedServiceFactory);
12290dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)};
12390dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)
124a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)#endif  // COMPONENTS_KEYED_SERVICE_CONTENT_BROWSER_CONTEXT_KEYED_SERVICE_FACTORY_H_
125