Commit b3ce57af authored by Noel Gordon's avatar Noel Gordon Committed by Commit Bot

[filesapp] Add DevToolsListener SpanToStringPiece helper

Add SpanToStringPiece helper and use it handle response messages from
the devtools agent. This avoids the implicit memcpy under-the-hood of
the span to temporary std::string (message) conversion.

Minor: remove unused includes, and add base::span, base::string_piece
includes. No change in behavior.

Tbr: benreich
Bug: 1113941
Change-Id: I9a9295230b0a807d3405d70af92cf32ac88cbdaa
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2470280
Commit-Queue: Noel Gordon <noel@chromium.org>
Reviewed-by: default avatarNoel Gordon <noel@chromium.org>
Cr-Commit-Position: refs/heads/master@{#816908}
parent 8774009c
...@@ -10,6 +10,7 @@ ...@@ -10,6 +10,7 @@
#include <string> #include <string>
#include <vector> #include <vector>
#include "base/containers/span.h"
#include "base/files/file_util.h" #include "base/files/file_util.h"
#include "base/hash/md5.h" #include "base/hash/md5.h"
#include "base/json/json_reader.h" #include "base/json/json_reader.h"
...@@ -17,16 +18,20 @@ ...@@ -17,16 +18,20 @@
#include "base/logging.h" #include "base/logging.h"
#include "base/run_loop.h" #include "base/run_loop.h"
#include "base/strings/strcat.h" #include "base/strings/strcat.h"
#include "base/strings/string_piece.h"
#include "base/strings/stringprintf.h" #include "base/strings/stringprintf.h"
#include "base/threading/thread_restrictions.h"
#include "url/url_util.h" #include "url/url_util.h"
namespace file_manager { namespace file_manager {
namespace { namespace {
base::span<const uint8_t> StringToSpan(const std::string& str) { base::span<const uint8_t> StringToSpan(const std::string& s) {
return base::as_bytes(base::make_span(str)); return base::as_bytes(base::make_span(s));
}
base::StringPiece SpanToStringPiece(const base::span<const uint8_t>& s) {
return {reinterpret_cast<const char*>(s.data()), s.size()};
} }
std::string EncodeURIComponent(const std::string& component) { std::string EncodeURIComponent(const std::string& component) {
...@@ -236,15 +241,12 @@ void DevToolsListener::AwaitMessageResponse(int id) { ...@@ -236,15 +241,12 @@ void DevToolsListener::AwaitMessageResponse(int id) {
void DevToolsListener::DispatchProtocolMessage( void DevToolsListener::DispatchProtocolMessage(
content::DevToolsAgentHost* host, content::DevToolsAgentHost* host,
base::span<const uint8_t> span_message) { base::span<const uint8_t> message) {
if (!navigated_) if (!navigated_)
return; return;
std::string message(reinterpret_cast<const char*>(span_message.data()), std::unique_ptr<base::DictionaryValue> response = base::DictionaryValue::From(
span_message.size()); base::JSONReader::ReadDeprecated(SpanToStringPiece(message)));
std::unique_ptr<base::DictionaryValue> response =
base::DictionaryValue::From(base::JSONReader::ReadDeprecated(message));
CHECK(response); CHECK(response);
std::string* method = response->FindStringPath("method"); std::string* method = response->FindStringPath("method");
......
...@@ -66,7 +66,7 @@ class DevToolsListener : public content::DevToolsAgentHostClient { ...@@ -66,7 +66,7 @@ class DevToolsListener : public content::DevToolsAgentHostClient {
// Receives CDP messages sent by host. // Receives CDP messages sent by host.
void DispatchProtocolMessage(content::DevToolsAgentHost* host, void DispatchProtocolMessage(content::DevToolsAgentHost* host,
base::span<const uint8_t> span_message) override; base::span<const uint8_t> message) override;
// Returns true if URL should be attached to. // Returns true if URL should be attached to.
bool MayAttachToURL(const GURL& url, bool is_webui) override; bool MayAttachToURL(const GURL& url, bool is_webui) override;
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment