cef/libcef/browser/devtools/devtools_util.h
Marshall Greenblatt 39aed35644 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-19 22:15:22 -04:00

72 lines
2.4 KiB
C++

// 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.
#ifndef CEF_LIBCEF_BROWSER_DEVTOOLS_DEVTOOLS_UTIL_H_
#define CEF_LIBCEF_BROWSER_DEVTOOLS_DEVTOOLS_UTIL_H_
#pragma once
#include "base/strings/string_piece.h"
namespace devtools_util {
// Fast parser for DevTools JSON protocol messages. This implementation makes
// certain assumptions about the JSON object structure (value order and
// formatting) to avoid stateful parsing of messages that may be large
// (sometimes > 1MB in size). The message must be a JSON dictionary that starts
// with a "method" or "id" value which is non-empty and of the expected data
// type. Messages that have a "method" value (event message) may optionally have
// a "params" dictionary. Messages that have an "id" value (result message) must
// have a "result" or "error" dictionary. The dictionary contents are not
// validated and may be empty ("{}").
//
// Example event message:
// {"method":"Target.targetDestroyed","params":{"targetId":"1234..."}}
//
// Example result messages:
// {"id":3,"result":{}}
// {"id":4,"result":{"debuggerId":"-2193881606781505058.81393575456727957"}}
// {"id":5,"error":{"code":-32000,"message":"Not supported"}}
struct ProtocolParser {
ProtocolParser() = default;
// Checks for a non-empty JSON dictionary.
static bool IsValidMessage(const base::StringPiece& message);
// Returns false if already initialized.
bool Initialize(const base::StringPiece& message);
bool IsInitialized() const { return status_ != UNINITIALIZED; }
bool IsEvent() const { return status_ == EVENT; }
bool IsResult() const { return status_ == RESULT; }
bool IsFailure() const { return status_ == FAILURE; }
void Reset() { status_ = UNINITIALIZED; }
// For event messages:
// "method" string:
base::StringPiece method_;
// For result messages:
// "id" int:
int message_id_ = 0;
// true if "result" value, false if "error" value:
bool success_ = false;
// For both:
// "params", "result" or "error" dictionary:
base::StringPiece params_;
private:
enum Status {
UNINITIALIZED,
EVENT, // Event message.
RESULT, // Result message.
FAILURE, // Parsing failure.
};
Status status_ = UNINITIALIZED;
};
} // namespace devtools_util
#endif // CEF_LIBCEF_BROWSER_DEVTOOLS_DEVTOOLS_UTIL_H_