xref: /aosp_15_r20/external/cronet/net/log/net_log_values.cc (revision 6777b5387eb2ff775bb5750e3f5d96f37fb7352b)
1 // Copyright 2012 The Chromium Authors
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
5 #include "net/log/net_log_values.h"
6 
7 #include "base/base64.h"
8 #include "base/strings/escape.h"
9 #include "base/strings/string_number_conversions.h"
10 #include "base/strings/string_util.h"
11 #include "base/values.h"
12 
13 namespace net {
14 
15 namespace {
16 
17 // IEEE 64-bit doubles have a 52-bit mantissa, and can therefore represent
18 // 53-bits worth of precision (see also documentation for JavaScript's
19 // Number.MAX_SAFE_INTEGER for more discussion on this).
20 //
21 // If the number can be represented with an int or double use that. Otherwise
22 // fallback to encoding it as a string.
23 template <typename T>
NetLogNumberValueHelper(T num)24 base::Value NetLogNumberValueHelper(T num) {
25   // Fits in a (32-bit) int: [-2^31, 2^31 - 1]
26   if ((!std::is_signed<T>::value || (num >= static_cast<T>(-2147483648))) &&
27       (num <= static_cast<T>(2147483647))) {
28     return base::Value(static_cast<int>(num));
29   }
30 
31   // Fits in a double: (-2^53, 2^53)
32   if ((!std::is_signed<T>::value ||
33        (num >= static_cast<T>(-9007199254740991))) &&
34       (num <= static_cast<T>(9007199254740991))) {
35     return base::Value(static_cast<double>(num));
36   }
37 
38   // Otherwise format as a string.
39   return base::Value(base::NumberToString(num));
40 }
41 
42 }  // namespace
43 
NetLogStringValue(std::string_view raw)44 base::Value NetLogStringValue(std::string_view raw) {
45   // The common case is that |raw| is ASCII. Represent this directly.
46   if (base::IsStringASCII(raw))
47     return base::Value(raw);
48 
49   // For everything else (including valid UTF-8) percent-escape |raw|, and add a
50   // prefix that "tags" the value as being a percent-escaped representation.
51   //
52   // Note that the sequence E2 80 8B is U+200B (zero-width space) in UTF-8. It
53   // is added so the escaped string is not itself also ASCII (otherwise there
54   // would be ambiguity for consumers as to when the value needs to be
55   // unescaped).
56   return base::Value("%ESCAPED:\xE2\x80\x8B " +
57                      base::EscapeNonASCIIAndPercent(raw));
58 }
59 
NetLogBinaryValue(base::span<const uint8_t> bytes)60 base::Value NetLogBinaryValue(base::span<const uint8_t> bytes) {
61   return NetLogBinaryValue(bytes.data(), bytes.size());
62 }
63 
NetLogBinaryValue(const void * bytes,size_t length)64 base::Value NetLogBinaryValue(const void* bytes, size_t length) {
65   std::string b64 = base::Base64Encode(
66       std::string_view(reinterpret_cast<const char*>(bytes), length));
67   return base::Value(std::move(b64));
68 }
69 
NetLogNumberValue(int64_t num)70 base::Value NetLogNumberValue(int64_t num) {
71   return NetLogNumberValueHelper(num);
72 }
73 
NetLogNumberValue(uint64_t num)74 base::Value NetLogNumberValue(uint64_t num) {
75   return NetLogNumberValueHelper(num);
76 }
77 
NetLogNumberValue(uint32_t num)78 base::Value NetLogNumberValue(uint32_t num) {
79   return NetLogNumberValueHelper(num);
80 }
81 
NetLogParamsWithInt(std::string_view name,int value)82 base::Value::Dict NetLogParamsWithInt(std::string_view name, int value) {
83   base::Value::Dict params;
84   params.Set(name, value);
85   return params;
86 }
87 
NetLogParamsWithInt64(std::string_view name,int64_t value)88 base::Value::Dict NetLogParamsWithInt64(std::string_view name, int64_t value) {
89   base::Value::Dict params;
90   params.Set(name, NetLogNumberValue(value));
91   return params;
92 }
93 
NetLogParamsWithBool(std::string_view name,bool value)94 base::Value::Dict NetLogParamsWithBool(std::string_view name, bool value) {
95   base::Value::Dict params;
96   params.Set(name, value);
97   return params;
98 }
99 
NetLogParamsWithString(std::string_view name,std::string_view value)100 base::Value::Dict NetLogParamsWithString(std::string_view name,
101                                          std::string_view value) {
102   base::Value::Dict params;
103   params.Set(name, value);
104   return params;
105 }
106 
107 }  // namespace net
108