electron/atom/browser/net/atom_network_delegate.h

103 lines
3 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.
#ifndef ATOM_BROWSER_NET_ATOM_NETWORK_DELEGATE_H_
#define ATOM_BROWSER_NET_ATOM_NETWORK_DELEGATE_H_
#include <map>
2015-12-03 22:24:26 +00:00
#include <set>
2015-12-01 04:52:22 +00:00
#include "brightray/browser/network_delegate.h"
#include "base/callback.h"
#include "base/values.h"
2015-12-03 22:24:26 +00:00
#include "extensions/common/url_pattern.h"
#include "net/base/net_errors.h"
2015-12-01 04:52:22 +00:00
#include "net/http/http_request_headers.h"
#include "net/http/http_response_headers.h"
2015-12-03 22:24:26 +00:00
namespace extensions {
class URLPattern;
}
2015-12-01 04:52:22 +00:00
namespace atom {
2015-12-11 13:02:44 +00:00
using URLPatterns = std::set<extensions::URLPattern>;
2015-12-01 04:52:22 +00:00
class AtomNetworkDelegate : public brightray::NetworkDelegate {
public:
2015-12-03 22:24:26 +00:00
struct BlockingResponse;
using Listener =
2015-12-09 19:16:03 +00:00
base::Callback<BlockingResponse(const base::DictionaryValue&)>;
2015-12-03 22:24:26 +00:00
2015-12-11 12:11:22 +00:00
enum EventType {
2015-12-01 04:52:22 +00:00
kInvalidEvent = 0,
kOnBeforeRequest = 1 << 0,
kOnBeforeSendHeaders = 1 << 1,
kOnSendHeaders = 1 << 2,
kOnHeadersReceived = 1 << 3,
kOnBeforeRedirect = 1 << 4,
kOnResponseStarted = 1 << 5,
2015-12-03 22:24:26 +00:00
kOnCompleted = 1 << 6,
kOnErrorOccurred = 1 << 7,
};
struct ListenerInfo {
2015-12-11 13:02:44 +00:00
URLPatterns url_patterns;
2015-12-03 22:24:26 +00:00
AtomNetworkDelegate::Listener callback;
2015-12-01 04:52:22 +00:00
};
struct BlockingResponse {
2015-12-09 19:16:03 +00:00
BlockingResponse() : cancel(false) {}
2015-12-01 04:52:22 +00:00
~BlockingResponse() {}
2015-12-09 19:16:03 +00:00
int Code() const {
2015-12-03 22:24:26 +00:00
return cancel ? net::ERR_BLOCKED_BY_CLIENT : net::OK;
}
2015-12-01 04:52:22 +00:00
bool cancel;
2015-12-09 19:16:03 +00:00
GURL redirect_url;
net::HttpRequestHeaders request_headers;
scoped_refptr<net::HttpResponseHeaders> response_headers;
2015-12-01 04:52:22 +00:00
};
AtomNetworkDelegate();
~AtomNetworkDelegate() override;
2015-12-11 12:11:22 +00:00
void SetListenerInIO(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);
protected:
2015-12-09 19:16:03 +00:00
void OnErrorOccurred(net::URLRequest* request);
2015-12-01 04:52:22 +00:00
// net::NetworkDelegate:
int OnBeforeURLRequest(net::URLRequest* request,
const net::CompletionCallback& callback,
GURL* new_url) override;
int OnBeforeSendHeaders(net::URLRequest* request,
const net::CompletionCallback& callback,
net::HttpRequestHeaders* headers) override;
void OnSendHeaders(net::URLRequest* request,
const net::HttpRequestHeaders& headers) override;
int 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) override;
void OnBeforeRedirect(net::URLRequest* request,
const GURL& new_location) override;
void OnResponseStarted(net::URLRequest* request) override;
void OnCompleted(net::URLRequest* request, bool started) override;
2015-12-03 22:24:26 +00:00
private:
2015-12-11 12:11:22 +00:00
std::map<EventType, ListenerInfo> event_listener_map_;
2015-12-01 04:52:22 +00:00
DISALLOW_COPY_AND_ASSIGN(AtomNetworkDelegate);
};
} // namespace atom
#endif // ATOM_BROWSER_NET_ATOM_NETWORK_DELEGATE_H_