1 // Copyright 2015 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/remote-object-id.h"
6 
7 #include "src/inspector/protocol/Protocol.h"
8 #include "src/inspector/string-util.h"
9 
10 namespace v8_inspector {
11 
RemoteObjectIdBase()12 RemoteObjectIdBase::RemoteObjectIdBase() : m_injectedScriptId(0) {}
13 
14 std::unique_ptr<protocol::DictionaryValue>
parseInjectedScriptId(const String16 & objectId)15 RemoteObjectIdBase::parseInjectedScriptId(const String16& objectId) {
16   std::unique_ptr<protocol::Value> parsedValue = protocol::parseJSON(objectId);
17   if (!parsedValue || parsedValue->type() != protocol::Value::TypeObject)
18     return nullptr;
19 
20   std::unique_ptr<protocol::DictionaryValue> parsedObjectId(
21       protocol::DictionaryValue::cast(parsedValue.release()));
22   bool success =
23       parsedObjectId->getInteger("injectedScriptId", &m_injectedScriptId);
24   if (success) return parsedObjectId;
25   return nullptr;
26 }
27 
RemoteObjectId()28 RemoteObjectId::RemoteObjectId() : RemoteObjectIdBase(), m_id(0) {}
29 
parse(const String16 & objectId,std::unique_ptr<RemoteObjectId> * result)30 Response RemoteObjectId::parse(const String16& objectId,
31                                std::unique_ptr<RemoteObjectId>* result) {
32   std::unique_ptr<RemoteObjectId> remoteObjectId(new RemoteObjectId());
33   std::unique_ptr<protocol::DictionaryValue> parsedObjectId =
34       remoteObjectId->parseInjectedScriptId(objectId);
35   if (!parsedObjectId) return Response::Error("Invalid remote object id");
36 
37   bool success = parsedObjectId->getInteger("id", &remoteObjectId->m_id);
38   if (!success) return Response::Error("Invalid remote object id");
39   *result = std::move(remoteObjectId);
40   return Response::OK();
41 }
42 
RemoteCallFrameId()43 RemoteCallFrameId::RemoteCallFrameId()
44     : RemoteObjectIdBase(), m_frameOrdinal(0) {}
45 
parse(const String16 & objectId,std::unique_ptr<RemoteCallFrameId> * result)46 Response RemoteCallFrameId::parse(const String16& objectId,
47                                   std::unique_ptr<RemoteCallFrameId>* result) {
48   std::unique_ptr<RemoteCallFrameId> remoteCallFrameId(new RemoteCallFrameId());
49   std::unique_ptr<protocol::DictionaryValue> parsedObjectId =
50       remoteCallFrameId->parseInjectedScriptId(objectId);
51   if (!parsedObjectId) return Response::Error("Invalid call frame id");
52 
53   bool success =
54       parsedObjectId->getInteger("ordinal", &remoteCallFrameId->m_frameOrdinal);
55   if (!success) return Response::Error("Invalid call frame id");
56   *result = std::move(remoteCallFrameId);
57   return Response::OK();
58 }
59 
serialize(int injectedScriptId,int frameOrdinal)60 String16 RemoteCallFrameId::serialize(int injectedScriptId, int frameOrdinal) {
61   return "{\"ordinal\":" + String16::fromInteger(frameOrdinal) +
62          ",\"injectedScriptId\":" + String16::fromInteger(injectedScriptId) +
63          "}";
64 }
65 
66 }  // namespace v8_inspector
67