From 67886cf513c1caccf340cd07f6e9797cce5c99d8 Mon Sep 17 00:00:00 2001 From: Cheng Zhao Date: Fri, 11 Dec 2015 21:14:13 +0800 Subject: [PATCH] Parse filters in API code --- atom/browser/api/atom_api_web_request.cc | 24 ++++++++++++++++++++--- atom/browser/net/atom_network_delegate.cc | 24 ++++------------------- atom/browser/net/atom_network_delegate.h | 2 +- vendor/native_mate | 2 +- 4 files changed, 27 insertions(+), 25 deletions(-) diff --git a/atom/browser/api/atom_api_web_request.cc b/atom/browser/api/atom_api_web_request.cc index ab583d355f7..5b9d2fbe640 100644 --- a/atom/browser/api/atom_api_web_request.cc +++ b/atom/browser/api/atom_api_web_request.cc @@ -15,6 +15,21 @@ using content::BrowserThread; +namespace mate { + +template<> +struct Converter { + static bool FromV8(v8::Isolate* isolate, v8::Local val, + extensions::URLPattern* out) { + std::string pattern; + if (!ConvertFromV8(isolate, val, &pattern)) + return false; + return out->Parse(pattern) == extensions::URLPattern::PARSE_SUCCESS; + } +}; + +} // namespace mate + namespace atom { namespace api { @@ -28,9 +43,12 @@ WebRequest::~WebRequest() { template void WebRequest::SetListener(mate::Arguments* args) { - scoped_ptr filter(new base::DictionaryValue); - args->GetNext(filter.get()); + // { urls }. + URLPatterns patterns; + mate::Dictionary dict; + args->GetNext(&dict) && dict.Get("urls", &patterns); + // Function or null. v8::Local value; AtomNetworkDelegate::Listener callback; if (!args->GetNext(&callback) && @@ -43,7 +61,7 @@ void WebRequest::SetListener(mate::Arguments* args) { BrowserThread::PostTask(BrowserThread::IO, FROM_HERE, base::Bind(&AtomNetworkDelegate::SetListenerInIO, base::Unretained(delegate), - type, base::Passed(&filter), callback)); + type, patterns, callback)); } // static diff --git a/atom/browser/net/atom_network_delegate.cc b/atom/browser/net/atom_network_delegate.cc index 311446804cc..9ec2fdafa02 100644 --- a/atom/browser/net/atom_network_delegate.cc +++ b/atom/browser/net/atom_network_delegate.cc @@ -167,31 +167,15 @@ AtomNetworkDelegate::AtomNetworkDelegate() { AtomNetworkDelegate::~AtomNetworkDelegate() { } -void AtomNetworkDelegate::SetListenerInIO( - EventType type, - scoped_ptr filter, - const Listener& callback) { +void AtomNetworkDelegate::SetListenerInIO(EventType type, + const URLPatterns& patterns, + const Listener& callback) { if (callback.is_null()) { event_listener_map_.erase(type); return; } - ListenerInfo info; - info.callback = callback; - - const base::ListValue* url_list = nullptr; - if (filter->GetList("urls", &url_list)) { - for (size_t i = 0; i < url_list->GetSize(); ++i) { - std::string url; - extensions::URLPattern pattern; - if (url_list->GetString(i, &url) && - pattern.Parse(url) == extensions::URLPattern::PARSE_SUCCESS) { - info.url_patterns.insert(pattern); - } - } - } - - event_listener_map_[type] = info; + event_listener_map_[type] = { patterns, callback }; } int AtomNetworkDelegate::OnBeforeURLRequest( diff --git a/atom/browser/net/atom_network_delegate.h b/atom/browser/net/atom_network_delegate.h index 075fcc77ee0..869c0ec09d2 100644 --- a/atom/browser/net/atom_network_delegate.h +++ b/atom/browser/net/atom_network_delegate.h @@ -65,7 +65,7 @@ class AtomNetworkDelegate : public brightray::NetworkDelegate { ~AtomNetworkDelegate() override; void SetListenerInIO(EventType type, - scoped_ptr filter, + const URLPatterns& patterns, const Listener& callback); protected: diff --git a/vendor/native_mate b/vendor/native_mate index 5e70868fd0c..a3dcf8ced66 160000 --- a/vendor/native_mate +++ b/vendor/native_mate @@ -1 +1 @@ -Subproject commit 5e70868fd0c005dc2c43bea15ca6e93da0b68741 +Subproject commit a3dcf8ced663e974ac94ad5e50a1d25a43995a9d