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"
|
|
|
|
|
2015-12-11 15:54:32 +00:00
|
|
|
#include <string>
|
|
|
|
|
2015-12-01 04:52:22 +00:00
|
|
|
#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-11 15:54:32 +00:00
|
|
|
void RunSimpleListener(const AtomNetworkDelegate::SimpleListener& listener,
|
|
|
|
scoped_ptr<base::DictionaryValue> details) {
|
|
|
|
return listener.Run(*(details.get()));
|
|
|
|
}
|
|
|
|
|
|
|
|
void RunResponseListener(
|
|
|
|
const AtomNetworkDelegate::ResponseListener& listener,
|
|
|
|
scoped_ptr<base::DictionaryValue> details,
|
|
|
|
const AtomNetworkDelegate::ResponseCallback& callback) {
|
|
|
|
return listener.Run(*(details.get()), callback);
|
2015-12-09 19:16:03 +00:00
|
|
|
}
|
|
|
|
|
2015-12-11 15:54:32 +00:00
|
|
|
// Test whether the URL of |request| matches |patterns|.
|
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 15:54:32 +00:00
|
|
|
// Overloaded by multiple types to fill the |details| object.
|
2015-12-12 05:20:53 +00:00
|
|
|
void ToDictionary(base::DictionaryValue* details, net::URLRequest* request) {
|
2015-12-11 12:40:39 +00:00
|
|
|
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-12 05:20:53 +00:00
|
|
|
void ToDictionary(base::DictionaryValue* details,
|
|
|
|
const net::HttpRequestHeaders& headers) {
|
2015-12-11 12:40:39 +00:00
|
|
|
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-12 05:20:53 +00:00
|
|
|
void ToDictionary(base::DictionaryValue* details,
|
|
|
|
const net::HttpResponseHeaders* headers) {
|
2015-12-11 12:40:39 +00:00
|
|
|
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)) {
|
|
|
|
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());
|
|
|
|
}
|
|
|
|
|
2015-12-12 05:20:53 +00:00
|
|
|
void ToDictionary(base::DictionaryValue* details, const GURL& location) {
|
2015-12-11 12:40:39 +00:00
|
|
|
details->SetString("redirectURL", location.spec());
|
|
|
|
}
|
|
|
|
|
2015-12-12 05:20:53 +00:00
|
|
|
void ToDictionary(base::DictionaryValue* details,
|
|
|
|
const net::HostPortPair& host_port) {
|
2015-12-11 12:40:39 +00:00
|
|
|
if (host_port.host().empty())
|
|
|
|
details->SetString("ip", host_port.host());
|
|
|
|
}
|
|
|
|
|
2015-12-12 05:20:53 +00:00
|
|
|
void ToDictionary(base::DictionaryValue* details, bool from_cache) {
|
2015-12-11 12:40:39 +00:00
|
|
|
details->SetBoolean("fromCache", from_cache);
|
|
|
|
}
|
|
|
|
|
2015-12-12 05:20:53 +00:00
|
|
|
void ToDictionary(base::DictionaryValue* details,
|
|
|
|
const net::URLRequestStatus& status) {
|
2015-12-11 12:40:39 +00:00
|
|
|
details->SetString("error", net::ErrorToString(status.error()));
|
2015-12-01 04:52:22 +00:00
|
|
|
}
|
|
|
|
|
2015-12-12 05:20:53 +00:00
|
|
|
// Helper function to fill |details| with arbitrary |args|.
|
|
|
|
template<typename Arg>
|
|
|
|
void FillDetailsObject(base::DictionaryValue* details, Arg arg) {
|
|
|
|
ToDictionary(details, arg);
|
|
|
|
}
|
|
|
|
|
|
|
|
template<typename Arg, typename... Args>
|
|
|
|
void FillDetailsObject(base::DictionaryValue* details, Arg arg, Args... args) {
|
|
|
|
ToDictionary(details, arg);
|
|
|
|
FillDetailsObject(details, args...);
|
|
|
|
}
|
|
|
|
|
2015-12-11 15:54:32 +00:00
|
|
|
// Fill the native types with the result from the response object.
|
|
|
|
void ReadFromResponseObject(const base::DictionaryValue& response,
|
|
|
|
GURL* new_location) {
|
|
|
|
std::string url;
|
|
|
|
if (response.GetString("redirectURL", &url))
|
|
|
|
*new_location = GURL(url);
|
2015-12-01 04:52:22 +00:00
|
|
|
}
|
|
|
|
|
2015-12-11 15:54:32 +00:00
|
|
|
void ReadFromResponseObject(const base::DictionaryValue& response,
|
|
|
|
net::HttpRequestHeaders* headers) {
|
|
|
|
const base::DictionaryValue* dict;
|
|
|
|
if (response.GetDictionary("requestHeaders", &dict)) {
|
|
|
|
for (base::DictionaryValue::Iterator it(*dict);
|
|
|
|
!it.IsAtEnd();
|
|
|
|
it.Advance()) {
|
|
|
|
std::string value;
|
|
|
|
if (it.value().GetAsString(&value))
|
|
|
|
headers->SetHeader(it.key(), value);
|
|
|
|
}
|
|
|
|
}
|
2015-12-01 04:52:22 +00:00
|
|
|
}
|
|
|
|
|
2015-12-11 15:54:32 +00:00
|
|
|
void ReadFromResponseObject(const base::DictionaryValue& response,
|
|
|
|
scoped_refptr<net::HttpResponseHeaders>* headers) {
|
|
|
|
const base::DictionaryValue* dict;
|
|
|
|
if (response.GetDictionary("responseHeaders", &dict)) {
|
|
|
|
*headers = new net::HttpResponseHeaders("");
|
|
|
|
for (base::DictionaryValue::Iterator it(*dict);
|
|
|
|
!it.IsAtEnd();
|
|
|
|
it.Advance()) {
|
2015-12-12 03:31:19 +00:00
|
|
|
const base::ListValue* list;
|
|
|
|
if (it.value().GetAsList(&list)) {
|
2015-12-11 15:54:32 +00:00
|
|
|
(*headers)->RemoveHeader(it.key());
|
2015-12-12 03:31:19 +00:00
|
|
|
for (size_t i = 0; i < list->GetSize(); ++i) {
|
|
|
|
std::string value;
|
|
|
|
if (list->GetString(i, &value))
|
|
|
|
(*headers)->AddHeader(it.key() + " : " + value);
|
|
|
|
}
|
2015-12-11 15:54:32 +00:00
|
|
|
}
|
2015-12-04 01:54:01 +00:00
|
|
|
}
|
|
|
|
}
|
2015-12-11 15:54:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Deal with the results of Listener.
|
|
|
|
template<typename T>
|
|
|
|
void OnListenerResultInIO(const net::CompletionCallback& callback,
|
|
|
|
T out,
|
|
|
|
scoped_ptr<base::DictionaryValue> response) {
|
|
|
|
ReadFromResponseObject(*response.get(), out);
|
|
|
|
|
|
|
|
bool cancel = false;
|
|
|
|
response->GetBoolean("cancel", &cancel);
|
|
|
|
callback.Run(cancel ? net::ERR_BLOCKED_BY_CLIENT : net::OK);
|
|
|
|
}
|
|
|
|
|
|
|
|
template<typename T>
|
|
|
|
void OnListenerResultInUI(const net::CompletionCallback& callback,
|
|
|
|
T out,
|
|
|
|
const base::DictionaryValue& response) {
|
|
|
|
scoped_ptr<base::DictionaryValue> copy = response.CreateDeepCopy();
|
|
|
|
BrowserThread::PostTask(
|
|
|
|
BrowserThread::IO, FROM_HERE,
|
|
|
|
base::Bind(OnListenerResultInIO<T>, callback, out, base::Passed(©)));
|
2015-12-01 04:52:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace
|
|
|
|
|
|
|
|
AtomNetworkDelegate::AtomNetworkDelegate() {
|
|
|
|
}
|
|
|
|
|
|
|
|
AtomNetworkDelegate::~AtomNetworkDelegate() {
|
|
|
|
}
|
|
|
|
|
2015-12-11 15:54:32 +00:00
|
|
|
void AtomNetworkDelegate::SetSimpleListenerInIO(
|
|
|
|
SimpleEvent type,
|
|
|
|
const URLPatterns& patterns,
|
|
|
|
const SimpleListener& callback) {
|
|
|
|
if (callback.is_null())
|
|
|
|
simple_listeners_.erase(type);
|
|
|
|
else
|
|
|
|
simple_listeners_[type] = { patterns, callback };
|
|
|
|
}
|
2015-12-09 19:16:03 +00:00
|
|
|
|
2015-12-11 15:54:32 +00:00
|
|
|
void AtomNetworkDelegate::SetResponseListenerInIO(
|
|
|
|
ResponseEvent type,
|
|
|
|
const URLPatterns& patterns,
|
|
|
|
const ResponseListener& callback) {
|
|
|
|
if (callback.is_null())
|
|
|
|
response_listeners_.erase(type);
|
|
|
|
else
|
|
|
|
response_listeners_[type] = { patterns, callback };
|
2015-12-01 04:52:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int AtomNetworkDelegate::OnBeforeURLRequest(
|
|
|
|
net::URLRequest* request,
|
|
|
|
const net::CompletionCallback& callback,
|
|
|
|
GURL* new_url) {
|
2015-12-11 15:54:32 +00:00
|
|
|
if (!ContainsKey(response_listeners_, kOnBeforeRequest))
|
2015-12-11 13:02:44 +00:00
|
|
|
return brightray::NetworkDelegate::OnBeforeURLRequest(
|
|
|
|
request, callback, new_url);
|
|
|
|
|
2015-12-11 15:54:32 +00:00
|
|
|
const auto& info = response_listeners_[kOnBeforeRequest];
|
2015-12-11 13:02:44 +00:00
|
|
|
if (!MatchesFilterCondition(request, info.url_patterns))
|
|
|
|
return net::OK;
|
|
|
|
|
|
|
|
scoped_ptr<base::DictionaryValue> details(new base::DictionaryValue);
|
|
|
|
FillDetailsObject(details.get(), request);
|
|
|
|
|
2015-12-11 15:54:32 +00:00
|
|
|
ResponseCallback response =
|
|
|
|
base::Bind(OnListenerResultInUI<GURL*>, callback, new_url);
|
|
|
|
BrowserThread::PostTask(
|
|
|
|
BrowserThread::UI, FROM_HERE,
|
|
|
|
base::Bind(RunResponseListener, info.listener, base::Passed(&details),
|
|
|
|
response));
|
2015-12-11 13:02:44 +00:00
|
|
|
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 15:54:32 +00:00
|
|
|
if (!ContainsKey(response_listeners_, kOnBeforeSendHeaders))
|
2015-12-11 13:02:44 +00:00
|
|
|
return brightray::NetworkDelegate::OnBeforeSendHeaders(
|
|
|
|
request, callback, headers);
|
|
|
|
|
2015-12-11 15:54:32 +00:00
|
|
|
const auto& info = response_listeners_[kOnBeforeSendHeaders];
|
2015-12-11 13:02:44 +00:00
|
|
|
if (!MatchesFilterCondition(request, info.url_patterns))
|
|
|
|
return net::OK;
|
|
|
|
|
|
|
|
scoped_ptr<base::DictionaryValue> details(new base::DictionaryValue);
|
2015-12-12 05:20:53 +00:00
|
|
|
FillDetailsObject(details.get(), request, *headers);
|
2015-12-11 13:02:44 +00:00
|
|
|
|
2015-12-11 15:54:32 +00:00
|
|
|
ResponseCallback response =
|
|
|
|
base::Bind(OnListenerResultInUI<net::HttpRequestHeaders*>,
|
|
|
|
callback, headers);
|
|
|
|
BrowserThread::PostTask(
|
|
|
|
BrowserThread::UI, FROM_HERE,
|
|
|
|
base::Bind(RunResponseListener, info.listener, base::Passed(&details),
|
|
|
|
response));
|
2015-12-11 13:02:44 +00:00
|
|
|
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 15:54:32 +00:00
|
|
|
if (!ContainsKey(simple_listeners_, 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
|
|
|
|
2015-12-11 15:54:32 +00:00
|
|
|
const auto& info = simple_listeners_[kOnSendHeaders];
|
2015-12-11 13:02:44 +00:00
|
|
|
if (!MatchesFilterCondition(request, info.url_patterns))
|
|
|
|
return;
|
|
|
|
|
|
|
|
scoped_ptr<base::DictionaryValue> details(new base::DictionaryValue);
|
2015-12-12 05:20:53 +00:00
|
|
|
FillDetailsObject(details.get(), request, headers);
|
2015-12-11 13:02:44 +00:00
|
|
|
|
2015-12-11 15:54:32 +00:00
|
|
|
BrowserThread::PostTask(
|
|
|
|
BrowserThread::UI, FROM_HERE,
|
|
|
|
base::Bind(&RunSimpleListener, info.listener, 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 15:54:32 +00:00
|
|
|
if (!ContainsKey(response_listeners_, kOnHeadersReceived))
|
2015-12-11 13:02:44 +00:00
|
|
|
return brightray::NetworkDelegate::OnHeadersReceived(
|
|
|
|
request, callback, original_response_headers, override_response_headers,
|
|
|
|
allowed_unsafe_redirect_url);
|
|
|
|
|
2015-12-11 15:54:32 +00:00
|
|
|
const auto& info = response_listeners_[kOnHeadersReceived];
|
2015-12-11 13:02:44 +00:00
|
|
|
if (!MatchesFilterCondition(request, info.url_patterns))
|
|
|
|
return net::OK;
|
|
|
|
|
|
|
|
scoped_ptr<base::DictionaryValue> details(new base::DictionaryValue);
|
2015-12-12 05:20:53 +00:00
|
|
|
FillDetailsObject(details.get(), request, original_response_headers);
|
2015-12-11 13:02:44 +00:00
|
|
|
|
2015-12-11 15:54:32 +00:00
|
|
|
ResponseCallback response =
|
|
|
|
base::Bind(OnListenerResultInUI<scoped_refptr<net::HttpResponseHeaders>*>,
|
|
|
|
callback, override_response_headers);
|
|
|
|
BrowserThread::PostTask(
|
|
|
|
BrowserThread::UI, FROM_HERE,
|
|
|
|
base::Bind(RunResponseListener, info.listener, base::Passed(&details),
|
|
|
|
response));
|
2015-12-11 13:02:44 +00:00
|
|
|
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 15:54:32 +00:00
|
|
|
if (!ContainsKey(simple_listeners_, 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
|
|
|
|
2015-12-11 15:54:32 +00:00
|
|
|
const auto& info = simple_listeners_[kOnBeforeRedirect];
|
2015-12-11 13:02:44 +00:00
|
|
|
if (!MatchesFilterCondition(request, info.url_patterns))
|
|
|
|
return;
|
|
|
|
|
|
|
|
scoped_ptr<base::DictionaryValue> details(new base::DictionaryValue);
|
2015-12-12 05:20:53 +00:00
|
|
|
FillDetailsObject(details.get(), request, new_location,
|
|
|
|
request->response_headers(), request->GetSocketAddress(),
|
|
|
|
request->was_cached());
|
2015-12-11 13:02:44 +00:00
|
|
|
|
2015-12-11 15:54:32 +00:00
|
|
|
BrowserThread::PostTask(
|
|
|
|
BrowserThread::UI, FROM_HERE,
|
|
|
|
base::Bind(&RunSimpleListener, info.listener, base::Passed(&details)));
|
2015-12-01 04:52:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void AtomNetworkDelegate::OnResponseStarted(net::URLRequest* request) {
|
2015-12-11 15:54:32 +00:00
|
|
|
if (!ContainsKey(simple_listeners_, 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;
|
|
|
|
|
2015-12-11 15:54:32 +00:00
|
|
|
const auto& info = simple_listeners_[kOnResponseStarted];
|
2015-12-11 13:02:44 +00:00
|
|
|
if (!MatchesFilterCondition(request, info.url_patterns))
|
|
|
|
return;
|
|
|
|
|
|
|
|
scoped_ptr<base::DictionaryValue> details(new base::DictionaryValue);
|
2015-12-12 05:20:53 +00:00
|
|
|
FillDetailsObject(details.get(), request, request->response_headers(),
|
|
|
|
request->was_cached());
|
2015-12-11 13:02:44 +00:00
|
|
|
|
2015-12-11 15:54:32 +00:00
|
|
|
BrowserThread::PostTask(
|
|
|
|
BrowserThread::UI, FROM_HERE,
|
|
|
|
base::Bind(RunSimpleListener, info.listener, 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.
|
2015-12-11 15:54:32 +00:00
|
|
|
if (ContainsKey(simple_listeners_, 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 15:54:32 +00:00
|
|
|
if (!ContainsKey(simple_listeners_, 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
|
|
|
|
2015-12-11 15:54:32 +00:00
|
|
|
const auto& info = simple_listeners_[kOnCompleted];
|
2015-12-11 13:02:44 +00:00
|
|
|
if (!MatchesFilterCondition(request, info.url_patterns))
|
|
|
|
return;
|
|
|
|
|
|
|
|
scoped_ptr<base::DictionaryValue> details(new base::DictionaryValue);
|
2015-12-12 05:20:53 +00:00
|
|
|
FillDetailsObject(details.get(), request, request->response_headers(),
|
|
|
|
request->was_cached());
|
2015-12-11 13:02:44 +00:00
|
|
|
|
2015-12-11 15:54:32 +00:00
|
|
|
BrowserThread::PostTask(
|
|
|
|
BrowserThread::UI, FROM_HERE,
|
|
|
|
base::Bind(RunSimpleListener, info.listener, base::Passed(&details)));
|
2015-12-03 22:24:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void AtomNetworkDelegate::OnErrorOccurred(net::URLRequest* request) {
|
2015-12-11 15:54:32 +00:00
|
|
|
const auto& info = simple_listeners_[kOnErrorOccurred];
|
2015-12-11 13:02:44 +00:00
|
|
|
if (!MatchesFilterCondition(request, info.url_patterns))
|
|
|
|
return;
|
|
|
|
|
|
|
|
scoped_ptr<base::DictionaryValue> details(new base::DictionaryValue);
|
2015-12-12 05:20:53 +00:00
|
|
|
FillDetailsObject(details.get(), request, request->was_cached(),
|
|
|
|
request->status());
|
2015-12-11 13:02:44 +00:00
|
|
|
|
2015-12-11 15:54:32 +00:00
|
|
|
BrowserThread::PostTask(
|
|
|
|
BrowserThread::UI, FROM_HERE,
|
|
|
|
base::Bind(RunSimpleListener, info.listener, base::Passed(&details)));
|
2015-12-01 04:52:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace atom
|