[go: nahoru, domu]

blob: 4dec0d32234d78bc9ae8f299ba38e3eb4443b3fd [file] [log] [blame]
Avi Drissman8ba1bad2022-09-13 19:22:361// Copyright 2022 The Chromium Authors
Luc Nguyen97ad3ae2022-05-20 19:54:432// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#include "components/local_state/local_state_utils.h"
6
7#include <string>
8#include <vector>
9
Claudio DeSouza6427103e2023-03-10 21:26:2610#include "base/json/json_writer.h"
Luc Nguyen97ad3ae2022-05-20 19:54:4311#include "base/strings/string_util.h"
12#include "base/values.h"
13#include "build/build_config.h"
Luc Nguyen97ad3ae2022-05-20 19:54:4314#include "components/prefs/pref_service.h"
Yann Dago6811ccd2023-07-18 21:01:2815#include "extensions/buildflags/buildflags.h"
Luc Nguyen97ad3ae2022-05-20 19:54:4316
Yann Dago6811ccd2023-07-18 21:01:2817namespace local_state_utils {
Luc Nguyen97ad3ae2022-05-20 19:54:4318namespace {
19
20// Returns true if |pref_name| starts with one of the |valid_prefixes|.
21bool HasValidPrefix(const std::string& pref_name,
22 const std::vector<std::string> valid_prefixes) {
23 for (const std::string& prefix : valid_prefixes) {
Avi Drissmanc8ec00c2023-02-08 17:46:4824 if (base::StartsWith(pref_name, prefix, base::CompareCase::SENSITIVE)) {
Luc Nguyen97ad3ae2022-05-20 19:54:4325 return true;
Avi Drissmanc8ec00c2023-02-08 17:46:4826 }
Luc Nguyen97ad3ae2022-05-20 19:54:4327 }
28 return false;
29}
30
Yann Dago6811ccd2023-07-18 21:01:2831base::Value::List GetPrefsMetadata(
32 PrefValueStore::PrefStoreType pref_value_store_type) {
33 base::Value::List metadata;
34 switch (pref_value_store_type) {
35 case PrefValueStore::PrefStoreType::MANAGED_STORE:
36 metadata.Append("managed");
37 break;
38 case PrefValueStore::PrefStoreType::SUPERVISED_USER_STORE:
39 metadata.Append("managed_by_custodian");
40 break;
41 case PrefValueStore::PrefStoreType::EXTENSION_STORE:
42#if BUILDFLAG(ENABLE_EXTENSIONS)
43 metadata.Append("extension_controlled");
44 metadata.Append("extension_modifiable");
45#else
46 NOTREACHED();
47#endif
48 break;
49 case PrefValueStore::PrefStoreType::STANDALONE_BROWSER_STORE:
50#if BUILDFLAG(IS_CHROMEOS_ASH)
51 metadata.Append("standalone_browser_controlled");
52 metadata.Append("standalone_browser_modifiable");
53#endif
54 metadata.Append("extension_modifiable");
55 break;
56 case PrefValueStore::PrefStoreType::COMMAND_LINE_STORE:
57 metadata.Append("command_line_controlled");
58#if BUILDFLAG(ENABLE_EXTENSIONS)
59 metadata.Append("extension_modifiable");
60#endif
61#if BUILDFLAG(IS_CHROMEOS_ASH)
62 metadata.Append("standalone_browser_modifiable");
63#endif
64 break;
65 case PrefValueStore::PrefStoreType::USER_STORE:
66 metadata.Append("user_controlled");
67 metadata.Append("user_modifiable");
68#if BUILDFLAG(ENABLE_EXTENSIONS)
69 metadata.Append("extension_modifiable");
70#endif
71#if BUILDFLAG(IS_CHROMEOS_ASH)
72 metadata.Append("standalone_browser_modifiable");
73#endif
74 break;
75 case PrefValueStore::PrefStoreType::RECOMMENDED_STORE:
76 metadata.Append("recommended");
77 metadata.Append("user_modifiable");
78#if BUILDFLAG(ENABLE_EXTENSIONS)
79 metadata.Append("extension_modifiable");
80#endif
81#if BUILDFLAG(IS_CHROMEOS_ASH)
82 metadata.Append("standalone_browser_modifiable");
83#endif
84 break;
85 case PrefValueStore::PrefStoreType::DEFAULT_STORE:
86 metadata.Append("default");
87 metadata.Append("user_modifiable");
88#if BUILDFLAG(ENABLE_EXTENSIONS)
89 metadata.Append("extension_modifiable");
90#endif
91#if BUILDFLAG(IS_CHROMEOS_ASH)
92 metadata.Append("standalone_browser_modifiable");
93#endif
94 break;
95 case PrefValueStore::PrefStoreType::INVALID_STORE:
96 metadata.Append("user_modifiable");
97#if BUILDFLAG(ENABLE_EXTENSIONS)
98 metadata.Append("extension_modifiable");
99#endif
100#if BUILDFLAG(IS_CHROMEOS_ASH)
101 metadata.Append("standalone_browser_modifiable");
102#endif
103 break;
Luc Nguyen97ad3ae2022-05-20 19:54:43104 }
Yann Dago6811ccd2023-07-18 21:01:28105 return metadata;
Luc Nguyen97ad3ae2022-05-20 19:54:43106}
107
Yann Dago6811ccd2023-07-18 21:01:28108} // namespace
Luc Nguyen97ad3ae2022-05-20 19:54:43109
Arthur Sonzognic571efb2024-01-26 20:26:18110std::optional<std::string> GetPrefsAsJson(
Yann Dago6811ccd2023-07-18 21:01:28111 PrefService* pref_service,
112 const std::vector<std::string>& accepted_prefixes) {
113 std::vector<PrefService::PreferenceValueAndStore> values =
114 pref_service->GetPreferencesValueAndStore();
115
116 base::Value::Dict local_state_values;
117 for (const auto& [name, value, pref_value_store_type] : values) {
Luc Nguyen97ad3ae2022-05-20 19:54:43118 // Filter out the prefs to only include variations and UMA related fields,
119 // which don't contain PII.
Yann Dago6811ccd2023-07-18 21:01:28120 if (!accepted_prefixes.empty() &&
121 !HasValidPrefix(name, accepted_prefixes)) {
122 continue;
123 }
124
125 base::Value::Dict pref_details;
126 pref_details.Set("value", value.Clone());
127 pref_details.Set("metadata", GetPrefsMetadata(pref_value_store_type));
128 local_state_values.SetByDottedPath(name, std::move(pref_details));
Luc Nguyen97ad3ae2022-05-20 19:54:43129 }
130
Claudio DeSouza6427103e2023-03-10 21:26:26131 std::string result;
132 if (!base::JSONWriter::WriteWithOptions(
133 local_state_values, base::JSONWriter::OPTIONS_PRETTY_PRINT,
134 &result)) {
Arthur Sonzognic571efb2024-01-26 20:26:18135 return std::nullopt;
Claudio DeSouza6427103e2023-03-10 21:26:26136 }
137 return result;
138}
Yann Dago6811ccd2023-07-18 21:01:28139
140} // namespace local_state_utils