Update To 11.40.268.0
[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/keyed_service/content/browser_context_keyed_base_factory.h"
14
15 namespace base {
16 class SequencedTaskRunner;
17 }
18
19 namespace content {
20 class BrowserContext;
21 }
22
23 namespace policy {
24
25 class UserCloudPolicyManager;
26
27 // BrowserContextKeyedBaseFactory implementation for UserCloudPolicyManager
28 // instances that initialize per-profile cloud policy settings on the desktop
29 // platforms.
30 //
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.
36 //
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 {
42  public:
43   // Returns an instance of the UserCloudPolicyManagerFactory singleton.
44   static UserCloudPolicyManagerFactory* GetInstance();
45
46   // Returns the UserCloudPolicyManager instance associated with |context|.
47   static UserCloudPolicyManager* GetForBrowserContext(
48       content::BrowserContext* context);
49
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.
55   //
56   // If |force_immediate_load| is true, policy is loaded synchronously from
57   // UserCloudPolicyStore at startup.
58   //
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
63   // BrowserThread.
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);
70
71   static UserCloudPolicyManager* RegisterForOffTheRecordBrowserContext(
72       content::BrowserContext* original_context,
73       content::BrowserContext* off_the_record_context);
74
75   typedef UserCloudPolicyManager*
76       (*TestingFactoryFunction)(content::BrowserContext* context);
77
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
82   // shut down.
83   void RegisterTestingFactory(TestingFactoryFunction factory);
84   void ClearTestingFactory();
85
86  private:
87   class ManagerWrapper;
88   friend struct DefaultSingletonTraits<UserCloudPolicyManagerFactory>;
89
90   UserCloudPolicyManagerFactory();
91   ~UserCloudPolicyManagerFactory() override;
92
93   // See comments for the static versions above.
94   UserCloudPolicyManager* GetManagerForBrowserContext(
95       content::BrowserContext* context);
96
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);
103
104   UserCloudPolicyManager* RegisterManagerForOffTheRecordBrowserContext(
105       content::BrowserContext* original_context,
106       content::BrowserContext* off_the_record_context);
107
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;
115
116   typedef std::map<content::BrowserContext*, ManagerWrapper*> ManagerWrapperMap;
117
118   ManagerWrapperMap manager_wrappers_;
119   TestingFactoryFunction testing_factory_;
120
121   DISALLOW_COPY_AND_ASSIGN(UserCloudPolicyManagerFactory);
122 };
123
124 }  // namespace policy
125
126 #endif  // CHROME_BROWSER_POLICY_CLOUD_USER_CLOUD_POLICY_MANAGER_FACTORY_H_