summaryrefslogtreecommitdiffstats
path: root/chrome/browser/policy/profile_policy_connector_factory.h
blob: b7ea5e93eecadd5cec823d9867a0421862b3be9f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
// Copyright (c) 2013 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef CHROME_BROWSER_POLICY_PROFILE_POLICY_CONNECTOR_FACTORY_H_
#define CHROME_BROWSER_POLICY_PROFILE_POLICY_CONNECTOR_FACTORY_H_

#include <list>
#include <map>

#include "base/macros.h"
#include "base/memory/scoped_ptr.h"
#include "components/keyed_service/content/browser_context_keyed_base_factory.h"

namespace base {
template <typename T>
struct DefaultSingletonTraits;

class SequencedTaskRunner;
}  // namespace base

namespace content {
class BrowserContext;
}

namespace policy {

class ConfigurationPolicyProvider;
class ProfilePolicyConnector;

// Creates ProfilePolicyConnectors for Profiles, which manage the common
// policy providers and other policy components.
// TODO(joaodasilva): convert this class to a proper BCKS once the PrefService,
// which depends on this class, becomes a BCKS too.
class ProfilePolicyConnectorFactory : public BrowserContextKeyedBaseFactory {
 public:
  // Returns the ProfilePolicyConnectorFactory singleton.
  static ProfilePolicyConnectorFactory* GetInstance();

  // Returns the ProfilePolicyConnector associated with |context|. This is only
  // valid before |context| is shut down.
  static ProfilePolicyConnector* GetForBrowserContext(
      content::BrowserContext* context);

  // Creates a new ProfilePolicyConnector for |context|, which must be managed
  // by the caller. Subsequent calls to GetForBrowserContext() will return the
  // instance created, as long as it lives.
  // If |force_immediate_load| is true then policy is loaded synchronously on
  // startup.
  static scoped_ptr<ProfilePolicyConnector> CreateForBrowserContext(
      content::BrowserContext* context,
      bool force_immediate_load);

  // Overrides the |connector| for the given |context|; use only in tests.
  // Once this class becomes a proper BCKS then it can reuse the testing
  // methods of BrowserContextKeyedServiceFactory.
  void SetServiceForTesting(content::BrowserContext* context,
                            ProfilePolicyConnector* connector);

  // The next Profile to call CreateForBrowserContext() will get a PolicyService
  // with |provider| as its sole policy provider. This can be called multiple
  // times to override the policy providers for more than 1 Profile.
  void PushProviderForTesting(ConfigurationPolicyProvider* provider);

 private:
  friend struct base::DefaultSingletonTraits<ProfilePolicyConnectorFactory>;

  ProfilePolicyConnectorFactory();
  ~ProfilePolicyConnectorFactory() override;

  ProfilePolicyConnector* GetForBrowserContextInternal(
      content::BrowserContext* context);

  scoped_ptr<ProfilePolicyConnector> CreateForBrowserContextInternal(
      content::BrowserContext* context,
      bool force_immediate_load);

  // BrowserContextKeyedBaseFactory:
  void BrowserContextShutdown(content::BrowserContext* context) override;
  void BrowserContextDestroyed(content::BrowserContext* context) override;
  void SetEmptyTestingFactory(content::BrowserContext* context) override;
  bool HasTestingFactory(content::BrowserContext* context) override;
  void CreateServiceNow(content::BrowserContext* context) override;

  typedef std::map<content::BrowserContext*, ProfilePolicyConnector*>
      ConnectorMap;
  ConnectorMap connectors_;
  std::list<ConfigurationPolicyProvider*> test_providers_;

  DISALLOW_COPY_AND_ASSIGN(ProfilePolicyConnectorFactory);
};

}  // namespace policy

#endif  // CHROME_BROWSER_POLICY_PROFILE_POLICY_CONNECTOR_FACTORY_H_