Files
brave-core/components/brave_sync/qr_code_data.cc
T
cdesouza-chromium 164ed33c3d [CodeHealth] Use base::DictValue/base::ListValue - Part XII (#33721)
These classes were hoisted and renamed. This has been replaced in
Chromium as well. This is a mechanical change for Brave, done with the
following script.

```
git grep -lw 'Value::List' | xargs sed -i 's/\bValue::List\b/ListValue/g'
git grep -lw 'Value::Dict' | xargs sed -i 's/\bValue::Dict\b/DictValue/g'
git cl format
```

Chromium changes:
https://chromium.googlesource.com/chromium/src/+/6bc468d481835992696083e99e556516fb7f5f80

```
commit 6bc468d481835992696083e99e556516fb7f5f80
Author: Avi Drissman <avi@chromium.org>
Date:   Thu Jan 29 22:14:50 2026 -0800

    Remove aliases for base::DictValue and base::ListValue

    This removes a few last stragglers as well.

    Fixed: 478100525
    Cq-Include-Trybots: luci.chromium.try:win-official,mac-official,linux-official,android-official,android-desktop-x64-official
    Change-Id: If92142b8ab0562a82c609b71c6b2a7665cea6ec6
    Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/7513889
    Auto-Submit: Avi Drissman <avi@chromium.org>
    Commit-Queue: Daniel Cheng <dcheng@chromium.org>
    Owners-Override: Daniel Cheng <dcheng@chromium.org>
    Reviewed-by: Daniel Cheng <dcheng@chromium.org>
    Cr-Commit-Position: refs/heads/main@{#1577038}
```

Issue: https://github.com/brave/brave-browser/issues/52435
2026-02-06 15:03:15 -03:00

119 lines
3.3 KiB
C++

/* Copyright (c) 2021 The Brave Authors. All rights reserved.
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this file,
* You can obtain one at http://mozilla.org/MPL/2.0/. */
#include "brave/components/brave_sync/qr_code_data.h"
#include <memory>
#include <optional>
#include <string>
#include "base/json/json_reader.h"
#include "base/json/json_writer.h"
#include "base/logging.h"
#include "base/strings/string_number_conversions.h"
#include "base/time/time.h"
// Example of the JSON:
// {
// "version": "2",
// "sync_code_hex" : "<current hex code>",
// "not_after": "1637080050"
// }
namespace brave_sync {
QrCodeData::QrCodeData() : version(kCurrentQrCodeDataVersion) {}
QrCodeData::QrCodeData(const std::string& sync_code_hex,
const base::Time& not_after)
: version(kCurrentQrCodeDataVersion),
sync_code_hex(sync_code_hex),
not_after(not_after) {}
base::Time QrCodeData::FromEpochSeconds(int64_t seconds_since_epoch) {
return base::Time::FromMillisecondsSinceUnixEpoch(seconds_since_epoch * 1000);
}
int64_t QrCodeData::ToEpochSeconds(const base::Time& time) {
return time.InMillisecondsSinceUnixEpoch() / 1000;
}
std::unique_ptr<QrCodeData> QrCodeData::CreateWithActualDate(
const std::string& sync_code_hex) {
return std::unique_ptr<QrCodeData>(new QrCodeData(
sync_code_hex,
base::Time::Now() + base::Minutes(kMinutesFromNowForValidCode)));
}
base::DictValue QrCodeData::ToValue() const {
base::DictValue dict;
dict.Set("version", base::NumberToString(version));
dict.Set("sync_code_hex", sync_code_hex);
dict.Set("not_after", base::NumberToString(ToEpochSeconds(not_after)));
return dict;
}
std::string QrCodeData::ToJson() {
auto dict = ToValue();
std::string json_string;
if (!base::JSONWriter::Write(dict, &json_string)) {
VLOG(1) << "Writing QR data to JSON failed";
json_string = std::string();
}
return json_string;
}
std::unique_ptr<QrCodeData> QrCodeData::FromJson(
const std::string& json_string) {
auto qr_data = std::unique_ptr<QrCodeData>(new QrCodeData());
std::optional<base::DictValue> value = base::JSONReader::ReadDict(
json_string, base::JSON_PARSE_CHROMIUM_EXTENSIONS);
if (!value) {
VLOG(1) << "Invalid JSON: " << *value;
return nullptr;
}
const auto& root = *value;
const std::string* version_value = root.FindString("version");
if (!version_value) {
VLOG(1) << "Missing version";
return nullptr;
}
int version;
if (!base::StringToInt(*version_value, &version)) {
VLOG(1) << "Version has wrong format";
return nullptr;
}
qr_data->version = version;
const std::string* sync_code_hex_value = root.FindString("sync_code_hex");
if (!sync_code_hex_value) {
VLOG(1) << "Missing sync code hex";
return nullptr;
}
qr_data->sync_code_hex = *sync_code_hex_value;
const std::string* not_after_string = root.FindString("not_after");
if (!not_after_string) {
VLOG(1) << "Missing not after time";
return nullptr;
}
int64_t not_after_int;
if (!base::StringToInt64(*not_after_string, &not_after_int)) {
VLOG(1) << "Wrong format for not after time";
return nullptr;
}
qr_data->not_after = FromEpochSeconds(not_after_int);
return qr_data;
}
} // namespace brave_sync