1 // Copyright 2013 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 COMPONENTS_POLICY_CORE_COMMON_REGISTRY_DICT_H_ 6 #define COMPONENTS_POLICY_CORE_COMMON_REGISTRY_DICT_H_ 7 8 #include <map> 9 #include <memory> 10 #include <string> 11 12 #include "base/macros.h" 13 #include "base/strings/string16.h" 14 #include "components/policy/policy_export.h" 15 16 #if defined(OS_WIN) 17 #include <windows.h> 18 #endif 19 20 namespace base { 21 class Value; 22 } 23 24 namespace policy { 25 26 class Schema; 27 28 // Converts a value (as read from the registry) to meet |schema|, converting 29 // types as necessary. Unconvertible types will show up as null values in the 30 // result. 31 std::unique_ptr<base::Value> POLICY_EXPORT 32 ConvertRegistryValue(const base::Value& value, const Schema& schema); 33 34 // A case-insensitive string comparison functor. 35 struct POLICY_EXPORT CaseInsensitiveStringCompare { 36 bool operator()(const std::string& a, const std::string& b) const; 37 }; 38 39 // In-memory representation of a registry subtree. Using a 40 // base::DictionaryValue directly seems tempting, but that doesn't handle the 41 // registry's case-insensitive-but-case-preserving semantics properly. 42 class POLICY_EXPORT RegistryDict { 43 public: 44 using KeyMap = std::map<std::string, 45 std::unique_ptr<RegistryDict>, 46 CaseInsensitiveStringCompare>; 47 using ValueMap = std::map<std::string, 48 std::unique_ptr<base::Value>, 49 CaseInsensitiveStringCompare>; 50 51 RegistryDict(); 52 ~RegistryDict(); 53 54 // Returns a pointer to an existing key, NULL if not present. 55 RegistryDict* GetKey(const std::string& name); 56 const RegistryDict* GetKey(const std::string& name) const; 57 // Sets a key. If |dict| is NULL, clears that key. 58 void SetKey(const std::string& name, std::unique_ptr<RegistryDict> dict); 59 // Removes a key. If the key doesn't exist, NULL is returned. 60 std::unique_ptr<RegistryDict> RemoveKey(const std::string& name); 61 // Clears all keys. 62 void ClearKeys(); 63 64 // Returns a pointer to a value, NULL if not present. 65 base::Value* GetValue(const std::string& name); 66 const base::Value* GetValue(const std::string& name) const; 67 // Sets a value. If |value| is NULL, removes the value. 68 void SetValue(const std::string& name, std::unique_ptr<base::Value> value); 69 // Removes a value. If the value doesn't exist, NULL is returned. 70 std::unique_ptr<base::Value> RemoveValue(const std::string& name); 71 // Clears all values. 72 void ClearValues(); 73 74 // Merge keys and values from |other|, giving precedence to |other|. 75 void Merge(const RegistryDict& other); 76 77 // Swap with |other|. 78 void Swap(RegistryDict* other); 79 80 #if defined(OS_WIN) 81 // Read a Windows registry subtree into this registry dictionary object. 82 void ReadRegistry(HKEY hive, const base::string16& root); 83 84 // Converts the dictionary to base::Value representation. For key/value name 85 // collisions, the key wins. |schema| is used to determine the expected type 86 // for each policy. 87 // The returned object is either a base::DictionaryValue or a base::ListValue. 88 std::unique_ptr<base::Value> ConvertToJSON(const class Schema& schema) const; 89 #endif 90 keys()91 const KeyMap& keys() const { return keys_; } values()92 const ValueMap& values() const { return values_; } 93 94 private: 95 KeyMap keys_; 96 ValueMap values_; 97 98 DISALLOW_COPY_AND_ASSIGN(RegistryDict); 99 }; 100 101 } // namespace policy 102 103 #endif // COMPONENTS_POLICY_CORE_COMMON_REGISTRY_DICT_H_ 104