File: early_prefs_reader.cc

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 (144 lines) | stat: -rw-r--r-- 3,971 bytes parent folder | download | duplicates (8)
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
// Copyright 2023 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "chromeos/ash/components/early_prefs/early_prefs_reader.h"

#include <memory>
#include <optional>
#include <utility>

#include "base/files/file_path.h"
#include "base/functional/bind.h"
#include "base/json/json_file_value_serializer.h"
#include "base/logging.h"
#include "base/task/sequenced_task_runner.h"
#include "base/values.h"
#include "chromeos/ash/components/early_prefs/early_prefs_constants.h"

namespace ash {
namespace {

constexpr int kCurrentSchemaVersion = 1;

std::unique_ptr<base::Value> ReadFileFromDisk(const base::FilePath& data_file) {
  std::unique_ptr<base::Value> result;

  int error_code;
  std::string error_msg;
  JSONFileValueDeserializer deserializer(data_file);
  result = deserializer.Deserialize(&error_code, &error_msg);
  if (!result) {
    if (error_code == JSONFileValueDeserializer::JSON_NO_SUCH_FILE) {
      // Somewhat expected situation, do not log.
      return nullptr;
    }
    LOG(ERROR) << "Error while loading early prefs file: " << error_msg;
  }
  return result;
}

}  // namespace

EarlyPrefsReader::EarlyPrefsReader(
    const base::FilePath& data_dir,
    scoped_refptr<base::SequencedTaskRunner> file_task_runner)
    : file_task_runner_(std::move(file_task_runner)) {
  data_file_ = data_dir.Append(early_prefs::kEarlyPrefsFileName);
}

EarlyPrefsReader::~EarlyPrefsReader() = default;

void EarlyPrefsReader::ReadFile(ResultCallback result_callback) {
  CHECK(!data_);
  file_task_runner_->PostTaskAndReplyWithResult(
      FROM_HERE, base::BindOnce(&ReadFileFromDisk, data_file_),
      base::BindOnce(&EarlyPrefsReader::OnFileRead, weak_factory_.GetWeakPtr(),
                     std::move(result_callback)));
}

void EarlyPrefsReader::OnFileRead(ResultCallback callback,
                                  std::unique_ptr<base::Value> root) {
  if (!root) {
    std::move(callback).Run(false);
    return;
  }
  if (!ValidateData(root->GetIfDict())) {
    std::move(callback).Run(false);
    return;
  }
  root_ = std::move(root);
  data_ = root_->GetDict().FindDict(early_prefs::kDataKey);
  CHECK(data_);
  std::move(callback).Run(true);
}

bool EarlyPrefsReader::ValidateData(const base::Value::Dict* root) const {
  if (!root) {
    return false;
  }
  if (root->size() != 2) {
    return false;
  }
  auto schema = root->FindInt(early_prefs::kSchemaKey);
  if (!schema || schema.value() != kCurrentSchemaVersion) {
    return false;
  }
  auto* data = root->FindDict(early_prefs::kDataKey);
  if (!data) {
    return false;
  }
  for (auto pref : *data) {
    if (!ValidatePref(pref.second)) {
      return false;
    }
  }
  return true;
}

bool EarlyPrefsReader::ValidatePref(const base::Value& pref) const {
  if (!pref.is_dict()) {
    return false;
  }
  const auto& dict = pref.GetDict();
  auto is_managed = dict.FindBool(early_prefs::kPrefIsManagedKey);
  if (!is_managed) {
    return false;
  }
  if (is_managed.value()) {
    auto is_recommended = dict.FindBool(early_prefs::kPrefIsRecommendedKey);
    if (!is_recommended) {
      return false;
    }
  }
  if (!dict.Find(early_prefs::kPrefValueKey)) {
    return false;
  }
  return true;
}

bool EarlyPrefsReader::HasPref(const std::string& key) const {
  if (!data_) {
    return false;
  }
  return data_->Find(key) != nullptr;
}

bool EarlyPrefsReader::IsManaged(const std::string& key) const {
  CHECK(HasPref(key));
  return data_->FindDict(key)->FindBool(early_prefs::kPrefIsManagedKey).value();
}

bool EarlyPrefsReader::IsRecommended(const std::string& key) const {
  CHECK(IsManaged(key));
  return data_->FindDict(key)
      ->FindBool(early_prefs::kPrefIsRecommendedKey)
      .value();
}

const base::Value* EarlyPrefsReader::GetValue(const std::string& key) const {
  CHECK(HasPref(key));
  return data_->FindDict(key)->Find(early_prefs::kPrefValueKey);
}

}  // namespace ash