electron/atom/browser/net/atom_network_delegate.cc

395 lines
14 KiB
C++
Raw Normal View History

2015-12-01 04:52:22 +00:00
// Copyright (c) 2015 GitHub, Inc.
// Use of this source code is governed by the MIT license that can be
// found in the LICENSE file.
#include "atom/browser/net/atom_network_delegate.h"
#include "atom/common/native_mate_converters/net_converter.h"
2015-12-11 13:02:44 +00:00
#include "base/stl_util.h"
2015-12-11 12:40:39 +00:00
#include "base/strings/string_util.h"
2015-12-01 04:52:22 +00:00
#include "content/public/browser/browser_thread.h"
#include "content/public/browser/resource_request_info.h"
#include "net/url_request/url_request.h"
using content::BrowserThread;
namespace atom {
namespace {
2015-12-11 12:40:39 +00:00
const char* ResourceTypeToString(content::ResourceType type) {
2015-12-03 22:24:26 +00:00
switch (type) {
case content::RESOURCE_TYPE_MAIN_FRAME:
2015-12-09 19:16:03 +00:00
return "mainFrame";
2015-12-03 22:24:26 +00:00
case content::RESOURCE_TYPE_SUB_FRAME:
2015-12-09 19:16:03 +00:00
return "subFrame";
2015-12-03 22:24:26 +00:00
case content::RESOURCE_TYPE_STYLESHEET:
return "stylesheet";
case content::RESOURCE_TYPE_SCRIPT:
return "script";
case content::RESOURCE_TYPE_IMAGE:
return "image";
case content::RESOURCE_TYPE_OBJECT:
return "object";
case content::RESOURCE_TYPE_XHR:
2015-12-09 19:16:03 +00:00
return "xhr";
2015-12-03 22:24:26 +00:00
default:
return "other";
}
}
2015-12-09 19:16:03 +00:00
AtomNetworkDelegate::BlockingResponse RunListener(
const AtomNetworkDelegate::Listener& callback,
scoped_ptr<base::DictionaryValue> details) {
return callback.Run(*(details.get()));
}
2015-12-11 13:02:44 +00:00
bool MatchesFilterCondition(net::URLRequest* request,
const URLPatterns& patterns) {
if (patterns.empty())
return true;
2015-12-03 22:24:26 +00:00
2015-12-11 13:02:44 +00:00
for (const auto& pattern : patterns) {
if (pattern.MatchesURL(request->url()))
return true;
}
return false;
2015-12-03 22:24:26 +00:00
}
2015-12-11 12:40:39 +00:00
void FillDetailsObject(base::DictionaryValue* details,
net::URLRequest* request) {
details->SetInteger("id", request->identifier());
details->SetString("url", request->url().spec());
details->SetString("method", request->method());
details->SetDouble("timestamp", base::Time::Now().ToDoubleT() * 1000);
2015-12-01 04:52:22 +00:00
auto info = content::ResourceRequestInfo::ForRequest(request);
2015-12-11 12:40:39 +00:00
details->SetString("resourceType",
info ? ResourceTypeToString(info->GetResourceType())
: "other");
2015-12-01 04:52:22 +00:00
}
2015-12-11 12:40:39 +00:00
void FillDetailsObject(base::DictionaryValue* details,
const net::HttpRequestHeaders& headers) {
scoped_ptr<base::DictionaryValue> dict(new base::DictionaryValue);
2015-12-01 04:52:22 +00:00
net::HttpRequestHeaders::Iterator it(headers);
while (it.GetNext())
2015-12-11 12:40:39 +00:00
dict->SetString(it.name(), it.value());
details->Set("requestHeaders", dict.Pass());
2015-12-01 04:52:22 +00:00
}
2015-12-11 12:40:39 +00:00
void FillDetailsObject(base::DictionaryValue* details,
net::HttpResponseHeaders* headers) {
if (!headers)
return;
scoped_ptr<base::DictionaryValue> dict(new base::DictionaryValue);
void* iter = nullptr;
std::string key;
std::string value;
while (headers->EnumerateHeaderLines(&iter, &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());
}
2015-12-01 04:52:22 +00:00
}
2015-12-11 12:40:39 +00:00
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()));
2015-12-01 04:52:22 +00:00
}
void OnBeforeURLRequestResponse(
const net::CompletionCallback& callback,
GURL* new_url,
const AtomNetworkDelegate::BlockingResponse& result) {
2015-12-09 19:16:03 +00:00
if (!result.redirect_url.is_empty())
*new_url = result.redirect_url;
callback.Run(result.Code());
2015-12-01 04:52:22 +00:00
}
void OnBeforeSendHeadersResponse(
const net::CompletionCallback& callback,
net::HttpRequestHeaders* headers,
const AtomNetworkDelegate::BlockingResponse& result) {
2015-12-09 19:16:03 +00:00
if (!result.request_headers.IsEmpty())
*headers = result.request_headers;
callback.Run(result.Code());
2015-12-01 04:52:22 +00:00
}
void OnHeadersReceivedResponse(
const net::CompletionCallback& callback,
2015-12-04 01:54:01 +00:00
const net::HttpResponseHeaders* original_response_headers,
2015-12-01 04:52:22 +00:00
scoped_refptr<net::HttpResponseHeaders>* override_response_headers,
const AtomNetworkDelegate::BlockingResponse& result) {
2015-12-09 19:16:03 +00:00
if (result.response_headers.get()) {
2015-12-04 01:54:01 +00:00
*override_response_headers = new net::HttpResponseHeaders(
original_response_headers->raw_headers());
void* iter = nullptr;
std::string key;
std::string value;
2015-12-09 19:16:03 +00:00
while (result.response_headers->EnumerateHeaderLines(&iter, &key, &value)) {
2015-12-04 01:54:01 +00:00
(*override_response_headers)->RemoveHeader(key);
(*override_response_headers)->AddHeader(key + ": " + value);
}
}
2015-12-09 19:16:03 +00:00
callback.Run(result.Code());
2015-12-01 04:52:22 +00:00
}
} // namespace
AtomNetworkDelegate::AtomNetworkDelegate() {
}
AtomNetworkDelegate::~AtomNetworkDelegate() {
}
void AtomNetworkDelegate::SetListenerInIO(
2015-12-11 12:11:22 +00:00
EventType type,
2015-12-09 19:16:03 +00:00
scoped_ptr<base::DictionaryValue> filter,
2015-12-01 04:52:22 +00:00
const Listener& callback) {
2015-12-09 19:16:03 +00:00
if (callback.is_null()) {
event_listener_map_.erase(type);
return;
}
2015-12-01 04:52:22 +00:00
ListenerInfo info;
info.callback = callback;
2015-12-03 22:24:26 +00:00
const base::ListValue* url_list = nullptr;
if (filter->GetList("urls", &url_list)) {
for (size_t i = 0; i < url_list->GetSize(); ++i) {
std::string url;
extensions::URLPattern pattern;
if (url_list->GetString(i, &url) &&
pattern.Parse(url) == extensions::URLPattern::PARSE_SUCCESS) {
info.url_patterns.insert(pattern);
}
}
}
2015-12-01 04:52:22 +00:00
event_listener_map_[type] = info;
}
int AtomNetworkDelegate::OnBeforeURLRequest(
net::URLRequest* request,
const net::CompletionCallback& callback,
GURL* new_url) {
2015-12-11 13:02:44 +00:00
if (!ContainsKey(event_listener_map_, kOnBeforeRequest))
return brightray::NetworkDelegate::OnBeforeURLRequest(
request, callback, new_url);
const ListenerInfo& info = event_listener_map_[kOnBeforeRequest];
if (!MatchesFilterCondition(request, info.url_patterns))
return net::OK;
scoped_ptr<base::DictionaryValue> details(new base::DictionaryValue);
FillDetailsObject(details.get(), request);
BrowserThread::PostTaskAndReplyWithResult(BrowserThread::UI, FROM_HERE,
base::Bind(&RunListener, info.callback, base::Passed(&details)),
base::Bind(&OnBeforeURLRequestResponse,
callback, new_url));
return net::ERR_IO_PENDING;
2015-12-01 04:52:22 +00:00
}
int AtomNetworkDelegate::OnBeforeSendHeaders(
net::URLRequest* request,
const net::CompletionCallback& callback,
net::HttpRequestHeaders* headers) {
2015-12-11 13:02:44 +00:00
if (!ContainsKey(event_listener_map_, kOnBeforeSendHeaders))
return brightray::NetworkDelegate::OnBeforeSendHeaders(
request, callback, headers);
const ListenerInfo& info = event_listener_map_[kOnBeforeSendHeaders];
if (!MatchesFilterCondition(request, info.url_patterns))
return net::OK;
scoped_ptr<base::DictionaryValue> details(new base::DictionaryValue);
FillDetailsObject(details.get(), request);
FillDetailsObject(details.get(), *headers);
BrowserThread::PostTaskAndReplyWithResult(BrowserThread::UI, FROM_HERE,
base::Bind(&RunListener, info.callback, base::Passed(&details)),
base::Bind(&OnBeforeSendHeadersResponse,
callback, headers));
return net::ERR_IO_PENDING;
2015-12-01 04:52:22 +00:00
}
void AtomNetworkDelegate::OnSendHeaders(
net::URLRequest* request,
const net::HttpRequestHeaders& headers) {
2015-12-11 13:02:44 +00:00
if (!ContainsKey(event_listener_map_, kOnSendHeaders)) {
2015-12-09 19:16:03 +00:00
brightray::NetworkDelegate::OnSendHeaders(request, headers);
2015-12-11 13:02:44 +00:00
return;
2015-12-01 04:52:22 +00:00
}
2015-12-11 13:02:44 +00:00
const ListenerInfo& info = event_listener_map_[kOnSendHeaders];
if (!MatchesFilterCondition(request, info.url_patterns))
return;
scoped_ptr<base::DictionaryValue> details(new base::DictionaryValue);
FillDetailsObject(details.get(), request);
FillDetailsObject(details.get(), headers);
BrowserThread::PostTask(BrowserThread::UI, FROM_HERE,
base::Bind(base::IgnoreResult(&RunListener),
info.callback,
base::Passed(&details)));
2015-12-01 04:52:22 +00:00
}
int AtomNetworkDelegate::OnHeadersReceived(
net::URLRequest* request,
const net::CompletionCallback& callback,
const net::HttpResponseHeaders* original_response_headers,
scoped_refptr<net::HttpResponseHeaders>* override_response_headers,
GURL* allowed_unsafe_redirect_url) {
2015-12-11 13:02:44 +00:00
if (!ContainsKey(event_listener_map_, kOnHeadersReceived))
return brightray::NetworkDelegate::OnHeadersReceived(
request, callback, original_response_headers, override_response_headers,
allowed_unsafe_redirect_url);
const ListenerInfo& info = event_listener_map_[kOnHeadersReceived];
if (!MatchesFilterCondition(request, info.url_patterns))
return net::OK;
scoped_ptr<base::DictionaryValue> details(new base::DictionaryValue);
FillDetailsObject(details.get(), request);
FillDetailsObject(details.get(), original_response_headers);
BrowserThread::PostTaskAndReplyWithResult(BrowserThread::UI, FROM_HERE,
base::Bind(&RunListener, info.callback, base::Passed(&details)),
base::Bind(&OnHeadersReceivedResponse,
callback,
original_response_headers,
override_response_headers));
return net::ERR_IO_PENDING;
2015-12-01 04:52:22 +00:00
}
void AtomNetworkDelegate::OnBeforeRedirect(net::URLRequest* request,
2015-12-11 12:40:39 +00:00
const GURL& new_location) {
2015-12-11 13:02:44 +00:00
if (!ContainsKey(event_listener_map_, kOnBeforeRedirect)) {
2015-12-09 19:16:03 +00:00
brightray::NetworkDelegate::OnBeforeRedirect(request, new_location);
2015-12-11 13:02:44 +00:00
return;
2015-12-01 04:52:22 +00:00
}
2015-12-11 13:02:44 +00:00
const ListenerInfo& info = event_listener_map_[kOnBeforeRedirect];
if (!MatchesFilterCondition(request, info.url_patterns))
return;
scoped_ptr<base::DictionaryValue> details(new base::DictionaryValue);
FillDetailsObject(details.get(), request);
FillDetailsObject(details.get(), new_location);
FillDetailsObject(details.get(), request->response_headers());
FillDetailsObject(details.get(), request->GetSocketAddress());
FillDetailsObject(details.get(), request->was_cached());
BrowserThread::PostTask(BrowserThread::UI, FROM_HERE,
base::Bind(base::IgnoreResult(&RunListener),
info.callback,
base::Passed(&details)));
2015-12-01 04:52:22 +00:00
}
void AtomNetworkDelegate::OnResponseStarted(net::URLRequest* request) {
2015-12-11 13:02:44 +00:00
if (!ContainsKey(event_listener_map_, kOnResponseStarted)) {
2015-12-09 19:16:03 +00:00
brightray::NetworkDelegate::OnResponseStarted(request);
2015-12-11 13:02:44 +00:00
return;
2015-12-01 04:52:22 +00:00
}
2015-12-11 13:02:44 +00:00
if (request->status().status() != net::URLRequestStatus::SUCCESS)
return;
const ListenerInfo& info = event_listener_map_[kOnResponseStarted];
if (!MatchesFilterCondition(request, info.url_patterns))
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());
BrowserThread::PostTask(BrowserThread::UI, FROM_HERE,
base::Bind(base::IgnoreResult(&RunListener),
info.callback,
base::Passed(&details)));
2015-12-01 04:52:22 +00:00
}
void AtomNetworkDelegate::OnCompleted(net::URLRequest* request, bool started) {
2015-12-11 13:02:44 +00:00
if (request->status().status() == net::URLRequestStatus::FAILED ||
request->status().status() == net::URLRequestStatus::CANCELED) {
// Error event.
if (ContainsKey(event_listener_map_, kOnErrorOccurred))
2015-12-09 19:16:03 +00:00
OnErrorOccurred(request);
2015-12-11 13:02:44 +00:00
else
brightray::NetworkDelegate::OnCompleted(request, started);
return;
} else if (request->response_headers() &&
net::HttpResponseHeaders::IsRedirectResponseCode(
request->response_headers()->response_code())) {
// Redirect event.
brightray::NetworkDelegate::OnCompleted(request, started);
return;
}
2015-12-03 22:24:26 +00:00
2015-12-11 13:02:44 +00:00
if (!ContainsKey(event_listener_map_, kOnCompleted)) {
2015-12-09 19:16:03 +00:00
brightray::NetworkDelegate::OnCompleted(request, started);
2015-12-11 13:02:44 +00:00
return;
2015-12-03 22:24:26 +00:00
}
2015-12-11 13:02:44 +00:00
const ListenerInfo& info = event_listener_map_[kOnCompleted];
if (!MatchesFilterCondition(request, info.url_patterns))
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());
BrowserThread::PostTask(BrowserThread::UI, FROM_HERE,
base::Bind(base::IgnoreResult(&RunListener),
info.callback,
base::Passed(&details)));
2015-12-03 22:24:26 +00:00
}
void AtomNetworkDelegate::OnErrorOccurred(net::URLRequest* request) {
2015-12-11 13:02:44 +00:00
const ListenerInfo& info = event_listener_map_[kOnErrorOccurred];
if (!MatchesFilterCondition(request, info.url_patterns))
return;
scoped_ptr<base::DictionaryValue> details(new base::DictionaryValue);
FillDetailsObject(details.get(), request);
FillDetailsObject(details.get(), request->was_cached());
FillDetailsObject(details.get(), request->status());
BrowserThread::PostTask(BrowserThread::UI, FROM_HERE,
base::Bind(base::IgnoreResult(&RunListener),
info.callback,
base::Passed(&details)));
2015-12-01 04:52:22 +00:00
}
} // namespace atom