File: prefs_util.h

package info (click to toggle)
chromium 138.0.7204.183-1~deb12u1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm-proposed-updates
  • size: 6,080,960 kB
  • sloc: cpp: 34,937,079; ansic: 7,176,967; javascript: 4,110,704; python: 1,419,954; asm: 946,768; xml: 739,971; pascal: 187,324; sh: 89,623; perl: 88,663; objc: 79,944; sql: 50,304; cs: 41,786; fortran: 24,137; makefile: 21,811; php: 13,980; tcl: 13,166; yacc: 8,925; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (117 lines) | stat: -rw-r--r-- 4,497 bytes parent folder | download | duplicates (5)
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
// 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_BROWSER_EXTENSIONS_API_SETTINGS_PRIVATE_PREFS_UTIL_H_
#define CHROME_BROWSER_EXTENSIONS_API_SETTINGS_PRIVATE_PREFS_UTIL_H_

#include <map>
#include <optional>
#include <string>

#include "base/memory/raw_ptr.h"
#include "build/build_config.h"
#include "build/chromeos_buildflags.h"
#include "chrome/browser/extensions/api/settings_private/prefs_util_enums.h"
#include "chrome/common/extensions/api/settings_private.h"

class PrefService;
class Profile;

namespace extensions {
class Extension;

class PrefsUtil {

 public:
  // TODO(dbeam): why is the key a std::string rather than const char*?
  using TypedPrefMap = std::map<std::string, api::settings_private::PrefType>;

  explicit PrefsUtil(Profile* profile);
  virtual ~PrefsUtil();

  // Gets the list of allowlisted pref keys -- that is, those which correspond
  // to prefs that clients of the settingsPrivate API may retrieve and
  // manipulate.
  const TypedPrefMap& GetAllowlistedKeys();

  // Returns the pref type for |pref_name| or PREF_TYPE_NONE if not in the
  // allowlist.
  api::settings_private::PrefType GetAllowlistedPrefType(
      const std::string& pref_name);

  // Gets the value of the pref with the given |name|. Returns a nullopt if no
  // pref is found for |name|.
  virtual std::optional<api::settings_private::PrefObject> GetPref(
      const std::string& name);

  // Sets the pref with the given name and value in the proper PrefService.
  virtual settings_private::SetPrefResult SetPref(const std::string& name,
                                                  const base::Value* value);

  // Appends the given |value| to the list setting specified by the path in
  // |pref_name|.
  virtual bool AppendToListCrosSetting(const std::string& pref_name,
                                       const base::Value& value);

  // Removes the given |value| from the list setting specified by the path in
  // |pref_name|.
  virtual bool RemoveFromListCrosSetting(const std::string& pref_name,
                                         const base::Value& value);

  // Returns a pointer to the appropriate PrefService instance for the given
  // |pref_name|.
  virtual PrefService* FindServiceForPref(const std::string& pref_name);

  // Returns whether or not the given pref is a CrOS-specific setting.
  virtual bool IsCrosSetting(const std::string& pref_name);

 protected:
  // Returns whether |pref_name| corresponds to a pref whose type is URL.
  bool IsPrefTypeURL(const std::string& pref_name);

#if BUILDFLAG(IS_CHROMEOS)
  // Returns whether |pref_name| corresponds to a pref that is enterprise
  // managed.
  bool IsPrefEnterpriseManaged(const std::string& pref_name);

  // Returns whether |pref_name| corresponds to a pref that is controlled by
  // the owner, and |profile_| is not the owner profile.
  bool IsPrefOwnerControlled(const std::string& pref_name);

  // Returns whether |pref_name| corresponds to a pref that is controlled by
  // the primary user, and |profile_| is not the primary profile.
  bool IsPrefPrimaryUserControlled(const std::string& pref_name);

  // Returns whether |pref_name| corresponds to the hotword enabled pref, if the
  // pref is disabled, and if |profile_| is a child user.
  bool IsHotwordDisabledForChildUser(const std::string& pref_name);
#endif

  // Returns whether |pref_name| corresponds to a pref that is controlled by
  // a supervisor, and |profile_| is supervised.
  bool IsPrefSupervisorControlled(const std::string& pref_name);

  // Returns whether |pref_name| corresponds to a pref that is user modifiable
  // (i.e., not made restricted by a user or device policy).
  bool IsPrefUserModifiable(const std::string& pref_name);

  api::settings_private::PrefType GetType(const std::string& name,
                                          base::Value::Type type);

  std::optional<api::settings_private::PrefObject> GetCrosSettingsPref(
      const std::string& name);

  settings_private::SetPrefResult SetCrosSettingsPref(const std::string& name,
                                                      const base::Value* value);

 private:
  const Extension* GetExtensionControllingPref(
      const api::settings_private::PrefObject& pref_object);

  raw_ptr<Profile> profile_;  // weak
};

}  // namespace extensions

#endif  // CHROME_BROWSER_EXTENSIONS_API_SETTINGS_PRIVATE_PREFS_UTIL_H_