net_log_values.h 2.7 KB

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