Implement URLRequest::Delegate, handle thread sync.
This commit is contained in:
parent
e8d4abe78f
commit
7521aeea09
8 changed files with 229 additions and 22 deletions
|
@ -57,6 +57,7 @@ void Initialize(v8::Local<v8::Object> exports, v8::Local<v8::Value> unused,
|
||||||
|
|
||||||
mate::Dictionary dict(isolate, exports);
|
mate::Dictionary dict(isolate, exports);
|
||||||
dict.Set("net", Net::Create(isolate));
|
dict.Set("net", Net::Create(isolate));
|
||||||
|
dict.Set("Net", Net::GetConstructor(isolate)->GetFunction());
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -3,13 +3,20 @@
|
||||||
// found in the LICENSE file.
|
// found in the LICENSE file.
|
||||||
|
|
||||||
#include "atom/browser/api/atom_api_url_request.h"
|
#include "atom/browser/api/atom_api_url_request.h"
|
||||||
|
#include "atom/browser/api/atom_api_session.h"
|
||||||
|
|
||||||
|
#include "native_mate/dictionary.h"
|
||||||
|
#include "atom/browser/net/atom_url_request.h"
|
||||||
|
|
||||||
|
|
||||||
namespace atom {
|
namespace atom {
|
||||||
|
|
||||||
namespace api {
|
namespace api {
|
||||||
|
|
||||||
URLRequest::URLRequest(v8::Isolate* isolate) {
|
URLRequest::URLRequest(v8::Isolate* isolate,
|
||||||
Init(isolate);
|
v8::Local<v8::Object> wrapper)
|
||||||
|
: weak_ptr_factory_(this) {
|
||||||
|
InitWith(isolate, wrapper);
|
||||||
}
|
}
|
||||||
|
|
||||||
URLRequest::~URLRequest() {
|
URLRequest::~URLRequest() {
|
||||||
|
@ -18,20 +25,72 @@ URLRequest::~URLRequest() {
|
||||||
// static
|
// static
|
||||||
mate::WrappableBase* URLRequest::New(mate::Arguments* args) {
|
mate::WrappableBase* URLRequest::New(mate::Arguments* args) {
|
||||||
|
|
||||||
return new URLRequest(args->isolate());
|
v8::Local<v8::Object> options;
|
||||||
|
args->GetNext(&options);
|
||||||
|
mate::Dictionary dict(args->isolate(), options);
|
||||||
|
std::string url;
|
||||||
|
dict.Get("url", &url);
|
||||||
|
std::string method;
|
||||||
|
dict.Get("method", &method);
|
||||||
|
std::string session_name;
|
||||||
|
dict.Get("session", &session_name);
|
||||||
|
|
||||||
|
auto session = Session::FromPartition(args->isolate(), session_name);
|
||||||
|
|
||||||
|
auto browser_context = session->browser_context();
|
||||||
|
|
||||||
|
//auto url_request_context_getter = browser_context->url_request_context_getter();
|
||||||
|
// auto url_request_context = url_request_context_getter->GetURLRequestContext();
|
||||||
|
|
||||||
|
//auto net_url_request = url_request_context->CreateRequest(GURL(url),
|
||||||
|
// net::RequestPriority::DEFAULT_PRIORITY,
|
||||||
|
// nullptr);
|
||||||
|
// net_url_request->set_method(method);
|
||||||
|
|
||||||
|
// auto atom_url_request = new URLRequest(args->isolate(), args->GetThis(), net_url_request.release());
|
||||||
|
|
||||||
|
auto api_url_request = new URLRequest(args->isolate(), args->GetThis());
|
||||||
|
auto weak_ptr = api_url_request->weak_ptr_factory_.GetWeakPtr();
|
||||||
|
auto atom_url_request = AtomURLRequest::create(browser_context, url, weak_ptr);
|
||||||
|
|
||||||
|
atom_url_request->set_method(method);
|
||||||
|
|
||||||
|
api_url_request->atom_url_request_ = atom_url_request;
|
||||||
|
|
||||||
|
|
||||||
|
return api_url_request;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
// static
|
// static
|
||||||
void URLRequest::BuildPrototype(v8::Isolate* isolate,
|
void URLRequest::BuildPrototype(v8::Isolate* isolate,
|
||||||
v8::Local<v8::FunctionTemplate> prototype) {
|
v8::Local<v8::FunctionTemplate> prototype) {
|
||||||
prototype->SetClassName(mate::StringToV8(isolate, "WebRequest"));
|
prototype->SetClassName(mate::StringToV8(isolate, "URLRequest"));
|
||||||
mate::ObjectTemplateBuilder(isolate, prototype->PrototypeTemplate())
|
mate::ObjectTemplateBuilder(isolate, prototype->PrototypeTemplate())
|
||||||
|
.MakeDestroyable()
|
||||||
.SetMethod("start", &URLRequest::start);
|
.SetMethod("start", &URLRequest::start);
|
||||||
}
|
}
|
||||||
|
|
||||||
void URLRequest::start() {
|
void URLRequest::start() {
|
||||||
|
pin();
|
||||||
|
atom_url_request_->Start();
|
||||||
|
}
|
||||||
|
|
||||||
|
void URLRequest::stop() {
|
||||||
|
|
||||||
|
}
|
||||||
|
void URLRequest::OnResponseStarted() {
|
||||||
|
Emit("response-started");
|
||||||
|
}
|
||||||
|
|
||||||
|
void URLRequest::pin() {
|
||||||
|
if (wrapper_.IsEmpty()) {
|
||||||
|
wrapper_.Reset(isolate(), GetWrapper());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void URLRequest::unpin() {
|
||||||
|
wrapper_.Reset();
|
||||||
}
|
}
|
||||||
|
|
||||||
} // namespace mate
|
} // namespace mate
|
||||||
|
|
|
@ -7,13 +7,15 @@
|
||||||
|
|
||||||
#include "atom/browser/api/trackable_object.h"
|
#include "atom/browser/api/trackable_object.h"
|
||||||
#include "native_mate/handle.h"
|
#include "native_mate/handle.h"
|
||||||
|
#include "net/url_request/url_request_context.h"
|
||||||
|
|
||||||
namespace atom {
|
namespace atom {
|
||||||
|
|
||||||
|
class AtomURLRequest;
|
||||||
|
|
||||||
namespace api {
|
namespace api {
|
||||||
|
|
||||||
|
class URLRequest : public mate::EventEmitter<URLRequest> {
|
||||||
class URLRequest : public mate::TrackableObject<URLRequest> {
|
|
||||||
public:
|
public:
|
||||||
static mate::WrappableBase* New(mate::Arguments* args);
|
static mate::WrappableBase* New(mate::Arguments* args);
|
||||||
|
|
||||||
|
@ -21,12 +23,23 @@ class URLRequest : public mate::TrackableObject<URLRequest> {
|
||||||
v8::Local<v8::FunctionTemplate> prototype);
|
v8::Local<v8::FunctionTemplate> prototype);
|
||||||
|
|
||||||
void start();
|
void start();
|
||||||
|
void stop();
|
||||||
|
void OnResponseStarted();
|
||||||
protected:
|
protected:
|
||||||
URLRequest(v8::Isolate* isolate);
|
URLRequest(v8::Isolate* isolate,
|
||||||
|
v8::Local<v8::Object> wrapper);
|
||||||
~URLRequest() override;
|
~URLRequest() override;
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
private:
|
private:
|
||||||
|
void pin();
|
||||||
|
void unpin();
|
||||||
|
|
||||||
|
scoped_refptr<AtomURLRequest> atom_url_request_;
|
||||||
|
v8::Global<v8::Object> wrapper_;
|
||||||
|
base::WeakPtrFactory<URLRequest> weak_ptr_factory_;
|
||||||
|
|
||||||
DISALLOW_COPY_AND_ASSIGN(URLRequest);
|
DISALLOW_COPY_AND_ASSIGN(URLRequest);
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
81
atom/browser/net/atom_url_request.cc
Normal file
81
atom/browser/net/atom_url_request.cc
Normal file
|
@ -0,0 +1,81 @@
|
||||||
|
// Copyright (c) 2013 GitHub, Inc.
|
||||||
|
// Copyright (c) 2011 The Chromium Authors. All rights reserved.
|
||||||
|
// Use of this source code is governed by the MIT license that can be
|
||||||
|
// found in the LICENSE file.
|
||||||
|
|
||||||
|
#include "atom/browser/net/atom_url_request.h"
|
||||||
|
#include "atom/browser/api/atom_api_url_request.h"
|
||||||
|
#include "atom/browser/atom_browser_context.h"
|
||||||
|
#include "base/callback.h"
|
||||||
|
#include "content/public/browser/browser_thread.h"
|
||||||
|
|
||||||
|
namespace atom {
|
||||||
|
|
||||||
|
AtomURLRequest::AtomURLRequest(base::WeakPtr<api::URLRequest> delegate)
|
||||||
|
: delegate_(delegate) {
|
||||||
|
}
|
||||||
|
|
||||||
|
AtomURLRequest::~AtomURLRequest() {
|
||||||
|
}
|
||||||
|
|
||||||
|
scoped_refptr<AtomURLRequest> AtomURLRequest::create(
|
||||||
|
AtomBrowserContext* browser_context,
|
||||||
|
const std::string& url,
|
||||||
|
base::WeakPtr<api::URLRequest> delegate) {
|
||||||
|
|
||||||
|
|
||||||
|
auto url_request_context_getter = browser_context->url_request_context_getter();
|
||||||
|
auto url_request_context = url_request_context_getter->GetURLRequestContext();
|
||||||
|
|
||||||
|
auto net_url_request = url_request_context->CreateRequest(GURL(url),
|
||||||
|
net::RequestPriority::DEFAULT_PRIORITY,
|
||||||
|
nullptr);
|
||||||
|
// net_url_request->set_method(method);
|
||||||
|
|
||||||
|
scoped_refptr<AtomURLRequest> atom_url_request = new AtomURLRequest(delegate);
|
||||||
|
|
||||||
|
net_url_request->set_delegate(atom_url_request.get());
|
||||||
|
|
||||||
|
atom_url_request->url_request_ = std::move(net_url_request);
|
||||||
|
|
||||||
|
return atom_url_request;
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
void AtomURLRequest::Start() {
|
||||||
|
// post to io thread
|
||||||
|
content::BrowserThread::PostTask(
|
||||||
|
content::BrowserThread::IO, FROM_HERE,
|
||||||
|
base::Bind(&AtomURLRequest::StartOnIOThread, this));
|
||||||
|
}
|
||||||
|
|
||||||
|
void AtomURLRequest::StartOnIOThread() {
|
||||||
|
url_request_->Start();
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
void AtomURLRequest::set_method(const std::string& method) {
|
||||||
|
url_request_->set_method(method);
|
||||||
|
}
|
||||||
|
|
||||||
|
void AtomURLRequest::OnResponseStarted(net::URLRequest* request)
|
||||||
|
{
|
||||||
|
// post to main thread
|
||||||
|
content::BrowserThread::PostTask(
|
||||||
|
content::BrowserThread::UI, FROM_HERE,
|
||||||
|
base::Bind(&AtomURLRequest::InformDelegeteResponseStarted, this));
|
||||||
|
}
|
||||||
|
|
||||||
|
void AtomURLRequest::OnReadCompleted(net::URLRequest* request, int bytes_read)
|
||||||
|
{
|
||||||
|
// post to main thread
|
||||||
|
}
|
||||||
|
|
||||||
|
void AtomURLRequest::InformDelegeteResponseStarted() {
|
||||||
|
if (delegate_) {
|
||||||
|
delegate_->OnResponseStarted();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
} // namespace atom
|
52
atom/browser/net/atom_url_request.h
Normal file
52
atom/browser/net/atom_url_request.h
Normal file
|
@ -0,0 +1,52 @@
|
||||||
|
// Copyright (c) 2013 GitHub, Inc.
|
||||||
|
// Copyright (c) 2011 The Chromium Authors. All rights reserved.
|
||||||
|
// Use of this source code is governed by the MIT license that can be
|
||||||
|
// found in the LICENSE file.
|
||||||
|
|
||||||
|
#ifndef ATOM_BROWSER_NET_ATOM_URL_REQUEST_H_
|
||||||
|
#define ATOM_BROWSER_NET_ATOM_URL_REQUEST_H_
|
||||||
|
|
||||||
|
#include "base/memory/ref_counted.h"
|
||||||
|
#include "net/url_request/url_request.h"
|
||||||
|
|
||||||
|
namespace atom {
|
||||||
|
|
||||||
|
class AtomBrowserContext;
|
||||||
|
|
||||||
|
namespace api {
|
||||||
|
class URLRequest;
|
||||||
|
}
|
||||||
|
|
||||||
|
class AtomURLRequest : public base::RefCountedThreadSafe<AtomURLRequest>,
|
||||||
|
public net::URLRequest::Delegate {
|
||||||
|
public:
|
||||||
|
static scoped_refptr<AtomURLRequest> create(
|
||||||
|
AtomBrowserContext* browser_context,
|
||||||
|
const std::string& url,
|
||||||
|
base::WeakPtr<api::URLRequest> delegate);
|
||||||
|
|
||||||
|
void Start();
|
||||||
|
void set_method(const std::string& method);
|
||||||
|
|
||||||
|
protected:
|
||||||
|
// Overrides of net::URLRequest::Delegate
|
||||||
|
virtual void OnResponseStarted(net::URLRequest* request) override;
|
||||||
|
virtual void OnReadCompleted(net::URLRequest* request, int bytes_read) override;
|
||||||
|
|
||||||
|
private:
|
||||||
|
friend class base::RefCountedThreadSafe<AtomURLRequest>;
|
||||||
|
void StartOnIOThread();
|
||||||
|
void InformDelegeteResponseStarted();
|
||||||
|
|
||||||
|
AtomURLRequest(base::WeakPtr<api::URLRequest> delegate);
|
||||||
|
virtual ~AtomURLRequest();
|
||||||
|
|
||||||
|
base::WeakPtr<api::URLRequest> delegate_;
|
||||||
|
std::unique_ptr<net::URLRequest> url_request_;
|
||||||
|
|
||||||
|
DISALLOW_COPY_AND_ASSIGN(AtomURLRequest);
|
||||||
|
};
|
||||||
|
|
||||||
|
} // namespace atom
|
||||||
|
|
||||||
|
#endif // ATOM_BROWSER_NET_ATOM_URL_REQUEST_H_
|
|
@ -238,6 +238,8 @@
|
||||||
'atom/browser/net/atom_network_delegate.h',
|
'atom/browser/net/atom_network_delegate.h',
|
||||||
'atom/browser/net/atom_ssl_config_service.cc',
|
'atom/browser/net/atom_ssl_config_service.cc',
|
||||||
'atom/browser/net/atom_ssl_config_service.h',
|
'atom/browser/net/atom_ssl_config_service.h',
|
||||||
|
'atom/browser/net/atom_url_request.cc',
|
||||||
|
'atom/browser/net/atom_url_request.h',
|
||||||
'atom/browser/net/atom_url_request_job_factory.cc',
|
'atom/browser/net/atom_url_request_job_factory.cc',
|
||||||
'atom/browser/net/atom_url_request_job_factory.h',
|
'atom/browser/net/atom_url_request_job_factory.h',
|
||||||
'atom/browser/net/http_protocol_handler.cc',
|
'atom/browser/net/http_protocol_handler.cc',
|
||||||
|
|
|
@ -107,12 +107,12 @@ Object.defineProperties(exports, {
|
||||||
return require('../web-contents')
|
return require('../web-contents')
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
//net: {
|
net: {
|
||||||
// enumerable: true,
|
enumerable: true,
|
||||||
// get: function () {
|
get: function () {
|
||||||
// return require('../net')
|
return require('../net')
|
||||||
// }
|
}
|
||||||
//},
|
},
|
||||||
|
|
||||||
// The internal modules, invisible unless you know their names.
|
// The internal modules, invisible unless you know their names.
|
||||||
NavigationController: {
|
NavigationController: {
|
||||||
|
|
|
@ -1,13 +1,12 @@
|
||||||
'use strict'
|
'use strict'
|
||||||
|
|
||||||
|
const {EventEmitter} = require('events')
|
||||||
const binding = process.atomBinding('net')
|
const binding = process.atomBinding('net')
|
||||||
const {URLRequest} = binding
|
const {net, Net} = binding
|
||||||
|
const {URLRequest} = net
|
||||||
|
|
||||||
// Public API.
|
Object.setPrototypeOf(Net.prototype, EventEmitter.prototype)
|
||||||
Object.defineProperties(exports, {
|
Object.setPrototypeOf(URLRequest.prototype, EventEmitter.prototype)
|
||||||
URLRequest: {
|
|
||||||
enumerable: true,
|
module.exports = net
|
||||||
value: URLRequest
|
|
||||||
}
|
|
||||||
})
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue