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