Eric Roman | 06bd974 | 2019-07-13 15:19:13 | [diff] [blame] | 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 | |
| 5 | #ifndef NET_LOG_NET_LOG_VALUES_H_ |
| 6 | #define NET_LOG_NET_LOG_VALUES_H_ |
| 7 | |
| 8 | #include <stdint.h> |
| 9 | |
Eric Roman | 06bd974 | 2019-07-13 15:19:13 | [diff] [blame] | 10 | #include "base/strings/string_piece_forward.h" |
| 11 | #include "net/base/net_export.h" |
| 12 | |
| 13 | namespace base { |
| 14 | class Value; |
| 15 | } |
| 16 | |
| 17 | namespace net { |
| 18 | |
Eric Roman | 45f155c | 2019-07-15 19:47:31 | [diff] [blame] | 19 | // Helpers to construct dictionaries with a single key and value. Useful for |
| 20 | // building parameters to include in a NetLog. |
Eric Roman | 06bd974 | 2019-07-13 15:19:13 | [diff] [blame] | 21 | NET_EXPORT base::Value NetLogParamsWithInt(base::StringPiece name, int value); |
| 22 | NET_EXPORT base::Value NetLogParamsWithInt64(base::StringPiece name, |
| 23 | int64_t value); |
| 24 | NET_EXPORT base::Value NetLogParamsWithBool(base::StringPiece name, bool value); |
| 25 | NET_EXPORT base::Value NetLogParamsWithString(base::StringPiece name, |
| 26 | base::StringPiece value); |
| 27 | |
Eric Roman | 45f155c | 2019-07-15 19:47:31 | [diff] [blame] | 28 | // 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. |
| 38 | NET_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. |
| 49 | NET_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. |
| 58 | NET_EXPORT base::Value NetLogNumberValue(int64_t num); |
| 59 | NET_EXPORT base::Value NetLogNumberValue(uint64_t num); |
| 60 | |
Eric Roman | 06bd974 | 2019-07-13 15:19:13 | [diff] [blame] | 61 | } // namespace net |
| 62 | |
| 63 | #endif // NET_LOG_NET_LOG_VALUES_H_ |