File: cros_settings.h

package info (click to toggle)
chromium 138.0.7204.183-1
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 6,071,908 kB
  • sloc: cpp: 34,937,088; ansic: 7,176,967; javascript: 4,110,704; python: 1,419,953; 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,806; 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 (158 lines) | stat: -rw-r--r-- 6,695 bytes parent folder | download | duplicates (3)
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
// Copyright 2012 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef CHROMEOS_ASH_COMPONENTS_SETTINGS_CROS_SETTINGS_H_
#define CHROMEOS_ASH_COMPONENTS_SETTINGS_CROS_SETTINGS_H_

#include <map>
#include <memory>
#include <optional>
#include <string>
#include <string_view>
#include <vector>

#include "base/callback_list.h"
#include "base/component_export.h"
#include "base/functional/callback_forward.h"
#include "base/memory/raw_ptr.h"
#include "base/sequence_checker.h"
#include "base/values.h"
#include "chromeos/ash/components/settings/cros_settings_names.h"
#include "chromeos/ash/components/settings/cros_settings_provider.h"
#include "components/user_manager/user_type.h"

static_assert(BUILDFLAG(IS_CHROMEOS), "For ChromeOS only");

namespace ash {

// This class manages per-device/global settings.
class COMPONENT_EXPORT(CHROMEOS_ASH_COMPONENTS_SETTINGS) CrosSettings {
 public:
  // Returns the global singleton instance.
  // Life time is managed by CrosSettingsHolder or, if it is in tests,
  // ScopedTestingCrosSettings.
  static bool IsInitialized();
  static CrosSettings* Get();

  // Creates an instance with no providers as yet.
  CrosSettings();
  CrosSettings(const CrosSettings&) = delete;
  CrosSettings& operator=(const CrosSettings&) = delete;
  ~CrosSettings();

  // Helper function to test if the given |path| is a valid cros setting.
  static bool IsCrosSettings(std::string_view path);

  // Returns setting value for the given |path|.
  const base::Value* GetPref(std::string_view path) const;

  // Requests that all providers ensure the values they are serving were read
  // from a trusted store:
  // * If all providers are serving trusted values, returns TRUSTED. This
  //   indicates that the cros settings returned by |this| can be trusted during
  //   the current loop cycle.
  // * If at least one provider ran into a permanent failure while trying to
  //   read values from its trusted store, returns PERMANENTLY_UNTRUSTED. This
  //   indicates that the cros settings will never become trusted.
  // * Otherwise, returns TEMPORARILY_UNTRUSTED. This indicates that at least
  //   one provider needs to read values from its trusted store first. The
  //   |callback| will be called back when the read is done.
  //   PrepareTrustedValues() should be called again at that point to determine
  //   whether all providers are serving trusted values now.
  CrosSettingsProvider::TrustedStatus PrepareTrustedValues(
      base::OnceClosure callback) const;

  // These are convenience forms of Get().  The value will be retrieved
  // and the return value will be true if the |path| is valid and the value at
  // the end of the path can be returned in the form specified.
  bool GetBoolean(std::string_view path, bool* out_value) const;
  bool GetInteger(std::string_view path, int* out_value) const;
  bool GetDouble(std::string_view path, double* out_value) const;
  bool GetString(std::string_view path, std::string* out_value) const;
  bool GetList(std::string_view path,
               const base::Value::List** out_value) const;
  bool GetDictionary(std::string_view path,
                     const base::Value::Dict** out_value) const;

  // Checks if the given username is on the list of users allowed to sign-in to
  // this device. |wildcard_match| may be nullptr. If it's present, it'll be set
  // to true if the list check was satisfied via a wildcard. In some
  // configurations user can be allowed based on the |user_type|. See
  // |DeviceFamilyLinkAccountsAllowed| policy.
  bool IsUserAllowlisted(
      const std::string& username,
      bool* wildcard_match,
      const std::optional<user_manager::UserType>& user_type) const;

  // Helper function for the allowlist op. Implemented here because we will need
  // this in a few places. The functions searches for |email| in the pref |path|
  // It respects allowlists so foo@bar.baz will match *@bar.baz too. If the
  // match was via a wildcard, |wildcard_match| is set to true.
  bool FindEmailInList(const std::string& path,
                       const std::string& email,
                       bool* wildcard_match) const;

  // Same as above, but receives already populated user list.
  static bool FindEmailInList(const base::Value::List& list,
                              const std::string& email,
                              bool* wildcard_match);

  // Sets a special CrosSettingsProvider for child account handling.
  // This can be called at most once per instance.
  void SetSupervisedUserCrosSettingsProvider(
      std::unique_ptr<CrosSettingsProvider> provider);

  // Adding/removing of providers.
  bool AddSettingsProvider(std::unique_ptr<CrosSettingsProvider> provider);
  std::unique_ptr<CrosSettingsProvider> RemoveSettingsProvider(
      CrosSettingsProvider* provider);

  // Add an observer Callback for changes for the given |path|.
  [[nodiscard]] base::CallbackListSubscription AddSettingsObserver(
      const std::string& path,
      base::RepeatingClosure callback);

  // Returns the provider that handles settings with the |path| or prefix.
  CrosSettingsProvider* GetProvider(std::string_view path) const;

  // TODO(hidehiko): Consider to migrate this into GetProvider().
  const CrosSettingsProvider* supervised_user_cros_settings_provider() const {
    return supervised_user_cros_settings_provider_;
  }

 private:
  friend class CrosSettingsTest;

  // Allows accessing to SetInstance.
  friend class CrosSettingsHolder;
  friend class ScopedTestingCrosSettings;

  // Sets `cros_settings` as a global instance. This does not take ownership,
  // so the caller still has the responsibility to destroy the instance
  // on appropriate timing. Also, the caller has the responsibility to call
  // `SetInstance(nullptr)` before destroying the instance.
  // If this is called while the global instance is already set, this will
  // cause crash.
  static void SetInstance(CrosSettings* cros_settings);

  // Fires system setting change callback.
  void FireObservers(const std::string& path);

  // List of ChromeOS system settings providers.
  std::vector<std::unique_ptr<CrosSettingsProvider>> providers_;

  // Owner unique pointer in |providers_|.
  raw_ptr<CrosSettingsProvider> supervised_user_cros_settings_provider_;

  // A map from settings names to a list of observers. Observers get fired in
  // the order they are added.
  std::map<std::string, std::unique_ptr<base::RepeatingClosureList>>
      settings_observers_;

  SEQUENCE_CHECKER(sequence_checker_);
};

}  // namespace ash

#endif  // CHROMEOS_ASH_COMPONENTS_SETTINGS_CROS_SETTINGS_H_