1// Copyright 2020 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#include "src/inspector/v8-debugger-id.h"
6
7#include "src/debug/debug-interface.h"
8#include "src/inspector/string-util.h"
9#include "src/inspector/v8-inspector-impl.h"
10
11namespace v8_inspector {
12
13V8DebuggerId::V8DebuggerId(std::pair<int64_t, int64_t> pair)
14    : m_first(pair.first), m_second(pair.second) {}
15
16std::unique_ptr<StringBuffer> V8DebuggerId::toString() const {
17  return StringBufferFrom(String16::fromInteger64(m_first) + "." +
18                          String16::fromInteger64(m_second));
19}
20
21bool V8DebuggerId::isValid() const { return m_first || m_second; }
22
23std::pair<int64_t, int64_t> V8DebuggerId::pair() const {
24  return std::make_pair(m_first, m_second);
25}
26
27namespace internal {
28
29V8DebuggerId::V8DebuggerId(std::pair<int64_t, int64_t> pair)
30    : m_debugger_id(pair) {}
31
32// static
33V8DebuggerId V8DebuggerId::generate(V8InspectorImpl* inspector) {
34  return V8DebuggerId(std::make_pair(inspector->generateUniqueId(),
35                                     inspector->generateUniqueId()));
36}
37
38V8DebuggerId::V8DebuggerId(const String16& debuggerId) {
39  const UChar dot = '.';
40  size_t pos = debuggerId.find(dot);
41  if (pos == String16::kNotFound) return;
42  bool ok = false;
43  int64_t first = debuggerId.substring(0, pos).toInteger64(&ok);
44  if (!ok) return;
45  int64_t second = debuggerId.substring(pos + 1).toInteger64(&ok);
46  if (!ok) return;
47  m_debugger_id = v8_inspector::V8DebuggerId(std::make_pair(first, second));
48}
49
50String16 V8DebuggerId::toString() const {
51  return toString16(m_debugger_id.toString()->string());
52}
53
54bool V8DebuggerId::isValid() const { return m_debugger_id.isValid(); }
55
56std::pair<int64_t, int64_t> V8DebuggerId::pair() const {
57  return m_debugger_id.pair();
58}
59
60}  // namespace internal
61}  // namespace v8_inspector
62