| Ben Murdoch | f3b273f | 2017-01-17 12:11:28 +0000 | [diff] [blame] | 1 | // Copyright 2016 the V8 project 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 |  | 
| Rubin Xu | 7bc1b61 | 2021-02-16 09:38:50 +0000 | [diff] [blame^] | 5 | #ifndef V8_INSPECTOR_STRING_UTIL_H_ | 
 | 6 | #define V8_INSPECTOR_STRING_UTIL_H_ | 
 | 7 |  | 
 | 8 | #include <stdint.h> | 
| Ben Murdoch | f3b273f | 2017-01-17 12:11:28 +0000 | [diff] [blame] | 9 |  | 
| Ben Murdoch | 62ed631 | 2017-06-06 11:06:27 +0100 | [diff] [blame] | 10 | #include <memory> | 
 | 11 |  | 
| Rubin Xu | 7bc1b61 | 2021-02-16 09:38:50 +0000 | [diff] [blame^] | 12 | #include "../../third_party/inspector_protocol/crdtp/protocol_core.h" | 
 | 13 | #include "include/v8-inspector.h" | 
| Ben Murdoch | 62ed631 | 2017-06-06 11:06:27 +0100 | [diff] [blame] | 14 | #include "src/base/logging.h" | 
| Ben Murdoch | f3b273f | 2017-01-17 12:11:28 +0000 | [diff] [blame] | 15 | #include "src/base/macros.h" | 
 | 16 | #include "src/inspector/string-16.h" | 
 | 17 |  | 
