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.
This commit is contained in:
Marshall Greenblatt
2020-06-12 20:54:08 -04:00
parent a9aef28966
commit 39aed35644
46 changed files with 2824 additions and 146 deletions

View File

@@ -41,6 +41,7 @@ enum client_menu_ids {
CLIENT_ID_INSPECT_ELEMENT,
CLIENT_ID_SHOW_SSL_INFO,
CLIENT_ID_CURSOR_CHANGE_DISABLED,
CLIENT_ID_OFFLINE,
CLIENT_ID_TESTMENU_SUBMENU,
CLIENT_ID_TESTMENU_CHECKITEM,
CLIENT_ID_TESTMENU_RADIOITEM1,
@@ -268,6 +269,7 @@ ClientHandler::ClientHandler(Delegate* delegate,
CefCommandLine::GetGlobalCommandLine();
mouse_cursor_change_disabled_ =
command_line->HasSwitch(switches::kMouseCursorChangeDisabled);
offline_ = command_line->HasSwitch(switches::kOffline);
}
void ClientHandler::DetachDelegate() {
@@ -334,6 +336,11 @@ void ClientHandler::OnBeforeContextMenu(CefRefPtr<CefBrowser> browser,
if (browser->GetHost()->IsMouseCursorChangeDisabled())
model->SetChecked(CLIENT_ID_CURSOR_CHANGE_DISABLED, true);
model->AddSeparator();
model->AddItem(CLIENT_ID_OFFLINE, "Offline mode");
if (offline_)
model->SetChecked(CLIENT_ID_OFFLINE, true);
// Test context menu features.
BuildTestMenu(model);
}
@@ -366,6 +373,10 @@ bool ClientHandler::OnContextMenuCommand(CefRefPtr<CefBrowser> browser,
browser->GetHost()->SetMouseCursorChangeDisabled(
!browser->GetHost()->IsMouseCursorChangeDisabled());
return true;
case CLIENT_ID_OFFLINE:
offline_ = !offline_;
SetOfflineState(browser, offline_);
return true;
default: // Allow default handling, if any.
return ExecuteTestMenu(command_id);
}
@@ -588,6 +599,10 @@ void ClientHandler::OnAfterCreated(CefRefPtr<CefBrowser> browser) {
if (mouse_cursor_change_disabled_)
browser->GetHost()->SetMouseCursorChangeDisabled(true);
// Set offline mode if requested via the command-line flag.
if (offline_)
SetOfflineState(browser, true);
if (browser->GetHost()->GetExtension()) {
// Browsers hosting extension apps should auto-resize.
browser->GetHost()->SetAutoResizeEnabled(true, CefSize(20, 20),
@@ -843,6 +858,16 @@ void ClientHandler::OnRenderProcessTerminated(CefRefPtr<CefBrowser> browser,
frame->LoadURL(startup_url_);
}
void ClientHandler::OnDocumentAvailableInMainFrame(
CefRefPtr<CefBrowser> browser) {
CEF_REQUIRE_UI_THREAD();
// Restore offline mode after main frame navigation. Otherwise, offline state
// (e.g. `navigator.onLine`) might be wrong in the renderer process.
if (offline_)
SetOfflineState(browser, true);
}
cef_return_value_t ClientHandler::OnBeforeResourceLoad(
CefRefPtr<CefBrowser> browser,
CefRefPtr<CefFrame> frame,
@@ -1178,4 +1203,16 @@ bool ClientHandler::ExecuteTestMenu(int command_id) {
return false;
}
void ClientHandler::SetOfflineState(CefRefPtr<CefBrowser> browser,
bool offline) {
// See DevTools protocol docs for message format specification.
CefRefPtr<CefDictionaryValue> params = CefDictionaryValue::Create();
params->SetBool("offline", offline);
params->SetDouble("latency", 0);
params->SetDouble("downloadThroughput", 0);
params->SetDouble("uploadThroughput", 0);
browser->GetHost()->ExecuteDevToolsMethod(
/*message_id=*/0, "Network.emulateNetworkConditions", params);
}
} // namespace client