#include "services/data_decoder/public/cpp/data_decoder.h"
#include <memory>
#include <string>
#include <utility>
#include "base/functional/callback.h"
#include "base/json/json_reader.h"
#include "base/memory/ref_counted.h"
#include "base/metrics/histogram_functions.h"
#include "base/no_destructor.h"
#include "base/rust_buildflags.h"
#include "base/task/sequenced_task_runner.h"
#include "base/task/thread_pool.h"
#include "base/time/time.h"
#include "base/timer/elapsed_timer.h"
#include "build/blink_buildflags.h"
#include "build/build_config.h"
#include "components/facilitated_payments/core/mojom/pix_code_validator.mojom.h"
#include "net/http/structured_headers.h"
#include "services/data_decoder/public/mojom/cbor_parser.mojom.h"
#include "services/data_decoder/public/mojom/gzipper.mojom.h"
#include "services/data_decoder/public/mojom/json_parser.mojom.h"
#include "services/data_decoder/public/mojom/structured_headers_parser.mojom.h"
#include "services/data_decoder/public/mojom/xml_parser.mojom.h"
#if BUILDFLAG(IS_ANDROID)
#include "base/types/expected_macros.h"
#include "services/data_decoder/public/cpp/json_sanitizer.h"
#endif
#if !BUILDFLAG(USE_BLINK)
#include "services/data_decoder/data_decoder_service.h"
#endif
namespace data_decoder {
namespace {
constexpr base::TimeDelta kServiceProcessIdleTimeoutDefault{ … };
template <typename T, typename V>
class ValueParseRequest : public base::RefCounted<ValueParseRequest<T, V>> { … };
#if !BUILDFLAG(USE_BLINK)
void BindInProcessService(
mojo::PendingReceiver<mojom::DataDecoderService> receiver) {
static base::NoDestructor<scoped_refptr<base::SequencedTaskRunner>>
task_runner{base::ThreadPool::CreateSequencedTaskRunner({})};
if (!(*task_runner)->RunsTasksInCurrentSequence()) {
(*task_runner)
->PostTask(FROM_HERE,
base::BindOnce(&BindInProcessService, std::move(receiver)));
return;
}
static base::NoDestructor<DataDecoderService> service;
service->BindReceiver(std::move(receiver));
}
#endif
#if BUILDFLAG(IS_ANDROID) || BUILDFLAG(BUILD_RUST_JSON_READER)
void ParsingComplete(scoped_refptr<DataDecoder::CancellationFlag> is_cancelled,
DataDecoder::ValueParseCallback callback,
base::JSONReader::Result value_with_error) { … }
#endif
}
DataDecoder::DataDecoder() : … { … }
DataDecoder::DataDecoder(base::TimeDelta idle_timeout)
: … { … }
DataDecoder::~DataDecoder() { … }
mojom::DataDecoderService* DataDecoder::GetService() { … }
void DataDecoder::ParseJson(const std::string& json,
ValueParseCallback callback) { … }
void DataDecoder::ParseJsonIsolated(const std::string& json,
ValueParseCallback callback) { … }
void DataDecoder::ParseStructuredHeaderItem(
const std::string& header,
StructuredHeaderParseItemCallback callback) { … }
void DataDecoder::ParseStructuredHeaderItemIsolated(
const std::string& header,
StructuredHeaderParseItemCallback callback) { … }
void DataDecoder::ParseStructuredHeaderList(
const std::string& header,
StructuredHeaderParseListCallback callback) { … }
void DataDecoder::ParseStructuredHeaderListIsolated(
const std::string& header,
StructuredHeaderParseListCallback callback) { … }
void DataDecoder::ParseStructuredHeaderDictionary(
const std::string& header,
StructuredHeaderParseDictionaryCallback callback) { … }
void DataDecoder::ParseStructuredHeaderDictionaryIsolated(
const std::string& header,
StructuredHeaderParseDictionaryCallback callback) { … }
void DataDecoder::ParseXml(
const std::string& xml,
mojom::XmlParser::WhitespaceBehavior whitespace_behavior,
ValueParseCallback callback) { … }
void DataDecoder::ParseXmlIsolated(
const std::string& xml,
mojom::XmlParser::WhitespaceBehavior whitespace_behavior,
ValueParseCallback callback) { … }
void DataDecoder::Deflate(base::span<const uint8_t> data,
GzipperCallback callback) { … }
void DataDecoder::Inflate(base::span<const uint8_t> data,
uint64_t max_uncompressed_size,
GzipperCallback callback) { … }
void DataDecoder::GzipCompress(base::span<const uint8_t> data,
GzipperCallback callback) { … }
void DataDecoder::GzipUncompress(base::span<const uint8_t> data,
GzipperCallback callback) { … }
void DataDecoder::ParseCbor(base::span<const uint8_t> data,
ValueParseCallback callback) { … }
void DataDecoder::ParseCborIsolated(base::span<const uint8_t> data,
ValueParseCallback callback) { … }
void DataDecoder::ValidatePixCode(const std::string& pix_code,
ValidationCallback callback) { … }
}