xref: /aosp_15_r20/external/cronet/net/log/net_log_values.h (revision 6777b5387eb2ff775bb5750e3f5d96f37fb7352b)
1 // Copyright 2019 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 #ifndef NET_LOG_NET_LOG_VALUES_H_
6 #define NET_LOG_NET_LOG_VALUES_H_
7 
8 #include <stddef.h>
9 #include <stdint.h>
10 
11 #include <string_view>
12 
13 #include "base/containers/span.h"
14 #include "base/values.h"
15 #include "net/base/net_export.h"
16 
17 namespace net {
18 
19 // Helpers to construct dictionaries with a single key and value. Useful for
20 // building parameters to include in a NetLog.
21 NET_EXPORT base::Value::Dict NetLogParamsWithInt(std::string_view name,
22                                                  int value);
23 NET_EXPORT base::Value::Dict NetLogParamsWithInt64(std::string_view name,
24                                                    int64_t value);
25 NET_EXPORT base::Value::Dict NetLogParamsWithBool(std::string_view name,
26                                                   bool value);
27 NET_EXPORT base::Value::Dict NetLogParamsWithString(std::string_view name,
28                                                     std::string_view value);
29 
30 // Creates a base::Value() to represent the byte string |raw| when adding it to
31 // the NetLog.
32 //
33 // When |raw| is an ASCII string, the returned value is a base::Value()
34 // containing that exact string. Otherwise it is represented by a
35 // percent-escaped version of the original string, along with a special prefix.
36 //
37 // This wrapper exists because base::Value strings are required to be UTF-8.
38 // Often times NetLog consumers just want to log a std::string, and that string
39 // may not be UTF-8.
40 NET_EXPORT base::Value NetLogStringValue(std::string_view raw);
41 
42 // Creates a base::Value() to represent the octets |bytes|. This should be
43 // used when adding binary data (i.e. not an ASCII or UTF-8 string) to the
44 // NetLog. The resulting base::Value() holds a copy of the input data.
45 //
46 // This wrapper must be used rather than directly adding base::Value parameters
47 // of type BINARY to the NetLog, since the JSON writer does not support
48 // serializing them.
49 //
50 // This wrapper encodes |bytes| as a Base64 encoded string.
51 NET_EXPORT base::Value NetLogBinaryValue(base::span<const uint8_t> bytes);
52 NET_EXPORT base::Value NetLogBinaryValue(const void* bytes, size_t length);
53 
54 // Creates a base::Value() to represent integers, including 64-bit ones.
55 // base::Value() does not directly support 64-bit integers, as it is not
56 // representable in JSON.
57 //
58 // These wrappers will return values that are either numbers, or a string
59 // representation of their decimal value, depending on what is needed to ensure
60 // no loss of precision when de-serializing from JavaScript.
61 NET_EXPORT base::Value NetLogNumberValue(int64_t num);
62 NET_EXPORT base::Value NetLogNumberValue(uint64_t num);
63 NET_EXPORT base::Value NetLogNumberValue(uint32_t num);
64 
65 }  // namespace net
66 
67 #endif  // NET_LOG_NET_LOG_VALUES_H_
68