Upstream version 11.40.271.0
[platform/framework/web/crosswalk.git] / src / components / keyed_service / core / refcounted_keyed_service_factory.h
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_CORE_REFCOUNTED_KEYED_SERVICE_FACTORY_H_
6 #define COMPONENTS_KEYED_SERVICE_CORE_REFCOUNTED_KEYED_SERVICE_FACTORY_H_
7
8 #include <map>
9
10 #include "base/basictypes.h"
11 #include "base/compiler_specific.h"
12 #include "base/memory/ref_counted.h"
13 #include "components/keyed_service/core/keyed_service_base_factory.h"
14 #include "components/keyed_service/core/keyed_service_export.h"
15
16 class RefcountedKeyedService;
17
18 // A specialized KeyedServiceBaseFactory that manages a RefcountedThreadSafe<>.
19 //
20 // While the factory returns RefcountedThreadSafe<>s, the factory itself is a
21 // base::NotThreadSafe. Only call methods on this object on the UI thread.
22 //
23 // Implementers of RefcountedKeyedService should note that we guarantee that
24 // ShutdownOnUIThread() is called on the UI thread, but actual object
25 // destruction can happen anywhere.
26 class KEYED_SERVICE_EXPORT RefcountedKeyedServiceFactory
27     : public KeyedServiceBaseFactory {
28  protected:
29   RefcountedKeyedServiceFactory(const char* name, DependencyManager* manager);
30   ~RefcountedKeyedServiceFactory() override;
31
32   // A function that supplies the instance of a KeyedService for a given
33   // |context|. This is used primarily for testing, where we want to feed
34   // a specific mock into the KeyedServiceFactory system.
35   typedef scoped_refptr<RefcountedKeyedService>(*TestingFactoryFunction)(
36       base::SupportsUserData* context);
37
38   // Associates |factory| with |context| so that |factory| is used to create
39   // the KeyedService when requested.  |factory| can be NULL to signal that
40   // KeyedService should be NULL. Multiple calls to SetTestingFactory() are
41   // allowed; previous services will be shut down.
42   void SetTestingFactory(base::SupportsUserData* context,
43                          TestingFactoryFunction factory);
44
45   // Associates |factory| with |context| and immediately returns the created
46   // KeyedService. Since the factory will be used immediately, it may not be
47   // NULL.
48   scoped_refptr<RefcountedKeyedService> SetTestingFactoryAndUse(
49       base::SupportsUserData* context,
50       TestingFactoryFunction factory);
51
52   // Common implementation that maps |context| to some service object. Deals
53   // with incognito contexts per subclass instructions with GetContextToUse()
54   // method on the base.  If |create| is true, the service will be created
55   // using BuildServiceInstanceFor() if it doesn't already exist.
56   scoped_refptr<RefcountedKeyedService> GetServiceForContext(
57       base::SupportsUserData* context,
58       bool create);
59
60   // Maps |context| to |service| with debug checks to prevent duplication.
61   void Associate(base::SupportsUserData* context,
62                  const scoped_refptr<RefcountedKeyedService>& service);
63
64   // Returns a new RefcountedKeyedService that will be associated with
65   // |context|.
66   virtual scoped_refptr<RefcountedKeyedService> BuildServiceInstanceFor(
67       base::SupportsUserData* context) const = 0;
68
69   // Returns whether the |context| is off-the-record or not.
70   virtual bool IsOffTheRecord(base::SupportsUserData* context) const = 0;
71
72   // KeyedServiceBaseFactory:
73   void ContextShutdown(base::SupportsUserData* context) override;
74   void ContextDestroyed(base::SupportsUserData* context) override;
75
76   void SetEmptyTestingFactory(base::SupportsUserData* context) override;
77   bool HasTestingFactory(base::SupportsUserData* context) override;
78   void CreateServiceNow(base::SupportsUserData* context) override;
79
80  private:
81   typedef std::map<base::SupportsUserData*,
82                    scoped_refptr<RefcountedKeyedService>> KeyedServices;
83   typedef std::map<base::SupportsUserData*, TestingFactoryFunction>
84       OverriddenTestingFunctions;
85
86   // The mapping between a context and its refcounted service.
87   KeyedServices mapping_;
88
89   // The mapping between a context and its overridden
90   // TestingFactoryFunction.
91   OverriddenTestingFunctions testing_factories_;
92
93   DISALLOW_COPY_AND_ASSIGN(RefcountedKeyedServiceFactory);
94 };
95
96 #endif  // COMPONENTS_KEYED_SERVICE_CORE_REFCOUNTED_KEYED_SERVICE_FACTORY_H_