File: registry_entry.h

package info (click to toggle)
chromium 139.0.7258.127-1
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 6,122,068 kB
  • sloc: cpp: 35,100,771; ansic: 7,163,530; javascript: 4,103,002; python: 1,436,920; asm: 946,517; xml: 746,709; pascal: 187,653; perl: 88,691; sh: 88,436; objc: 79,953; sql: 51,488; cs: 44,583; fortran: 24,137; makefile: 22,147; tcl: 15,277; php: 13,980; yacc: 8,984; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (128 lines) | stat: -rw-r--r-- 5,222 bytes parent folder | download | duplicates (7)
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
// Copyright 2015 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef CHROME_INSTALLER_UTIL_REGISTRY_ENTRY_H_
#define CHROME_INSTALLER_UTIL_REGISTRY_ENTRY_H_

#include <windows.h>

#include <stdint.h>

#include <string>

class WorkItemList;

// This class represents a single registry entry (a key and its value). A
// collection of registry entries should be collected into a list and written
// transactionally using a WorkItemList. This is preferred to writing to the
// registry directly, because if anything goes wrong, they can be rolled back.
//
// NOTE: This uses the default WOW64 view (32-bit on 32-bit applications, 64-bit
// on 64-bit applications). If the view needs to be customized, a parameter
// should be added, like in WorkItem. http://crbug.com/569816.
class RegistryEntry {
 public:
  // A bit-field enum of places to look for this key in the Windows registry.
  enum LookForIn {
    LOOK_IN_HKCU = 1 << 0,
    LOOK_IN_HKLM = 1 << 1,
    LOOK_IN_HKCU_THEN_HKLM = LOOK_IN_HKCU | LOOK_IN_HKLM,
  };

  // Identifies the type of removal this RegistryEntry is flagged for, if any.
  enum class RemovalFlag {
    // Default case: install the key/value.
    NONE,
    // Registry value under |key_path_|\|name_| is flagged for deletion.
    VALUE,
    // Registry key under |key_path_| is flag for deletion.
    KEY,
  };

  // Create an object that represent default value of a key.
  RegistryEntry(const std::wstring& key_path, const std::wstring& value);

  // Create an object that represent a key of type REG_SZ.
  RegistryEntry(const std::wstring& key_path,
                const std::wstring& name,
                const std::wstring& value);

  // Create an object that represent a key of integer type.
  RegistryEntry(const std::wstring& key_path,
                const std::wstring& name,
                DWORD value);

  RegistryEntry(const RegistryEntry&) = delete;
  RegistryEntry& operator=(const RegistryEntry&) = delete;

  // Flags this RegistryKey with |removal_flag|, indicating that it should be
  // removed rather than created. Note that this will not result in cleaning up
  // the entire registry hierarchy below RegistryEntry even if it is left empty
  // by this operation (this should thus not be used for uninstall, but only to
  // unregister keys that should explicitly no longer be active in the current
  // configuration).
  void set_removal_flag(RemovalFlag removal_flag) {
    removal_flag_ = removal_flag;
  }

  // Generates work_item tasks required to create (or potentially delete based
  // on |removal_flag_|) the current RegistryEntry and add them to the given
  // work item list.
  void AddToWorkItemList(HKEY root, WorkItemList* items) const;

  // Returns true if this key is flagged for removal.
  bool IsFlaggedForRemoval() const {
    return removal_flag_ != RemovalFlag::NONE;
  }

  // Checks if the current registry entry exists in HKCU\|key_path_|\|name_|
  // and value is |value_|. If the key does NOT exist in HKCU, checks for
  // the correct name and value in HKLM.
  // |look_for_in| specifies roots (HKCU and/or HKLM) in which to look for the
  // key, unspecified roots are not looked into (i.e. the the key is assumed not
  // to exist in them).
  // |look_for_in| must at least specify one root to look into.
  // If |look_for_in| is LOOK_IN_HKCU_THEN_HKLM, this method mimics Windows'
  // behavior when searching in HKCR (HKCU takes precedence over HKLM). For
  // registrations outside of HKCR on versions of Windows prior to Win8,
  // Chrome's values go in HKLM. This function will make unnecessary (but
  // harmless) queries into HKCU in that case.
  bool ExistsInRegistry(uint32_t look_for_in) const;

  // Checks if the current registry entry exists in \|key_path_|\|name_|,
  // regardless of value. Same lookup rules as ExistsInRegistry.
  // Unlike ExistsInRegistry, this returns true if some other value is present
  // with the same key.
  bool KeyExistsInRegistry(uint32_t look_for_in) const;

  const std::wstring& key_path() const { return key_path_; }

 private:
  // States this RegistryKey can be in compared to the registry.
  enum RegistryStatus {
    // |name_| does not exist in the registry
    DOES_NOT_EXIST,
    // |name_| exists, but its value != |value_|
    DIFFERENT_VALUE,
    // |name_| exists and its value is |value_|
    SAME_VALUE,
  };

  std::wstring key_path_;    // key path for the registry entry
  std::wstring name_;        // name of the registry entry
  bool is_string_;           // true if current registry entry is of type REG_SZ
  std::wstring value_;       // string value (useful if is_string_ = true)
  DWORD int_value_;          // integer value (useful if is_string_ = false)

  // Identifies whether this RegistryEntry is flagged for removal (i.e. no
  // longer relevant on the configuration it was created under).
  RemovalFlag removal_flag_;

  // Helper function for ExistsInRegistry().
  // Returns the RegistryStatus of the current registry entry in
  // |root|\|key_path_|\|name_|.
  RegistryStatus StatusInRegistryUnderRoot(HKEY root) const;
};

#endif  // CHROME_INSTALLER_UTIL_REGISTRY_ENTRY_H_