File: registry_hash_store_contents_win.cc

package info (click to toggle)
chromium-browser 57.0.2987.98-1~deb8u1
  • links: PTS, VCS
  • area: main
  • in suites: jessie
  • size: 2,637,852 kB
  • ctags: 2,544,394
  • sloc: cpp: 12,815,961; ansic: 3,676,222; python: 1,147,112; asm: 526,608; java: 523,212; xml: 286,794; perl: 92,654; sh: 86,408; objc: 73,271; makefile: 27,698; cs: 18,487; yacc: 13,031; tcl: 12,957; pascal: 4,875; ml: 4,716; lex: 3,904; sql: 3,862; ruby: 1,982; lisp: 1,508; php: 1,368; exp: 404; awk: 325; csh: 117; jsp: 39; sed: 37
file content (182 lines) | stat: -rw-r--r-- 6,303 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
// Copyright (c) 2016 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "components/user_prefs/tracked/registry_hash_store_contents_win.h"

#include <windows.h>

#include "base/logging.h"
#include "base/memory/ptr_util.h"
#include "base/numerics/safe_conversions.h"
#include "base/strings/string_split.h"
#include "base/strings/stringprintf.h"
#include "base/strings/utf_string_conversions.h"
#include "base/values.h"
#include "base/win/registry.h"
#include "components/user_prefs/tracked/tracked_preference_histogram_names.h"

using base::win::RegistryValueIterator;

namespace {

constexpr size_t kMacSize = 64;

base::string16 GetSplitPrefKeyName(const base::string16& reg_key_name,
                                   const std::string& split_key_name) {
  return reg_key_name + L"\\" + base::UTF8ToUTF16(split_key_name);
}

bool ReadMacFromRegistry(const base::win::RegKey& key,
                         const std::string& value_name,
                         std::string* out_mac) {
  base::string16 string_value;
  if (key.ReadValue(base::UTF8ToUTF16(value_name).c_str(), &string_value) ==
          ERROR_SUCCESS &&
      string_value.size() == kMacSize) {
    out_mac->assign(base::UTF16ToUTF8(string_value));
    return true;
  }
  return false;
}

// Removes |value_name| under |reg_key_name|. Returns true if found and
// successfully removed.
bool ClearAtomicMac(const base::string16& reg_key_name,
                    const std::string& value_name) {
  base::win::RegKey key;
  if (key.Open(HKEY_CURRENT_USER, reg_key_name.c_str(),
               KEY_SET_VALUE | KEY_WOW64_32KEY) == ERROR_SUCCESS) {
    return key.DeleteValue(base::UTF8ToUTF16(value_name).c_str()) ==
           ERROR_SUCCESS;
  }
  return false;
}

// Deletes |split_key_name| under |reg_key_name|. Returns true if found and
// successfully removed.
bool ClearSplitMac(const base::string16& reg_key_name,
                   const std::string& split_key_name) {
  base::win::RegKey key;
  if (key.Open(HKEY_CURRENT_USER,
               GetSplitPrefKeyName(reg_key_name, split_key_name).c_str(),
               KEY_SET_VALUE | KEY_WOW64_32KEY) == ERROR_SUCCESS) {
    return key.DeleteKey(L"") == ERROR_SUCCESS;
  }
  return false;
}

}  // namespace

RegistryHashStoreContentsWin::RegistryHashStoreContentsWin(
    const base::string16& registry_path,
    const base::string16& store_key)
    : preference_key_name_(registry_path + L"\\PreferenceMACs\\" + store_key) {}

RegistryHashStoreContentsWin::RegistryHashStoreContentsWin(
    const RegistryHashStoreContentsWin& other) = default;

bool RegistryHashStoreContentsWin::IsCopyable() const {
  return true;
}

std::unique_ptr<HashStoreContents> RegistryHashStoreContentsWin::MakeCopy()
    const {
  return base::WrapUnique(new RegistryHashStoreContentsWin(*this));
}

