cef/libcef/common/json_impl.cc

95 lines
2.8 KiB
C++
Raw Normal View History

// Copyright (c) 2015 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 "include/cef_parser.h"
#include "libcef/common/values_impl.h"
#include "base/json/json_reader.h"
#include "base/json/json_writer.h"
#include "base/values.h"
namespace {
int GetJSONReaderOptions(cef_json_parser_options_t options) {
int op = base::JSON_PARSE_RFC;
2023-01-02 23:59:03 +01:00
if (options & JSON_PARSER_ALLOW_TRAILING_COMMAS) {
op |= base::JSON_ALLOW_TRAILING_COMMAS;
2023-01-02 23:59:03 +01:00
}
return op;
}
int GetJSONWriterOptions(cef_json_writer_options_t options) {
int op = 0;
2023-01-02 23:59:03 +01:00
if (options & JSON_WRITER_OMIT_BINARY_VALUES) {
op |= base::JSONWriter::OPTIONS_OMIT_BINARY_VALUES;
2023-01-02 23:59:03 +01:00
}
if (options & JSON_WRITER_OMIT_DOUBLE_TYPE_PRESERVATION) {
op |= base::JSONWriter::OPTIONS_OMIT_DOUBLE_TYPE_PRESERVATION;
2023-01-02 23:59:03 +01:00
}
if (options & JSON_WRITER_PRETTY_PRINT) {
op |= base::JSONWriter::OPTIONS_PRETTY_PRINT;
2023-01-02 23:59:03 +01:00
}
return op;
}
} // namespace
CefRefPtr<CefValue> CefParseJSON(const CefString& json_string,
cef_json_parser_options_t options) {
const std::string& json = json_string.ToString();
Add support for direct DevTools protocol messaging (fixes issue #2961). This change allows the client to directly send and receive DevTools protocol messages (send method calls, and receive method results and events) without requiring a DevTools front-end or remote-debugging session. This change includes additional supporting changes: - Add a new CefRequestHandler::OnDocumentAvailableInMainFrame callback (see issue #1454). - Add a CefParseJSON variant that accepts a UTF8-encoded buffer. - Add a `--devtools-protocol-log-file=<path>` command-line flag for logging protocol messages sent to/from the DevTools front-end while it is displayed. This is useful for understanding existing DevTools protocol usage. - Add a new "libcef_static_unittests" executable target to support light-weight unit tests of libcef_static internals (e.g. without requiring exposure via the CEF API). Files to be unittested are placed in the new "libcef_static_unittested" source_set which is then included by both the existing libcef_static library and the new unittests executable target. - Linux: Remove use_bundled_fontconfig=false, which is no longer required and causes unittest build errors (see issue #2424). This change also adds a cefclient demo for configuring offline mode using the DevTools protocol (fixes issue #245). This is controlled by the "Offline mode" context menu option and the `--offline` command-line switch which will launch cefclient in offline mode. When cefclient is offline all network requests will fail with ERR_INTERNET_DISCONNECTED and navigator.onLine will return false when called from JavaScript in any frame. This mode is per-browser so newly created browser windows will have the default mode. Note that configuring offline mode in this way will not update the Network tab UI ("Throtting" option) in a displayed DevTools front-end instance.
2020-06-13 02:54:08 +02:00
return CefParseJSON(json.data(), json.size(), options);
}
CefRefPtr<CefValue> CefParseJSON(const void* json,
size_t json_size,
cef_json_parser_options_t options) {
2023-01-02 23:59:03 +01:00
if (!json || json_size == 0) {
Add support for direct DevTools protocol messaging (fixes issue #2961). This change allows the client to directly send and receive DevTools protocol messages (send method calls, and receive method results and events) without requiring a DevTools front-end or remote-debugging session. This change includes additional supporting changes: - Add a new CefRequestHandler::OnDocumentAvailableInMainFrame callback (see issue #1454). - Add a CefParseJSON variant that accepts a UTF8-encoded buffer. - Add a `--devtools-protocol-log-file=<path>` command-line flag for logging protocol messages sent to/from the DevTools front-end while it is displayed. This is useful for understanding existing DevTools protocol usage. - Add a new "libcef_static_unittests" executable target to support light-weight unit tests of libcef_static internals (e.g. without requiring exposure via the CEF API). Files to be unittested are placed in the new "libcef_static_unittested" source_set which is then included by both the existing libcef_static library and the new unittests executable target. - Linux: Remove use_bundled_fontconfig=false, which is no longer required and causes unittest build errors (see issue #2424). This change also adds a cefclient demo for configuring offline mode using the DevTools protocol (fixes issue #245). This is controlled by the "Offline mode" context menu option and the `--offline` command-line switch which will launch cefclient in offline mode. When cefclient is offline all network requests will fail with ERR_INTERNET_DISCONNECTED and navigator.onLine will return false when called from JavaScript in any frame. This mode is per-browser so newly created browser windows will have the default mode. Note that configuring offline mode in this way will not update the Network tab UI ("Throtting" option) in a displayed DevTools front-end instance.
2020-06-13 02:54:08 +02:00
return nullptr;
2023-01-02 23:59:03 +01:00
}
absl::optional<base::Value> parse_result = base::JSONReader::Read(
Add support for direct DevTools protocol messaging (fixes issue #2961). This change allows the client to directly send and receive DevTools protocol messages (send method calls, and receive method results and events) without requiring a DevTools front-end or remote-debugging session. This change includes additional supporting changes: - Add a new CefRequestHandler::OnDocumentAvailableInMainFrame callback (see issue #1454). - Add a CefParseJSON variant that accepts a UTF8-encoded buffer. - Add a `--devtools-protocol-log-file=<path>` command-line flag for logging protocol messages sent to/from the DevTools front-end while it is displayed. This is useful for understanding existing DevTools protocol usage. - Add a new "libcef_static_unittests" executable target to support light-weight unit tests of libcef_static internals (e.g. without requiring exposure via the CEF API). Files to be unittested are placed in the new "libcef_static_unittested" source_set which is then included by both the existing libcef_static library and the new unittests executable target. - Linux: Remove use_bundled_fontconfig=false, which is no longer required and causes unittest build errors (see issue #2424). This change also adds a cefclient demo for configuring offline mode using the DevTools protocol (fixes issue #245). This is controlled by the "Offline mode" context menu option and the `--offline` command-line switch which will launch cefclient in offline mode. When cefclient is offline all network requests will fail with ERR_INTERNET_DISCONNECTED and navigator.onLine will return false when called from JavaScript in any frame. This mode is per-browser so newly created browser windows will have the default mode. Note that configuring offline mode in this way will not update the Network tab UI ("Throtting" option) in a displayed DevTools front-end instance.
2020-06-13 02:54:08 +02:00
base::StringPiece(static_cast<const char*>(json), json_size),
GetJSONReaderOptions(options));
if (parse_result) {
return new CefValueImpl(
base::Value::ToUniquePtrValue(std::move(parse_result.value()))
.release());
}
return nullptr;
}
CefRefPtr<CefValue> CefParseJSONAndReturnError(
const CefString& json_string,
cef_json_parser_options_t options,
CefString& error_msg_out) {
const std::string& json = json_string.ToString();
std::string error_msg;
auto result = base::JSONReader::ReadAndReturnValueWithError(
json, GetJSONReaderOptions(options));
if (result.has_value()) {
return new CefValueImpl(
base::Value::ToUniquePtrValue(std::move(*result)).release());
}
error_msg_out = result.error().message;
return nullptr;
}
CefString CefWriteJSON(CefRefPtr<CefValue> node,
cef_json_writer_options_t options) {
2023-01-02 23:59:03 +01:00
if (!node.get() || !node->IsValid()) {
return CefString();
2023-01-02 23:59:03 +01:00
}
CefValueImpl* impl = static_cast<CefValueImpl*>(node.get());
CefValueImpl::ScopedLockedValue scoped_value(impl);
std::string json_string;
if (base::JSONWriter::WriteWithOptions(
*scoped_value.value(), GetJSONWriterOptions(options), &json_string)) {
return json_string;
}
return CefString();
}