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 "../../third_party/inspector_protocol/crdtp/json.h"
8 #include "src/inspector/protocol/Protocol.h"
9 #include "src/inspector/string-util.h"
10 
11 namespace v8_inspector {
12 
13 namespace {
14 
serializeId(uint64_t isolateId, int injectedScriptId, int id)15 String16 serializeId(uint64_t isolateId, int injectedScriptId, int id) {
16   return String16::concat(
17       String16::fromInteger64(static_cast<int64_t>(isolateId)), ".",
18       String16::fromInteger(injectedScriptId), ".", String16::fromInteger(id));
19 }
20 
21 }  // namespace
22 
RemoteObjectIdBase()23 RemoteObjectIdBase::RemoteObjectIdBase()
24     : m_isolateId(0), m_injectedScriptId(0), m_id(0) {}
25 
parseId(const String16& objectId)26 bool RemoteObjectIdBase::parseId(const String16& objectId) {
27   const UChar dot = '.';
28   size_t firstDotPos = objectId.find(dot);
29   if (firstDotPos == String16::kNotFound) return false;
30   bool ok = false;
31   int64_t isolateId = objectId.substring(0, firstDotPos).toInteger64(&ok);
32   if (!ok) return false;
33   firstDotPos++;
34   size_t secondDotPos = objectId.find(dot, firstDotPos);
35   if (secondDotPos == String16::kNotFound) return false;
36   int injectedScriptId =
37       objectId.substring(firstDotPos, secondDotPos - firstDotPos)
38           .toInteger(&ok);
39   if (!ok) return false;
40   secondDotPos++;
41   int id = objectId.substring(secondDotPos).toInteger(&ok);
42   if (!ok) return false;
43   m_isolateId = static_cast<uint64_t>(isolateId);
44   m_injectedScriptId = injectedScriptId;
45   m_id = id;
46   return true;
47 }
48 
parse(const String16& objectId, std::unique_ptr<RemoteObjectId>* result)49 Response RemoteObjectId::parse(const String16& objectId,
50                                std::unique_ptr<RemoteObjectId>* result) {
51   std::unique_ptr<RemoteObjectId> remoteObjectId(new RemoteObjectId());
52   if (!remoteObjectId->parseId(objectId))
53     return Response::ServerError("Invalid remote object id");
54   *result = std::move(remoteObjectId);
55   return Response::Success();
56 }
57 
serialize(uint64_t isolateId, int injectedScriptId, int id)58 String16 RemoteObjectId::serialize(uint64_t isolateId, int injectedScriptId,
59                                    int id) {
60   return serializeId(isolateId, injectedScriptId, id);
61 }
62 
parse(const String16& objectId, std::unique_ptr<RemoteCallFrameId>* result)63 Response RemoteCallFrameId::parse(const String16& objectId,
64                                   std::unique_ptr<RemoteCallFrameId>* result) {
65   std::unique_ptr<RemoteCallFrameId> remoteCallFrameId(new RemoteCallFrameId());
66   if (!remoteCallFrameId->parseId(objectId))
67     return Response::ServerError("Invalid call frame id");
68   *result = std::move(remoteCallFrameId);
69   return Response::Success();
70 }
71 
serialize(uint64_t isolateId, int injectedScriptId, int frameOrdinal)72 String16 RemoteCallFrameId::serialize(uint64_t isolateId, int injectedScriptId,
73                                       int frameOrdinal) {
74   return serializeId(isolateId, injectedScriptId, frameOrdinal);
75 }
76 
77 }  // namespace v8_inspector
78