Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(473)

Unified Diff: content/shell/browser/shell_devtools_frontend.cc

Issue 969573002: DevTools: response writers need to post to the UI while sending into streams. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@loadNetwork
Patch Set: Created 5 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: content/shell/browser/shell_devtools_frontend.cc
diff --git a/content/shell/browser/shell_devtools_frontend.cc b/content/shell/browser/shell_devtools_frontend.cc
index 28c63f1d458c22d9a79f25603f6534ebd3cf6ad4..f2d0d0ca6576b9ab095f3d727b38e98d263837c0 100644
--- a/content/shell/browser/shell_devtools_frontend.cc
+++ b/content/shell/browser/shell_devtools_frontend.cc
@@ -7,9 +7,11 @@
#include "base/command_line.h"
#include "base/json/json_reader.h"
#include "base/json/json_writer.h"
+#include "base/lazy_instance.h"
#include "base/strings/string_number_conversions.h"
#include "base/strings/stringprintf.h"
#include "base/strings/utf_string_conversions.h"
+#include "content/public/browser/browser_thread.h"
#include "content/public/browser/devtools_http_handler.h"
#include "content/public/browser/render_frame_host.h"
#include "content/public/browser/render_view_host.h"
@@ -32,12 +34,15 @@ namespace content {
namespace {
+using StreamOwners = std::map<int, Shell*>;
+base::LazyInstance<StreamOwners>::Leaky g_stream_owners =
+ LAZY_INSTANCE_INITIALIZER;
// ResponseWriter -------------------------------------------------------------
class ResponseWriter : public net::URLFetcherResponseWriter {
public:
- ResponseWriter(Shell* shell, int stream_id);
+ explicit ResponseWriter(int stream_id);
~ResponseWriter() override;
// URLFetcherResponseWriter overrides:
@@ -48,16 +53,13 @@ class ResponseWriter : public net::URLFetcherResponseWriter {
int Finish(const net::CompletionCallback& callback) override;
private:
- Shell* shell_;
int stream_id_;
DISALLOW_COPY_AND_ASSIGN(ResponseWriter);
};
-ResponseWriter::ResponseWriter(Shell* shell,
- int stream_id)
- : shell_(shell),
- stream_id_(stream_id) {
+ResponseWriter::ResponseWriter(int stream_id)
+ : stream_id_(stream_id) {
}
ResponseWriter::~ResponseWriter() {
@@ -67,26 +69,35 @@ int ResponseWriter::Initialize(const net::CompletionCallback& callback) {
return net::OK;
}
+static void RespondOnUI(int stream_id, const std::string& code) {
+ auto it = g_stream_owners.Get().find(stream_id);
+ if (it != g_stream_owners.Get().end()) {
+ it->second->web_contents()->GetMainFrame()->ExecuteJavaScript(
+ base::UTF8ToUTF16(code));
+ }
+}
+
int ResponseWriter::Write(net::IOBuffer* buffer,
int num_bytes,
const net::CompletionCallback& callback) {
base::StringValue chunk(std::string(buffer->data(), num_bytes));
std::string encoded;
base::JSONWriter::Write(&chunk, &encoded);
-
std::string code = base::StringPrintf(
"DevToolsAPI.streamWrite(%d, %s)", stream_id_, encoded.c_str());
- shell_->web_contents()->GetMainFrame()->ExecuteJavaScript(
- base::UTF8ToUTF16(code));
+ content::BrowserThread::PostTask(
+ content::BrowserThread::UI, FROM_HERE,
+ base::Bind(&RespondOnUI, stream_id_, code));
return num_bytes;
}
int ResponseWriter::Finish(const net::CompletionCallback& callback) {
std::string code = base::StringPrintf(
"DevToolsAPI.streamFinish(%d)", stream_id_);
- shell_->web_contents()->GetMainFrame()->ExecuteJavaScript(
- base::UTF8ToUTF16(code));
+ content::BrowserThread::PostTask(
+ content::BrowserThread::UI, FROM_HERE,
+ base::Bind(&RespondOnUI, stream_id_, code));
return net::OK;
}
@@ -151,6 +162,14 @@ ShellDevToolsFrontend::ShellDevToolsFrontend(Shell* frontend_shell,
ShellDevToolsFrontend::~ShellDevToolsFrontend() {
for (const auto& pair : pending_requests_)
delete pair.first;
+
+ for (auto it = g_stream_owners.Get().begin();
+ it != g_stream_owners.Get().end(); ) {
+ if (it->second == frontend_shell())
+ g_stream_owners.Get().erase(it);
+ else
+ ++it;
+ }
}
void ShellDevToolsFrontend::RenderViewCreated(
@@ -217,14 +236,16 @@ void ShellDevToolsFrontend::HandleMessageFromDevToolsFrontend(
return;
}
+ g_stream_owners.Get()[stream_id] = frontend_shell();
+
net::URLFetcher* fetcher =
net::URLFetcher::Create(gurl, net::URLFetcher::GET, this);
- pending_requests_[fetcher] = id;
+ pending_requests_[fetcher] = std::make_pair(id, stream_id);
fetcher->SetRequestContext(web_contents()->GetBrowserContext()->
GetRequestContext());
fetcher->SetExtraRequestHeaders(headers);
fetcher->SaveResponseWithWriter(scoped_ptr<net::URLFetcherResponseWriter>(
- new ResponseWriter(frontend_shell(), stream_id)));
+ new ResponseWriter(stream_id)));
fetcher->Start();
return;
} else {
@@ -288,13 +309,16 @@ void ShellDevToolsFrontend::OnURLFetchComplete(const net::URLFetcher* source) {
std::string json;
base::JSONWriter::Write(&response, &json);
+ int request_id = it->second.first;
+ int stream_id = it->second.second;
std::string message = base::StringPrintf(
"DevToolsAPI.embedderMessageAck(%d, %s)",
- it->second,
+ request_id,
json.c_str());
web_contents()->GetMainFrame()->
ExecuteJavaScript(base::UTF8ToUTF16(message));
+ g_stream_owners.Get().erase(stream_id);
pending_requests_.erase(it);
delete source;
}

Powered by Google App Engine
This is Rietveld 408576698