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>
|
2018-09-13 00:25:56 +00:00
|
|
|
#include <memory>
|
2015-12-03 22:24:26 +00:00
|
|
|
#include <set>
|
2016-04-18 06:05:24 +00:00
|
|
|
#include <string>
|
2018-08-23 09:55:13 +00:00
|
|
|
#include <vector>
|
2015-12-01 04:52:22 +00:00
|
|
|
|
2015-12-22 15:46:25 +00:00
|
|
|
#include "base/callback.h"
|
2016-04-19 04:31:38 +00:00
|
|
|
#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 "content/public/browser/resource_request_info.h"
|
2015-12-03 22:24:26 +00:00
|
|
|
#include "extensions/common/url_pattern.h"
|
2018-08-23 09:55:13 +00:00
|
|
|
#include "net/base/network_delegate.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
|
|
|
class URLPattern;
|
|
|
|
|
2015-12-01 04:52:22 +00:00
|
|
|
namespace atom {
|
|
|
|
|
2017-01-24 08:29:35 +00:00
|
|
|
using URLPatterns = std::set<URLPattern>;
|
2015-12-11 13:02:44 +00:00
|
|
|
|
2016-04-08 18:19:36 +00:00
|
|
|
const char* ResourceTypeToString(content::ResourceType type);
|
|
|
|
|
2018-08-23 09:55:13 +00:00
|
|
|
class LoginHandler;
|
|
|
|
|
|
|
|
class AtomNetworkDelegate : public net::NetworkDelegate {
|
2015-12-01 04:52:22 +00:00
|
|
|
public:
|
2015-12-11 15:54:32 +00:00
|
|
|
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
|
|
|
|
2015-12-11 15:54:32 +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
|
|
|
};
|
|
|
|
|
2015-12-11 15:54:32 +00:00
|
|
|
enum ResponseEvent {
|
|
|
|
kOnBeforeRequest,
|
|
|
|
kOnBeforeSendHeaders,
|
|
|
|
kOnHeadersReceived,
|
2015-12-01 04:52:22 +00:00
|
|
|
};
|
|
|
|
|
2015-12-11 15:54:32 +00:00
|
|
|
struct SimpleListenerInfo {
|
|
|
|
URLPatterns url_patterns;
|
|
|
|
SimpleListener listener;
|
2018-04-17 23:37:22 +00:00
|
|
|
|
|
|
|
SimpleListenerInfo(URLPatterns, SimpleListener);
|
|
|
|
SimpleListenerInfo();
|
|
|
|
~SimpleListenerInfo();
|
2015-12-11 15:54:32 +00:00
|
|
|
};
|
2015-12-03 22:24:26 +00:00
|
|
|
|
2015-12-11 15:54:32 +00:00
|
|
|
struct ResponseListenerInfo {
|
|
|
|
URLPatterns url_patterns;
|
|
|
|
ResponseListener listener;
|
2018-04-17 23:37:22 +00:00
|
|
|
|
|
|
|
ResponseListenerInfo(URLPatterns, ResponseListener);
|
|
|
|
ResponseListenerInfo();
|
|
|
|
~ResponseListenerInfo();
|
2015-12-01 04:52:22 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
AtomNetworkDelegate();
|
|
|
|
~AtomNetworkDelegate() override;
|
|
|
|
|
2015-12-11 15:54:32 +00:00
|
|
|
void SetSimpleListenerInIO(SimpleEvent type,
|
2018-02-26 14:24:00 +00:00
|
|
|
URLPatterns patterns,
|
|
|
|
SimpleListener callback);
|
2015-12-11 15:54:32 +00:00
|
|
|
void SetResponseListenerInIO(ResponseEvent type,
|
2018-02-26 14:24:00 +00:00
|
|
|
URLPatterns patterns,
|
|
|
|
ResponseListener callback);
|
2015-12-01 04:52:22 +00:00
|
|
|
|
|
|
|
protected:
|
|
|
|
// net::NetworkDelegate:
|
|
|
|
int OnBeforeURLRequest(net::URLRequest* request,
|
2018-10-02 21:36:10 +00:00
|
|
|
net::CompletionOnceCallback callback,
|
2015-12-01 04:52:22 +00:00
|
|
|
GURL* new_url) override;
|
2016-09-06 08:24:37 +00:00
|
|
|
int OnBeforeStartTransaction(net::URLRequest* request,
|
2018-10-02 21:36:10 +00:00
|
|
|
net::CompletionOnceCallback callback,
|
2016-09-06 08:24:37 +00:00
|
|
|
net::HttpRequestHeaders* headers) override;
|
2018-08-23 09:55:13 +00:00
|
|
|
void OnBeforeSendHeaders(net::URLRequest* request,
|
|
|
|
const net::ProxyInfo& proxy_info,
|
|
|
|
const net::ProxyRetryInfoMap& proxy_retry_info,
|
|
|
|
net::HttpRequestHeaders* headers) override {}
|
2016-09-06 08:24:37 +00:00
|
|
|
void OnStartTransaction(net::URLRequest* request,
|
|
|
|
const net::HttpRequestHeaders& headers) override;
|
2015-12-01 04:52:22 +00:00
|
|
|
int OnHeadersReceived(
|
|
|
|
net::URLRequest* request,
|
2018-10-02 21:36:10 +00:00
|
|
|
net::CompletionOnceCallback callback,
|
2015-12-01 04:52:22 +00:00
|
|
|
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;
|
2018-04-08 18:17:17 +00:00
|
|
|
void OnResponseStarted(net::URLRequest* request, int net_error) override;
|
2018-08-23 09:55:13 +00:00
|
|
|
void OnNetworkBytesReceived(net::URLRequest* request,
|
|
|
|
int64_t bytes_read) override {}
|
|
|
|
void OnNetworkBytesSent(net::URLRequest* request,
|
|
|
|
int64_t bytes_sent) override {}
|
2018-10-02 22:17:26 +00:00
|
|
|
void OnCompleted(net::URLRequest* request,
|
|
|
|
bool started,
|
|
|
|
int net_error) override;
|
2015-12-22 14:08:33 +00:00
|
|
|
void OnURLRequestDestroyed(net::URLRequest* request) override;
|
2018-08-23 09:55:13 +00:00
|
|
|
void OnPACScriptError(int line_number, const base::string16& error) override {
|
|
|
|
}
|
|
|
|
AuthRequiredResponse OnAuthRequired(
|
|
|
|
net::URLRequest* request,
|
|
|
|
const net::AuthChallengeInfo& auth_info,
|
2018-10-02 22:13:16 +00:00
|
|
|
AuthCallback callback,
|
2018-08-23 09:55:13 +00:00
|
|
|
net::AuthCredentials* credentials) override;
|
|
|
|
bool OnCanGetCookies(const net::URLRequest& request,
|
|
|
|
const net::CookieList& cookie_list) override;
|
|
|
|
bool OnCanSetCookie(const net::URLRequest& request,
|
|
|
|
const net::CanonicalCookie& cookie_line,
|
|
|
|
net::CookieOptions* options) override;
|
|
|
|
bool OnCanAccessFile(const net::URLRequest& request,
|
|
|
|
const base::FilePath& original_path,
|
|
|
|
const base::FilePath& absolute_path) const override;
|
|
|
|
bool OnCanEnablePrivacyMode(
|
|
|
|
const GURL& url,
|
|
|
|
const GURL& first_party_for_cookies) const override;
|
|
|
|
bool OnAreExperimentalCookieFeaturesEnabled() const override;
|
|
|
|
bool OnCancelURLRequestWithPolicyViolatingReferrerHeader(
|
|
|
|
const net::URLRequest& request,
|
|
|
|
const GURL& target_url,
|
|
|
|
const GURL& referrer_url) const override;
|
|
|
|
bool OnCanQueueReportingReport(const url::Origin& origin) const override;
|
|
|
|
void OnCanSendReportingReports(std::set<url::Origin> origins,
|
|
|
|
base::OnceCallback<void(std::set<url::Origin>)>
|
|
|
|
result_callback) const override;
|
|
|
|
bool OnCanSetReportingClient(const url::Origin& origin,
|
|
|
|
const GURL& endpoint) const override;
|
|
|
|
bool OnCanUseReportingClient(const url::Origin& origin,
|
|
|
|
const GURL& endpoint) const override;
|
2015-12-11 13:22:23 +00:00
|
|
|
|
2015-12-03 22:24:26 +00:00
|
|
|
private:
|
2018-10-02 22:17:26 +00:00
|
|
|
void OnErrorOccurred(net::URLRequest* request, bool started, int net_error);
|
2015-12-22 14:08:33 +00:00
|
|
|
|
2018-04-18 01:44:10 +00:00
|
|
|
template <typename... Args>
|
2015-12-12 05:49:58 +00:00
|
|
|
void HandleSimpleEvent(SimpleEvent type,
|
|
|
|
net::URLRequest* request,
|
|
|
|
Args... args);
|
2018-04-18 01:44:10 +00:00
|
|
|
template <typename Out, typename... Args>
|
2015-12-12 05:49:58 +00:00
|
|
|
int HandleResponseEvent(ResponseEvent type,
|
|
|
|
net::URLRequest* request,
|
2018-10-02 21:36:10 +00:00
|
|
|
net::CompletionOnceCallback callback,
|
2015-12-12 05:49:58 +00:00
|
|
|
Out out,
|
|
|
|
Args... args);
|
|
|
|
|
2015-12-22 15:46:25 +00:00
|
|
|
// Deal with the results of Listener.
|
2018-04-18 01:44:10 +00:00
|
|
|
template <typename T>
|
|
|
|
void OnListenerResultInIO(uint64_t id,
|
|
|
|
T out,
|
|
|
|
std::unique_ptr<base::DictionaryValue> response);
|
|
|
|
template <typename T>
|
|
|
|
void OnListenerResultInUI(uint64_t id,
|
|
|
|
T out,
|
|
|
|
const base::DictionaryValue& response);
|
2015-12-22 15:46:25 +00:00
|
|
|
|
2018-08-23 09:55:13 +00:00
|
|
|
std::map<uint64_t, scoped_refptr<LoginHandler>> login_handler_map_;
|
2015-12-11 15:54:32 +00:00
|
|
|
std::map<SimpleEvent, SimpleListenerInfo> simple_listeners_;
|
2015-12-22 14:08:33 +00:00
|
|
|
std::map<ResponseEvent, ResponseListenerInfo> response_listeners_;
|
2018-10-02 21:36:10 +00:00
|
|
|
std::map<uint64_t, net::CompletionOnceCallback> callbacks_;
|
2018-08-23 09:55:13 +00:00
|
|
|
std::vector<std::string> ignore_connections_limit_domains_;
|
2015-12-01 04:52:22 +00:00
|
|
|
|
|
|
|
DISALLOW_COPY_AND_ASSIGN(AtomNetworkDelegate);
|
|
|
|
};
|
|
|
|
|
2018-04-18 01:44:10 +00:00
|
|
|
} // namespace atom
|
2015-12-01 04:52:22 +00:00
|
|
|
|
|
|
|
#endif // ATOM_BROWSER_NET_ATOM_NETWORK_DELEGATE_H_
|