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