OLD | NEW |
1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 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 // Most of this code is copied from: | 5 // Most of this code is copied from: |
6 // src/chrome/browser/policy/asynchronous_policy_loader.{h,cc} | 6 // src/chrome/browser/policy/asynchronous_policy_loader.{h,cc} |
7 | 7 |
8 #include "remoting/host/policy_watcher.h" | 8 #include "remoting/host/policy_watcher.h" |
9 | 9 |
10 #include "base/bind.h" | 10 #include "base/bind.h" |
(...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
55 | 55 |
56 // If the policy isn't in |from|, use the default. | 56 // If the policy isn't in |from|, use the default. |
57 if (!from.Get(i.key(), &value)) { | 57 if (!from.Get(i.key(), &value)) { |
58 continue; | 58 continue; |
59 } | 59 } |
60 | 60 |
61 CHECK(value->IsType(i.value().GetType())); | 61 CHECK(value->IsType(i.value().GetType())); |
62 to->Set(i.key(), value->DeepCopy()); | 62 to->Set(i.key(), value->DeepCopy()); |
63 } | 63 } |
64 | 64 |
65 return to.Pass(); | 65 return to; |
66 } | 66 } |
67 | 67 |
68 policy::PolicyNamespace GetPolicyNamespace() { | 68 policy::PolicyNamespace GetPolicyNamespace() { |
69 return policy::PolicyNamespace(policy::POLICY_DOMAIN_CHROME, std::string()); | 69 return policy::PolicyNamespace(policy::POLICY_DOMAIN_CHROME, std::string()); |
70 } | 70 } |
71 | 71 |
72 scoped_ptr<policy::SchemaRegistry> CreateSchemaRegistry() { | 72 scoped_ptr<policy::SchemaRegistry> CreateSchemaRegistry() { |
73 // TODO(lukasza): Schema below should ideally only cover Chromoting-specific | 73 // TODO(lukasza): Schema below should ideally only cover Chromoting-specific |
74 // policies (expecting perf and maintanability improvement, but no functional | 74 // policies (expecting perf and maintanability improvement, but no functional |
75 // impact). | 75 // impact). |
76 policy::Schema schema = policy::Schema::Wrap(policy::GetChromeSchemaData()); | 76 policy::Schema schema = policy::Schema::Wrap(policy::GetChromeSchemaData()); |
77 | 77 |
78 scoped_ptr<policy::SchemaRegistry> schema_registry( | 78 scoped_ptr<policy::SchemaRegistry> schema_registry( |
79 new policy::SchemaRegistry()); | 79 new policy::SchemaRegistry()); |
80 schema_registry->RegisterComponent(GetPolicyNamespace(), schema); | 80 schema_registry->RegisterComponent(GetPolicyNamespace(), schema); |
81 return schema_registry.Pass(); | 81 return schema_registry; |
82 } | 82 } |
83 | 83 |
84 scoped_ptr<base::DictionaryValue> CopyChromotingPoliciesIntoDictionary( | 84 scoped_ptr<base::DictionaryValue> CopyChromotingPoliciesIntoDictionary( |
85 const policy::PolicyMap& current) { | 85 const policy::PolicyMap& current) { |
86 const char kPolicyNameSubstring[] = "RemoteAccessHost"; | 86 const char kPolicyNameSubstring[] = "RemoteAccessHost"; |
87 scoped_ptr<base::DictionaryValue> policy_dict(new base::DictionaryValue()); | 87 scoped_ptr<base::DictionaryValue> policy_dict(new base::DictionaryValue()); |
88 for (auto it = current.begin(); it != current.end(); ++it) { | 88 for (auto it = current.begin(); it != current.end(); ++it) { |
89 const std::string& key = it->first; | 89 const std::string& key = it->first; |
90 const base::Value* value = it->second.value; | 90 const base::Value* value = it->second.value; |
91 | 91 |
92 // Copying only Chromoting-specific policies helps avoid false alarms | 92 // Copying only Chromoting-specific policies helps avoid false alarms |
93 // raised by NormalizePolicies below (such alarms shutdown the host). | 93 // raised by NormalizePolicies below (such alarms shutdown the host). |
94 // TODO(lukasza): Removing this somewhat brittle filtering will be possible | 94 // TODO(lukasza): Removing this somewhat brittle filtering will be possible |
95 // after having separate, Chromoting-specific schema. | 95 // after having separate, Chromoting-specific schema. |
96 if (key.find(kPolicyNameSubstring) != std::string::npos) { | 96 if (key.find(kPolicyNameSubstring) != std::string::npos) { |
97 policy_dict->Set(key, value->DeepCopy()); | 97 policy_dict->Set(key, value->DeepCopy()); |
98 } | 98 } |
99 } | 99 } |
100 | 100 |
101 return policy_dict.Pass(); | 101 return policy_dict; |
102 } | 102 } |
103 | 103 |
104 // Takes a dictionary containing only 1) recognized policy names and 2) | 104 // Takes a dictionary containing only 1) recognized policy names and 2) |
105 // well-typed policy values and further verifies policy contents. | 105 // well-typed policy values and further verifies policy contents. |
106 bool VerifyWellformedness(const base::DictionaryValue& changed_policies) { | 106 bool VerifyWellformedness(const base::DictionaryValue& changed_policies) { |
107 // Verify ThirdPartyAuthConfig policy. | 107 // Verify ThirdPartyAuthConfig policy. |
108 ThirdPartyAuthConfig not_used; | 108 ThirdPartyAuthConfig not_used; |
109 switch (ThirdPartyAuthConfig::Parse(changed_policies, ¬_used)) { | 109 switch (ThirdPartyAuthConfig::Parse(changed_policies, ¬_used)) { |
110 case ThirdPartyAuthConfig::NoPolicy: | 110 case ThirdPartyAuthConfig::NoPolicy: |
111 case ThirdPartyAuthConfig::ParsingSuccess: | 111 case ThirdPartyAuthConfig::ParsingSuccess: |
(...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
159 } | 159 } |
160 | 160 |
161 PolicyWatcher::PolicyWatcher( | 161 PolicyWatcher::PolicyWatcher( |
162 policy::PolicyService* policy_service, | 162 policy::PolicyService* policy_service, |
163 scoped_ptr<policy::PolicyService> owned_policy_service, | 163 scoped_ptr<policy::PolicyService> owned_policy_service, |
164 scoped_ptr<policy::ConfigurationPolicyProvider> owned_policy_provider, | 164 scoped_ptr<policy::ConfigurationPolicyProvider> owned_policy_provider, |
165 scoped_ptr<policy::SchemaRegistry> owned_schema_registry) | 165 scoped_ptr<policy::SchemaRegistry> owned_schema_registry) |
166 : old_policies_(new base::DictionaryValue()), | 166 : old_policies_(new base::DictionaryValue()), |
167 default_values_(new base::DictionaryValue()), | 167 default_values_(new base::DictionaryValue()), |
168 policy_service_(policy_service), | 168 policy_service_(policy_service), |
169 owned_schema_registry_(owned_schema_registry.Pass()), | 169 owned_schema_registry_(std::move(owned_schema_registry)), |
170 owned_policy_provider_(owned_policy_provider.Pass()), | 170 owned_policy_provider_(std::move(owned_policy_provider)), |
171 owned_policy_service_(owned_policy_service.Pass()) { | 171 owned_policy_service_(std::move(owned_policy_service)) { |
172 DCHECK(policy_service_); | 172 DCHECK(policy_service_); |
173 DCHECK(owned_schema_registry_); | 173 DCHECK(owned_schema_registry_); |
174 | 174 |
175 // Initialize the default values for each policy. | 175 // Initialize the default values for each policy. |
176 default_values_->SetBoolean(key::kRemoteAccessHostFirewallTraversal, true); | 176 default_values_->SetBoolean(key::kRemoteAccessHostFirewallTraversal, true); |
177 default_values_->SetBoolean(key::kRemoteAccessHostRequireCurtain, false); | 177 default_values_->SetBoolean(key::kRemoteAccessHostRequireCurtain, false); |
178 default_values_->SetBoolean(key::kRemoteAccessHostMatchUsername, false); | 178 default_values_->SetBoolean(key::kRemoteAccessHostMatchUsername, false); |
179 default_values_->SetString(key::kRemoteAccessHostDomain, std::string()); | 179 default_values_->SetString(key::kRemoteAccessHostDomain, std::string()); |
180 default_values_->SetString(key::kRemoteAccessHostTalkGadgetPrefix, | 180 default_values_->SetString(key::kRemoteAccessHostTalkGadgetPrefix, |
181 kDefaultHostTalkGadgetPrefix); | 181 kDefaultHostTalkGadgetPrefix); |
(...skipping 85 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
267 key::kRemoteAccessHostTokenUrl); | 267 key::kRemoteAccessHostTokenUrl); |
268 CopyDictionaryValue(*new_policies, *changed_policies, | 268 CopyDictionaryValue(*new_policies, *changed_policies, |
269 key::kRemoteAccessHostTokenValidationUrl); | 269 key::kRemoteAccessHostTokenValidationUrl); |
270 CopyDictionaryValue(*new_policies, *changed_policies, | 270 CopyDictionaryValue(*new_policies, *changed_policies, |
271 key::kRemoteAccessHostTokenValidationCertificateIssuer); | 271 key::kRemoteAccessHostTokenValidationCertificateIssuer); |
272 } | 272 } |
273 | 273 |
274 // Save the new policies. | 274 // Save the new policies. |
275 old_policies_.swap(new_policies); | 275 old_policies_.swap(new_policies); |
276 | 276 |
277 return changed_policies.Pass(); | 277 return changed_policies; |
278 } | 278 } |
279 | 279 |
280 void PolicyWatcher::OnPolicyUpdated(const policy::PolicyNamespace& ns, | 280 void PolicyWatcher::OnPolicyUpdated(const policy::PolicyNamespace& ns, |
281 const policy::PolicyMap& previous, | 281 const policy::PolicyMap& previous, |
282 const policy::PolicyMap& current) { | 282 const policy::PolicyMap& current) { |
283 scoped_ptr<base::DictionaryValue> new_policies = | 283 scoped_ptr<base::DictionaryValue> new_policies = |
284 CopyChromotingPoliciesIntoDictionary(current); | 284 CopyChromotingPoliciesIntoDictionary(current); |
285 | 285 |
286 // Check for mistyped values and get rid of unknown policies. | 286 // Check for mistyped values and get rid of unknown policies. |
287 if (!NormalizePolicies(new_policies.get())) { | 287 if (!NormalizePolicies(new_policies.get())) { |
288 SignalPolicyError(); | 288 SignalPolicyError(); |
289 return; | 289 return; |
290 } | 290 } |
291 | 291 |
292 // Use default values for any missing policies. | 292 // Use default values for any missing policies. |
293 scoped_ptr<base::DictionaryValue> filled_policies = | 293 scoped_ptr<base::DictionaryValue> filled_policies = |
294 CopyValuesAndAddDefaults(*new_policies, *default_values_); | 294 CopyValuesAndAddDefaults(*new_policies, *default_values_); |
295 | 295 |
296 // Limit reporting to only the policies that were changed. | 296 // Limit reporting to only the policies that were changed. |
297 scoped_ptr<base::DictionaryValue> changed_policies = | 297 scoped_ptr<base::DictionaryValue> changed_policies = |
298 StoreNewAndReturnChangedPolicies(filled_policies.Pass()); | 298 StoreNewAndReturnChangedPolicies(std::move(filled_policies)); |
299 if (changed_policies->empty()) { | 299 if (changed_policies->empty()) { |
300 return; | 300 return; |
301 } | 301 } |
302 | 302 |
303 // Verify that we are calling the callback with valid policies. | 303 // Verify that we are calling the callback with valid policies. |
304 if (!VerifyWellformedness(*changed_policies)) { | 304 if (!VerifyWellformedness(*changed_policies)) { |
305 SignalPolicyError(); | 305 SignalPolicyError(); |
306 return; | 306 return; |
307 } | 307 } |
308 | 308 |
309 // Notify our client of the changed policies. | 309 // Notify our client of the changed policies. |
310 policy_updated_callback_.Run(changed_policies.Pass()); | 310 policy_updated_callback_.Run(std::move(changed_policies)); |
311 } | 311 } |
312 | 312 |
313 void PolicyWatcher::OnPolicyServiceInitialized(policy::PolicyDomain domain) { | 313 void PolicyWatcher::OnPolicyServiceInitialized(policy::PolicyDomain domain) { |
314 policy::PolicyNamespace ns = GetPolicyNamespace(); | 314 policy::PolicyNamespace ns = GetPolicyNamespace(); |
315 const policy::PolicyMap& current = policy_service_->GetPolicies(ns); | 315 const policy::PolicyMap& current = policy_service_->GetPolicies(ns); |
316 OnPolicyUpdated(ns, current, current); | 316 OnPolicyUpdated(ns, current, current); |
317 } | 317 } |
318 | 318 |
319 scoped_ptr<PolicyWatcher> PolicyWatcher::CreateFromPolicyLoader( | 319 scoped_ptr<PolicyWatcher> PolicyWatcher::CreateFromPolicyLoader( |
320 scoped_ptr<policy::AsyncPolicyLoader> async_policy_loader) { | 320 scoped_ptr<policy::AsyncPolicyLoader> async_policy_loader) { |
321 scoped_ptr<policy::SchemaRegistry> schema_registry = CreateSchemaRegistry(); | 321 scoped_ptr<policy::SchemaRegistry> schema_registry = CreateSchemaRegistry(); |
322 scoped_ptr<policy::AsyncPolicyProvider> policy_provider( | 322 scoped_ptr<policy::AsyncPolicyProvider> policy_provider( |
323 new policy::AsyncPolicyProvider(schema_registry.get(), | 323 new policy::AsyncPolicyProvider(schema_registry.get(), |
324 async_policy_loader.Pass())); | 324 std::move(async_policy_loader))); |
325 policy_provider->Init(schema_registry.get()); | 325 policy_provider->Init(schema_registry.get()); |
326 | 326 |
327 policy::PolicyServiceImpl::Providers providers; | 327 policy::PolicyServiceImpl::Providers providers; |
328 providers.push_back(policy_provider.get()); | 328 providers.push_back(policy_provider.get()); |
329 scoped_ptr<policy::PolicyService> policy_service( | 329 scoped_ptr<policy::PolicyService> policy_service( |
330 new policy::PolicyServiceImpl(providers)); | 330 new policy::PolicyServiceImpl(providers)); |
331 | 331 |
332 policy::PolicyService* borrowed_policy_service = policy_service.get(); | 332 policy::PolicyService* borrowed_policy_service = policy_service.get(); |
333 return make_scoped_ptr( | 333 return make_scoped_ptr(new PolicyWatcher( |
334 new PolicyWatcher(borrowed_policy_service, policy_service.Pass(), | 334 borrowed_policy_service, std::move(policy_service), |
335 policy_provider.Pass(), schema_registry.Pass())); | 335 std::move(policy_provider), std::move(schema_registry))); |
336 } | 336 } |
337 | 337 |
338 scoped_ptr<PolicyWatcher> PolicyWatcher::Create( | 338 scoped_ptr<PolicyWatcher> PolicyWatcher::Create( |
339 policy::PolicyService* policy_service, | 339 policy::PolicyService* policy_service, |
340 const scoped_refptr<base::SingleThreadTaskRunner>& file_task_runner) { | 340 const scoped_refptr<base::SingleThreadTaskRunner>& file_task_runner) { |
341 #if defined(OS_CHROMEOS) | 341 #if defined(OS_CHROMEOS) |
342 // On Chrome OS the PolicyService is owned by the browser. | 342 // On Chrome OS the PolicyService is owned by the browser. |
343 DCHECK(policy_service); | 343 DCHECK(policy_service); |
344 return make_scoped_ptr(new PolicyWatcher(policy_service, nullptr, nullptr, | 344 return make_scoped_ptr(new PolicyWatcher(policy_service, nullptr, nullptr, |
345 CreateSchemaRegistry())); | 345 CreateSchemaRegistry())); |
(...skipping 16 matching lines...) Expand all Loading... |
362 new MacPreferences(), bundle_id)); | 362 new MacPreferences(), bundle_id)); |
363 #elif defined(OS_POSIX) && !defined(OS_ANDROID) | 363 #elif defined(OS_POSIX) && !defined(OS_ANDROID) |
364 policy_loader.reset(new policy::ConfigDirPolicyLoader( | 364 policy_loader.reset(new policy::ConfigDirPolicyLoader( |
365 file_task_runner, | 365 file_task_runner, |
366 base::FilePath(FILE_PATH_LITERAL("/etc/opt/chrome/policies")), | 366 base::FilePath(FILE_PATH_LITERAL("/etc/opt/chrome/policies")), |
367 policy::POLICY_SCOPE_MACHINE)); | 367 policy::POLICY_SCOPE_MACHINE)); |
368 #else | 368 #else |
369 #error OS that is not yet supported by PolicyWatcher code. | 369 #error OS that is not yet supported by PolicyWatcher code. |
370 #endif | 370 #endif |
371 | 371 |
372 return PolicyWatcher::CreateFromPolicyLoader(policy_loader.Pass()); | 372 return PolicyWatcher::CreateFromPolicyLoader(std::move(policy_loader)); |
373 #endif // !(OS_CHROMEOS) | 373 #endif // !(OS_CHROMEOS) |
374 } | 374 } |
375 | 375 |
376 } // namespace remoting | 376 } // namespace remoting |
OLD | NEW |