electron/atom/browser/net/atom_network_delegate.cc

394 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"
#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-03 22:24:26 +00:00
std::string ResourceTypeToString(content::ResourceType type) {
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-03 22:24:26 +00:00
bool MatchesFilterCondition(
net::URLRequest* request,
const AtomNetworkDelegate::ListenerInfo& info) {
if (!info.url_patterns.empty()) {
auto url = request->url();
for (auto& pattern : info.url_patterns)
if (pattern.MatchesURL(url))
return true;
2015-12-04 01:54:01 +00:00
return false;
2015-12-03 22:24:26 +00:00
}
2015-12-04 01:54:01 +00:00
return true;
2015-12-03 22:24:26 +00:00
}
2015-12-09 19:16:03 +00:00
scoped_ptr<base::DictionaryValue> ExtractRequestInfo(net::URLRequest* request) {
scoped_ptr<base::DictionaryValue> dict(new base::DictionaryValue());
2015-12-01 04:52:22 +00:00
dict->SetInteger("id", request->identifier());
dict->SetString("url", request->url().spec());
dict->SetString("method", request->method());
content::ResourceType resourceType = content::RESOURCE_TYPE_LAST_TYPE;
auto info = content::ResourceRequestInfo::ForRequest(request);
if (info)
resourceType = info->GetResourceType();
2015-12-03 22:24:26 +00:00
dict->SetString("resourceType", ResourceTypeToString(resourceType));
2015-12-01 04:52:22 +00:00
dict->SetDouble("timestamp", base::Time::Now().ToDoubleT() * 1000);
2015-12-09 19:16:03 +00:00
return dict.Pass();
2015-12-01 04:52:22 +00:00
}
2015-12-09 19:16:03 +00:00
scoped_ptr<base::DictionaryValue> GetRequestHeadersDict(
2015-12-01 04:52:22 +00:00
const net::HttpRequestHeaders& headers) {
2015-12-09 19:16:03 +00:00
scoped_ptr<base::DictionaryValue> header_dict(new base::DictionaryValue());
2015-12-01 04:52:22 +00:00
net::HttpRequestHeaders::Iterator it(headers);
while (it.GetNext())
header_dict->SetString(it.name(), it.value());
2015-12-09 19:16:03 +00:00
return header_dict.Pass();
2015-12-01 04:52:22 +00:00
}
2015-12-09 19:16:03 +00:00
scoped_ptr<base::DictionaryValue> GetResponseHeadersDict(
2015-12-01 04:52:22 +00:00
const net::HttpResponseHeaders* headers) {
2015-12-09 19:16:03 +00:00
scoped_ptr<base::DictionaryValue> header_dict(new base::DictionaryValue());
2015-12-01 04:52:22 +00:00
if (headers) {
void* iter = nullptr;
std::string key;
std::string value;
while (headers->EnumerateHeaderLines(&iter, &key, &value))
header_dict->SetString(key, value);
}
2015-12-09 19:16:03 +00:00
return header_dict.Pass();
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(
EventTypes 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-09 19:16:03 +00:00
auto listener_info = event_listener_map_.find(kOnBeforeRequest);
if (listener_info != event_listener_map_.end()) {
if (!MatchesFilterCondition(request, listener_info->second))
return net::OK;
2015-12-03 22:24:26 +00:00
2015-12-09 19:16:03 +00:00
auto wrapped_callback = listener_info->second.callback;
2015-12-01 04:52:22 +00:00
auto details = ExtractRequestInfo(request);
BrowserThread::PostTaskAndReplyWithResult(BrowserThread::UI, FROM_HERE,
2015-12-09 19:16:03 +00:00
base::Bind(&RunListener, wrapped_callback, base::Passed(&details)),
2015-12-01 04:52:22 +00:00
base::Bind(&OnBeforeURLRequestResponse,
callback, new_url));
return net::ERR_IO_PENDING;
}
2015-12-09 19:16:03 +00:00
return brightray::NetworkDelegate::OnBeforeURLRequest(request,
callback,
new_url);
2015-12-01 04:52:22 +00:00
}
int AtomNetworkDelegate::OnBeforeSendHeaders(
net::URLRequest* request,
const net::CompletionCallback& callback,
net::HttpRequestHeaders* headers) {
2015-12-09 19:16:03 +00:00
auto listener_info = event_listener_map_.find(kOnBeforeSendHeaders);
if (listener_info != event_listener_map_.end()) {
if (!MatchesFilterCondition(request, listener_info->second))
return net::OK;
2015-12-03 22:24:26 +00:00
2015-12-09 19:16:03 +00:00
auto wrapped_callback = listener_info->second.callback;
2015-12-01 04:52:22 +00:00
auto details = ExtractRequestInfo(request);
2015-12-09 19:16:03 +00:00
details->Set("requestHeaders", GetRequestHeadersDict(*headers).release());
2015-12-01 04:52:22 +00:00
BrowserThread::PostTaskAndReplyWithResult(BrowserThread::UI, FROM_HERE,
2015-12-09 19:16:03 +00:00
base::Bind(&RunListener, wrapped_callback, base::Passed(&details)),
2015-12-01 04:52:22 +00:00
base::Bind(&OnBeforeSendHeadersResponse,
callback, headers));
return net::ERR_IO_PENDING;
}
2015-12-09 19:16:03 +00:00
return brightray::NetworkDelegate::OnBeforeSendHeaders(request,
callback,
headers);
2015-12-01 04:52:22 +00:00
}
void AtomNetworkDelegate::OnSendHeaders(
net::URLRequest* request,
const net::HttpRequestHeaders& headers) {
2015-12-09 19:16:03 +00:00
auto listener_info = event_listener_map_.find(kOnSendHeaders);
if (listener_info != event_listener_map_.end()) {
if (!MatchesFilterCondition(request, listener_info->second))
return;
2015-12-03 22:24:26 +00:00
2015-12-09 19:16:03 +00:00
auto wrapped_callback = listener_info->second.callback;
2015-12-01 04:52:22 +00:00
auto details = ExtractRequestInfo(request);
2015-12-09 19:16:03 +00:00
details->Set("requestHeaders", GetRequestHeadersDict(headers).release());
2015-12-01 04:52:22 +00:00
BrowserThread::PostTask(BrowserThread::UI, FROM_HERE,
2015-12-09 19:16:03 +00:00
base::Bind(base::IgnoreResult(&RunListener),
wrapped_callback,
base::Passed(&details)));
} else {
brightray::NetworkDelegate::OnSendHeaders(request, headers);
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-09 19:16:03 +00:00
auto listener_info = event_listener_map_.find(kOnHeadersReceived);
if (listener_info != event_listener_map_.end()) {
if (!MatchesFilterCondition(request, listener_info->second))
return net::OK;
2015-12-03 22:24:26 +00:00
2015-12-09 19:16:03 +00:00
auto wrapped_callback = listener_info->second.callback;
2015-12-01 04:52:22 +00:00
auto details = ExtractRequestInfo(request);
2015-12-03 22:24:26 +00:00
details->SetString("statusLine",
original_response_headers->GetStatusLine());
details->SetInteger("statusCode",
original_response_headers->response_code());
2015-12-01 04:52:22 +00:00
details->Set("responseHeaders",
2015-12-09 19:16:03 +00:00
GetResponseHeadersDict(original_response_headers).release());
2015-12-01 04:52:22 +00:00
BrowserThread::PostTaskAndReplyWithResult(BrowserThread::UI, FROM_HERE,
2015-12-09 19:16:03 +00:00
base::Bind(&RunListener, wrapped_callback, base::Passed(&details)),
2015-12-01 04:52:22 +00:00
base::Bind(&OnHeadersReceivedResponse,
callback,
2015-12-04 01:54:01 +00:00
original_response_headers,
2015-12-01 04:52:22 +00:00
override_response_headers));
return net::ERR_IO_PENDING;
}
2015-12-09 19:16:03 +00:00
return brightray::NetworkDelegate::OnHeadersReceived(
request, callback, original_response_headers, override_response_headers,
allowed_unsafe_redirect_url);
2015-12-01 04:52:22 +00:00
}
void AtomNetworkDelegate::OnBeforeRedirect(net::URLRequest* request,
const GURL& new_location) {
2015-12-09 19:16:03 +00:00
auto listener_info = event_listener_map_.find(kOnBeforeRedirect);
if (listener_info != event_listener_map_.end()) {
if (!MatchesFilterCondition(request, listener_info->second))
return;
2015-12-03 22:24:26 +00:00
2015-12-09 19:16:03 +00:00
auto wrapped_callback = listener_info->second.callback;
2015-12-01 04:52:22 +00:00
auto details = ExtractRequestInfo(request);
details->SetString("redirectURL", new_location.spec());
2015-12-03 22:24:26 +00:00
details->SetInteger("statusCode", request->GetResponseCode());
auto ip = request->GetSocketAddress().host();
if (!ip.empty())
details->SetString("ip", ip);
details->SetBoolean("fromCache", request->was_cached());
2015-12-04 00:03:56 +00:00
details->Set("responseHeaders",
2015-12-09 19:16:03 +00:00
GetResponseHeadersDict(request->response_headers()).release());
2015-12-01 04:52:22 +00:00
BrowserThread::PostTask(BrowserThread::UI, FROM_HERE,
2015-12-09 19:16:03 +00:00
base::Bind(base::IgnoreResult(&RunListener),
wrapped_callback,
base::Passed(&details)));
} else {
brightray::NetworkDelegate::OnBeforeRedirect(request, new_location);
2015-12-01 04:52:22 +00:00
}
}
void AtomNetworkDelegate::OnResponseStarted(net::URLRequest* request) {
2015-12-09 19:16:03 +00:00
auto listener_info = event_listener_map_.find(kOnResponseStarted);
if (listener_info != event_listener_map_.end()) {
if (request->status().status() != net::URLRequestStatus::SUCCESS)
return;
2015-12-03 22:24:26 +00:00
2015-12-09 19:16:03 +00:00
if (!MatchesFilterCondition(request, listener_info->second))
return;
2015-12-03 22:24:26 +00:00
2015-12-09 19:16:03 +00:00
auto wrapped_callback = listener_info->second.callback;
2015-12-01 04:52:22 +00:00
auto details = ExtractRequestInfo(request);
details->Set("responseHeaders",
2015-12-09 19:16:03 +00:00
GetResponseHeadersDict(request->response_headers()).release());
2015-12-03 22:24:26 +00:00
details->SetBoolean("fromCache", request->was_cached());
2015-12-04 01:54:01 +00:00
auto response_headers = request->response_headers();
2015-12-03 22:24:26 +00:00
details->SetInteger("statusCode",
2015-12-04 01:54:01 +00:00
response_headers ?
response_headers->response_code() : 200);
2015-12-04 00:03:56 +00:00
details->SetString("statusLine",
2015-12-04 01:54:01 +00:00
response_headers ?
response_headers->GetStatusLine() : std::string());
2015-12-01 04:52:22 +00:00
BrowserThread::PostTask(BrowserThread::UI, FROM_HERE,
2015-12-09 19:16:03 +00:00
base::Bind(base::IgnoreResult(&RunListener),
wrapped_callback,
base::Passed(&details)));
} else {
brightray::NetworkDelegate::OnResponseStarted(request);
2015-12-01 04:52:22 +00:00
}
}
void AtomNetworkDelegate::OnCompleted(net::URLRequest* request, bool started) {
2015-12-09 19:16:03 +00:00
auto listener_info = event_listener_map_.find(kOnCompleted);
if (listener_info != event_listener_map_.end()) {
if (request->status().status() == net::URLRequestStatus::FAILED ||
request->status().status() == net::URLRequestStatus::CANCELED) {
OnErrorOccurred(request);
2015-12-03 22:24:26 +00:00
return;
2015-12-09 19:16:03 +00:00
} else {
bool is_redirect = request->response_headers() &&
net::HttpResponseHeaders::IsRedirectResponseCode(
request->response_headers()->response_code());
if (is_redirect)
return;
}
2015-12-03 22:24:26 +00:00
2015-12-09 19:16:03 +00:00
if (!MatchesFilterCondition(request, listener_info->second))
return;
2015-12-03 22:24:26 +00:00
2015-12-09 19:16:03 +00:00
auto wrapped_callback = listener_info->second.callback;
2015-12-03 22:24:26 +00:00
auto details = ExtractRequestInfo(request);
details->Set("responseHeaders",
2015-12-09 19:16:03 +00:00
GetResponseHeadersDict(request->response_headers()).release());
2015-12-03 22:24:26 +00:00
details->SetBoolean("fromCache", request->was_cached());
2015-12-04 01:54:01 +00:00
auto response_headers = request->response_headers();
2015-12-03 22:24:26 +00:00
details->SetInteger("statusCode",
2015-12-04 01:54:01 +00:00
response_headers ?
response_headers->response_code() : 200);
2015-12-04 00:03:56 +00:00
details->SetString("statusLine",
2015-12-04 01:54:01 +00:00
response_headers ?
response_headers->GetStatusLine() : std::string());
2015-12-03 22:24:26 +00:00
BrowserThread::PostTask(BrowserThread::UI, FROM_HERE,
2015-12-09 19:16:03 +00:00
base::Bind(base::IgnoreResult(&RunListener),
wrapped_callback,
base::Passed(&details)));
} else {
brightray::NetworkDelegate::OnCompleted(request, started);
2015-12-03 22:24:26 +00:00
}
}
void AtomNetworkDelegate::OnErrorOccurred(net::URLRequest* request) {
2015-12-09 19:16:03 +00:00
auto listener_info = event_listener_map_.find(kOnErrorOccurred);
if (listener_info != event_listener_map_.end()) {
if (!MatchesFilterCondition(request, listener_info->second))
return;
2015-12-03 22:24:26 +00:00
2015-12-09 19:16:03 +00:00
auto wrapped_callback = listener_info->second.callback;
2015-12-03 22:24:26 +00:00
auto details = ExtractRequestInfo(request);
details->SetBoolean("fromCache", request->was_cached());
details->SetString("error", net::ErrorToString(request->status().error()));
BrowserThread::PostTask(BrowserThread::UI, FROM_HERE,
2015-12-09 19:16:03 +00:00
base::Bind(base::IgnoreResult(&RunListener),
wrapped_callback,
base::Passed(&details)));
2015-12-03 22:24:26 +00:00
}
2015-12-01 04:52:22 +00:00
}
} // namespace atom