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 5 #ifndef V8_INSPECTOR_STRING_UTIL_H_ 6 #define V8_INSPECTOR_STRING_UTIL_H_ 7 8 #include <stdint.h> 9 10 #include <memory> 11 12 #include "../../third_party/inspector_protocol/crdtp/protocol_core.h" 13 #include "include/v8-inspector.h" 14 #include "src/base/logging.h" 15 #include "src/base/macros.h" 16 #include "src/inspector/string-16.h" 17 18 namespace v8_inspector { 19 20 namespace protocol { 21 22 class Value; 23 24 using String = v8_inspector::String16; 25 26 class StringUtil { 27 public: fromUTF8(const uint8_t * data,size_t length)28 static String fromUTF8(const uint8_t* data, size_t length) { 29 return String16::fromUTF8(reinterpret_cast<const char*>(data), length); 30 } 31 fromUTF16LE(const uint16_t * data,size_t length)32 static String fromUTF16LE(const uint16_t* data, size_t length) { 33 return String16::fromUTF16LE(data, length); 34 } 35 CharactersLatin1(const String & s)36 static const uint8_t* CharactersLatin1(const String& s) { return nullptr; } CharactersUTF8(const String & s)37 static const uint8_t* CharactersUTF8(const String& s) { return nullptr; } CharactersUTF16(const String & s)38 static const uint16_t* CharactersUTF16(const String& s) { 39 return s.characters16(); 40 } CharacterCount(const String & s)41 static size_t CharacterCount(const String& s) { return s.length(); } 42 }; 43 44 // A read-only sequence of uninterpreted bytes with reference-counted storage. 45 class V8_EXPORT Binary { 46 public: 47 Binary() = default; 48 data()49 const uint8_t* data() const { return bytes_->data(); } size()50 size_t size() const { return bytes_->size(); } 51 String toBase64() const; 52 static Binary fromBase64(const String& base64, bool* success); fromSpan(const uint8_t * data,size_t size)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 Binary(std::shared_ptr<std::vector<uint8_t>> bytes)60 explicit Binary(std::shared_ptr<std::vector<uint8_t>> bytes) 61 : bytes_(bytes) {} 62 }; 63 } // namespace protocol 64 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. 70 String16 toProtocolString(v8::Isolate*, v8::Local<v8::String>); 71 String16 toProtocolStringWithTypeCheck(v8::Isolate*, v8::Local<v8::Value>); 72 String16 toString16(const StringView&); 73 StringView toStringView(const String16&); 74 bool stringViewStartsWith(const StringView&, const char*); 75 76 // Creates a string buffer instance which owns |str|, a 16 bit string. 77 std::unique_ptr<StringBuffer> StringBufferFrom(String16 str); 78 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); 83 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); 97 }; 98 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 }; 106 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_ 129