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