| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 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 "chrome/browser/policy/configuration_policy_provider_delegate_win.h" | 5 #include "chrome/browser/policy/policy_loader_win.h" |
| 6 | 6 |
| 7 #include <string> | 7 #include <string> |
| 8 | 8 |
| 9 #include <string.h> | 9 #include <string.h> |
| 10 | 10 |
| 11 #include <userenv.h> |
| 12 |
| 13 // userenv.dll is required for RegisterGPNotification(). |
| 14 #pragma comment(lib, "userenv.lib") |
| 15 |
| 11 #include "base/basictypes.h" | 16 #include "base/basictypes.h" |
| 12 #include "base/json/json_reader.h" | 17 #include "base/json/json_reader.h" |
| 13 #include "base/logging.h" | 18 #include "base/logging.h" |
| 14 #include "base/memory/scoped_ptr.h" | 19 #include "base/memory/scoped_ptr.h" |
| 20 #include "base/string16.h" |
| 15 #include "base/string_number_conversions.h" | 21 #include "base/string_number_conversions.h" |
| 16 #include "base/utf_string_conversions.h" | 22 #include "base/utf_string_conversions.h" |
| 17 #include "base/values.h" | 23 #include "base/values.h" |
| 18 #include "base/win/registry.h" | 24 #include "base/win/registry.h" |
| 19 #include "chrome/browser/policy/policy_bundle.h" | 25 #include "chrome/browser/policy/policy_bundle.h" |
| 20 #include "chrome/browser/policy/policy_map.h" | 26 #include "chrome/browser/policy/policy_map.h" |
| 21 #include "policy/policy_constants.h" | 27 #include "policy/policy_constants.h" |
| 22 | 28 |
| 23 using base::win::RegKey; | 29 using base::win::RegKey; |
| 24 | 30 |
| (...skipping 171 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 196 if (!LoadHighestPriorityKey(string16(), name, &key, level, scope)) | 202 if (!LoadHighestPriorityKey(string16(), name, &key, level, scope)) |
| 197 return NULL; | 203 return NULL; |
| 198 string16 value; | 204 string16 value; |
| 199 if (!ReadRegistryString(&key, name, &value)) | 205 if (!ReadRegistryString(&key, name, &value)) |
| 200 return NULL; | 206 return NULL; |
| 201 return base::JSONReader::Read(UTF16ToUTF8(value)); | 207 return base::JSONReader::Read(UTF16ToUTF8(value)); |
| 202 } | 208 } |
| 203 | 209 |
| 204 } // namespace | 210 } // namespace |
| 205 | 211 |
| 206 ConfigurationPolicyProviderDelegateWin::ConfigurationPolicyProviderDelegateWin( | 212 PolicyLoaderWin::PolicyLoaderWin(const PolicyDefinitionList* policy_list) |
| 207 const PolicyDefinitionList* policy_definition_list) | 213 : is_initialized_(false), |
| 208 : policy_definition_list_(policy_definition_list) {} | 214 policy_list_(policy_list), |
| 215 user_policy_changed_event_(false, false), |
| 216 machine_policy_changed_event_(false, false), |
| 217 user_policy_watcher_failed_(false), |
| 218 machine_policy_watcher_failed_(false) { |
| 219 if (!RegisterGPNotification(user_policy_changed_event_.handle(), false)) { |
| 220 DPLOG(WARNING) << "Failed to register user group policy notification"; |
| 221 user_policy_watcher_failed_ = true; |
| 222 } |
| 223 if (!RegisterGPNotification(machine_policy_changed_event_.handle(), true)) { |
| 224 DPLOG(WARNING) << "Failed to register machine group policy notification."; |
| 225 machine_policy_watcher_failed_ = true; |
| 226 } |
| 227 } |
| 209 | 228 |
| 210 scoped_ptr<PolicyBundle> ConfigurationPolicyProviderDelegateWin::Load() { | 229 PolicyLoaderWin::~PolicyLoaderWin() { |
| 230 user_policy_watcher_.StopWatching(); |
| 231 machine_policy_watcher_.StopWatching(); |
| 232 } |
| 233 |
| 234 void PolicyLoaderWin::InitOnFile() { |
| 235 is_initialized_ = true; |
| 236 SetupWatches(); |
| 237 } |
| 238 |
| 239 scoped_ptr<PolicyBundle> PolicyLoaderWin::Load() { |
| 240 // Reset the watches BEFORE reading the individual policies to avoid |
| 241 // missing a change notification. |
| 242 if (is_initialized_) |
| 243 SetupWatches(); |
| 244 |
| 211 scoped_ptr<PolicyBundle> bundle(new PolicyBundle()); | 245 scoped_ptr<PolicyBundle> bundle(new PolicyBundle()); |
| 212 PolicyMap& chrome_policy = bundle->Get(POLICY_DOMAIN_CHROME, std::string()); | 246 PolicyMap& chrome_policy = bundle->Get(POLICY_DOMAIN_CHROME, std::string()); |
| 213 | 247 |
| 214 const PolicyDefinitionList::Entry* current; | 248 const PolicyDefinitionList::Entry* current; |
| 215 for (current = policy_definition_list_->begin; | 249 for (current = policy_list_->begin; current != policy_list_->end; ++current) { |
| 216 current != policy_definition_list_->end; | |
| 217 ++current) { | |
| 218 const string16 name(ASCIIToUTF16(current->name)); | 250 const string16 name(ASCIIToUTF16(current->name)); |
| 219 PolicyLevel level = POLICY_LEVEL_MANDATORY; | 251 PolicyLevel level = POLICY_LEVEL_MANDATORY; |
| 220 PolicyScope scope = POLICY_SCOPE_MACHINE; | 252 PolicyScope scope = POLICY_SCOPE_MACHINE; |
| 221 Value* value = NULL; | 253 Value* value = NULL; |
| 222 | 254 |
| 223 switch (current->value_type) { | 255 switch (current->value_type) { |
| 224 case Value::TYPE_STRING: | 256 case Value::TYPE_STRING: |
| 225 value = ReadStringValue(name, &level, &scope); | 257 value = ReadStringValue(name, &level, &scope); |
| 226 break; | 258 break; |
| 227 | 259 |
| (...skipping 17 matching lines...) Expand all Loading... |
| 245 NOTREACHED(); | 277 NOTREACHED(); |
| 246 } | 278 } |
| 247 | 279 |
| 248 if (value) | 280 if (value) |
| 249 chrome_policy.Set(current->name, level, scope, value); | 281 chrome_policy.Set(current->name, level, scope, value); |
| 250 } | 282 } |
| 251 | 283 |
| 252 return bundle.Pass(); | 284 return bundle.Pass(); |
| 253 } | 285 } |
| 254 | 286 |
| 287 void PolicyLoaderWin::SetupWatches() { |
| 288 DCHECK(is_initialized_); |
| 289 if (!user_policy_watcher_failed_ && |
| 290 !user_policy_watcher_.GetWatchedObject() && |
| 291 !user_policy_watcher_.StartWatching( |
| 292 user_policy_changed_event_.handle(), this)) { |
| 293 DLOG(WARNING) << "Failed to start watch for user policy change event"; |
| 294 user_policy_watcher_failed_ = true; |
| 295 } |
| 296 if (!machine_policy_watcher_failed_ && |
| 297 !machine_policy_watcher_.GetWatchedObject() && |
| 298 !machine_policy_watcher_.StartWatching( |
| 299 machine_policy_changed_event_.handle(), this)) { |
| 300 DLOG(WARNING) << "Failed to start watch for machine policy change event"; |
| 301 machine_policy_watcher_failed_ = true; |
| 302 } |
| 303 } |
| 304 |
| 305 void PolicyLoaderWin::OnObjectSignaled(HANDLE object) { |
| 306 DCHECK(object == user_policy_changed_event_.handle() || |
| 307 object == machine_policy_changed_event_.handle()) |
| 308 << "unexpected object signaled policy reload, obj = " |
| 309 << std::showbase << std::hex << object; |
| 310 Reload(false); |
| 311 } |
| 312 |
| 255 } // namespace policy | 313 } // namespace policy |
| OLD | NEW |