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/keyed_service/content/browser_context_keyed_base_factory.h"
16 class SequencedTaskRunner
;
25 class UserCloudPolicyManager
;
27 // BrowserContextKeyedBaseFactory implementation for UserCloudPolicyManager
28 // instances that initialize per-profile cloud policy settings on the desktop
31 // UserCloudPolicyManager is handled different than other
32 // KeyedServices because it is a dependency of PrefService.
33 // Therefore, lifetime of instances is managed by Profile, Profile startup code
34 // invokes CreateForBrowserContext() explicitly, takes ownership, and the
35 // instance is only deleted after PrefService destruction.
37 // TODO(mnissler): Remove the special lifetime management in favor of
38 // PrefService directly depending on UserCloudPolicyManager once the former has
39 // been converted to a KeyedService.
40 // See also http://crbug.com/131843 and http://crbug.com/131844.
41 class UserCloudPolicyManagerFactory
: public BrowserContextKeyedBaseFactory
{
43 // Returns an instance of the UserCloudPolicyManagerFactory singleton.
44 static UserCloudPolicyManagerFactory
* GetInstance();
46 // Returns the UserCloudPolicyManager instance associated with |context|.
47 static UserCloudPolicyManager
* GetForBrowserContext(
48 content::BrowserContext
* context
);
50 // Creates an instance for |context|. Note that the caller is responsible for
51 // managing the lifetime of the instance. Subsequent calls to
52 // GetForBrowserContext() will return the created instance as long as it
53 // lives. If RegisterTestingFactory() has been called, then calls to
54 // this method will return null.
56 // If |force_immediate_load| is true, policy is loaded synchronously from
57 // UserCloudPolicyStore at startup.
59 // |background_task_runner| is used for the cloud policy store.
60 // |file_task_runner| is used for file operations. Currently this must be the
61 // FILE BrowserThread.
62 // |io_task_runner| is used for network IO. Currently this must be the IO
64 static scoped_ptr
<UserCloudPolicyManager
> CreateForOriginalBrowserContext(
65 content::BrowserContext
* context
,
66 bool force_immediate_load
,
67 const scoped_refptr
<base::SequencedTaskRunner
>& background_task_runner
,
68 const scoped_refptr
<base::SequencedTaskRunner
>& file_task_runner
,
69 const scoped_refptr
<base::SequencedTaskRunner
>& io_task_runner
);
71 static UserCloudPolicyManager
* RegisterForOffTheRecordBrowserContext(
72 content::BrowserContext
* original_context
,
73 content::BrowserContext
* off_the_record_context
);
75 typedef UserCloudPolicyManager
*
76 (*TestingFactoryFunction
)(content::BrowserContext
* context
);
78 // Allows testing code to inject UserCloudPolicyManager objects for tests.
79 // The factory function will be invoked for every Profile created. Because
80 // this class does not free the UserCloudPolicyManager objects it manages,
81 // it is up to the tests themselves to free the objects after the profile is
83 void RegisterTestingFactory(TestingFactoryFunction factory
);
84 void ClearTestingFactory();
88 friend struct DefaultSingletonTraits
<UserCloudPolicyManagerFactory
>;
90 UserCloudPolicyManagerFactory();
91 ~UserCloudPolicyManagerFactory() override
;
93 // See comments for the static versions above.
94 UserCloudPolicyManager
* GetManagerForBrowserContext(
95 content::BrowserContext
* context
);
97 scoped_ptr
<UserCloudPolicyManager
> CreateManagerForOriginalBrowserContext(
98 content::BrowserContext
* context
,
99 bool force_immediate_load
,
100 const scoped_refptr
<base::SequencedTaskRunner
>& background_task_runner
,
101 const scoped_refptr
<base::SequencedTaskRunner
>& file_task_runner
,
102 const scoped_refptr
<base::SequencedTaskRunner
>& io_task_runner
);
104 UserCloudPolicyManager
* RegisterManagerForOffTheRecordBrowserContext(
105 content::BrowserContext
* original_context
,
106 content::BrowserContext
* off_the_record_context
);
108 // BrowserContextKeyedBaseFactory:
109 void BrowserContextShutdown(content::BrowserContext
* context
) override
;
110 void BrowserContextDestroyed(content::BrowserContext
* context
) override
;
111 void SetEmptyTestingFactory(content::BrowserContext
* context
) override
;
112 bool HasTestingFactory(content::BrowserContext
* context
) override
;
113 void CreateServiceNow(content::BrowserContext
* context
) override
;
114 bool ServiceIsCreatedWithBrowserContext() const override
;
116 typedef std::map
<content::BrowserContext
*, ManagerWrapper
*> ManagerWrapperMap
;
118 ManagerWrapperMap manager_wrappers_
;
119 TestingFactoryFunction testing_factory_
;
121 DISALLOW_COPY_AND_ASSIGN(UserCloudPolicyManagerFactory
);
124 } // namespace policy
126 #endif // CHROME_BROWSER_POLICY_CLOUD_USER_CLOUD_POLICY_MANAGER_FACTORY_H_