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