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