fill net::URLRequest details on IO thread

This commit is contained in:
deepak1556 2016-06-08 19:22:21 +05:30
parent 414183e0fd
commit 0e0235407b
14 changed files with 59 additions and 44 deletions

View file

@ -263,13 +263,14 @@ void App::OnContinueUserActivity(
}
#endif
void App::OnLogin(LoginHandler* login_handler) {
void App::OnLogin(LoginHandler* login_handler,
const base::DictionaryValue& request_details) {
v8::Locker locker(isolate());
v8::HandleScope handle_scope(isolate());
bool prevent_default = Emit(
"login",
WebContents::CreateFrom(isolate(), login_handler->GetWebContents()),
login_handler->request(),
request_details,
login_handler->auth_info(),
base::Bind(&PassLoginInformation, make_scoped_refptr(login_handler)));

View file

@ -70,7 +70,8 @@ class App : public AtomBrowserClient::Delegate,
void OnActivate(bool has_visible_windows) override;
void OnWillFinishLaunching() override;
void OnFinishLaunching() override;
void OnLogin(LoginHandler* login_handler) override;
void OnLogin(LoginHandler* login_handler,
const base::DictionaryValue& request_details) override;
#if defined(OS_MACOSX)
void OnContinueUserActivity(
bool* prevent_default,

View file

@ -12,7 +12,7 @@
#include "atom/browser/net/url_request_fetch_job.h"
#include "atom/browser/net/url_request_string_job.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 "atom/common/node_includes.h"
#include "atom/common/options_switches.h"
#include "base/command_line.h"

View file

@ -18,6 +18,10 @@
#include "native_mate/handle.h"
#include "native_mate/wrappable.h"
namespace base {
class DictionaryValue;
}
namespace net {
class URLRequest;
class URLRequestContextGetter;
@ -33,7 +37,7 @@ namespace api {
class Protocol : public mate::Wrappable<Protocol> {
public:
using Handler =
base::Callback<void(const net::URLRequest*, v8::Local<v8::Value>)>;
base::Callback<void(const base::DictionaryValue&, v8::Local<v8::Value>)>;
using CompletionCallback = base::Callback<void(v8::Local<v8::Value>)>;
using BooleanCallback = base::Callback<void(bool)>;

View file

@ -151,8 +151,12 @@ void Browser::DidFinishLaunching() {
FOR_EACH_OBSERVER(BrowserObserver, observers_, OnFinishLaunching());
}
void Browser::RequestLogin(LoginHandler* login_handler) {
FOR_EACH_OBSERVER(BrowserObserver, observers_, OnLogin(login_handler));
void Browser::RequestLogin(
LoginHandler* login_handler,
std::unique_ptr<base::DictionaryValue> request_details) {
FOR_EACH_OBSERVER(BrowserObserver,
observers_,
OnLogin(login_handler, *(request_details.get())));
}
void Browser::NotifyAndShutdown() {

View file

@ -21,6 +21,7 @@
#endif
namespace base {
class DictionaryValue;
class FilePath;
}
@ -165,7 +166,8 @@ class Browser : public WindowListObserver {
void DidFinishLaunching();
// Request basic auth login.
void RequestLogin(LoginHandler* login_handler);
void RequestLogin(LoginHandler* login_handler,
std::unique_ptr<base::DictionaryValue> request_details);
void AddObserver(BrowserObserver* obs) {
observers_.AddObserver(obs);

View file

@ -49,7 +49,8 @@ class BrowserObserver {
virtual void OnFinishLaunching() {}
// The browser requests HTTP login.
virtual void OnLogin(LoginHandler* login_handler) {}
virtual void OnLogin(LoginHandler* login_handler,
const base::DictionaryValue& request_details) {}
#if defined(OS_MACOSX)
// The browser wants to resume a user activity via handoff. (OS X only)

View file

@ -5,6 +5,8 @@
#include "atom/browser/login_handler.h"
#include "atom/browser/browser.h"
#include "atom/common/native_mate_converters/net_converter.h"
#include "base/values.h"
#include "content/public/browser/browser_thread.h"
#include "content/public/browser/render_frame_host.h"
#include "content/public/browser/resource_dispatcher_host.h"
@ -37,11 +39,18 @@ LoginHandler::LoginHandler(net::AuthChallengeInfo* auth_info,
render_frame_id_(0) {
content::ResourceRequestInfo::ForRequest(request_)->GetAssociatedRenderFrame(
&render_process_host_id_, &render_frame_id_);
// Fill request details on IO thread.
std::unique_ptr<base::DictionaryValue> request_details(
new base::DictionaryValue);
FillRequestDetails(request_details.get(), request_);
BrowserThread::PostTask(
BrowserThread::UI, FROM_HERE,
base::Bind(&Browser::RequestLogin,
base::Unretained(Browser::Get()),
base::RetainedRef(make_scoped_refptr(this))));
base::RetainedRef(make_scoped_refptr(this)),
base::Passed(&request_details)));
}
LoginHandler::~LoginHandler() {

View file

@ -36,7 +36,6 @@ class LoginHandler : public content::ResourceDispatcherHostLoginDelegate {
void Login(const base::string16& username, const base::string16& password);
const net::AuthChallengeInfo* auth_info() const { return auth_info_.get(); }
const net::URLRequest* request() const { return request_; }
protected:
~LoginHandler() override;

View file

@ -71,18 +71,13 @@ bool MatchesFilterCondition(net::URLRequest* request,
// Overloaded by multiple types to fill the |details| object.
void ToDictionary(base::DictionaryValue* details, net::URLRequest* request) {
FillRequestDetails(details, request);
details->SetInteger("id", request->identifier());
details->SetString("url", request->url().spec());
details->SetString("method", request->method());
details->SetDouble("timestamp", base::Time::Now().ToDoubleT() * 1000);
auto info = content::ResourceRequestInfo::ForRequest(request);
details->SetString("resourceType",
info ? ResourceTypeToString(info->GetResourceType())
: "other");
std::unique_ptr<base::ListValue> list(new base::ListValue);
GetUploadData(list.get(), request);
if (!list->empty())
details->Set("uploadData", std::move(list));
}
void ToDictionary(base::DictionaryValue* details,

View file

@ -44,7 +44,7 @@ void HandlerCallback(const BeforeStartCallback& before_start,
void AskForOptions(v8::Isolate* isolate,
const JavaScriptHandler& handler,
net::URLRequest* request,
std::unique_ptr<base::DictionaryValue> request_details,
const BeforeStartCallback& before_start,
const ResponseCallback& callback) {
DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
@ -53,7 +53,7 @@ void AskForOptions(v8::Isolate* isolate,
v8::Local<v8::Context> context = isolate->GetCurrentContext();
v8::Context::Scope context_scope(context);
handler.Run(
request,
*(request_details.get()),
mate::ConvertToV8(isolate,
base::Bind(&HandlerCallback, before_start, callback)));
}

View file

@ -5,6 +5,7 @@
#ifndef ATOM_BROWSER_NET_JS_ASKER_H_
#define ATOM_BROWSER_NET_JS_ASKER_H_
#include "atom/common/native_mate_converters/net_converter.h"
#include "base/callback.h"
#include "base/memory/ref_counted.h"
#include "base/memory/weak_ptr.h"
@ -19,7 +20,7 @@
namespace atom {
using JavaScriptHandler =
base::Callback<void(const net::URLRequest*, v8::Local<v8::Value>)>;
base::Callback<void(const base::DictionaryValue&, v8::Local<v8::Value>)>;
namespace internal {
@ -31,7 +32,7 @@ using ResponseCallback =
// Ask handler for options in UI thread.
void AskForOptions(v8::Isolate* isolate,
const JavaScriptHandler& handler,
net::URLRequest* request,
std::unique_ptr<base::DictionaryValue> request_details,
const BeforeStartCallback& before_start,
const ResponseCallback& callback);
@ -67,12 +68,15 @@ class JsAsker : public RequestJob {
private:
// RequestJob:
void Start() override {
std::unique_ptr<base::DictionaryValue> request_details(
new base::DictionaryValue);
FillRequestDetails(request_details.get(), RequestJob::request());
content::BrowserThread::PostTask(
content::BrowserThread::UI, FROM_HERE,
base::Bind(&internal::AskForOptions,
isolate_,
handler_,
RequestJob::request(),
base::Passed(&request_details),
base::Bind(&JsAsker::BeforeStartInUI,
weak_factory_.GetWeakPtr()),
base::Bind(&JsAsker::OnResponse,

View file

@ -22,22 +22,6 @@
namespace mate {
// static
v8::Local<v8::Value> Converter<const net::URLRequest*>::ToV8(
v8::Isolate* isolate, const net::URLRequest* val) {
std::unique_ptr<base::DictionaryValue> dict(new base::DictionaryValue);
dict->SetString("method", val->method());
std::string url;
if (!val->url_chain().empty()) url = val->url().spec();
dict->SetStringWithoutPathExpansion("url", url);
dict->SetString("referrer", val->referrer());
std::unique_ptr<base::ListValue> list(new base::ListValue);
atom::GetUploadData(list.get(), val);
if (!list->empty())
dict->Set("uploadData", std::move(list));
return mate::ConvertToV8(isolate, *(dict.get()));
}
// static
v8::Local<v8::Value> Converter<const net::AuthChallengeInfo*>::ToV8(
v8::Isolate* isolate, const net::AuthChallengeInfo* val) {
@ -69,6 +53,19 @@ v8::Local<v8::Value> Converter<scoped_refptr<net::X509Certificate>>::ToV8(
namespace atom {
void FillRequestDetails(base::DictionaryValue* details,
const net::URLRequest* request) {
details->SetString("method", request->method());
std::string url;
if (!request->url_chain().empty()) url = request->url().spec();
details->SetStringWithoutPathExpansion("url", url);
details->SetString("referrer", request->referrer());
std::unique_ptr<base::ListValue> list(new base::ListValue);
GetUploadData(list.get(), request);
if (!list->empty())
details->Set("uploadData", std::move(list));
}
void GetUploadData(base::ListValue* upload_data_list,
const net::URLRequest* request) {
const net::UploadDataStream* upload_data = request->get_upload();

View file

@ -9,6 +9,7 @@
#include "native_mate/converter.h"
namespace base {
class DictionaryValue;
class ListValue;
}
@ -20,12 +21,6 @@ class X509Certificate;
namespace mate {
template<>
struct Converter<const net::URLRequest*> {
static v8::Local<v8::Value> ToV8(v8::Isolate* isolate,
const net::URLRequest* val);
};
template<>
struct Converter<const net::AuthChallengeInfo*> {
static v8::Local<v8::Value> ToV8(v8::Isolate* isolate,
@ -42,6 +37,9 @@ struct Converter<scoped_refptr<net::X509Certificate>> {
namespace atom {
void FillRequestDetails(base::DictionaryValue* details,
const net::URLRequest* request);
void GetUploadData(base::ListValue* upload_data_list,
const net::URLRequest* request);