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