electron/atom/browser/net/atom_network_delegate.h

134 lines
4.4 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>
#include <string>
2015-12-01 04:52:22 +00:00
2015-12-22 15:46:25 +00:00
#include "base/callback.h"
#include "base/synchronization/lock.h"
2015-12-01 04:52:22 +00:00
#include "base/values.h"
2016-08-26 22:30:02 +00:00
#include "brightray/browser/network_delegate.h"
#include "content/public/browser/resource_request_info.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>;
const char* ResourceTypeToString(content::ResourceType type);
2015-12-01 04:52:22 +00:00
class AtomNetworkDelegate : public brightray::NetworkDelegate {
public:
using ResponseCallback = base::Callback<void(const base::DictionaryValue&)>;
using SimpleListener = base::Callback<void(const base::DictionaryValue&)>;
using ResponseListener = base::Callback<void(const base::DictionaryValue&,
const ResponseCallback&)>;
2015-12-03 22:24:26 +00:00
enum SimpleEvent {
2015-12-11 13:22:23 +00:00
kOnSendHeaders,
kOnBeforeRedirect,
kOnResponseStarted,
kOnCompleted,
kOnErrorOccurred,
2015-12-03 22:24:26 +00:00
};
enum ResponseEvent {
kOnBeforeRequest,
kOnBeforeSendHeaders,
kOnHeadersReceived,
2015-12-01 04:52:22 +00:00
};
struct SimpleListenerInfo {
URLPatterns url_patterns;
SimpleListener listener;
};
2015-12-03 22:24:26 +00:00
struct ResponseListenerInfo {
URLPatterns url_patterns;
ResponseListener listener;
2015-12-01 04:52:22 +00:00
};
AtomNetworkDelegate();
~AtomNetworkDelegate() override;
void SetSimpleListenerInIO(SimpleEvent type,
const URLPatterns& patterns,
const SimpleListener& callback);
void SetResponseListenerInIO(ResponseEvent type,
const URLPatterns& patterns,
const ResponseListener& callback);
2015-12-01 04:52:22 +00:00
void SetDevToolsNetworkEmulationClientId(const std::string& client_id);
2015-12-01 04:52:22 +00:00
protected:
// 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;
void OnURLRequestDestroyed(net::URLRequest* request) override;
2015-12-11 13:22:23 +00:00
2015-12-03 22:24:26 +00:00
private:
void OnErrorOccurred(net::URLRequest* request, bool started);
2015-12-12 05:49:58 +00:00
template<typename...Args>
void HandleSimpleEvent(SimpleEvent type,
net::URLRequest* request,
Args... args);
template<typename Out, typename... Args>
int HandleResponseEvent(ResponseEvent type,
net::URLRequest* request,
const net::CompletionCallback& callback,
Out out,
Args... args);
2015-12-22 15:46:25 +00:00
// Deal with the results of Listener.
template<typename T>
void OnListenerResultInIO(
2016-05-23 01:59:39 +00:00
uint64_t id, T out, std::unique_ptr<base::DictionaryValue> response);
2015-12-22 15:46:25 +00:00
template<typename T>
void OnListenerResultInUI(
uint64_t id, T out, const base::DictionaryValue& response);
std::map<SimpleEvent, SimpleListenerInfo> simple_listeners_;
std::map<ResponseEvent, ResponseListenerInfo> response_listeners_;
2015-12-22 15:46:25 +00:00
std::map<uint64_t, net::CompletionCallback> callbacks_;
2015-12-01 04:52:22 +00:00
base::Lock lock_;
2016-04-20 00:18:50 +00:00
// Client id for devtools network emulation.
std::string client_id_;
2015-12-01 04:52:22 +00:00
DISALLOW_COPY_AND_ASSIGN(AtomNetworkDelegate);
};
} // namespace atom
#endif // ATOM_BROWSER_NET_ATOM_NETWORK_DELEGATE_H_