// Copyright (c) 2019 GitHub, Inc. // Use of this source code is governed by the MIT license that can be // found in the LICENSE file. #include "shell/browser/api/atom_api_web_request_ns.h" #include #include #include #include "gin/converter.h" #include "gin/dictionary.h" #include "gin/object_template_builder.h" #include "shell/browser/api/atom_api_session.h" #include "shell/browser/atom_browser_context.h" #include "shell/common/gin_converters/callback_converter_gin_adapter.h" #include "shell/common/gin_converters/std_converter.h" #include "shell/common/gin_converters/value_converter_gin_adapter.h" namespace gin { template <> struct Converter { static bool FromV8(v8::Isolate* isolate, v8::Local val, URLPattern* out) { std::string pattern; if (!ConvertFromV8(isolate, val, &pattern)) return false; *out = URLPattern(URLPattern::SCHEME_ALL); return out->Parse(pattern) == URLPattern::ParseResult::kSuccess; } }; } // namespace gin namespace electron { namespace api { namespace { const char* kUserDataKey = "WebRequestNS"; // BrowserContext <=> WebRequestNS relationship. struct UserData : public base::SupportsUserData::Data { explicit UserData(WebRequestNS* data) : data(data) {} WebRequestNS* data; }; // Test whether the URL of |request| matches |patterns|. bool MatchesFilterCondition(extensions::WebRequestInfo* request, const std::set& patterns) { if (patterns.empty()) return true; for (const auto& pattern : patterns) { if (pattern.MatchesURL(request->url)) return true; } return false; } } // namespace gin::WrapperInfo WebRequestNS::kWrapperInfo = {gin::kEmbedderNativeGin}; WebRequestNS::SimpleListenerInfo::SimpleListenerInfo( std::set patterns_, SimpleListener listener_) : url_patterns(std::move(patterns_)), listener(listener_) {} WebRequestNS::SimpleListenerInfo::SimpleListenerInfo() = default; WebRequestNS::SimpleListenerInfo::~SimpleListenerInfo() = default; WebRequestNS::ResponseListenerInfo::ResponseListenerInfo( std::set patterns_, ResponseListener listener_) : url_patterns(std::move(patterns_)), listener(listener_) {} WebRequestNS::ResponseListenerInfo::ResponseListenerInfo() = default; WebRequestNS::ResponseListenerInfo::~ResponseListenerInfo() = default; WebRequestNS::WebRequestNS(v8::Isolate* isolate, content::BrowserContext* browser_context) : browser_context_(browser_context) { browser_context_->SetUserData(kUserDataKey, std::make_unique(this)); } WebRequestNS::~WebRequestNS() { browser_context_->RemoveUserData(kUserDataKey); } gin::ObjectTemplateBuilder WebRequestNS::GetObjectTemplateBuilder( v8::Isolate* isolate) { return gin::Wrappable::GetObjectTemplateBuilder(isolate) .SetMethod("onBeforeRequest", &WebRequestNS::SetResponseListener) .SetMethod("onBeforeSendHeaders", &WebRequestNS::SetResponseListener) .SetMethod("onHeadersReceived", &WebRequestNS::SetResponseListener) .SetMethod("onSendHeaders", &WebRequestNS::SetSimpleListener) .SetMethod("onBeforeRedirect", &WebRequestNS::SetSimpleListener) .SetMethod("onResponseStarted", &WebRequestNS::SetSimpleListener) .SetMethod("onErrorOccurred", &WebRequestNS::SetSimpleListener) .SetMethod("onCompleted", &WebRequestNS::SetSimpleListener); } const char* WebRequestNS::GetTypeName() { return "WebRequest"; } int WebRequestNS::OnBeforeRequest(extensions::WebRequestInfo* request, net::CompletionOnceCallback callback, GURL* new_url) { return HandleResponseEvent(kOnBeforeRequest, request, std::move(callback), new_url); } int WebRequestNS::OnBeforeSendHeaders(extensions::WebRequestInfo* request, BeforeSendHeadersCallback callback, net::HttpRequestHeaders* headers) { // TODO(zcbenz): Figure out how to handle this generally. return net::OK; } int WebRequestNS::OnHeadersReceived( extensions::WebRequestInfo* request, net::CompletionOnceCallback callback, const net::HttpResponseHeaders* original_response_headers, scoped_refptr* override_response_headers, GURL* allowed_unsafe_redirect_url) { return HandleResponseEvent(kOnHeadersReceived, request, std::move(callback), original_response_headers, override_response_headers, allowed_unsafe_redirect_url); } void WebRequestNS::OnSendHeaders(extensions::WebRequestInfo* request, const net::HttpRequestHeaders& headers) { HandleSimpleEvent(kOnSendHeaders, request, headers); } void WebRequestNS::OnBeforeRedirect(extensions::WebRequestInfo* request, const GURL& new_location) { HandleSimpleEvent(kOnBeforeRedirect, request, new_location); } void WebRequestNS::OnResponseStarted(extensions::WebRequestInfo* request) { HandleSimpleEvent(kOnResponseStarted, request); } void WebRequestNS::OnErrorOccurred(extensions::WebRequestInfo* request, int net_error) { HandleSimpleEvent(kOnErrorOccurred, request, net_error); } void WebRequestNS::OnCompleted(extensions::WebRequestInfo* request, int net_error) { HandleSimpleEvent(kOnCompleted, request, net_error); } template void WebRequestNS::SetSimpleListener(gin::Arguments* args) { SetListener(event, &simple_listeners_, args); } template void WebRequestNS::SetResponseListener(gin::Arguments* args) { SetListener(event, &response_listeners_, args); } template void WebRequestNS::SetListener(Event event, Listeners* listeners, gin::Arguments* args) { // { urls }. std::set patterns; gin::Dictionary dict(args->isolate()); args->GetNext(&dict) && dict.Get("urls", &patterns); // Function or null. v8::Local value; Listener listener; if (!args->GetNext(&listener) && !(args->GetNext(&value) && value->IsNull())) { args->ThrowTypeError("Must pass null or a Function"); return; } if (listener.is_null()) listeners->erase(event); else (*listeners)[event] = {std::move(patterns), std::move(listener)}; } template void WebRequestNS::HandleSimpleEvent(SimpleEvent event, extensions::WebRequestInfo* request, Args... args) { const auto& info = simple_listeners_[event]; if (!MatchesFilterCondition(request, info.url_patterns)) return; // TODO(zcbenz): Invoke the listener. } template int WebRequestNS::HandleResponseEvent(ResponseEvent event, extensions::WebRequestInfo* request, net::CompletionOnceCallback callback, Out out, Args... args) { const auto& info = response_listeners_[event]; if (!MatchesFilterCondition(request, info.url_patterns)) return net::OK; // TODO(zcbenz): Invoke the listener. return net::OK; } // static gin::Handle WebRequestNS::FromOrCreate( v8::Isolate* isolate, content::BrowserContext* browser_context) { gin::Handle handle = From(isolate, browser_context); if (handle.IsEmpty()) { // Make sure the |Session| object has the |webRequest| property created. v8::Local web_request = Session::CreateFrom(isolate, static_cast(browser_context)) ->WebRequest(isolate); gin::ConvertFromV8(isolate, web_request, &handle); } DCHECK(!handle.IsEmpty()); return handle; } // static gin::Handle WebRequestNS::Create( v8::Isolate* isolate, content::BrowserContext* browser_context) { DCHECK(From(isolate, browser_context).IsEmpty()) << "WebRequestNS already created"; return gin::CreateHandle(isolate, new WebRequestNS(isolate, browser_context)); } // static gin::Handle WebRequestNS::From( v8::Isolate* isolate, content::BrowserContext* browser_context) { if (!browser_context) return gin::Handle(); auto* user_data = static_cast(browser_context->GetUserData(kUserDataKey)); if (!user_data) return gin::Handle(); return gin::CreateHandle(isolate, user_data->data); } } // namespace api } // namespace electron