// Copyright (c) 2015 GitHub, Inc. // Use of this source code is governed by the MIT license that can be // found in the LICENSE file. #include "atom/browser/api/atom_api_web_request.h" #include "atom/browser/atom_browser_context.h" #include "atom/browser/net/atom_network_delegate.h" #include "atom/common/native_mate_converters/callback.h" #include "atom/common/native_mate_converters/net_converter.h" #include "atom/common/native_mate_converters/value_converter.h" #include "content/public/browser/browser_thread.h" #include "native_mate/dictionary.h" #include "native_mate/object_template_builder.h" using content::BrowserThread; namespace atom { namespace api { WebRequest::WebRequest(AtomBrowserContext* browser_context) : browser_context_(browser_context) { } WebRequest::~WebRequest() { } template void WebRequest::SetListener(mate::Arguments* args) { DCHECK_CURRENTLY_ON(BrowserThread::UI); base::DictionaryValue* filter = new base::DictionaryValue(); args->GetNext(filter); AtomNetworkDelegate::Listener callback; if (!args->GetNext(&callback)) { args->ThrowError("Must pass null or a function"); return; } auto delegate = browser_context_->network_delegate(); BrowserThread::PostTask(BrowserThread::IO, FROM_HERE, base::Bind(&AtomNetworkDelegate::SetListenerInIO, base::Unretained(delegate), type, filter, callback)); } // static mate::Handle WebRequest::Create( v8::Isolate* isolate, AtomBrowserContext* browser_context) { return mate::CreateHandle(isolate, new WebRequest(browser_context)); } // static void WebRequest::BuildPrototype(v8::Isolate* isolate, v8::Local prototype) { mate::ObjectTemplateBuilder(isolate, prototype) .SetMethod("onBeforeRequest", &WebRequest::SetListener< AtomNetworkDelegate::kOnBeforeRequest>) .SetMethod("onBeforeSendHeaders", &WebRequest::SetListener< AtomNetworkDelegate::kOnBeforeSendHeaders>) .SetMethod("onSendHeaders", &WebRequest::SetListener< AtomNetworkDelegate::kOnSendHeaders>) .SetMethod("onHeadersReceived", &WebRequest::SetListener< AtomNetworkDelegate::kOnHeadersReceived>) .SetMethod("onBeforeRedirect", &WebRequest::SetListener< AtomNetworkDelegate::kOnBeforeRedirect>) .SetMethod("onResponseStarted", &WebRequest::SetListener< AtomNetworkDelegate::kOnResponseStarted>) .SetMethod("onCompleted", &WebRequest::SetListener< AtomNetworkDelegate::kOnCompleted>) .SetMethod("onErrorOccurred", &WebRequest::SetListener< AtomNetworkDelegate::kOnErrorOccurred>); } } // namespace api } // namespace atom