base::StringPiece RegistryHashStoreContentsWin::GetUMASuffix() const {
  return user_prefs::tracked::kTrackedPrefRegistryValidationSuffix;
}

void RegistryHashStoreContentsWin::Reset() {
  base::win::RegKey key;
  if (key.Open(HKEY_CURRENT_USER, preference_key_name_.c_str(),
               KEY_SET_VALUE | KEY_WOW64_32KEY) == ERROR_SUCCESS) {
    LONG result = key.DeleteKey(L"");
    DCHECK(result == ERROR_SUCCESS || result == ERROR_FILE_NOT_FOUND) << result;
  }
}

bool RegistryHashStoreContentsWin::GetMac(const std::string& path,
                                          std::string* out_value) {
  base::win::RegKey key;
  if (key.Open(HKEY_CURRENT_USER, preference_key_name_.c_str(),
               KEY_QUERY_VALUE | KEY_WOW64_32KEY) == ERROR_SUCCESS) {
    return ReadMacFromRegistry(key, path, out_value);
  }

  return false;
}

bool RegistryHashStoreContentsWin::GetSplitMacs(
    const std::string& path,
    std::map<std::string, std::string>* split_macs) {
  DCHECK(split_macs);
  DCHECK(split_macs->empty());

  RegistryValueIterator iter_key(
      HKEY_CURRENT_USER,
      GetSplitPrefKeyName(preference_key_name_, path).c_str());

  for (; iter_key.Valid(); ++iter_key) {
    split_macs->insert(make_pair(base::UTF16ToUTF8(iter_key.Name()),
                                 base::UTF16ToUTF8(iter_key.Value())));
  }

  return !split_macs->empty();
}

void RegistryHashStoreContentsWin::SetMac(const std::string& path,
                                          const std::string& value) {
  base::win::RegKey key;
  DCHECK_EQ(kMacSize, value.size());

  if (key.Create(HKEY_CURRENT_USER, preference_key_name_.c_str(),
                 KEY_SET_VALUE | KEY_WOW64_32KEY) == ERROR_SUCCESS) {
    key.WriteValue(base::UTF8ToUTF16(path).c_str(),
                   base::UTF8ToUTF16(value).c_str());
  }
}

void RegistryHashStoreContentsWin::SetSplitMac(const std::string& path,
                                               const std::string& split_path,
                                               const std::string& value) {
  base::win::RegKey key;
  DCHECK_EQ(kMacSize, value.size());

  if (key.Create(HKEY_CURRENT_USER,
                 GetSplitPrefKeyName(preference_key_name_, path).c_str(),
                 KEY_SET_VALUE | KEY_WOW64_32KEY) == ERROR_SUCCESS) {
    key.WriteValue(base::UTF8ToUTF16(split_path).c_str(),
                   base::UTF8ToUTF16(value).c_str());
  }
}

bool RegistryHashStoreContentsWin::RemoveEntry(const std::string& path) {
  return ClearAtomicMac(preference_key_name_, path) ||
         ClearSplitMac(preference_key_name_, path);
}

void RegistryHashStoreContentsWin::ImportEntry(const std::string& path,
                                               const base::Value* in_value) {
  NOTREACHED()
      << "RegistryHashStoreContents does not support the ImportEntry operation";
}

const base::DictionaryValue* RegistryHashStoreContentsWin::GetContents() const {
  NOTREACHED()
      << "RegistryHashStoreContents does not support the GetContents operation";
  return NULL;
}

std::string RegistryHashStoreContentsWin::GetSuperMac() const {
  NOTREACHED()
      << "RegistryHashStoreContents does not support the GetSuperMac operation";
  return NULL;
}

void RegistryHashStoreContentsWin::SetSuperMac(const std::string& super_mac) {
  NOTREACHED()
      << "RegistryHashStoreContents does not support the SetSuperMac operation";
}