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