feat: migrate webRequest module to NetworkService (Part 6) (#19752)
* Implement OnBeforeSendHeaders * Pass the request * Handle simple listeners * Handle response listeners * Read responses from listener
This commit is contained in:
parent
e59095423e
commit
c3bb73a711
6 changed files with 297 additions and 64 deletions
|
@ -8,12 +8,16 @@
|
||||||
#include <string>
|
#include <string>
|
||||||
#include <utility>
|
#include <utility>
|
||||||
|
|
||||||
|
#include "base/stl_util.h"
|
||||||
#include "gin/converter.h"
|
#include "gin/converter.h"
|
||||||
#include "gin/dictionary.h"
|
#include "gin/dictionary.h"
|
||||||
#include "gin/object_template_builder.h"
|
#include "gin/object_template_builder.h"
|
||||||
#include "shell/browser/api/atom_api_session.h"
|
#include "shell/browser/api/atom_api_session.h"
|
||||||
|
#include "shell/browser/api/atom_api_web_contents.h"
|
||||||
#include "shell/browser/atom_browser_context.h"
|
#include "shell/browser/atom_browser_context.h"
|
||||||
#include "shell/common/gin_converters/callback_converter_gin_adapter.h"
|
#include "shell/common/gin_converters/callback_converter_gin_adapter.h"
|
||||||
|
#include "shell/common/gin_converters/gurl_converter.h"
|
||||||
|
#include "shell/common/gin_converters/net_converter.h"
|
||||||
#include "shell/common/gin_converters/std_converter.h"
|
#include "shell/common/gin_converters/std_converter.h"
|
||||||
#include "shell/common/gin_converters/value_converter_gin_adapter.h"
|
#include "shell/common/gin_converters/value_converter_gin_adapter.h"
|
||||||
|
|
||||||
|
@ -32,6 +36,40 @@ struct Converter<URLPattern> {
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
template <>
|
||||||
|
struct Converter<content::ResourceType> {
|
||||||
|
static v8::Local<v8::Value> ToV8(v8::Isolate* isolate,
|
||||||
|
content::ResourceType type) {
|
||||||
|
const char* result;
|
||||||
|
switch (type) {
|
||||||
|
case content::ResourceType::kMainFrame:
|
||||||
|
result = "mainFrame";
|
||||||
|
break;
|
||||||
|
case content::ResourceType::kSubFrame:
|
||||||
|
result = "subFrame";
|
||||||
|
break;
|
||||||
|
case content::ResourceType::kStylesheet:
|
||||||
|
result = "stylesheet";
|
||||||
|
break;
|
||||||
|
case content::ResourceType::kScript:
|
||||||
|
result = "script";
|
||||||
|
break;
|
||||||
|
case content::ResourceType::kImage:
|
||||||
|
result = "image";
|
||||||
|
break;
|
||||||
|
case content::ResourceType::kObject:
|
||||||
|
result = "object";
|
||||||
|
break;
|
||||||
|
case content::ResourceType::kXhr:
|
||||||
|
result = "xhr";
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
result = "other";
|
||||||
|
}
|
||||||
|
return StringToV8(isolate, result);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
} // namespace gin
|
} // namespace gin
|
||||||
|
|
||||||
namespace electron {
|
namespace electron {
|
||||||
|
@ -49,18 +87,106 @@ struct UserData : public base::SupportsUserData::Data {
|
||||||
};
|
};
|
||||||
|
|
||||||
// Test whether the URL of |request| matches |patterns|.
|
// Test whether the URL of |request| matches |patterns|.
|
||||||
bool MatchesFilterCondition(extensions::WebRequestInfo* request,
|
bool MatchesFilterCondition(extensions::WebRequestInfo* info,
|
||||||
const std::set<URLPattern>& patterns) {
|
const std::set<URLPattern>& patterns) {
|
||||||
if (patterns.empty())
|
if (patterns.empty())
|
||||||
return true;
|
return true;
|
||||||
|
|
||||||
for (const auto& pattern : patterns) {
|
for (const auto& pattern : patterns) {
|
||||||
if (pattern.MatchesURL(request->url))
|
if (pattern.MatchesURL(info->url))
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Overloaded by multiple types to fill the |details| object.
|
||||||
|
void ToDictionary(gin::Dictionary* details, extensions::WebRequestInfo* info) {
|
||||||
|
details->Set("id", info->id);
|
||||||
|
details->Set("url", info->url);
|
||||||
|
details->Set("method", info->method);
|
||||||
|
details->Set("timestamp", base::Time::Now().ToDoubleT() * 1000);
|
||||||
|
if (!info->response_ip.empty())
|
||||||
|
details->Set("ip", info->response_ip);
|
||||||
|
if (info->type.has_value())
|
||||||
|
details->Set("resourceType", info->type.value());
|
||||||
|
else
|
||||||
|
details->Set("resourceType", base::StringPiece("other"));
|
||||||
|
if (info->response_headers) {
|
||||||
|
details->Set("fromCache", info->response_from_cache);
|
||||||
|
details->Set("responseHeaders", info->response_headers.get());
|
||||||
|
details->Set("statusLine", info->response_headers->GetStatusLine());
|
||||||
|
details->Set("statusCode", info->response_headers->response_code());
|
||||||
|
}
|
||||||
|
|
||||||
|
auto* web_contents = content::WebContents::FromRenderFrameHost(
|
||||||
|
content::RenderFrameHost::FromID(info->render_process_id,
|
||||||
|
info->frame_id));
|
||||||
|
int32_t id = api::WebContents::GetIDFromWrappedClass(web_contents);
|
||||||
|
// id must be greater than zero.
|
||||||
|
if (id > 0)
|
||||||
|
details->Set("webContentsId", id);
|
||||||
|
}
|
||||||
|
|
||||||
|
void ToDictionary(gin::Dictionary* details,
|
||||||
|
const network::ResourceRequest& request) {
|
||||||
|
details->Set("referrer", request.referrer);
|
||||||
|
}
|
||||||
|
|
||||||
|
void ToDictionary(gin::Dictionary* details,
|
||||||
|
const net::HttpRequestHeaders& headers) {
|
||||||
|
details->Set("requestHeaders", headers);
|
||||||
|
}
|
||||||
|
|
||||||
|
void ToDictionary(gin::Dictionary* details, const GURL& location) {
|
||||||
|
details->Set("redirectURL", location);
|
||||||
|
}
|
||||||
|
|
||||||
|
void ToDictionary(gin::Dictionary* details, int net_error) {
|
||||||
|
details->Set("error", net::ErrorToString(net_error));
|
||||||
|
}
|
||||||
|
|
||||||
|
// Helper function to fill |details| with arbitrary |args|.
|
||||||
|
template <typename Arg>
|
||||||
|
void FillDetails(gin::Dictionary* details, Arg arg) {
|
||||||
|
ToDictionary(details, arg);
|
||||||
|
}
|
||||||
|
|
||||||
|
template <typename Arg, typename... Args>
|
||||||
|
void FillDetails(gin::Dictionary* details, Arg arg, Args... args) {
|
||||||
|
ToDictionary(details, arg);
|
||||||
|
FillDetails(details, args...);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Fill the native types with the result from the response object.
|
||||||
|
void ReadFromResponse(v8::Isolate* isolate,
|
||||||
|
gin::Dictionary* response,
|
||||||
|
GURL* new_location) {
|
||||||
|
response->Get("redirectURL", new_location);
|
||||||
|
}
|
||||||
|
|
||||||
|
void ReadFromResponse(v8::Isolate* isolate,
|
||||||
|
gin::Dictionary* response,
|
||||||
|
net::HttpRequestHeaders* headers) {
|
||||||
|
headers->Clear();
|
||||||
|
gin::ConvertFromV8(isolate, gin::ConvertToV8(isolate, *response), headers);
|
||||||
|
}
|
||||||
|
|
||||||
|
void ReadFromResponse(v8::Isolate* isolate,
|
||||||
|
gin::Dictionary* response,
|
||||||
|
const std::pair<scoped_refptr<net::HttpResponseHeaders>*,
|
||||||
|
const std::string>& headers) {
|
||||||
|
std::string status_line;
|
||||||
|
if (!response->Get("statusLine", &status_line))
|
||||||
|
status_line = headers.second;
|
||||||
|
v8::Local<v8::Value> value;
|
||||||
|
if (response->Get("responseHeaders", &value)) {
|
||||||
|
*headers.first = new net::HttpResponseHeaders("");
|
||||||
|
(*headers.first)->ReplaceStatusLine(status_line);
|
||||||
|
gin::Converter<net::HttpResponseHeaders*>::FromV8(isolate, value,
|
||||||
|
(*headers.first).get());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
} // namespace
|
} // namespace
|
||||||
|
|
||||||
gin::WrapperInfo WebRequestNS::kWrapperInfo = {gin::kEmbedderNativeGin};
|
gin::WrapperInfo WebRequestNS::kWrapperInfo = {gin::kEmbedderNativeGin};
|
||||||
|
@ -113,54 +239,70 @@ const char* WebRequestNS::GetTypeName() {
|
||||||
return "WebRequest";
|
return "WebRequest";
|
||||||
}
|
}
|
||||||
|
|
||||||
int WebRequestNS::OnBeforeRequest(extensions::WebRequestInfo* request,
|
int WebRequestNS::OnBeforeRequest(extensions::WebRequestInfo* info,
|
||||||
|
const network::ResourceRequest& request,
|
||||||
net::CompletionOnceCallback callback,
|
net::CompletionOnceCallback callback,
|
||||||
GURL* new_url) {
|
GURL* new_url) {
|
||||||
return HandleResponseEvent(kOnBeforeRequest, request, std::move(callback),
|
return HandleResponseEvent(kOnBeforeRequest, info, std::move(callback),
|
||||||
new_url);
|
new_url, request);
|
||||||
}
|
}
|
||||||
|
|
||||||
int WebRequestNS::OnBeforeSendHeaders(extensions::WebRequestInfo* request,
|
int WebRequestNS::OnBeforeSendHeaders(extensions::WebRequestInfo* info,
|
||||||
|
const network::ResourceRequest& request,
|
||||||
BeforeSendHeadersCallback callback,
|
BeforeSendHeadersCallback callback,
|
||||||
net::HttpRequestHeaders* headers) {
|
net::HttpRequestHeaders* headers) {
|
||||||
// TODO(zcbenz): Figure out how to handle this generally.
|
return HandleResponseEvent(
|
||||||
return net::OK;
|
kOnBeforeSendHeaders, info,
|
||||||
|
base::BindOnce(std::move(callback), std::set<std::string>(),
|
||||||
|
std::set<std::string>()),
|
||||||
|
headers, request);
|
||||||
}
|
}
|
||||||
|
|
||||||
int WebRequestNS::OnHeadersReceived(
|
int WebRequestNS::OnHeadersReceived(
|
||||||
extensions::WebRequestInfo* request,
|
extensions::WebRequestInfo* info,
|
||||||
|
const network::ResourceRequest& request,
|
||||||
net::CompletionOnceCallback callback,
|
net::CompletionOnceCallback callback,
|
||||||
const net::HttpResponseHeaders* original_response_headers,
|
const net::HttpResponseHeaders* original_response_headers,
|
||||||
scoped_refptr<net::HttpResponseHeaders>* override_response_headers,
|
scoped_refptr<net::HttpResponseHeaders>* override_response_headers,
|
||||||
GURL* allowed_unsafe_redirect_url) {
|
GURL* allowed_unsafe_redirect_url) {
|
||||||
return HandleResponseEvent(kOnHeadersReceived, request, std::move(callback),
|
return HandleResponseEvent(
|
||||||
original_response_headers,
|
kOnHeadersReceived, info, std::move(callback),
|
||||||
override_response_headers,
|
std::make_pair(override_response_headers,
|
||||||
allowed_unsafe_redirect_url);
|
original_response_headers->GetStatusLine()),
|
||||||
|
request);
|
||||||
}
|
}
|
||||||
|
|
||||||
void WebRequestNS::OnSendHeaders(extensions::WebRequestInfo* request,
|
void WebRequestNS::OnSendHeaders(extensions::WebRequestInfo* info,
|
||||||
|
const network::ResourceRequest& request,
|
||||||
const net::HttpRequestHeaders& headers) {
|
const net::HttpRequestHeaders& headers) {
|
||||||
HandleSimpleEvent(kOnSendHeaders, request, headers);
|
HandleSimpleEvent(kOnSendHeaders, info, request, headers);
|
||||||
}
|
}
|
||||||
|
|
||||||
void WebRequestNS::OnBeforeRedirect(extensions::WebRequestInfo* request,
|
void WebRequestNS::OnBeforeRedirect(extensions::WebRequestInfo* info,
|
||||||
|
const network::ResourceRequest& request,
|
||||||
const GURL& new_location) {
|
const GURL& new_location) {
|
||||||
HandleSimpleEvent(kOnBeforeRedirect, request, new_location);
|
HandleSimpleEvent(kOnBeforeRedirect, info, request, new_location);
|
||||||
}
|
}
|
||||||
|
|
||||||
void WebRequestNS::OnResponseStarted(extensions::WebRequestInfo* request) {
|
void WebRequestNS::OnResponseStarted(extensions::WebRequestInfo* info,
|
||||||
HandleSimpleEvent(kOnResponseStarted, request);
|
const network::ResourceRequest& request) {
|
||||||
|
HandleSimpleEvent(kOnResponseStarted, info, request);
|
||||||
}
|
}
|
||||||
|
|
||||||
void WebRequestNS::OnErrorOccurred(extensions::WebRequestInfo* request,
|
void WebRequestNS::OnErrorOccurred(extensions::WebRequestInfo* info,
|
||||||
|
const network::ResourceRequest& request,
|
||||||
int net_error) {
|
int net_error) {
|
||||||
HandleSimpleEvent(kOnErrorOccurred, request, net_error);
|
callbacks_.erase(info->id);
|
||||||
|
|
||||||
|
HandleSimpleEvent(kOnErrorOccurred, info, request, net_error);
|
||||||
}
|
}
|
||||||
|
|
||||||
void WebRequestNS::OnCompleted(extensions::WebRequestInfo* request,
|
void WebRequestNS::OnCompleted(extensions::WebRequestInfo* info,
|
||||||
|
const network::ResourceRequest& request,
|
||||||
int net_error) {
|
int net_error) {
|
||||||
HandleSimpleEvent(kOnCompleted, request, net_error);
|
callbacks_.erase(info->id);
|
||||||
|
|
||||||
|
HandleSimpleEvent(kOnCompleted, info, request, net_error);
|
||||||
}
|
}
|
||||||
|
|
||||||
template <WebRequestNS::SimpleEvent event>
|
template <WebRequestNS::SimpleEvent event>
|
||||||
|
@ -199,27 +341,64 @@ void WebRequestNS::SetListener(Event event,
|
||||||
|
|
||||||
template <typename... Args>
|
template <typename... Args>
|
||||||
void WebRequestNS::HandleSimpleEvent(SimpleEvent event,
|
void WebRequestNS::HandleSimpleEvent(SimpleEvent event,
|
||||||
extensions::WebRequestInfo* request,
|
extensions::WebRequestInfo* request_info,
|
||||||
Args... args) {
|
Args... args) {
|
||||||
const auto& info = simple_listeners_[event];
|
const auto& info = simple_listeners_[event];
|
||||||
if (!MatchesFilterCondition(request, info.url_patterns))
|
if (!MatchesFilterCondition(request_info, info.url_patterns))
|
||||||
return;
|
return;
|
||||||
|
|
||||||
// TODO(zcbenz): Invoke the listener.
|
v8::Isolate* isolate = v8::Isolate::GetCurrent();
|
||||||
|
v8::HandleScope handle_scope(isolate);
|
||||||
|
gin::Dictionary details(isolate, v8::Object::New(isolate));
|
||||||
|
FillDetails(&details, request_info, args...);
|
||||||
|
info.listener.Run(gin::ConvertToV8(isolate, details));
|
||||||
}
|
}
|
||||||
|
|
||||||
template <typename Out, typename... Args>
|
template <typename Out, typename... Args>
|
||||||
int WebRequestNS::HandleResponseEvent(ResponseEvent event,
|
int WebRequestNS::HandleResponseEvent(ResponseEvent event,
|
||||||
extensions::WebRequestInfo* request,
|
extensions::WebRequestInfo* request_info,
|
||||||
net::CompletionOnceCallback callback,
|
net::CompletionOnceCallback callback,
|
||||||
Out out,
|
Out out,
|
||||||
Args... args) {
|
Args... args) {
|
||||||
const auto& info = response_listeners_[event];
|
const auto& info = response_listeners_[event];
|
||||||
if (!MatchesFilterCondition(request, info.url_patterns))
|
if (!MatchesFilterCondition(request_info, info.url_patterns))
|
||||||
return net::OK;
|
return net::OK;
|
||||||
|
|
||||||
// TODO(zcbenz): Invoke the listener.
|
callbacks_[request_info->id] = std::move(callback);
|
||||||
return net::OK;
|
|
||||||
|
v8::Isolate* isolate = v8::Isolate::GetCurrent();
|
||||||
|
v8::HandleScope handle_scope(isolate);
|
||||||
|
gin::Dictionary details(isolate, v8::Object::New(isolate));
|
||||||
|
FillDetails(&details, request_info, args...);
|
||||||
|
|
||||||
|
ResponseCallback response =
|
||||||
|
base::BindOnce(&WebRequestNS::OnListenerResult<Out>,
|
||||||
|
base::Unretained(this), request_info->id, out);
|
||||||
|
info.listener.Run(gin::ConvertToV8(isolate, details), std::move(response));
|
||||||
|
return net::ERR_IO_PENDING;
|
||||||
|
}
|
||||||
|
|
||||||
|
template <typename T>
|
||||||
|
void WebRequestNS::OnListenerResult(uint64_t id,
|
||||||
|
T out,
|
||||||
|
v8::Local<v8::Value> response) {
|
||||||
|
if (!base::Contains(callbacks_, id))
|
||||||
|
return;
|
||||||
|
|
||||||
|
int result = net::OK;
|
||||||
|
if (response->IsObject()) {
|
||||||
|
v8::Isolate* isolate = v8::Isolate::GetCurrent();
|
||||||
|
gin::Dictionary dict(isolate, response.As<v8::Object>());
|
||||||
|
|
||||||
|
bool cancel = false;
|
||||||
|
dict.Get("cancel", &cancel);
|
||||||
|
if (cancel)
|
||||||
|
result = net::ERR_BLOCKED_BY_CLIENT;
|
||||||
|
else
|
||||||
|
ReadFromResponse(isolate, &dict, out);
|
||||||
|
}
|
||||||
|
|
||||||
|
std::move(callbacks_[id]).Run(result);
|
||||||
}
|
}
|
||||||
|
|
||||||
// static
|
// static
|
||||||
|
|
|
@ -57,26 +57,35 @@ class WebRequestNS : public gin::Wrappable<WebRequestNS>, public WebRequestAPI {
|
||||||
~WebRequestNS() override;
|
~WebRequestNS() override;
|
||||||
|
|
||||||
// WebRequestAPI:
|
// WebRequestAPI:
|
||||||
int OnBeforeRequest(extensions::WebRequestInfo* request,
|
int OnBeforeRequest(extensions::WebRequestInfo* info,
|
||||||
|
const network::ResourceRequest& request,
|
||||||
net::CompletionOnceCallback callback,
|
net::CompletionOnceCallback callback,
|
||||||
GURL* new_url) override;
|
GURL* new_url) override;
|
||||||
int OnBeforeSendHeaders(extensions::WebRequestInfo* request,
|
int OnBeforeSendHeaders(extensions::WebRequestInfo* info,
|
||||||
|
const network::ResourceRequest& request,
|
||||||
BeforeSendHeadersCallback callback,
|
BeforeSendHeadersCallback callback,
|
||||||
net::HttpRequestHeaders* headers) override;
|
net::HttpRequestHeaders* headers) override;
|
||||||
int OnHeadersReceived(
|
int OnHeadersReceived(
|
||||||
extensions::WebRequestInfo* request,
|
extensions::WebRequestInfo* info,
|
||||||
|
const network::ResourceRequest& request,
|
||||||
net::CompletionOnceCallback callback,
|
net::CompletionOnceCallback callback,
|
||||||
const net::HttpResponseHeaders* original_response_headers,
|
const net::HttpResponseHeaders* original_response_headers,
|
||||||
scoped_refptr<net::HttpResponseHeaders>* override_response_headers,
|
scoped_refptr<net::HttpResponseHeaders>* override_response_headers,
|
||||||
GURL* allowed_unsafe_redirect_url) override;
|
GURL* allowed_unsafe_redirect_url) override;
|
||||||
void OnSendHeaders(extensions::WebRequestInfo* request,
|
void OnSendHeaders(extensions::WebRequestInfo* info,
|
||||||
|
const network::ResourceRequest& request,
|
||||||
const net::HttpRequestHeaders& headers) override;
|
const net::HttpRequestHeaders& headers) override;
|
||||||
void OnBeforeRedirect(extensions::WebRequestInfo* request,
|
void OnBeforeRedirect(extensions::WebRequestInfo* info,
|
||||||
|
const network::ResourceRequest& request,
|
||||||
const GURL& new_location) override;
|
const GURL& new_location) override;
|
||||||
void OnResponseStarted(extensions::WebRequestInfo* request) override;
|
void OnResponseStarted(extensions::WebRequestInfo* info,
|
||||||
void OnErrorOccurred(extensions::WebRequestInfo* request,
|
const network::ResourceRequest& request) override;
|
||||||
|
void OnErrorOccurred(extensions::WebRequestInfo* info,
|
||||||
|
const network::ResourceRequest& request,
|
||||||
int net_error) override;
|
int net_error) override;
|
||||||
void OnCompleted(extensions::WebRequestInfo* request, int net_error) override;
|
void OnCompleted(extensions::WebRequestInfo* info,
|
||||||
|
const network::ResourceRequest& request,
|
||||||
|
int net_error) override;
|
||||||
|
|
||||||
enum SimpleEvent {
|
enum SimpleEvent {
|
||||||
kOnSendHeaders,
|
kOnSendHeaders,
|
||||||
|
@ -91,10 +100,10 @@ class WebRequestNS : public gin::Wrappable<WebRequestNS>, public WebRequestAPI {
|
||||||
kOnHeadersReceived,
|
kOnHeadersReceived,
|
||||||
};
|
};
|
||||||
|
|
||||||
using SimpleListener = base::RepeatingCallback<void(const base::Value&)>;
|
using SimpleListener = base::RepeatingCallback<void(v8::Local<v8::Value>)>;
|
||||||
using ResponseCallback = base::OnceCallback<void(const base::Value&)>;
|
using ResponseCallback = base::OnceCallback<void(v8::Local<v8::Value>)>;
|
||||||
using ResponseListener =
|
using ResponseListener =
|
||||||
base::RepeatingCallback<void(const base::Value&, ResponseCallback)>;
|
base::RepeatingCallback<void(v8::Local<v8::Value>, ResponseCallback)>;
|
||||||
|
|
||||||
template <SimpleEvent event>
|
template <SimpleEvent event>
|
||||||
void SetSimpleListener(gin::Arguments* args);
|
void SetSimpleListener(gin::Arguments* args);
|
||||||
|
@ -105,15 +114,18 @@ class WebRequestNS : public gin::Wrappable<WebRequestNS>, public WebRequestAPI {
|
||||||
|
|
||||||
template <typename... Args>
|
template <typename... Args>
|
||||||
void HandleSimpleEvent(SimpleEvent event,
|
void HandleSimpleEvent(SimpleEvent event,
|
||||||
extensions::WebRequestInfo* request,
|
extensions::WebRequestInfo* info,
|
||||||
Args... args);
|
Args... args);
|
||||||
template <typename Out, typename... Args>
|
template <typename Out, typename... Args>
|
||||||
int HandleResponseEvent(ResponseEvent event,
|
int HandleResponseEvent(ResponseEvent event,
|
||||||
extensions::WebRequestInfo* request,
|
extensions::WebRequestInfo* info,
|
||||||
net::CompletionOnceCallback callback,
|
net::CompletionOnceCallback callback,
|
||||||
Out out,
|
Out out,
|
||||||
Args... args);
|
Args... args);
|
||||||
|
|
||||||
|
template <typename T>
|
||||||
|
void OnListenerResult(uint64_t id, T out, v8::Local<v8::Value> response);
|
||||||
|
|
||||||
struct SimpleListenerInfo {
|
struct SimpleListenerInfo {
|
||||||
std::set<URLPattern> url_patterns;
|
std::set<URLPattern> url_patterns;
|
||||||
SimpleListener listener;
|
SimpleListener listener;
|
||||||
|
|
|
@ -105,7 +105,7 @@ void ProxyingURLLoaderFactory::InProgressRequest::RestartInternal() {
|
||||||
}
|
}
|
||||||
redirect_url_ = GURL();
|
redirect_url_ = GURL();
|
||||||
int result = factory_->web_request_api()->OnBeforeRequest(
|
int result = factory_->web_request_api()->OnBeforeRequest(
|
||||||
&info_.value(), continuation, &redirect_url_);
|
&info_.value(), request_, continuation, &redirect_url_);
|
||||||
if (result == net::ERR_BLOCKED_BY_CLIENT) {
|
if (result == net::ERR_BLOCKED_BY_CLIENT) {
|
||||||
// The request was cancelled synchronously. Dispatch an error notification
|
// The request was cancelled synchronously. Dispatch an error notification
|
||||||
// and terminate the request.
|
// and terminate the request.
|
||||||
|
@ -259,7 +259,8 @@ void ProxyingURLLoaderFactory::InProgressRequest::OnComplete(
|
||||||
}
|
}
|
||||||
|
|
||||||
target_client_->OnComplete(status);
|
target_client_->OnComplete(status);
|
||||||
factory_->web_request_api()->OnCompleted(&info_.value(), status.error_code);
|
factory_->web_request_api()->OnCompleted(&info_.value(), request_,
|
||||||
|
status.error_code);
|
||||||
|
|
||||||
// Deletes |this|.
|
// Deletes |this|.
|
||||||
factory_->RemoveRequest(network_service_request_id_, request_id_);
|
factory_->RemoveRequest(network_service_request_id_, request_id_);
|
||||||
|
@ -318,7 +319,7 @@ void ProxyingURLLoaderFactory::InProgressRequest::ContinueToBeforeSendHeaders(
|
||||||
&InProgressRequest::ContinueToSendHeaders, weak_factory_.GetWeakPtr());
|
&InProgressRequest::ContinueToSendHeaders, weak_factory_.GetWeakPtr());
|
||||||
// Note: In Electron onBeforeSendHeaders is called for all protocols.
|
// Note: In Electron onBeforeSendHeaders is called for all protocols.
|
||||||
int result = factory_->web_request_api()->OnBeforeSendHeaders(
|
int result = factory_->web_request_api()->OnBeforeSendHeaders(
|
||||||
&info_.value(), continuation, &request_.headers);
|
&info_.value(), request_, continuation, &request_.headers);
|
||||||
|
|
||||||
if (result == net::ERR_BLOCKED_BY_CLIENT) {
|
if (result == net::ERR_BLOCKED_BY_CLIENT) {
|
||||||
// The request was cancelled synchronously. Dispatch an error notification
|
// The request was cancelled synchronously. Dispatch an error notification
|
||||||
|
@ -385,7 +386,8 @@ void ProxyingURLLoaderFactory::InProgressRequest::ContinueToSendHeaders(
|
||||||
proxied_client_binding_.ResumeIncomingMethodCallProcessing();
|
proxied_client_binding_.ResumeIncomingMethodCallProcessing();
|
||||||
|
|
||||||
// Note: In Electron onSendHeaders is called for all protocols.
|
// Note: In Electron onSendHeaders is called for all protocols.
|
||||||
factory_->web_request_api()->OnSendHeaders(&info_.value(), request_.headers);
|
factory_->web_request_api()->OnSendHeaders(&info_.value(), request_,
|
||||||
|
request_.headers);
|
||||||
|
|
||||||
if (!current_request_uses_header_client_)
|
if (!current_request_uses_header_client_)
|
||||||
ContinueToStartRequest(net::OK);
|
ContinueToStartRequest(net::OK);
|
||||||
|
@ -498,7 +500,7 @@ void ProxyingURLLoaderFactory::InProgressRequest::ContinueToResponseStarted(
|
||||||
|
|
||||||
proxied_client_binding_.ResumeIncomingMethodCallProcessing();
|
proxied_client_binding_.ResumeIncomingMethodCallProcessing();
|
||||||
|
|
||||||
factory_->web_request_api()->OnResponseStarted(&info_.value());
|
factory_->web_request_api()->OnResponseStarted(&info_.value(), request_);
|
||||||
target_client_->OnReceiveResponse(current_response_);
|
target_client_->OnReceiveResponse(current_response_);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -515,7 +517,7 @@ void ProxyingURLLoaderFactory::InProgressRequest::ContinueToBeforeRedirect(
|
||||||
if (proxied_client_binding_.is_bound())
|
if (proxied_client_binding_.is_bound())
|
||||||
proxied_client_binding_.ResumeIncomingMethodCallProcessing();
|
proxied_client_binding_.ResumeIncomingMethodCallProcessing();
|
||||||
|
|
||||||
factory_->web_request_api()->OnBeforeRedirect(&info_.value(),
|
factory_->web_request_api()->OnBeforeRedirect(&info_.value(), request_,
|
||||||
redirect_info.new_url);
|
redirect_info.new_url);
|
||||||
target_client_->OnReceiveRedirect(redirect_info, current_response_);
|
target_client_->OnReceiveRedirect(redirect_info, current_response_);
|
||||||
request_.url = redirect_info.new_url;
|
request_.url = redirect_info.new_url;
|
||||||
|
@ -610,8 +612,8 @@ void ProxyingURLLoaderFactory::InProgressRequest::
|
||||||
base::AdaptCallbackForRepeating(std::move(continuation));
|
base::AdaptCallbackForRepeating(std::move(continuation));
|
||||||
DCHECK(info_.has_value());
|
DCHECK(info_.has_value());
|
||||||
int result = factory_->web_request_api()->OnHeadersReceived(
|
int result = factory_->web_request_api()->OnHeadersReceived(
|
||||||
&info_.value(), copyable_callback, current_response_.headers.get(),
|
&info_.value(), request_, copyable_callback,
|
||||||
&override_headers_, &redirect_url_);
|
current_response_.headers.get(), &override_headers_, &redirect_url_);
|
||||||
if (result == net::ERR_BLOCKED_BY_CLIENT) {
|
if (result == net::ERR_BLOCKED_BY_CLIENT) {
|
||||||
OnRequestError(network::URLLoaderCompletionStatus(result));
|
OnRequestError(network::URLLoaderCompletionStatus(result));
|
||||||
return;
|
return;
|
||||||
|
@ -636,7 +638,7 @@ void ProxyingURLLoaderFactory::InProgressRequest::OnRequestError(
|
||||||
const network::URLLoaderCompletionStatus& status) {
|
const network::URLLoaderCompletionStatus& status) {
|
||||||
if (!request_completed_) {
|
if (!request_completed_) {
|
||||||
target_client_->OnComplete(status);
|
target_client_->OnComplete(status);
|
||||||
factory_->web_request_api()->OnErrorOccurred(&info_.value(),
|
factory_->web_request_api()->OnErrorOccurred(&info_.value(), request_,
|
||||||
status.error_code);
|
status.error_code);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -31,26 +31,34 @@ class WebRequestAPI {
|
||||||
const std::set<std::string>& set_headers,
|
const std::set<std::string>& set_headers,
|
||||||
int error_code)>;
|
int error_code)>;
|
||||||
|
|
||||||
virtual int OnBeforeRequest(extensions::WebRequestInfo* request,
|
virtual int OnBeforeRequest(extensions::WebRequestInfo* info,
|
||||||
|
const network::ResourceRequest& request,
|
||||||
net::CompletionOnceCallback callback,
|
net::CompletionOnceCallback callback,
|
||||||
GURL* new_url) = 0;
|
GURL* new_url) = 0;
|
||||||
virtual int OnBeforeSendHeaders(extensions::WebRequestInfo* request,
|
virtual int OnBeforeSendHeaders(extensions::WebRequestInfo* info,
|
||||||
|
const network::ResourceRequest& request,
|
||||||
BeforeSendHeadersCallback callback,
|
BeforeSendHeadersCallback callback,
|
||||||
net::HttpRequestHeaders* headers) = 0;
|
net::HttpRequestHeaders* headers) = 0;
|
||||||
virtual int OnHeadersReceived(
|
virtual int OnHeadersReceived(
|
||||||
extensions::WebRequestInfo* request,
|
extensions::WebRequestInfo* info,
|
||||||
|
const network::ResourceRequest& request,
|
||||||
net::CompletionOnceCallback callback,
|
net::CompletionOnceCallback callback,
|
||||||
const net::HttpResponseHeaders* original_response_headers,
|
const net::HttpResponseHeaders* original_response_headers,
|
||||||
scoped_refptr<net::HttpResponseHeaders>* override_response_headers,
|
scoped_refptr<net::HttpResponseHeaders>* override_response_headers,
|
||||||
GURL* allowed_unsafe_redirect_url) = 0;
|
GURL* allowed_unsafe_redirect_url) = 0;
|
||||||
virtual void OnSendHeaders(extensions::WebRequestInfo* request,
|
virtual void OnSendHeaders(extensions::WebRequestInfo* info,
|
||||||
|
const network::ResourceRequest& request,
|
||||||
const net::HttpRequestHeaders& headers) = 0;
|
const net::HttpRequestHeaders& headers) = 0;
|
||||||
virtual void OnBeforeRedirect(extensions::WebRequestInfo* request,
|
virtual void OnBeforeRedirect(extensions::WebRequestInfo* info,
|
||||||
|
const network::ResourceRequest& request,
|
||||||
const GURL& new_location) = 0;
|
const GURL& new_location) = 0;
|
||||||
virtual void OnResponseStarted(extensions::WebRequestInfo* request) = 0;
|
virtual void OnResponseStarted(extensions::WebRequestInfo* info,
|
||||||
virtual void OnErrorOccurred(extensions::WebRequestInfo* request,
|
const network::ResourceRequest& request) = 0;
|
||||||
|
virtual void OnErrorOccurred(extensions::WebRequestInfo* info,
|
||||||
|
const network::ResourceRequest& request,
|
||||||
int net_error) = 0;
|
int net_error) = 0;
|
||||||
virtual void OnCompleted(extensions::WebRequestInfo* request,
|
virtual void OnCompleted(extensions::WebRequestInfo* info,
|
||||||
|
const network::ResourceRequest& request,
|
||||||
int net_error) = 0;
|
int net_error) = 0;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
|
@ -225,6 +225,32 @@ bool Converter<net::HttpResponseHeaders*>::FromV8(
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// static
|
||||||
|
v8::Local<v8::Value> Converter<net::HttpRequestHeaders>::ToV8(
|
||||||
|
v8::Isolate* isolate,
|
||||||
|
const net::HttpRequestHeaders& val) {
|
||||||
|
gin::Dictionary headers(isolate, v8::Object::New(isolate));
|
||||||
|
for (net::HttpRequestHeaders::Iterator it(val); it.GetNext();)
|
||||||
|
headers.Set(it.name(), it.value());
|
||||||
|
return ConvertToV8(isolate, headers);
|
||||||
|
}
|
||||||
|
|
||||||
|
// static
|
||||||
|
bool Converter<net::HttpRequestHeaders>::FromV8(v8::Isolate* isolate,
|
||||||
|
v8::Local<v8::Value> val,
|
||||||
|
net::HttpRequestHeaders* out) {
|
||||||
|
base::DictionaryValue dict;
|
||||||
|
if (!ConvertFromV8(isolate, val, &dict))
|
||||||
|
return false;
|
||||||
|
for (base::DictionaryValue::Iterator it(dict); !it.IsAtEnd(); it.Advance()) {
|
||||||
|
if (it.value().is_string()) {
|
||||||
|
std::string value = it.value().GetString();
|
||||||
|
out->SetHeader(it.key(), value);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
// static
|
// static
|
||||||
v8::Local<v8::Value> Converter<network::ResourceRequest>::ToV8(
|
v8::Local<v8::Value> Converter<network::ResourceRequest>::ToV8(
|
||||||
v8::Isolate* isolate,
|
v8::Isolate* isolate,
|
||||||
|
@ -233,10 +259,7 @@ v8::Local<v8::Value> Converter<network::ResourceRequest>::ToV8(
|
||||||
dict.Set("method", val.method);
|
dict.Set("method", val.method);
|
||||||
dict.Set("url", val.url.spec());
|
dict.Set("url", val.url.spec());
|
||||||
dict.Set("referrer", val.referrer.spec());
|
dict.Set("referrer", val.referrer.spec());
|
||||||
gin::Dictionary headers(isolate, v8::Object::New(isolate));
|
dict.Set("headers", val.headers);
|
||||||
for (net::HttpRequestHeaders::Iterator it(val.headers); it.GetNext();)
|
|
||||||
headers.Set(it.name(), it.value());
|
|
||||||
dict.Set("headers", headers);
|
|
||||||
if (val.request_body) {
|
if (val.request_body) {
|
||||||
const auto& elements = *val.request_body->elements();
|
const auto& elements = *val.request_body->elements();
|
||||||
v8::Local<v8::Array> arr = v8::Array::New(isolate, elements.size());
|
v8::Local<v8::Array> arr = v8::Array::New(isolate, elements.size());
|
||||||
|
|
|
@ -60,6 +60,15 @@ struct Converter<net::HttpResponseHeaders*> {
|
||||||
net::HttpResponseHeaders* out);
|
net::HttpResponseHeaders* out);
|
||||||
};
|
};
|
||||||
|
|
||||||
|
template <>
|
||||||
|
struct Converter<net::HttpRequestHeaders> {
|
||||||
|
static v8::Local<v8::Value> ToV8(v8::Isolate* isolate,
|
||||||
|
const net::HttpRequestHeaders& headers);
|
||||||
|
static bool FromV8(v8::Isolate* isolate,
|
||||||
|
v8::Local<v8::Value> val,
|
||||||
|
net::HttpRequestHeaders* out);
|
||||||
|
};
|
||||||
|
|
||||||
template <>
|
template <>
|
||||||
struct Converter<network::ResourceRequest> {
|
struct Converter<network::ResourceRequest> {
|
||||||
static v8::Local<v8::Value> ToV8(v8::Isolate* isolate,
|
static v8::Local<v8::Value> ToV8(v8::Isolate* isolate,
|
||||||
|
|
Loading…
Reference in a new issue