11cb0ef41Sopenharmony_ci// Copyright 2016 the V8 project authors. All rights reserved.
21cb0ef41Sopenharmony_ci// Use of this source code is governed by a BSD-style license that can be
31cb0ef41Sopenharmony_ci// found in the LICENSE file.
41cb0ef41Sopenharmony_ci
51cb0ef41Sopenharmony_ci#ifndef V8_INSPECTOR_STRING_UTIL_H_
61cb0ef41Sopenharmony_ci#define V8_INSPECTOR_STRING_UTIL_H_
71cb0ef41Sopenharmony_ci
81cb0ef41Sopenharmony_ci#include <stdint.h>
91cb0ef41Sopenharmony_ci
101cb0ef41Sopenharmony_ci#include <memory>
111cb0ef41Sopenharmony_ci
121cb0ef41Sopenharmony_ci#include "../../third_party/inspector_protocol/crdtp/protocol_core.h"
131cb0ef41Sopenharmony_ci#include "include/v8-inspector.h"
141cb0ef41Sopenharmony_ci#include "src/base/logging.h"
151cb0ef41Sopenharmony_ci#include "src/base/macros.h"
161cb0ef41Sopenharmony_ci#include "src/inspector/string-16.h"
171cb0ef41Sopenharmony_ci
181cb0ef41Sopenharmony_cinamespace v8_inspector {
191cb0ef41Sopenharmony_ci
201cb0ef41Sopenharmony_cinamespace protocol {
211cb0ef41Sopenharmony_ci
221cb0ef41Sopenharmony_ciclass Value;
231cb0ef41Sopenharmony_ci
241cb0ef41Sopenharmony_ciusing String = v8_inspector::String16;
251cb0ef41Sopenharmony_ci
261cb0ef41Sopenharmony_ciclass StringUtil {
271cb0ef41Sopenharmony_ci public:
281cb0ef41Sopenharmony_ci  static String fromUTF8(const uint8_t* data, size_t length) {
291cb0ef41Sopenharmony_ci    return String16::fromUTF8(reinterpret_cast<const char*>(data), length);
301cb0ef41Sopenharmony_ci  }
311cb0ef41Sopenharmony_ci
321cb0ef41Sopenharmony_ci  static String fromUTF16LE(const uint16_t* data, size_t length) {
331cb0ef41Sopenharmony_ci    return String16::fromUTF16LE(data, length);
341cb0ef41Sopenharmony_ci  }
351cb0ef41Sopenharmony_ci
361cb0ef41Sopenharmony_ci  static const uint8_t* CharactersLatin1(const String& s) { return nullptr; }
371cb0ef41Sopenharmony_ci  static const uint8_t* CharactersUTF8(const String& s) { return nullptr; }
381cb0ef41Sopenharmony_ci  static const uint16_t* CharactersUTF16(const String& s) {
391cb0ef41Sopenharmony_ci    return s.characters16();
401cb0ef41Sopenharmony_ci  }
411cb0ef41Sopenharmony_ci  static size_t CharacterCount(const String& s) { return s.length(); }
421cb0ef41Sopenharmony_ci};
431cb0ef41Sopenharmony_ci
441cb0ef41Sopenharmony_ci// A read-only sequence of uninterpreted bytes with reference-counted storage.
451cb0ef41Sopenharmony_ciclass V8_EXPORT Binary {
461cb0ef41Sopenharmony_ci public:
471cb0ef41Sopenharmony_ci  Binary() = default;
481cb0ef41Sopenharmony_ci
491cb0ef41Sopenharmony_ci  const uint8_t* data() const { return bytes_->data(); }
501cb0ef41Sopenharmony_ci  size_t size() const { return bytes_->size(); }
511cb0ef41Sopenharmony_ci  String toBase64() const;
521cb0ef41Sopenharmony_ci  static Binary fromBase64(const String& base64, bool* success);
531cb0ef41Sopenharmony_ci  static Binary fromSpan(const uint8_t* data, size_t size) {
541cb0ef41Sopenharmony_ci    return Binary(std::make_shared<std::vector<uint8_t>>(data, data + size));
551cb0ef41Sopenharmony_ci  }
561cb0ef41Sopenharmony_ci
571cb0ef41Sopenharmony_ci private:
581cb0ef41Sopenharmony_ci  std::shared_ptr<std::vector<uint8_t>> bytes_;
591cb0ef41Sopenharmony_ci
601cb0ef41Sopenharmony_ci  explicit Binary(std::shared_ptr<std::vector<uint8_t>> bytes)
611cb0ef41Sopenharmony_ci      : bytes_(bytes) {}
621cb0ef41Sopenharmony_ci};
631cb0ef41Sopenharmony_ci}  // namespace protocol
641cb0ef41Sopenharmony_ci
651cb0ef41Sopenharmony_civ8::Local<v8::String> toV8String(v8::Isolate*, const String16&);
661cb0ef41Sopenharmony_civ8::Local<v8::String> toV8StringInternalized(v8::Isolate*, const String16&);
671cb0ef41Sopenharmony_civ8::Local<v8::String> toV8StringInternalized(v8::Isolate*, const char*);
681cb0ef41Sopenharmony_civ8::Local<v8::String> toV8String(v8::Isolate*, const StringView&);
691cb0ef41Sopenharmony_ci// TODO(dgozman): rename to toString16.
701cb0ef41Sopenharmony_ciString16 toProtocolString(v8::Isolate*, v8::Local<v8::String>);
711cb0ef41Sopenharmony_ciString16 toProtocolStringWithTypeCheck(v8::Isolate*, v8::Local<v8::Value>);
721cb0ef41Sopenharmony_ciString16 toString16(const StringView&);
731cb0ef41Sopenharmony_ciStringView toStringView(const String16&);
741cb0ef41Sopenharmony_citemplate <size_t N>
751cb0ef41Sopenharmony_ciStringView toStringView(const char* str[N]) {
761cb0ef41Sopenharmony_ci  return StringView(reinterpret_cast<const uint8_t*>(str), N);
771cb0ef41Sopenharmony_ci}
781cb0ef41Sopenharmony_cibool stringViewStartsWith(const StringView&, const char*);
791cb0ef41Sopenharmony_ci
801cb0ef41Sopenharmony_ci// Creates a string buffer instance which owns |str|, a 16 bit string.
811cb0ef41Sopenharmony_cistd::unique_ptr<StringBuffer> StringBufferFrom(String16 str);
821cb0ef41Sopenharmony_ci
831cb0ef41Sopenharmony_ci// Creates a string buffer instance which owns |str|, an 8 bit string.
841cb0ef41Sopenharmony_ci// 8 bit strings are used for LATIN1 text (which subsumes 7 bit ASCII, e.g.
851cb0ef41Sopenharmony_ci// our generated JSON), as well as for CBOR encoded binary messages.
861cb0ef41Sopenharmony_cistd::unique_ptr<StringBuffer> StringBufferFrom(std::vector<uint8_t> str);
871cb0ef41Sopenharmony_ci
881cb0ef41Sopenharmony_ciString16 stackTraceIdToString(uintptr_t id);
891cb0ef41Sopenharmony_ci
901cb0ef41Sopenharmony_ci}  // namespace v8_inspector
911cb0ef41Sopenharmony_ci
921cb0ef41Sopenharmony_ci// See third_party/inspector_protocol/crdtp/serializer_traits.h.
931cb0ef41Sopenharmony_cinamespace v8_crdtp {
941cb0ef41Sopenharmony_ci
951cb0ef41Sopenharmony_citemplate <>
961cb0ef41Sopenharmony_cistruct ProtocolTypeTraits<v8_inspector::String16> {
971cb0ef41Sopenharmony_ci  static bool Deserialize(DeserializerState* state,
981cb0ef41Sopenharmony_ci                          v8_inspector::String16* value);
991cb0ef41Sopenharmony_ci  static void Serialize(const v8_inspector::String16& value,
1001cb0ef41Sopenharmony_ci                        std::vector<uint8_t>* bytes);
1011cb0ef41Sopenharmony_ci};
1021cb0ef41Sopenharmony_ci
1031cb0ef41Sopenharmony_citemplate <>
1041cb0ef41Sopenharmony_cistruct ProtocolTypeTraits<v8_inspector::protocol::Binary> {
1051cb0ef41Sopenharmony_ci  static bool Deserialize(DeserializerState* state,
1061cb0ef41Sopenharmony_ci                          v8_inspector::protocol::Binary* value);
1071cb0ef41Sopenharmony_ci  static void Serialize(const v8_inspector::protocol::Binary& value,
1081cb0ef41Sopenharmony_ci                        std::vector<uint8_t>* bytes);
1091cb0ef41Sopenharmony_ci};
1101cb0ef41Sopenharmony_ci
1111cb0ef41Sopenharmony_cinamespace detail {
1121cb0ef41Sopenharmony_citemplate <>
1131cb0ef41Sopenharmony_cistruct MaybeTypedef<v8_inspector::String16> {
1141cb0ef41Sopenharmony_ci  typedef ValueMaybe<v8_inspector::String16> type;
1151cb0ef41Sopenharmony_ci};
1161cb0ef41Sopenharmony_ci
1171cb0ef41Sopenharmony_citemplate <>
1181cb0ef41Sopenharmony_cistruct MaybeTypedef<v8_inspector::protocol::Binary> {
1191cb0ef41Sopenharmony_ci  typedef ValueMaybe<v8_inspector::protocol::Binary> type;
1201cb0ef41Sopenharmony_ci};
1211cb0ef41Sopenharmony_ci
1221cb0ef41Sopenharmony_ci}  // namespace detail
1231cb0ef41Sopenharmony_ci
1241cb0ef41Sopenharmony_ci}  // namespace v8_crdtp
1251cb0ef41Sopenharmony_ci
1261cb0ef41Sopenharmony_ci#endif  // V8_INSPECTOR_STRING_UTIL_H_
127