Simpily the code that fills |details|
This commit is contained in:
parent
e295eb0de7
commit
9d406b695f
1 changed files with 96 additions and 85 deletions
|
@ -5,6 +5,7 @@
|
||||||
#include "atom/browser/net/atom_network_delegate.h"
|
#include "atom/browser/net/atom_network_delegate.h"
|
||||||
|
|
||||||
#include "atom/common/native_mate_converters/net_converter.h"
|
#include "atom/common/native_mate_converters/net_converter.h"
|
||||||
|
#include "base/strings/string_util.h"
|
||||||
#include "content/public/browser/browser_thread.h"
|
#include "content/public/browser/browser_thread.h"
|
||||||
#include "content/public/browser/resource_request_info.h"
|
#include "content/public/browser/resource_request_info.h"
|
||||||
#include "net/url_request/url_request.h"
|
#include "net/url_request/url_request.h"
|
||||||
|
@ -15,7 +16,7 @@ namespace atom {
|
||||||
|
|
||||||
namespace {
|
namespace {
|
||||||
|
|
||||||
std::string ResourceTypeToString(content::ResourceType type) {
|
const char* ResourceTypeToString(content::ResourceType type) {
|
||||||
switch (type) {
|
switch (type) {
|
||||||
case content::RESOURCE_TYPE_MAIN_FRAME:
|
case content::RESOURCE_TYPE_MAIN_FRAME:
|
||||||
return "mainFrame";
|
return "mainFrame";
|
||||||
|
@ -56,41 +57,70 @@ bool MatchesFilterCondition(
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
scoped_ptr<base::DictionaryValue> ExtractRequestInfo(net::URLRequest* request) {
|
void FillDetailsObject(base::DictionaryValue* details,
|
||||||
scoped_ptr<base::DictionaryValue> dict(new base::DictionaryValue());
|
net::URLRequest* request) {
|
||||||
dict->SetInteger("id", request->identifier());
|
details->SetInteger("id", request->identifier());
|
||||||
dict->SetString("url", request->url().spec());
|
details->SetString("url", request->url().spec());
|
||||||
dict->SetString("method", request->method());
|
details->SetString("method", request->method());
|
||||||
content::ResourceType resourceType = content::RESOURCE_TYPE_LAST_TYPE;
|
details->SetDouble("timestamp", base::Time::Now().ToDoubleT() * 1000);
|
||||||
auto info = content::ResourceRequestInfo::ForRequest(request);
|
auto info = content::ResourceRequestInfo::ForRequest(request);
|
||||||
if (info)
|
details->SetString("resourceType",
|
||||||
resourceType = info->GetResourceType();
|
info ? ResourceTypeToString(info->GetResourceType())
|
||||||
dict->SetString("resourceType", ResourceTypeToString(resourceType));
|
: "other");
|
||||||
dict->SetDouble("timestamp", base::Time::Now().ToDoubleT() * 1000);
|
|
||||||
|
|
||||||
return dict.Pass();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
scoped_ptr<base::DictionaryValue> GetRequestHeadersDict(
|
void FillDetailsObject(base::DictionaryValue* details,
|
||||||
const net::HttpRequestHeaders& headers) {
|
const net::HttpRequestHeaders& headers) {
|
||||||
scoped_ptr<base::DictionaryValue> header_dict(new base::DictionaryValue());
|
scoped_ptr<base::DictionaryValue> dict(new base::DictionaryValue);
|
||||||
net::HttpRequestHeaders::Iterator it(headers);
|
net::HttpRequestHeaders::Iterator it(headers);
|
||||||
while (it.GetNext())
|
while (it.GetNext())
|
||||||
header_dict->SetString(it.name(), it.value());
|
dict->SetString(it.name(), it.value());
|
||||||
return header_dict.Pass();
|
details->Set("requestHeaders", dict.Pass());
|
||||||
}
|
}
|
||||||
|
|
||||||
scoped_ptr<base::DictionaryValue> GetResponseHeadersDict(
|
void FillDetailsObject(base::DictionaryValue* details,
|
||||||
const net::HttpResponseHeaders* headers) {
|
net::HttpResponseHeaders* headers) {
|
||||||
scoped_ptr<base::DictionaryValue> header_dict(new base::DictionaryValue());
|
if (!headers)
|
||||||
if (headers) {
|
return;
|
||||||
|
|
||||||
|
scoped_ptr<base::DictionaryValue> dict(new base::DictionaryValue);
|
||||||
void* iter = nullptr;
|
void* iter = nullptr;
|
||||||
std::string key;
|
std::string key;
|
||||||
std::string value;
|
std::string value;
|
||||||
while (headers->EnumerateHeaderLines(&iter, &key, &value))
|
while (headers->EnumerateHeaderLines(&iter, &key, &value)) {
|
||||||
header_dict->SetString(key, value);
|
key = base::ToLowerASCII(key);
|
||||||
|
if (dict->HasKey(key)) {
|
||||||
|
base::ListValue* values = nullptr;
|
||||||
|
if (dict->GetList(key, &values))
|
||||||
|
values->AppendString(value);
|
||||||
|
} else {
|
||||||
|
scoped_ptr<base::ListValue> values(new base::ListValue);
|
||||||
|
values->AppendString(value);
|
||||||
|
dict->Set(key, values.Pass());
|
||||||
}
|
}
|
||||||
return header_dict.Pass();
|
}
|
||||||
|
details->Set("responseHeaders", dict.Pass());
|
||||||
|
details->SetString("statusLine", headers->GetStatusLine());
|
||||||
|
details->SetInteger("statusCode", headers->response_code());
|
||||||
|
}
|
||||||
|
|
||||||
|
void FillDetailsObject(base::DictionaryValue* details, const GURL& location) {
|
||||||
|
details->SetString("redirectURL", location.spec());
|
||||||
|
}
|
||||||
|
|
||||||
|
void FillDetailsObject(base::DictionaryValue* details,
|
||||||
|
const net::HostPortPair& host_port) {
|
||||||
|
if (host_port.host().empty())
|
||||||
|
details->SetString("ip", host_port.host());
|
||||||
|
}
|
||||||
|
|
||||||
|
void FillDetailsObject(base::DictionaryValue* details, bool from_cache) {
|
||||||
|
details->SetBoolean("fromCache", from_cache);
|
||||||
|
}
|
||||||
|
|
||||||
|
void FillDetailsObject(base::DictionaryValue* details,
|
||||||
|
const net::URLRequestStatus& status) {
|
||||||
|
details->SetString("error", net::ErrorToString(status.error()));
|
||||||
}
|
}
|
||||||
|
|
||||||
void OnBeforeURLRequestResponse(
|
void OnBeforeURLRequestResponse(
|
||||||
|
@ -174,9 +204,10 @@ int AtomNetworkDelegate::OnBeforeURLRequest(
|
||||||
if (!MatchesFilterCondition(request, listener_info->second))
|
if (!MatchesFilterCondition(request, listener_info->second))
|
||||||
return net::OK;
|
return net::OK;
|
||||||
|
|
||||||
auto wrapped_callback = listener_info->second.callback;
|
scoped_ptr<base::DictionaryValue> details(new base::DictionaryValue);
|
||||||
auto details = ExtractRequestInfo(request);
|
FillDetailsObject(details.get(), request);
|
||||||
|
|
||||||
|
auto wrapped_callback = listener_info->second.callback;
|
||||||
BrowserThread::PostTaskAndReplyWithResult(BrowserThread::UI, FROM_HERE,
|
BrowserThread::PostTaskAndReplyWithResult(BrowserThread::UI, FROM_HERE,
|
||||||
base::Bind(&RunListener, wrapped_callback, base::Passed(&details)),
|
base::Bind(&RunListener, wrapped_callback, base::Passed(&details)),
|
||||||
base::Bind(&OnBeforeURLRequestResponse,
|
base::Bind(&OnBeforeURLRequestResponse,
|
||||||
|
@ -199,10 +230,11 @@ int AtomNetworkDelegate::OnBeforeSendHeaders(
|
||||||
if (!MatchesFilterCondition(request, listener_info->second))
|
if (!MatchesFilterCondition(request, listener_info->second))
|
||||||
return net::OK;
|
return net::OK;
|
||||||
|
|
||||||
auto wrapped_callback = listener_info->second.callback;
|
scoped_ptr<base::DictionaryValue> details(new base::DictionaryValue);
|
||||||
auto details = ExtractRequestInfo(request);
|
FillDetailsObject(details.get(), request);
|
||||||
details->Set("requestHeaders", GetRequestHeadersDict(*headers).get());
|
FillDetailsObject(details.get(), *headers);
|
||||||
|
|
||||||
|
auto wrapped_callback = listener_info->second.callback;
|
||||||
BrowserThread::PostTaskAndReplyWithResult(BrowserThread::UI, FROM_HERE,
|
BrowserThread::PostTaskAndReplyWithResult(BrowserThread::UI, FROM_HERE,
|
||||||
base::Bind(&RunListener, wrapped_callback, base::Passed(&details)),
|
base::Bind(&RunListener, wrapped_callback, base::Passed(&details)),
|
||||||
base::Bind(&OnBeforeSendHeadersResponse,
|
base::Bind(&OnBeforeSendHeadersResponse,
|
||||||
|
@ -224,10 +256,11 @@ void AtomNetworkDelegate::OnSendHeaders(
|
||||||
if (!MatchesFilterCondition(request, listener_info->second))
|
if (!MatchesFilterCondition(request, listener_info->second))
|
||||||
return;
|
return;
|
||||||
|
|
||||||
auto wrapped_callback = listener_info->second.callback;
|
scoped_ptr<base::DictionaryValue> details(new base::DictionaryValue);
|
||||||
auto details = ExtractRequestInfo(request);
|
FillDetailsObject(details.get(), request);
|
||||||
details->Set("requestHeaders", GetRequestHeadersDict(headers).get());
|
FillDetailsObject(details.get(), headers);
|
||||||
|
|
||||||
|
auto wrapped_callback = listener_info->second.callback;
|
||||||
BrowserThread::PostTask(BrowserThread::UI, FROM_HERE,
|
BrowserThread::PostTask(BrowserThread::UI, FROM_HERE,
|
||||||
base::Bind(base::IgnoreResult(&RunListener),
|
base::Bind(base::IgnoreResult(&RunListener),
|
||||||
wrapped_callback,
|
wrapped_callback,
|
||||||
|
@ -248,15 +281,11 @@ int AtomNetworkDelegate::OnHeadersReceived(
|
||||||
if (!MatchesFilterCondition(request, listener_info->second))
|
if (!MatchesFilterCondition(request, listener_info->second))
|
||||||
return net::OK;
|
return net::OK;
|
||||||
|
|
||||||
auto wrapped_callback = listener_info->second.callback;
|
scoped_ptr<base::DictionaryValue> details(new base::DictionaryValue);
|
||||||
auto details = ExtractRequestInfo(request);
|
FillDetailsObject(details.get(), request);
|
||||||
details->SetString("statusLine",
|
FillDetailsObject(details.get(), original_response_headers);
|
||||||
original_response_headers->GetStatusLine());
|
|
||||||
details->SetInteger("statusCode",
|
|
||||||
original_response_headers->response_code());
|
|
||||||
details->Set("responseHeaders",
|
|
||||||
GetResponseHeadersDict(original_response_headers).get());
|
|
||||||
|
|
||||||
|
auto wrapped_callback = listener_info->second.callback;
|
||||||
BrowserThread::PostTaskAndReplyWithResult(BrowserThread::UI, FROM_HERE,
|
BrowserThread::PostTaskAndReplyWithResult(BrowserThread::UI, FROM_HERE,
|
||||||
base::Bind(&RunListener, wrapped_callback, base::Passed(&details)),
|
base::Bind(&RunListener, wrapped_callback, base::Passed(&details)),
|
||||||
base::Bind(&OnHeadersReceivedResponse,
|
base::Bind(&OnHeadersReceivedResponse,
|
||||||
|
@ -279,17 +308,14 @@ void AtomNetworkDelegate::OnBeforeRedirect(net::URLRequest* request,
|
||||||
if (!MatchesFilterCondition(request, listener_info->second))
|
if (!MatchesFilterCondition(request, listener_info->second))
|
||||||
return;
|
return;
|
||||||
|
|
||||||
auto wrapped_callback = listener_info->second.callback;
|
scoped_ptr<base::DictionaryValue> details(new base::DictionaryValue);
|
||||||
auto details = ExtractRequestInfo(request);
|
FillDetailsObject(details.get(), request);
|
||||||
details->SetString("redirectURL", new_location.spec());
|
FillDetailsObject(details.get(), new_location);
|
||||||
details->SetInteger("statusCode", request->GetResponseCode());
|
FillDetailsObject(details.get(), request->response_headers());
|
||||||
auto ip = request->GetSocketAddress().host();
|
FillDetailsObject(details.get(), request->GetSocketAddress());
|
||||||
if (!ip.empty())
|
FillDetailsObject(details.get(), request->was_cached());
|
||||||
details->SetString("ip", ip);
|
|
||||||
details->SetBoolean("fromCache", request->was_cached());
|
|
||||||
details->Set("responseHeaders",
|
|
||||||
GetResponseHeadersDict(request->response_headers()).get());
|
|
||||||
|
|
||||||
|
auto wrapped_callback = listener_info->second.callback;
|
||||||
BrowserThread::PostTask(BrowserThread::UI, FROM_HERE,
|
BrowserThread::PostTask(BrowserThread::UI, FROM_HERE,
|
||||||
base::Bind(base::IgnoreResult(&RunListener),
|
base::Bind(base::IgnoreResult(&RunListener),
|
||||||
wrapped_callback,
|
wrapped_callback,
|
||||||
|
@ -308,20 +334,12 @@ void AtomNetworkDelegate::OnResponseStarted(net::URLRequest* request) {
|
||||||
if (!MatchesFilterCondition(request, listener_info->second))
|
if (!MatchesFilterCondition(request, listener_info->second))
|
||||||
return;
|
return;
|
||||||
|
|
||||||
|
scoped_ptr<base::DictionaryValue> details(new base::DictionaryValue);
|
||||||
|
FillDetailsObject(details.get(), request);
|
||||||
|
FillDetailsObject(details.get(), request->response_headers());
|
||||||
|
FillDetailsObject(details.get(), request->was_cached());
|
||||||
|
|
||||||
auto wrapped_callback = listener_info->second.callback;
|
auto wrapped_callback = listener_info->second.callback;
|
||||||
auto details = ExtractRequestInfo(request);
|
|
||||||
details->Set("responseHeaders",
|
|
||||||
GetResponseHeadersDict(request->response_headers()).get());
|
|
||||||
details->SetBoolean("fromCache", request->was_cached());
|
|
||||||
|
|
||||||
auto response_headers = request->response_headers();
|
|
||||||
details->SetInteger("statusCode",
|
|
||||||
response_headers ?
|
|
||||||
response_headers->response_code() : 200);
|
|
||||||
details->SetString("statusLine",
|
|
||||||
response_headers ?
|
|
||||||
response_headers->GetStatusLine() : std::string());
|
|
||||||
|
|
||||||
BrowserThread::PostTask(BrowserThread::UI, FROM_HERE,
|
BrowserThread::PostTask(BrowserThread::UI, FROM_HERE,
|
||||||
base::Bind(base::IgnoreResult(&RunListener),
|
base::Bind(base::IgnoreResult(&RunListener),
|
||||||
wrapped_callback,
|
wrapped_callback,
|
||||||
|
@ -349,20 +367,12 @@ void AtomNetworkDelegate::OnCompleted(net::URLRequest* request, bool started) {
|
||||||
if (!MatchesFilterCondition(request, listener_info->second))
|
if (!MatchesFilterCondition(request, listener_info->second))
|
||||||
return;
|
return;
|
||||||
|
|
||||||
|
scoped_ptr<base::DictionaryValue> details(new base::DictionaryValue);
|
||||||
|
FillDetailsObject(details.get(), request);
|
||||||
|
FillDetailsObject(details.get(), request->response_headers());
|
||||||
|
FillDetailsObject(details.get(), request->was_cached());
|
||||||
|
|
||||||
auto wrapped_callback = listener_info->second.callback;
|
auto wrapped_callback = listener_info->second.callback;
|
||||||
auto details = ExtractRequestInfo(request);
|
|
||||||
details->Set("responseHeaders",
|
|
||||||
GetResponseHeadersDict(request->response_headers()).get());
|
|
||||||
details->SetBoolean("fromCache", request->was_cached());
|
|
||||||
|
|
||||||
auto response_headers = request->response_headers();
|
|
||||||
details->SetInteger("statusCode",
|
|
||||||
response_headers ?
|
|
||||||
response_headers->response_code() : 200);
|
|
||||||
details->SetString("statusLine",
|
|
||||||
response_headers ?
|
|
||||||
response_headers->GetStatusLine() : std::string());
|
|
||||||
|
|
||||||
BrowserThread::PostTask(BrowserThread::UI, FROM_HERE,
|
BrowserThread::PostTask(BrowserThread::UI, FROM_HERE,
|
||||||
base::Bind(base::IgnoreResult(&RunListener),
|
base::Bind(base::IgnoreResult(&RunListener),
|
||||||
wrapped_callback,
|
wrapped_callback,
|
||||||
|
@ -378,11 +388,12 @@ void AtomNetworkDelegate::OnErrorOccurred(net::URLRequest* request) {
|
||||||
if (!MatchesFilterCondition(request, listener_info->second))
|
if (!MatchesFilterCondition(request, listener_info->second))
|
||||||
return;
|
return;
|
||||||
|
|
||||||
auto wrapped_callback = listener_info->second.callback;
|
scoped_ptr<base::DictionaryValue> details(new base::DictionaryValue);
|
||||||
auto details = ExtractRequestInfo(request);
|
FillDetailsObject(details.get(), request);
|
||||||
details->SetBoolean("fromCache", request->was_cached());
|
FillDetailsObject(details.get(), request->was_cached());
|
||||||
details->SetString("error", net::ErrorToString(request->status().error()));
|
FillDetailsObject(details.get(), request->status());
|
||||||
|
|
||||||
|
auto wrapped_callback = listener_info->second.callback;
|
||||||
BrowserThread::PostTask(BrowserThread::UI, FROM_HERE,
|
BrowserThread::PostTask(BrowserThread::UI, FROM_HERE,
|
||||||
base::Bind(base::IgnoreResult(&RunListener),
|
base::Bind(base::IgnoreResult(&RunListener),
|
||||||
wrapped_callback,
|
wrapped_callback,
|
||||||
|
|
Loading…
Reference in a new issue