2020-06-13 02:54:08 +02:00
|
|
|
// Copyright (c) 2020 The Chromium Embedded Framework 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 "libcef/browser/devtools/devtools_util.h"
|
|
|
|
|
|
|
|
#include "base/strings/string_number_conversions.h"
|
2020-08-29 00:39:23 +02:00
|
|
|
#include "base/strings/string_util.h"
|
2020-06-13 02:54:08 +02:00
|
|
|
|
|
|
|
namespace devtools_util {
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
|
|
|
bool IsValidDictionary(const base::StringPiece& str, bool allow_empty) {
|
|
|
|
return str.length() >= (allow_empty ? 2 : 3) && str[0] == '{' &&
|
|
|
|
str[str.length() - 1] == '}';
|
|
|
|
}
|
|
|
|
|
|
|
|
// Example:
|
|
|
|
// {"method":"Target.targetDestroyed","params":{"targetId":"1234..."}}
|
|
|
|
bool ParseEvent(const base::StringPiece& message,
|
|
|
|
base::StringPiece& method,
|
|
|
|
base::StringPiece& params) {
|
|
|
|
static const char kMethodStart[] = "{\"method\":\"";
|
|
|
|
static const char kMethodEnd[] = "\"";
|
|
|
|
static const char kParamsStart[] = ",\"params\":";
|
|
|
|
|
2023-01-02 23:59:03 +01:00
|
|
|
if (!base::StartsWith(message, kMethodStart)) {
|
2020-06-13 02:54:08 +02:00
|
|
|
return false;
|
2023-01-02 23:59:03 +01:00
|
|
|
}
|
2020-06-13 02:54:08 +02:00
|
|
|
|
|
|
|
const size_t method_start = sizeof(kMethodStart) - 1;
|
|
|
|
const size_t method_end = message.find(kMethodEnd, method_start);
|
2023-01-02 23:59:03 +01:00
|
|
|
if (method_end == base::StringPiece::npos) {
|
2020-06-13 02:54:08 +02:00
|
|
|
return false;
|
2023-01-02 23:59:03 +01:00
|
|
|
}
|
2020-06-13 02:54:08 +02:00
|
|
|
method = message.substr(method_start, method_end - method_start);
|
2023-01-02 23:59:03 +01:00
|
|
|
if (method.empty()) {
|
2020-06-13 02:54:08 +02:00
|
|
|
return false;
|
2023-01-02 23:59:03 +01:00
|
|
|
}
|
2020-06-13 02:54:08 +02:00
|
|
|
|
|
|
|
size_t remainder_start = method_end + sizeof(kMethodEnd) - 1;
|
|
|
|
if (remainder_start == message.size() - 1) {
|
|
|
|
// No more contents.
|
|
|
|
params = base::StringPiece();
|
|
|
|
} else {
|
|
|
|
const base::StringPiece& remainder = message.substr(remainder_start);
|
2020-08-29 00:39:23 +02:00
|
|
|
if (base::StartsWith(remainder, kParamsStart)) {
|
2020-06-13 02:54:08 +02:00
|
|
|
// Stop immediately before the message closing bracket.
|
|
|
|
remainder_start += sizeof(kParamsStart) - 1;
|
|
|
|
params =
|
|
|
|
message.substr(remainder_start, message.size() - 1 - remainder_start);
|
|
|
|
} else {
|
|
|
|
// Invalid format.
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2023-01-02 23:59:03 +01:00
|
|
|
if (!IsValidDictionary(params, /*allow_empty=*/true)) {
|
2020-06-13 02:54:08 +02:00
|
|
|
return false;
|
2023-01-02 23:59:03 +01:00
|
|
|
}
|
2020-06-13 02:54:08 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Examples:
|
|
|
|
// {"id":3,"result":{}}
|
|
|
|
// {"id":4,"result":{"debuggerId":"-2193881606781505058.81393575456727957"}}
|
|
|
|
// {"id":5,"error":{"code":-32000,"message":"Not supported"}}
|
|
|
|
bool ParseResult(const base::StringPiece& message,
|
|
|
|
int& message_id,
|
|
|
|
bool& success,
|
|
|
|
base::StringPiece& result) {
|
|
|
|
static const char kIdStart[] = "{\"id\":";
|
|
|
|
static const char kIdEnd[] = ",";
|
|
|
|
static const char kResultStart[] = "\"result\":";
|
|
|
|
static const char kErrorStart[] = "\"error\":";
|
|
|
|
|
2023-01-02 23:59:03 +01:00
|
|
|
if (!base::StartsWith(message, kIdStart)) {
|
2020-06-13 02:54:08 +02:00
|
|
|
return false;
|
2023-01-02 23:59:03 +01:00
|
|
|
}
|
2020-06-13 02:54:08 +02:00
|
|
|
|
|
|
|
const size_t id_start = sizeof(kIdStart) - 1;
|
|
|
|
const size_t id_end = message.find(kIdEnd, id_start);
|
2023-01-02 23:59:03 +01:00
|
|
|
if (id_end == base::StringPiece::npos) {
|
2020-06-13 02:54:08 +02:00
|
|
|
return false;
|
2023-01-02 23:59:03 +01:00
|
|
|
}
|
2020-06-13 02:54:08 +02:00
|
|
|
const base::StringPiece& id_str = message.substr(id_start, id_end - id_start);
|
2023-01-02 23:59:03 +01:00
|
|
|
if (id_str.empty() || !base::StringToInt(id_str, &message_id)) {
|
2020-06-13 02:54:08 +02:00
|
|
|
return false;
|
2023-01-02 23:59:03 +01:00
|
|
|
}
|
2020-06-13 02:54:08 +02:00
|
|
|
|
|
|
|
size_t remainder_start = id_end + sizeof(kIdEnd) - 1;
|
|
|
|
const base::StringPiece& remainder = message.substr(remainder_start);
|
2020-08-29 00:39:23 +02:00
|
|
|
if (base::StartsWith(remainder, kResultStart)) {
|
2020-06-13 02:54:08 +02:00
|
|
|
// Stop immediately before the message closing bracket.
|
|
|
|
remainder_start += sizeof(kResultStart) - 1;
|
|
|
|
result =
|
|
|
|
message.substr(remainder_start, message.size() - 1 - remainder_start);
|
|
|
|
success = true;
|
2020-08-29 00:39:23 +02:00
|
|
|
} else if (base::StartsWith(remainder, kErrorStart)) {
|
2020-06-13 02:54:08 +02:00
|
|
|
// Stop immediately before the message closing bracket.
|
|
|
|
remainder_start += sizeof(kErrorStart) - 1;
|
|
|
|
result =
|
|
|
|
message.substr(remainder_start, message.size() - 1 - remainder_start);
|
|
|
|
success = false;
|
|
|
|
} else {
|
|
|
|
// Invalid format.
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2023-01-02 23:59:03 +01:00
|
|
|
if (!IsValidDictionary(result, /*allow_empty=*/true)) {
|
2020-06-13 02:54:08 +02:00
|
|
|
return false;
|
2023-01-02 23:59:03 +01:00
|
|
|
}
|
2020-06-13 02:54:08 +02:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace
|
|
|
|
|
|
|
|
// static
|
|
|
|
bool ProtocolParser::IsValidMessage(const base::StringPiece& message) {
|
|
|
|
return IsValidDictionary(message, /*allow_empty=*/false);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool ProtocolParser::Initialize(const base::StringPiece& message) {
|
2023-01-02 23:59:03 +01:00
|
|
|
if (status_ != UNINITIALIZED) {
|
2020-06-13 02:54:08 +02:00
|
|
|
return false;
|
2023-01-02 23:59:03 +01:00
|
|
|
}
|
2020-06-13 02:54:08 +02:00
|
|
|
|
|
|
|
if (ParseEvent(message, method_, params_)) {
|
|
|
|
status_ = EVENT;
|
|
|
|
} else if (ParseResult(message, message_id_, success_, params_)) {
|
|
|
|
status_ = RESULT;
|
|
|
|
} else {
|
|
|
|
status_ = FAILURE;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace devtools_util
|