// Copyright (c) 2019 GitHub, Inc. // Use of this source code is governed by the MIT license that can be // found in the LICENSE file. #ifndef SHELL_BROWSER_API_ATOM_API_WEB_REQUEST_NS_H_ #define SHELL_BROWSER_API_ATOM_API_WEB_REQUEST_NS_H_ #include #include #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 { namespace api { class WebRequestNS : public gin::Wrappable, public WebRequestAPI { public: static gin::WrapperInfo kWrapperInfo; // 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 FromOrCreate( v8::Isolate* isolate, content::BrowserContext* browser_context); // Return a new WebRequest object, this should only be called by Session. static gin::Handle Create( v8::Isolate* isolate, content::BrowserContext* browser_context); // Find the WebRequest object attached to |browser_context|. static gin::Handle From( v8::Isolate* isolate, content::BrowserContext* browser_context); // gin::Wrappable: gin::ObjectTemplateBuilder GetObjectTemplateBuilder( v8::Isolate* isolate) override; const char* GetTypeName() override; private: WebRequestNS(v8::Isolate* isolate, content::BrowserContext* browser_context); ~WebRequestNS() override; // WebRequestAPI: int OnBeforeRequest(extensions::WebRequestInfo* info, const network::ResourceRequest& request, net::CompletionOnceCallback callback, GURL* new_url) override; int OnBeforeSendHeaders(extensions::WebRequestInfo* info, const network::ResourceRequest& request, BeforeSendHeadersCallback callback, net::HttpRequestHeaders* headers) override; int OnHeadersReceived( extensions::WebRequestInfo* info, const network::ResourceRequest& request, net::CompletionOnceCallback callback, const net::HttpResponseHeaders* original_response_headers, scoped_refptr* override_response_headers, GURL* allowed_unsafe_redirect_url) override; void OnSendHeaders(extensions::WebRequestInfo* info, const network::ResourceRequest& request, const net::HttpRequestHeaders& headers) override; void OnBeforeRedirect(extensions::WebRequestInfo* info, const network::ResourceRequest& request, const GURL& new_location) override; void OnResponseStarted(extensions::WebRequestInfo* info, const network::ResourceRequest& request) override; void OnErrorOccurred(extensions::WebRequestInfo* info, const network::ResourceRequest& request, int net_error) override; void OnCompleted(extensions::WebRequestInfo* info, const network::ResourceRequest& request, int net_error) override; enum SimpleEvent { kOnSendHeaders, kOnBeforeRedirect, kOnResponseStarted, kOnCompleted, kOnErrorOccurred, }; enum ResponseEvent { kOnBeforeRequest, kOnBeforeSendHeaders, kOnHeadersReceived, }; using SimpleListener = base::RepeatingCallback)>; using ResponseCallback = base::OnceCallback)>; using ResponseListener = base::RepeatingCallback, ResponseCallback)>; template void SetSimpleListener(gin::Arguments* args); template void SetResponseListener(gin::Arguments* args); template void SetListener(Event event, Listeners* listeners, gin::Arguments* args); template void HandleSimpleEvent(SimpleEvent event, extensions::WebRequestInfo* info, Args... args); template int HandleResponseEvent(ResponseEvent event, extensions::WebRequestInfo* info, net::CompletionOnceCallback callback, Out out, Args... args); template void OnListenerResult(uint64_t id, T out, v8::Local response); struct SimpleListenerInfo { std::set url_patterns; SimpleListener listener; SimpleListenerInfo(std::set, SimpleListener); SimpleListenerInfo(); ~SimpleListenerInfo(); }; struct ResponseListenerInfo { std::set url_patterns; ResponseListener listener; ResponseListenerInfo(std::set, ResponseListener); ResponseListenerInfo(); ~ResponseListenerInfo(); }; std::map simple_listeners_; std::map response_listeners_; std::map callbacks_; // Weak-ref, it manages us. content::BrowserContext* browser_context_; }; } // namespace api } // namespace electron #endif // SHELL_BROWSER_API_ATOM_API_WEB_REQUEST_NS_H_