Commit 1e8f0d0d authored by pfeldman's avatar pfeldman Committed by Commit bot

DevTools: response writers need to post to the UI while sending into streams.

NOTRY=true
R=dgozman
BUG=

Review URL: https://codereview.chromium.org/969573002

Cr-Commit-Position: refs/heads/master@{#318617}
parent f5329166
...@@ -242,7 +242,7 @@ InfoBarService* DefaultBindingsDelegate::GetInfoBarService() { ...@@ -242,7 +242,7 @@ InfoBarService* DefaultBindingsDelegate::GetInfoBarService() {
class ResponseWriter : public net::URLFetcherResponseWriter { class ResponseWriter : public net::URLFetcherResponseWriter {
public: public:
ResponseWriter(DevToolsUIBindings* bindings, int stream_id); ResponseWriter(base::WeakPtr<DevToolsUIBindings> bindings, int stream_id);
~ResponseWriter() override; ~ResponseWriter() override;
// URLFetcherResponseWriter overrides: // URLFetcherResponseWriter overrides:
...@@ -253,13 +253,13 @@ class ResponseWriter : public net::URLFetcherResponseWriter { ...@@ -253,13 +253,13 @@ class ResponseWriter : public net::URLFetcherResponseWriter {
int Finish(const net::CompletionCallback& callback) override; int Finish(const net::CompletionCallback& callback) override;
private: private:
DevToolsUIBindings* bindings_; base::WeakPtr<DevToolsUIBindings> bindings_;
int stream_id_; int stream_id_;
DISALLOW_COPY_AND_ASSIGN(ResponseWriter); DISALLOW_COPY_AND_ASSIGN(ResponseWriter);
}; };
ResponseWriter::ResponseWriter(DevToolsUIBindings* bindings, ResponseWriter::ResponseWriter(base::WeakPtr<DevToolsUIBindings> bindings,
int stream_id) int stream_id)
: bindings_(bindings), : bindings_(bindings),
stream_id_(stream_id) { stream_id_(stream_id) {
...@@ -275,17 +275,19 @@ int ResponseWriter::Initialize(const net::CompletionCallback& callback) { ...@@ -275,17 +275,19 @@ int ResponseWriter::Initialize(const net::CompletionCallback& callback) {
int ResponseWriter::Write(net::IOBuffer* buffer, int ResponseWriter::Write(net::IOBuffer* buffer,
int num_bytes, int num_bytes,
const net::CompletionCallback& callback) { const net::CompletionCallback& callback) {
base::FundamentalValue id(stream_id_); base::FundamentalValue* id = new base::FundamentalValue(stream_id_);
base::StringValue chunk(std::string(buffer->data(), num_bytes)); base::StringValue* chunk =
bindings_->CallClientFunction( new base::StringValue(std::string(buffer->data(), num_bytes));
"DevToolsAPI.streamWrite", &id, &chunk, nullptr);
content::BrowserThread::PostTask(
content::BrowserThread::UI, FROM_HERE,
base::Bind(&DevToolsUIBindings::CallClientFunction,
bindings_, "DevToolsAPI.streamWrite",
base::Owned(id), base::Owned(chunk), nullptr));
return num_bytes; return num_bytes;
} }
int ResponseWriter::Finish(const net::CompletionCallback& callback) { int ResponseWriter::Finish(const net::CompletionCallback& callback) {
base::FundamentalValue id(stream_id_);
bindings_->CallClientFunction(
"DevToolsAPI.streamFinish", &id, nullptr, nullptr);
return net::OK; return net::OK;
} }
...@@ -587,7 +589,7 @@ void DevToolsUIBindings::LoadNetworkResource(int request_id, ...@@ -587,7 +589,7 @@ void DevToolsUIBindings::LoadNetworkResource(int request_id,
fetcher->SetRequestContext(profile_->GetRequestContext()); fetcher->SetRequestContext(profile_->GetRequestContext());
fetcher->SetExtraRequestHeaders(headers); fetcher->SetExtraRequestHeaders(headers);
fetcher->SaveResponseWithWriter(scoped_ptr<net::URLFetcherResponseWriter>( fetcher->SaveResponseWithWriter(scoped_ptr<net::URLFetcherResponseWriter>(
new ResponseWriter(this, stream_id))); new ResponseWriter(weak_factory_.GetWeakPtr(), stream_id)));
fetcher->Start(); fetcher->Start();
} }
......
...@@ -10,6 +10,8 @@ ...@@ -10,6 +10,8 @@
#include "base/strings/string_number_conversions.h" #include "base/strings/string_number_conversions.h"
#include "base/strings/stringprintf.h" #include "base/strings/stringprintf.h"
#include "base/strings/utf_string_conversions.h" #include "base/strings/utf_string_conversions.h"
#include "base/values.h"
#include "content/public/browser/browser_thread.h"
#include "content/public/browser/devtools_http_handler.h" #include "content/public/browser/devtools_http_handler.h"
#include "content/public/browser/render_frame_host.h" #include "content/public/browser/render_frame_host.h"
#include "content/public/browser/render_view_host.h" #include "content/public/browser/render_view_host.h"
...@@ -37,7 +39,8 @@ namespace { ...@@ -37,7 +39,8 @@ namespace {
class ResponseWriter : public net::URLFetcherResponseWriter { class ResponseWriter : public net::URLFetcherResponseWriter {
public: public:
ResponseWriter(Shell* shell, int stream_id); ResponseWriter(base::WeakPtr<ShellDevToolsFrontend> shell_devtools_,
int stream_id);
~ResponseWriter() override; ~ResponseWriter() override;
// URLFetcherResponseWriter overrides: // URLFetcherResponseWriter overrides:
...@@ -48,15 +51,16 @@ class ResponseWriter : public net::URLFetcherResponseWriter { ...@@ -48,15 +51,16 @@ class ResponseWriter : public net::URLFetcherResponseWriter {
int Finish(const net::CompletionCallback& callback) override; int Finish(const net::CompletionCallback& callback) override;
private: private:
Shell* shell_; base::WeakPtr<ShellDevToolsFrontend> shell_devtools_;
int stream_id_; int stream_id_;
DISALLOW_COPY_AND_ASSIGN(ResponseWriter); DISALLOW_COPY_AND_ASSIGN(ResponseWriter);
}; };
ResponseWriter::ResponseWriter(Shell* shell, ResponseWriter::ResponseWriter(
base::WeakPtr<ShellDevToolsFrontend> shell_devtools,
int stream_id) int stream_id)
: shell_(shell), : shell_devtools_(shell_devtools),
stream_id_(stream_id) { stream_id_(stream_id) {
} }
...@@ -70,23 +74,19 @@ int ResponseWriter::Initialize(const net::CompletionCallback& callback) { ...@@ -70,23 +74,19 @@ int ResponseWriter::Initialize(const net::CompletionCallback& callback) {
int ResponseWriter::Write(net::IOBuffer* buffer, int ResponseWriter::Write(net::IOBuffer* buffer,
int num_bytes, int num_bytes,
const net::CompletionCallback& callback) { const net::CompletionCallback& callback) {
base::StringValue chunk(std::string(buffer->data(), num_bytes)); base::FundamentalValue* id = new base::FundamentalValue(stream_id_);
std::string encoded; base::StringValue* chunk =
base::JSONWriter::Write(&chunk, &encoded); new base::StringValue(std::string(buffer->data(), num_bytes));
std::string code = base::StringPrintf( content::BrowserThread::PostTask(
"DevToolsAPI.streamWrite(%d, %s)", stream_id_, encoded.c_str()); content::BrowserThread::UI, FROM_HERE,
shell_->web_contents()->GetMainFrame()->ExecuteJavaScript( base::Bind(&ShellDevToolsFrontend::CallClientFunction,
base::UTF8ToUTF16(code)); shell_devtools_, "DevToolsAPI.streamWrite",
base::Owned(id), base::Owned(chunk), nullptr));
return num_bytes; return num_bytes;
} }
int ResponseWriter::Finish(const net::CompletionCallback& callback) { 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));
return net::OK; return net::OK;
} }
...@@ -145,7 +145,8 @@ ShellDevToolsFrontend::ShellDevToolsFrontend(Shell* frontend_shell, ...@@ -145,7 +145,8 @@ ShellDevToolsFrontend::ShellDevToolsFrontend(Shell* frontend_shell,
DevToolsAgentHost* agent_host) DevToolsAgentHost* agent_host)
: WebContentsObserver(frontend_shell->web_contents()), : WebContentsObserver(frontend_shell->web_contents()),
frontend_shell_(frontend_shell), frontend_shell_(frontend_shell),
agent_host_(agent_host) { agent_host_(agent_host),
weak_factory_(this) {
} }
ShellDevToolsFrontend::~ShellDevToolsFrontend() { ShellDevToolsFrontend::~ShellDevToolsFrontend() {
...@@ -187,8 +188,8 @@ void ShellDevToolsFrontend::HandleMessageFromDevToolsFrontend( ...@@ -187,8 +188,8 @@ void ShellDevToolsFrontend::HandleMessageFromDevToolsFrontend(
!dict->GetString("method", &method)) { !dict->GetString("method", &method)) {
return; return;
} }
int id = 0; int request_id = 0;
dict->GetInteger("id", &id); dict->GetInteger("id", &request_id);
dict->GetList("params", &params); dict->GetList("params", &params);
std::string browser_message; std::string browser_message;
...@@ -208,35 +209,31 @@ void ShellDevToolsFrontend::HandleMessageFromDevToolsFrontend( ...@@ -208,35 +209,31 @@ void ShellDevToolsFrontend::HandleMessageFromDevToolsFrontend(
!params->GetInteger(2, &stream_id)) { !params->GetInteger(2, &stream_id)) {
return; return;
} }
GURL gurl(url); GURL gurl(url);
if (!gurl.is_valid()) { if (!gurl.is_valid()) {
std::string code = base::StringPrintf( base::DictionaryValue response;
"DevToolsAPI.embedderMessageAck(%d, { statusCode: 404 });", id); response.SetInteger("statusCode", 404);
web_contents()->GetMainFrame()->ExecuteJavaScript( SendMessageAck(request_id, &response);
base::UTF8ToUTF16(code));
return; return;
} }
net::URLFetcher* fetcher = net::URLFetcher* fetcher =
net::URLFetcher::Create(gurl, net::URLFetcher::GET, this); net::URLFetcher::Create(gurl, net::URLFetcher::GET, this);
pending_requests_[fetcher] = id; pending_requests_[fetcher] = request_id;
fetcher->SetRequestContext(web_contents()->GetBrowserContext()-> fetcher->SetRequestContext(web_contents()->GetBrowserContext()->
GetRequestContext()); GetRequestContext());
fetcher->SetExtraRequestHeaders(headers); fetcher->SetExtraRequestHeaders(headers);
fetcher->SaveResponseWithWriter(scoped_ptr<net::URLFetcherResponseWriter>( fetcher->SaveResponseWithWriter(scoped_ptr<net::URLFetcherResponseWriter>(
new ResponseWriter(frontend_shell(), stream_id))); new ResponseWriter(weak_factory_.GetWeakPtr(), stream_id)));
fetcher->Start(); fetcher->Start();
return; return;
} else { } else {
return; return;
} }
if (id) { if (request_id)
std::string code = "DevToolsAPI.embedderMessageAck(" + SendMessageAck(request_id, nullptr);
base::IntToString(id) + ",\"\");";
base::string16 javascript = base::UTF8ToUTF16(code);
web_contents()->GetMainFrame()->ExecuteJavaScript(javascript);
}
} }
void ShellDevToolsFrontend::HandleMessageFromDevToolsFrontendToBackend( void ShellDevToolsFrontend::HandleMessageFromDevToolsFrontendToBackend(
...@@ -285,20 +282,42 @@ void ShellDevToolsFrontend::OnURLFetchComplete(const net::URLFetcher* source) { ...@@ -285,20 +282,42 @@ void ShellDevToolsFrontend::OnURLFetchComplete(const net::URLFetcher* source) {
while (rh && rh->EnumerateHeaderLines(&iterator, &name, &value)) while (rh && rh->EnumerateHeaderLines(&iterator, &name, &value))
headers->SetString(name, value); headers->SetString(name, value);
std::string json; SendMessageAck(it->second, &response);
base::JSONWriter::Write(&response, &json);
std::string message = base::StringPrintf(
"DevToolsAPI.embedderMessageAck(%d, %s)",
it->second,
json.c_str());
web_contents()->GetMainFrame()->
ExecuteJavaScript(base::UTF8ToUTF16(message));
pending_requests_.erase(it); pending_requests_.erase(it);
delete source; delete source;
} }
void ShellDevToolsFrontend::CallClientFunction(
const std::string& function_name,
const base::Value* arg1,
const base::Value* arg2,
const base::Value* arg3) {
std::string javascript = function_name + "(";
if (arg1) {
std::string json;
base::JSONWriter::Write(arg1, &json);
javascript.append(json);
if (arg2) {
base::JSONWriter::Write(arg2, &json);
javascript.append(", ").append(json);
if (arg3) {
base::JSONWriter::Write(arg3, &json);
javascript.append(", ").append(json);
}
}
}
javascript.append(");");
web_contents()->GetMainFrame()->ExecuteJavaScript(
base::UTF8ToUTF16(javascript));
}
void ShellDevToolsFrontend::SendMessageAck(int request_id,
const base::Value* arg) {
base::FundamentalValue id_value(request_id);
CallClientFunction("DevToolsAPI.embedderMessageAck",
&id_value, arg, nullptr);
}
void ShellDevToolsFrontend::AttachTo(WebContents* inspected_contents) { void ShellDevToolsFrontend::AttachTo(WebContents* inspected_contents) {
DisconnectFromTarget(); DisconnectFromTarget();
agent_host_ = DevToolsAgentHost::GetOrCreateFor(inspected_contents); agent_host_ = DevToolsAgentHost::GetOrCreateFor(inspected_contents);
......
...@@ -9,11 +9,16 @@ ...@@ -9,11 +9,16 @@
#include "base/compiler_specific.h" #include "base/compiler_specific.h"
#include "base/memory/ref_counted.h" #include "base/memory/ref_counted.h"
#include "base/memory/scoped_ptr.h" #include "base/memory/scoped_ptr.h"
#include "base/memory/weak_ptr.h"
#include "content/public/browser/devtools_agent_host.h" #include "content/public/browser/devtools_agent_host.h"
#include "content/public/browser/devtools_frontend_host.h" #include "content/public/browser/devtools_frontend_host.h"
#include "content/public/browser/web_contents_observer.h" #include "content/public/browser/web_contents_observer.h"
#include "net/url_request/url_fetcher_delegate.h" #include "net/url_request/url_fetcher_delegate.h"
namespace base {
class Value;
}
namespace content { namespace content {
class RenderViewHost; class RenderViewHost;
...@@ -36,6 +41,11 @@ class ShellDevToolsFrontend : public WebContentsObserver, ...@@ -36,6 +41,11 @@ class ShellDevToolsFrontend : public WebContentsObserver,
Shell* frontend_shell() const { return frontend_shell_; } Shell* frontend_shell() const { return frontend_shell_; }
void CallClientFunction(const std::string& function_name,
const base::Value* arg1,
const base::Value* arg2,
const base::Value* arg3);
protected: protected:
ShellDevToolsFrontend(Shell* frontend_shell, DevToolsAgentHost* agent_host); ShellDevToolsFrontend(Shell* frontend_shell, DevToolsAgentHost* agent_host);
~ShellDevToolsFrontend() override; ~ShellDevToolsFrontend() override;
...@@ -62,11 +72,15 @@ class ShellDevToolsFrontend : public WebContentsObserver, ...@@ -62,11 +72,15 @@ class ShellDevToolsFrontend : public WebContentsObserver,
// net::URLFetcherDelegate overrides. // net::URLFetcherDelegate overrides.
void OnURLFetchComplete(const net::URLFetcher* source) override; void OnURLFetchComplete(const net::URLFetcher* source) override;
void SendMessageAck(int request_id,
const base::Value* arg1);
Shell* frontend_shell_; Shell* frontend_shell_;
scoped_refptr<DevToolsAgentHost> agent_host_; scoped_refptr<DevToolsAgentHost> agent_host_;
scoped_ptr<DevToolsFrontendHost> frontend_host_; scoped_ptr<DevToolsFrontendHost> frontend_host_;
using PendingRequestsMap = std::map<const net::URLFetcher*, int>; using PendingRequestsMap = std::map<const net::URLFetcher*, int>;
PendingRequestsMap pending_requests_; PendingRequestsMap pending_requests_;
base::WeakPtrFactory<ShellDevToolsFrontend> weak_factory_;
DISALLOW_COPY_AND_ASSIGN(ShellDevToolsFrontend); DISALLOW_COPY_AND_ASSIGN(ShellDevToolsFrontend);
}; };
......
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