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