1// Copyright (c) 2013 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 CHROME_BROWSER_CHROMEOS_POLICY_USER_CLOUD_POLICY_MANAGER_FACTORY_CHROMEOS_H_
6#define CHROME_BROWSER_CHROMEOS_POLICY_USER_CLOUD_POLICY_MANAGER_FACTORY_CHROMEOS_H_
7
8#include <map>
9
10#include "base/basictypes.h"
11#include "base/memory/ref_counted.h"
12#include "base/memory/singleton.h"
13#include "components/keyed_service/content/browser_context_keyed_base_factory.h"
14
15class Profile;
16
17namespace base {
18class SequencedTaskRunner;
19}
20
21namespace content {
22class BrowserContext;
23}
24
25namespace policy {
26
27class UserCloudPolicyManagerChromeOS;
28
29// BrowserContextKeyedBaseFactory implementation
30// for UserCloudPolicyManagerChromeOS instances that initialize per-profile
31// cloud policy settings on ChromeOS.
32//
33// UserCloudPolicyManagerChromeOS is handled different than other
34// KeyedServices because it is a dependency of PrefService.
35// Therefore, lifetime of instances is managed by Profile, Profile startup code
36// invokes CreateForProfile() explicitly, takes ownership, and the instance
37// is only deleted after PrefService destruction.
38//
39// TODO(mnissler): Remove the special lifetime management in favor of
40// PrefService directly depending on UserCloudPolicyManagerChromeOS once the
41// former has been converted to a KeyedService.
42// See also http://crbug.com/131843 and http://crbug.com/131844.
43class UserCloudPolicyManagerFactoryChromeOS
44    : public BrowserContextKeyedBaseFactory {
45 public:
46  // Returns an instance of the UserCloudPolicyManagerFactoryChromeOS singleton.
47  static UserCloudPolicyManagerFactoryChromeOS* GetInstance();
48
49  // Returns the UserCloudPolicyManagerChromeOS instance associated with
50  // |profile|.
51  static UserCloudPolicyManagerChromeOS* GetForProfile(Profile* profile);
52
53  // Creates an instance for |profile|. Note that the caller is responsible for
54  // managing the lifetime of the instance. Subsequent calls to GetForProfile()
55  // will return the created instance as long as it lives.
56  //
57  // If |force_immediate_load| is true, policy is loaded synchronously from
58  // UserCloudPolicyStore at startup.
59  static scoped_ptr<UserCloudPolicyManagerChromeOS> CreateForProfile(
60      Profile* profile,
61      bool force_immediate_load,
62      scoped_refptr<base::SequencedTaskRunner> background_task_runner);
63
64 private:
65  friend struct DefaultSingletonTraits<UserCloudPolicyManagerFactoryChromeOS>;
66
67  UserCloudPolicyManagerFactoryChromeOS();
68  virtual ~UserCloudPolicyManagerFactoryChromeOS();
69
70  // See comments for the static versions above.
71  UserCloudPolicyManagerChromeOS* GetManagerForProfile(Profile* profile);
72  scoped_ptr<UserCloudPolicyManagerChromeOS> CreateManagerForProfile(
73      Profile* profile,
74      bool force_immediate_load,
75      scoped_refptr<base::SequencedTaskRunner> background_task_runner);
76
77  // BrowserContextKeyedBaseFactory:
78  virtual void BrowserContextShutdown(
79      content::BrowserContext* context) OVERRIDE;
80  virtual void BrowserContextDestroyed(
81      content::BrowserContext* context) OVERRIDE;
82  virtual void SetEmptyTestingFactory(
83      content::BrowserContext* context) OVERRIDE;
84  virtual bool HasTestingFactory(content::BrowserContext* context) OVERRIDE;
85  virtual void CreateServiceNow(content::BrowserContext* context) OVERRIDE;
86
87  typedef std::map<Profile*, UserCloudPolicyManagerChromeOS*> ManagerMap;
88  ManagerMap managers_;
89
90  DISALLOW_COPY_AND_ASSIGN(UserCloudPolicyManagerFactoryChromeOS);
91};
92
93}  // namespace policy
94
95#endif  // CHROME_BROWSER_CHROMEOS_POLICY_USER_CLOUD_POLICY_MANAGER_FACTORY_CHROMEOS_H_
96