OLD | NEW |
---|---|
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "remoting/host/policy_hack/policy_service_watcher.h" | |
6 | |
5 #include "base/files/file_path.h" | 7 #include "base/files/file_path.h" |
6 #include "base/single_thread_task_runner.h" | 8 #include "base/single_thread_task_runner.h" |
7 #include "components/policy/core/common/async_policy_loader.h" | 9 #include "components/policy/core/common/async_policy_loader.h" |
8 #include "components/policy/core/common/async_policy_provider.h" | 10 #include "components/policy/core/common/async_policy_provider.h" |
9 #include "components/policy/core/common/policy_namespace.h" | 11 #include "components/policy/core/common/policy_namespace.h" |
10 #include "components/policy/core/common/policy_service.h" | 12 #include "components/policy/core/common/policy_service.h" |
Mattias Nissler (ping if slow)
2015/01/23 08:27:40
already in header
Łukasz Anforowicz
2015/01/23 17:49:55
Done.
| |
11 #include "components/policy/core/common/policy_service_impl.h" | 13 #include "components/policy/core/common/policy_service_impl.h" |
12 #include "components/policy/core/common/schema.h" | 14 #include "components/policy/core/common/schema.h" |
13 #include "components/policy/core/common/schema_registry.h" | 15 #include "components/policy/core/common/schema_registry.h" |
14 #include "policy/policy_constants.h" | 16 #include "policy/policy_constants.h" |
15 #include "remoting/host/policy_hack/policy_watcher.h" | |
16 | 17 |
17 #if defined(OS_CHROMEOS) | 18 #if defined(OS_CHROMEOS) |
18 #include "content/public/browser/browser_thread.h" | 19 #include "content/public/browser/browser_thread.h" |
19 #elif defined(OS_WIN) | 20 #elif defined(OS_WIN) |
20 #include "components/policy/core/common/policy_loader_win.h" | 21 #include "components/policy/core/common/policy_loader_win.h" |
21 #elif defined(OS_MACOSX) | 22 #elif defined(OS_MACOSX) |
22 #include "components/policy/core/common/policy_loader_mac.h" | 23 #include "components/policy/core/common/policy_loader_mac.h" |
23 #include "components/policy/core/common/preferences_mac.h" | 24 #include "components/policy/core/common/preferences_mac.h" |
24 #elif defined(OS_POSIX) && !defined(OS_ANDROID) | 25 #elif defined(OS_POSIX) && !defined(OS_ANDROID) |
25 #include "components/policy/core/common/config_dir_policy_loader.h" | 26 #include "components/policy/core/common/config_dir_policy_loader.h" |
26 #endif | 27 #endif |
27 | 28 |
28 using namespace policy; | 29 using namespace policy; |
29 | 30 |
30 namespace remoting { | 31 namespace remoting { |
31 namespace policy_hack { | 32 namespace policy_hack { |
32 | 33 |
33 namespace { | 34 namespace { |
34 | 35 |
35 // TODO(lukasza): Merge PolicyServiceWatcher with PolicyWatcher class | |
36 // (after removing other classes derived from PolicyWatcher - i.e. after | |
37 // removing FakePolicyWatcher class and replacing it with mocks of classes | |
38 // from components/policy instead). | |
39 | |
40 // PolicyServiceWatcher is a concrete implementation of PolicyWatcher that wraps | |
41 // an instance of PolicyService. | |
42 class PolicyServiceWatcher : public PolicyWatcher, | |
43 public PolicyService::Observer { | |
44 public: | |
45 // Constructor for the case when |policy_service| is borrowed. | |
46 // | |
47 // |policy_service_task_runner| is the task runner where it is safe | |
48 // to call |policy_service| methods and where we expect to get callbacks | |
49 // from |policy_service|. | |
50 PolicyServiceWatcher(const scoped_refptr<base::SingleThreadTaskRunner>& | |
51 policy_service_task_runner, | |
52 PolicyService* policy_service); | |
53 | |
54 // Constructor for the case when |policy_service| is owned (and uses also | |
55 // owned |owned_policy_provider| and |owned_schema_registry|. | |
56 // | |
57 // |policy_service_task_runner| is the task runner where it is safe | |
58 // to call |policy_service| methods and where we expect to get callbacks | |
59 // from |policy_service|. | |
60 PolicyServiceWatcher( | |
61 const scoped_refptr<base::SingleThreadTaskRunner>& | |
62 policy_service_task_runner, | |
63 scoped_ptr<PolicyService> owned_policy_service, | |
64 scoped_ptr<ConfigurationPolicyProvider> owned_policy_provider, | |
65 scoped_ptr<SchemaRegistry> owned_schema_registry); | |
66 | |
67 ~PolicyServiceWatcher() override; | |
68 | |
69 // PolicyService::Observer interface. | |
70 void OnPolicyUpdated(const PolicyNamespace& ns, | |
71 const PolicyMap& previous, | |
72 const PolicyMap& current) override; | |
73 void OnPolicyServiceInitialized(PolicyDomain domain) override; | |
74 | |
75 protected: | |
76 // PolicyWatcher overrides. | |
77 void StartWatchingInternal() override; | |
78 void StopWatchingInternal() override; | |
79 | |
80 private: | |
81 PolicyService* policy_service_; | |
82 | |
83 // Order of fields below is important to ensure destruction takes object | |
84 // dependencies into account: | |
85 // - |owned_policy_service_| uses |owned_policy_provider_| | |
86 // - |owned_policy_provider_| uses |owned_schema_registry_| | |
87 scoped_ptr<SchemaRegistry> owned_schema_registry_; | |
88 scoped_ptr<ConfigurationPolicyProvider> owned_policy_provider_; | |
89 scoped_ptr<PolicyService> owned_policy_service_; | |
90 | |
91 DISALLOW_COPY_AND_ASSIGN(PolicyServiceWatcher); | |
92 }; | |
93 | |
94 PolicyNamespace GetPolicyNamespace() { | 36 PolicyNamespace GetPolicyNamespace() { |
95 return PolicyNamespace(POLICY_DOMAIN_CHROME, std::string()); | 37 return PolicyNamespace(POLICY_DOMAIN_CHROME, std::string()); |
96 } | 38 } |
97 | 39 |
40 } // namespace | |
41 | |
98 PolicyServiceWatcher::PolicyServiceWatcher( | 42 PolicyServiceWatcher::PolicyServiceWatcher( |
99 const scoped_refptr<base::SingleThreadTaskRunner>& | 43 const scoped_refptr<base::SingleThreadTaskRunner>& |
100 policy_service_task_runner, | 44 policy_service_task_runner, |
101 PolicyService* policy_service) | 45 PolicyService* policy_service) |
102 : PolicyWatcher(policy_service_task_runner) { | 46 : PolicyWatcher(policy_service_task_runner) { |
103 policy_service_ = policy_service; | 47 policy_service_ = policy_service; |
104 } | 48 } |
105 | 49 |
106 PolicyServiceWatcher::PolicyServiceWatcher( | 50 PolicyServiceWatcher::PolicyServiceWatcher( |
107 const scoped_refptr<base::SingleThreadTaskRunner>& | 51 const scoped_refptr<base::SingleThreadTaskRunner>& |
(...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
147 // Process current policy state. | 91 // Process current policy state. |
148 if (policy_service_->IsInitializationComplete(POLICY_DOMAIN_CHROME)) { | 92 if (policy_service_->IsInitializationComplete(POLICY_DOMAIN_CHROME)) { |
149 OnPolicyServiceInitialized(POLICY_DOMAIN_CHROME); | 93 OnPolicyServiceInitialized(POLICY_DOMAIN_CHROME); |
150 } | 94 } |
151 } | 95 } |
152 | 96 |
153 void PolicyServiceWatcher::StopWatchingInternal() { | 97 void PolicyServiceWatcher::StopWatchingInternal() { |
154 policy_service_->RemoveObserver(POLICY_DOMAIN_CHROME, this); | 98 policy_service_->RemoveObserver(POLICY_DOMAIN_CHROME, this); |
155 } | 99 } |
156 | 100 |
157 #if !defined(OS_CHROMEOS) | 101 scoped_ptr<PolicyServiceWatcher> PolicyServiceWatcher::CreateFromPolicyLoader( |
158 | |
159 // Creates PolicyServiceWatcher that wraps the owned |async_policy_loader| | |
160 // with an appropriate PolicySchema. | |
161 // | |
162 // |policy_service_task_runner| is passed through to the constructor | |
163 // of PolicyServiceWatcher. | |
164 scoped_ptr<PolicyServiceWatcher> CreateFromPolicyLoader( | |
165 const scoped_refptr<base::SingleThreadTaskRunner>& | 102 const scoped_refptr<base::SingleThreadTaskRunner>& |
166 policy_service_task_runner, | 103 policy_service_task_runner, |
167 scoped_ptr<AsyncPolicyLoader> async_policy_loader) { | 104 scoped_ptr<AsyncPolicyLoader> async_policy_loader) { |
168 // TODO(lukasza): Schema below should ideally only cover Chromoting-specific | 105 // TODO(lukasza): Schema below should ideally only cover Chromoting-specific |
169 // policies (expecting perf and maintanability improvement, but no functional | 106 // policies (expecting perf and maintanability improvement, but no functional |
170 // impact). | 107 // impact). |
171 Schema schema = Schema::Wrap(GetChromeSchemaData()); | 108 Schema schema = Schema::Wrap(GetChromeSchemaData()); |
172 | 109 |
173 scoped_ptr<SchemaRegistry> schema_registry(new SchemaRegistry()); | 110 scoped_ptr<SchemaRegistry> schema_registry(new SchemaRegistry()); |
174 schema_registry->RegisterComponent(GetPolicyNamespace(), schema); | 111 schema_registry->RegisterComponent(GetPolicyNamespace(), schema); |
175 | 112 |
176 scoped_ptr<AsyncPolicyProvider> policy_provider(new AsyncPolicyProvider( | 113 scoped_ptr<AsyncPolicyProvider> policy_provider(new AsyncPolicyProvider( |
177 schema_registry.get(), async_policy_loader.Pass())); | 114 schema_registry.get(), async_policy_loader.Pass())); |
178 policy_provider->Init(schema_registry.get()); | 115 policy_provider->Init(schema_registry.get()); |
179 | 116 |
180 PolicyServiceImpl::Providers providers; | 117 PolicyServiceImpl::Providers providers; |
181 providers.push_back(policy_provider.get()); | 118 providers.push_back(policy_provider.get()); |
182 scoped_ptr<PolicyService> policy_service(new PolicyServiceImpl(providers)); | 119 scoped_ptr<PolicyService> policy_service(new PolicyServiceImpl(providers)); |
183 | 120 |
184 return make_scoped_ptr(new PolicyServiceWatcher( | 121 return make_scoped_ptr(new PolicyServiceWatcher( |
185 policy_service_task_runner, policy_service.Pass(), policy_provider.Pass(), | 122 policy_service_task_runner, policy_service.Pass(), policy_provider.Pass(), |
186 schema_registry.Pass())); | 123 schema_registry.Pass())); |
187 } | 124 } |
188 | 125 |
189 #endif | |
190 | |
191 } // anonymous namespace | |
192 | |
193 scoped_ptr<PolicyWatcher> PolicyWatcher::Create( | 126 scoped_ptr<PolicyWatcher> PolicyWatcher::Create( |
194 policy::PolicyService* policy_service, | 127 policy::PolicyService* policy_service, |
195 const scoped_refptr<base::SingleThreadTaskRunner>& network_task_runner) { | 128 const scoped_refptr<base::SingleThreadTaskRunner>& network_task_runner) { |
196 #if defined(OS_CHROMEOS) | 129 #if defined(OS_CHROMEOS) |
197 DCHECK(policy_service); | 130 DCHECK(policy_service); |
198 return make_scoped_ptr(new PolicyServiceWatcher( | 131 return make_scoped_ptr(new PolicyServiceWatcher( |
199 content::BrowserThread::GetMessageLoopProxyForThread( | 132 content::BrowserThread::GetMessageLoopProxyForThread( |
200 content::BrowserThread::UI), | 133 content::BrowserThread::UI), |
201 policy_service)); | 134 policy_service)); |
202 #elif defined(OS_WIN) | 135 #elif defined(OS_WIN) |
203 DCHECK(!policy_service); | 136 DCHECK(!policy_service); |
204 static const wchar_t kRegistryKey[] = L"SOFTWARE\\Policies\\Google\\Chrome"; | 137 static const wchar_t kRegistryKey[] = L"SOFTWARE\\Policies\\Google\\Chrome"; |
205 return CreateFromPolicyLoader( | 138 return PolicyServiceWatcher::CreateFromPolicyLoader( |
206 network_task_runner, | 139 network_task_runner, |
207 PolicyLoaderWin::Create(network_task_runner, kRegistryKey)); | 140 PolicyLoaderWin::Create(network_task_runner, kRegistryKey)); |
208 #elif defined(OS_MACOSX) | 141 #elif defined(OS_MACOSX) |
209 CFStringRef bundle_id = CFSTR("com.google.Chrome"); | 142 CFStringRef bundle_id = CFSTR("com.google.Chrome"); |
210 DCHECK(!policy_service); | 143 DCHECK(!policy_service); |
211 return CreateFromPolicyLoader( | 144 return PolicyServiceWatcher::CreateFromPolicyLoader( |
212 network_task_runner, | 145 network_task_runner, |
213 make_scoped_ptr(new PolicyLoaderMac( | 146 make_scoped_ptr(new PolicyLoaderMac( |
214 network_task_runner, | 147 network_task_runner, |
215 policy::PolicyLoaderMac::GetManagedPolicyPath(bundle_id), | 148 policy::PolicyLoaderMac::GetManagedPolicyPath(bundle_id), |
216 new MacPreferences(), bundle_id))); | 149 new MacPreferences(), bundle_id))); |
217 #elif defined(OS_POSIX) && !defined(OS_ANDROID) | 150 #elif defined(OS_POSIX) && !defined(OS_ANDROID) |
218 DCHECK(!policy_service); | 151 DCHECK(!policy_service); |
219 // Always read the Chrome policies (even on Chromium) so that policy | 152 // Always read the Chrome policies (even on Chromium) so that policy |
220 // enforcement can't be bypassed by running Chromium. | 153 // enforcement can't be bypassed by running Chromium. |
221 static const base::FilePath::CharType kPolicyDir[] = | 154 static const base::FilePath::CharType kPolicyDir[] = |
222 FILE_PATH_LITERAL("/etc/opt/chrome/policies"); | 155 FILE_PATH_LITERAL("/etc/opt/chrome/policies"); |
223 return CreateFromPolicyLoader( | 156 return PolicyServiceWatcher::CreateFromPolicyLoader( |
224 network_task_runner, make_scoped_ptr(new ConfigDirPolicyLoader( | 157 network_task_runner, make_scoped_ptr(new ConfigDirPolicyLoader( |
225 network_task_runner, base::FilePath(kPolicyDir), | 158 network_task_runner, base::FilePath(kPolicyDir), |
226 POLICY_SCOPE_MACHINE))); | 159 POLICY_SCOPE_MACHINE))); |
227 #else | 160 #else |
228 #error OS that is not yet supported by PolicyWatcher code. | 161 #error OS that is not yet supported by PolicyWatcher code. |
229 #endif | 162 #endif |
230 } | 163 } |
231 | 164 |
232 } // namespace policy_hack | 165 } // namespace policy_hack |
233 } // namespace remoting | 166 } // namespace remoting |
OLD | NEW |