feat: migrate webRequest module to NetworkService (Part 5) (#19714)
* Pass WebRequest to ProxyingURLLoaderFactory * Call WebRequestAPI in InProgressRequest * Store the listeners * Pass the request and response * Add stub to handle the events * Use extensions::WebRequestInfo * Make sure webRequest is managed by Session * chore: make creation of WebRequestNS more clear * fix: check WebContents for service workers
This commit is contained in:
parent
9713fa09e7
commit
69eac0d9d2
7 changed files with 365 additions and 46 deletions
|
@ -5,25 +5,47 @@
|
|||
#ifndef SHELL_BROWSER_API_ATOM_API_WEB_REQUEST_NS_H_
|
||||
#define SHELL_BROWSER_API_ATOM_API_WEB_REQUEST_NS_H_
|
||||
|
||||
#include <map>
|
||||
#include <set>
|
||||
|
||||
#include "base/values.h"
|
||||
#include "extensions/common/url_pattern.h"
|
||||
#include "gin/arguments.h"
|
||||
#include "gin/handle.h"
|
||||
#include "gin/wrappable.h"
|
||||
#include "native_mate/dictionary.h"
|
||||
#include "native_mate/handle.h"
|
||||
#include "shell/browser/net/proxying_url_loader_factory.h"
|
||||
|
||||
namespace content {
|
||||
class BrowserContext;
|
||||
}
|
||||
|
||||
namespace electron {
|
||||
|
||||
class AtomBrowserContext;
|
||||
|
||||
namespace api {
|
||||
|
||||
class WebRequestNS : public gin::Wrappable<WebRequestNS> {
|
||||
class WebRequestNS : public gin::Wrappable<WebRequestNS>, public WebRequestAPI {
|
||||
public:
|
||||
static gin::WrapperInfo kWrapperInfo;
|
||||
|
||||
static gin::Handle<WebRequestNS> Create(v8::Isolate* isolate,
|
||||
AtomBrowserContext* browser_context);
|
||||
// Return the WebRequest object attached to |browser_context|, create if there
|
||||
// is no one.
|
||||
// Note that the lifetime of WebRequest object is managed by Session, instead
|
||||
// of the caller.
|
||||
static gin::Handle<WebRequestNS> FromOrCreate(
|
||||
v8::Isolate* isolate,
|
||||
content::BrowserContext* browser_context);
|
||||
|
||||
// Return a new WebRequest object, this should only be called by Session.
|
||||
static gin::Handle<WebRequestNS> Create(
|
||||
v8::Isolate* isolate,
|
||||
content::BrowserContext* browser_context);
|
||||
|
||||
// Find the WebRequest object attached to |browser_context|.
|
||||
static gin::Handle<WebRequestNS> From(
|
||||
v8::Isolate* isolate,
|
||||
content::BrowserContext* browser_context);
|
||||
|
||||
// gin::Wrappable:
|
||||
gin::ObjectTemplateBuilder GetObjectTemplateBuilder(
|
||||
|
@ -31,9 +53,31 @@ class WebRequestNS : public gin::Wrappable<WebRequestNS> {
|
|||
const char* GetTypeName() override;
|
||||
|
||||
private:
|
||||
WebRequestNS(v8::Isolate* isolate, AtomBrowserContext* browser_context);
|
||||
WebRequestNS(v8::Isolate* isolate, content::BrowserContext* browser_context);
|
||||
~WebRequestNS() override;
|
||||
|
||||
// WebRequestAPI:
|
||||
int OnBeforeRequest(extensions::WebRequestInfo* request,
|
||||
net::CompletionOnceCallback callback,
|
||||
GURL* new_url) override;
|
||||
int OnBeforeSendHeaders(extensions::WebRequestInfo* request,
|
||||
BeforeSendHeadersCallback callback,
|
||||
net::HttpRequestHeaders* headers) override;
|
||||
int OnHeadersReceived(
|
||||
extensions::WebRequestInfo* request,
|
||||
net::CompletionOnceCallback callback,
|
||||
const net::HttpResponseHeaders* original_response_headers,
|
||||
scoped_refptr<net::HttpResponseHeaders>* override_response_headers,
|
||||
GURL* allowed_unsafe_redirect_url) override;
|
||||
void OnSendHeaders(extensions::WebRequestInfo* request,
|
||||
const net::HttpRequestHeaders& headers) override;
|
||||
void OnBeforeRedirect(extensions::WebRequestInfo* request,
|
||||
const GURL& new_location) override;
|
||||
void OnResponseStarted(extensions::WebRequestInfo* request) override;
|
||||
void OnErrorOccurred(extensions::WebRequestInfo* request,
|
||||
int net_error) override;
|
||||
void OnCompleted(extensions::WebRequestInfo* request, int net_error) override;
|
||||
|
||||
enum SimpleEvent {
|
||||
kOnSendHeaders,
|
||||
kOnBeforeRedirect,
|
||||
|
@ -56,8 +100,44 @@ class WebRequestNS : public gin::Wrappable<WebRequestNS> {
|
|||
void SetSimpleListener(gin::Arguments* args);
|
||||
template <ResponseEvent event>
|
||||
void SetResponseListener(gin::Arguments* args);
|
||||
template <typename Listener, typename Event>
|
||||
void SetListener(Event event, gin::Arguments* args);
|
||||
template <typename Listener, typename Listeners, typename Event>
|
||||
void SetListener(Event event, Listeners* listeners, gin::Arguments* args);
|
||||
|
||||
template <typename... Args>
|
||||
void HandleSimpleEvent(SimpleEvent event,
|
||||
extensions::WebRequestInfo* request,
|
||||
Args... args);
|
||||
template <typename Out, typename... Args>
|
||||
int HandleResponseEvent(ResponseEvent event,
|
||||
extensions::WebRequestInfo* request,
|
||||
net::CompletionOnceCallback callback,
|
||||
Out out,
|
||||
Args... args);
|
||||
|
||||
struct SimpleListenerInfo {
|
||||
std::set<URLPattern> url_patterns;
|
||||
SimpleListener listener;
|
||||
|
||||
SimpleListenerInfo(std::set<URLPattern>, SimpleListener);
|
||||
SimpleListenerInfo();
|
||||
~SimpleListenerInfo();
|
||||
};
|
||||
|
||||
struct ResponseListenerInfo {
|
||||
std::set<URLPattern> url_patterns;
|
||||
ResponseListener listener;
|
||||
|
||||
ResponseListenerInfo(std::set<URLPattern>, ResponseListener);
|
||||
ResponseListenerInfo();
|
||||
~ResponseListenerInfo();
|
||||
};
|
||||
|
||||
std::map<SimpleEvent, SimpleListenerInfo> simple_listeners_;
|
||||
std::map<ResponseEvent, ResponseListenerInfo> response_listeners_;
|
||||
std::map<uint64_t, net::CompletionOnceCallback> callbacks_;
|
||||
|
||||
// Weak-ref, it manages us.
|
||||
content::BrowserContext* browser_context_;
|
||||
};
|
||||
|
||||
} // namespace api
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue