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.
5 #ifndef CHROME_BROWSER_POLICY_CLOUD_USER_CLOUD_POLICY_MANAGER_FACTORY_H_
6 #define CHROME_BROWSER_POLICY_CLOUD_USER_CLOUD_POLICY_MANAGER_FACTORY_H_
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"
18 class SequencedTaskRunner;
23 class UserCloudPolicyManager;
25 // BrowserContextKeyedBaseFactory implementation for UserCloudPolicyManager
26 // instances that initialize per-profile cloud policy settings on the desktop
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.
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 {
41 // Returns an instance of the UserCloudPolicyManagerFactory singleton.
42 static UserCloudPolicyManagerFactory* GetInstance();
44 // Returns the UserCloudPolicyManager instance associated with |profile|.
45 static UserCloudPolicyManager* GetForProfile(Profile* profile);
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.
51 // If |force_immediate_load| is true, policy is loaded synchronously from
52 // UserCloudPolicyStore at startup.
53 static scoped_ptr<UserCloudPolicyManager> CreateForProfile(
55 bool force_immediate_load,
56 scoped_refptr<base::SequencedTaskRunner> background_task_runner);
59 friend class UserCloudPolicyManager;
60 friend struct DefaultSingletonTraits<UserCloudPolicyManagerFactory>;
62 UserCloudPolicyManagerFactory();
63 virtual ~UserCloudPolicyManagerFactory();
65 // See comments for the static versions above.
66 UserCloudPolicyManager* GetManagerForProfile(Profile* profile);
67 scoped_ptr<UserCloudPolicyManager> CreateManagerForProfile(
69 bool force_immediate_load,
70 scoped_refptr<base::SequencedTaskRunner> background_task_runner);
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;
79 // Invoked by UserCloudPolicyManager to register/unregister instances.
80 void Register(Profile* profile, UserCloudPolicyManager* instance);
81 void Unregister(Profile* profile, UserCloudPolicyManager* instance);
83 typedef std::map<Profile*, UserCloudPolicyManager*> ManagerMap;
86 DISALLOW_COPY_AND_ASSIGN(UserCloudPolicyManagerFactory);
91 #endif // CHROME_BROWSER_POLICY_CLOUD_USER_CLOUD_POLICY_MANAGER_FACTORY_H_