remote-object-id.cc 2.59 KB
Newer Older
1 2 3 4
// 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.

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

#include "src/inspector/protocol/Protocol.h"
8
#include "src/inspector/string-util.h"
9 10 11 12 13 14 15

namespace v8_inspector {

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

std::unique_ptr<protocol::DictionaryValue>
RemoteObjectIdBase::parseInjectedScriptId(const String16& objectId) {
16 17
  std::unique_ptr<protocol::Value> parsedValue =
      protocol::StringUtil::parseJSON(objectId);
18 19 20 21 22 23 24 25 26 27 28 29 30
  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) {}

31 32 33
Response RemoteObjectId::parse(const String16& objectId,
                               std::unique_ptr<RemoteObjectId>* result) {
  std::unique_ptr<RemoteObjectId> remoteObjectId(new RemoteObjectId());
34
  std::unique_ptr<protocol::DictionaryValue> parsedObjectId =
35 36
      remoteObjectId->parseInjectedScriptId(objectId);
  if (!parsedObjectId) return Response::Error("Invalid remote object id");
37

38 39 40 41
  bool success = parsedObjectId->getInteger("id", &remoteObjectId->m_id);
  if (!success) return Response::Error("Invalid remote object id");
  *result = std::move(remoteObjectId);
  return Response::OK();
42 43 44 45 46
}

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

47 48 49
Response RemoteCallFrameId::parse(const String16& objectId,
                                  std::unique_ptr<RemoteCallFrameId>* result) {
  std::unique_ptr<RemoteCallFrameId> remoteCallFrameId(new RemoteCallFrameId());
50
  std::unique_ptr<protocol::DictionaryValue> parsedObjectId =
51 52
      remoteCallFrameId->parseInjectedScriptId(objectId);
  if (!parsedObjectId) return Response::Error("Invalid call frame id");
53

54 55 56 57 58
  bool success =
      parsedObjectId->getInteger("ordinal", &remoteCallFrameId->m_frameOrdinal);
  if (!success) return Response::Error("Invalid call frame id");
  *result = std::move(remoteCallFrameId);
  return Response::OK();
59 60 61 62 63 64 65 66 67
}

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

}  // namespace v8_inspector