net_log_values.cc 3.4 KB

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