| Ben Murdoch | f3b273f | 2017-01-17 12:11:28 +0000 | [diff] [blame] | 18 | namespace v8_inspector { | 
 | 19 |  | 
 | 20 | namespace protocol { | 
 | 21 |  | 
 | 22 | class Value; | 
 | 23 |  | 
 | 24 | using String = v8_inspector::String16; | 
| Ben Murdoch | f3b273f | 2017-01-17 12:11:28 +0000 | [diff] [blame] | 25 |  | 
 | 26 | class StringUtil { | 
 | 27 |  public: | 
| Rubin Xu | 7bc1b61 | 2021-02-16 09:38:50 +0000 | [diff] [blame^] | 28 |   static String fromUTF8(const uint8_t* data, size_t length) { | 
 | 29 |     return String16::fromUTF8(reinterpret_cast<const char*>(data), length); | 
| Ben Murdoch | f3b273f | 2017-01-17 12:11:28 +0000 | [diff] [blame] | 30 |   } | 
| Rubin Xu | 7bc1b61 | 2021-02-16 09:38:50 +0000 | [diff] [blame^] | 31 |  | 
 | 32 |   static String fromUTF16LE(const uint16_t* data, size_t length) { | 
 | 33 |     return String16::fromUTF16LE(data, length); | 
| Ben Murdoch | f3b273f | 2017-01-17 12:11:28 +0000 | [diff] [blame] | 34 |   } | 
| Rubin Xu | 7bc1b61 | 2021-02-16 09:38:50 +0000 | [diff] [blame^] | 35 |  | 
 | 36 |   static const uint8_t* CharactersLatin1(const String& s) { return nullptr; } | 
 | 37 |   static const uint8_t* CharactersUTF8(const String& s) { return nullptr; } | 
 | 38 |   static const uint16_t* CharactersUTF16(const String& s) { | 
 | 39 |     return s.characters16(); | 
| Ben Murdoch | 62ed631 | 2017-06-06 11:06:27 +0100 | [diff] [blame] | 40 |   } | 
| Rubin Xu | 7bc1b61 | 2021-02-16 09:38:50 +0000 | [diff] [blame^] | 41 |   static size_t CharacterCount(const String& s) { return s.length(); } | 
| Ben Murdoch | f3b273f | 2017-01-17 12:11:28 +0000 | [diff] [blame] | 42 | }; | 
 | 43 |  | 
| Rubin Xu | 7bc1b61 | 2021-02-16 09:38:50 +0000 | [diff] [blame^] | 44 | // A read-only sequence of uninterpreted bytes with reference-counted storage. | 
 | 45 | class V8_EXPORT Binary { | 
 | 46 |  public: | 
 | 47 |   Binary() = default; | 
 | 48 |  | 
 | 49 |   const uint8_t* data() const { return bytes_->data(); } | 
 | 50 |   size_t size() const { return bytes_->size(); } | 
 | 51 |   String toBase64() const; | 
 | 52 |   static Binary fromBase64(const String& base64, bool* success); | 
 | 53 |   static Binary fromSpan(const uint8_t* data, size_t size) { | 
 | 54 |     return Binary(std::make_shared<std::vector<uint8_t>>(data, data + size)); | 
 | 55 |   } | 
 | 56 |  | 
 | 57 |  private: | 
 | 58 |   std::shared_ptr<std::vector<uint8_t>> bytes_; | 
 | 59 |  | 
 | 60 |   explicit Binary(std::shared_ptr<std::vector<uint8_t>> bytes) | 
 | 61 |       : bytes_(bytes) {} | 
 | 62 | }; | 
| Ben Murdoch | f3b273f | 2017-01-17 12:11:28 +0000 | [diff] [blame] | 63 | }  // namespace protocol | 
 | 64 |  | 
| Ben Murdoch | f3b273f | 2017-01-17 12:11:28 +0000 | [diff] [blame] | 65 | v8::Local<v8::String> toV8String(v8::Isolate*, const String16&); | 
 | 66 | v8::Local<v8::String> toV8StringInternalized(v8::Isolate*, const String16&); | 
 | 67 | v8::Local<v8::String> toV8StringInternalized(v8::Isolate*, const char*); | 
 | 68 | v8::Local<v8::String> toV8String(v8::Isolate*, const StringView&); | 
 | 69 | // TODO(dgozman): rename to toString16. | 
| Rubin Xu | 7bc1b61 | 2021-02-16 09:38:50 +0000 | [diff] [blame^] | 70 | String16 toProtocolString(v8::Isolate*, v8::Local<v8::String>); | 
 | 71 | String16 toProtocolStringWithTypeCheck(v8::Isolate*, v8::Local<v8::Value>); | 
| Ben Murdoch | f3b273f | 2017-01-17 12:11:28 +0000 | [diff] [blame] | 72 | String16 toString16(const StringView&); | 
 | 73 | StringView toStringView(const String16&); | 
 | 74 | bool stringViewStartsWith(const StringView&, const char*); | 
 | 75 |  | 
| Rubin Xu | 7bc1b61 | 2021-02-16 09:38:50 +0000 | [diff] [blame^] | 76 | // Creates a string buffer instance which owns |str|, a 16 bit string. | 
 | 77 | std::unique_ptr<StringBuffer> StringBufferFrom(String16 str); | 
| Ben Murdoch | f3b273f | 2017-01-17 12:11:28 +0000 | [diff] [blame] | 78 |  | 
| Rubin Xu | 7bc1b61 | 2021-02-16 09:38:50 +0000 | [diff] [blame^] | 79 | // Creates a string buffer instance which owns |str|, an 8 bit string. | 
 | 80 | // 8 bit strings are used for LATIN1 text (which subsumes 7 bit ASCII, e.g. | 
 | 81 | // our generated JSON), as well as for CBOR encoded binary messages. | 
 | 82 | std::unique_ptr<StringBuffer> StringBufferFrom(std::vector<uint8_t> str); | 
| Ben Murdoch | f3b273f | 2017-01-17 12:11:28 +0000 | [diff] [blame] | 83 |  | 
| Rubin Xu | 7bc1b61 | 2021-02-16 09:38:50 +0000 | [diff] [blame^] | 84 | String16 stackTraceIdToString(uintptr_t id); | 
 | 85 |  | 
 | 86 | }  // namespace v8_inspector | 
 | 87 |  | 
 | 88 | // See third_party/inspector_protocol/crdtp/serializer_traits.h. | 
 | 89 | namespace v8_crdtp { | 
 | 90 |  | 
 | 91 | template <> | 
 | 92 | struct ProtocolTypeTraits<v8_inspector::String16> { | 
 | 93 |   static bool Deserialize(DeserializerState* state, | 
 | 94 |                           v8_inspector::String16* value); | 
 | 95 |   static void Serialize(const v8_inspector::String16& value, | 
 | 96 |                         std::vector<uint8_t>* bytes); | 
| Ben Murdoch | f3b273f | 2017-01-17 12:11:28 +0000 | [diff] [blame] | 97 | }; | 
 | 98 |  | 
| Rubin Xu | 7bc1b61 | 2021-02-16 09:38:50 +0000 | [diff] [blame^] | 99 | template <> | 
 | 100 | struct ProtocolTypeTraits<v8_inspector::protocol::Binary> { | 
 | 101 |   static bool Deserialize(DeserializerState* state, | 
 | 102 |                           v8_inspector::protocol::Binary* value); | 
 | 103 |   static void Serialize(const v8_inspector::protocol::Binary& value, | 
 | 104 |                         std::vector<uint8_t>* bytes); | 
 | 105 | }; | 
| Ben Murdoch | f3b273f | 2017-01-17 12:11:28 +0000 | [diff] [blame] | 106 |  | 
| Rubin Xu | 7bc1b61 | 2021-02-16 09:38:50 +0000 | [diff] [blame^] | 107 | template <> | 
 | 108 | struct SerializerTraits<v8_inspector::protocol::Binary> { | 
 | 109 |   static void Serialize(const v8_inspector::protocol::Binary& binary, | 
 | 110 |                         std::vector<uint8_t>* out); | 
 | 111 | }; | 
 | 112 |  | 
 | 113 | namespace detail { | 
 | 114 | template <> | 
 | 115 | struct MaybeTypedef<v8_inspector::String16> { | 
 | 116 |   typedef ValueMaybe<v8_inspector::String16> type; | 
 | 117 | }; | 
 | 118 |  | 
 | 119 | template <> | 
 | 120 | struct MaybeTypedef<v8_inspector::protocol::Binary> { | 
 | 121 |   typedef ValueMaybe<v8_inspector::protocol::Binary> type; | 
 | 122 | }; | 
 | 123 |  | 
 | 124 | }  // namespace detail | 
 | 125 |  | 
 | 126 | }  // namespace v8_crdtp | 
 | 127 |  | 
 | 128 | #endif  // V8_INSPECTOR_STRING_UTIL_H_ |