summaryrefslogtreecommitdiff
path: root/deps/v8/src/inspector/remote-object-id.cc
blob: d83020c6f20208cc3115a0f57d931a53dad6e6b3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
// Copyright 2015 the V8 project authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "src/inspector/remote-object-id.h"

#include "src/inspector/protocol/Protocol.h"
#include "src/inspector/string-util.h"

namespace v8_inspector {

RemoteObjectIdBase::RemoteObjectIdBase() : m_injectedScriptId(0) {}

std::unique_ptr<protocol::DictionaryValue>
RemoteObjectIdBase::parseInjectedScriptId(const String16& objectId) {
  std::unique_ptr<protocol::Value> parsedValue = protocol::parseJSON(objectId);
  if (!parsedValue || parsedValue->type() != protocol::Value::TypeObject)
    return nullptr;

  std::unique_ptr<protocol::DictionaryValue> parsedObjectId(
      protocol::DictionaryValue::cast(parsedValue.release()));
  bool success =
      parsedObjectId->getInteger("injectedScriptId", &m_injectedScriptId);
  if (success) return parsedObjectId;
  return nullptr;
}

RemoteObjectId::RemoteObjectId() : RemoteObjectIdBase(), m_id(0) {}

std::unique_ptr<RemoteObjectId> RemoteObjectId::parse(
    ErrorString* errorString, const String16& objectId) {
  std::unique_ptr<RemoteObjectId> result(new RemoteObjectId());
  std::unique_ptr<protocol::DictionaryValue> parsedObjectId =
      result->parseInjectedScriptId(objectId);
  if (!parsedObjectId) {
    *errorString = "Invalid remote object id";
    return nullptr;
  }

  bool success = parsedObjectId->getInteger("id", &result->m_id);
  if (!success) {
    *errorString = "Invalid remote object id";
    return nullptr;
  }
  return result;
}

RemoteCallFrameId::RemoteCallFrameId()
    : RemoteObjectIdBase(), m_frameOrdinal(0) {}

std::unique_ptr<RemoteCallFrameId> RemoteCallFrameId::parse(
    ErrorString* errorString, const String16& objectId) {
  std::unique_ptr<RemoteCallFrameId> result(new RemoteCallFrameId());
  std::unique_ptr<protocol::DictionaryValue> parsedObjectId =
      result->parseInjectedScriptId(objectId);
  if (!parsedObjectId) {
    *errorString = "Invalid call frame id";
    return nullptr;
  }

  bool success = parsedObjectId->getInteger("ordinal", &result->m_frameOrdinal);
  if (!success) {
    *errorString = "Invalid call frame id";
    return nullptr;
  }

  return result;
}

String16 RemoteCallFrameId::serialize(int injectedScriptId, int frameOrdinal) {
  return "{\"ordinal\":" + String16::fromInteger(frameOrdinal) +
         ",\"injectedScriptId\":" + String16::fromInteger(injectedScriptId) +
         "}";
}

}  // namespace v8_inspector