Make sure BrowserContext is deleted after Protocol
This commit is contained in:
parent
bb10551d23
commit
9372d4df32
2 changed files with 31 additions and 23 deletions
|
@ -5,7 +5,6 @@
|
||||||
#include "atom/browser/api/atom_api_protocol.h"
|
#include "atom/browser/api/atom_api_protocol.h"
|
||||||
|
|
||||||
#include "atom/browser/atom_browser_client.h"
|
#include "atom/browser/atom_browser_client.h"
|
||||||
#include "atom/browser/atom_browser_context.h"
|
|
||||||
#include "atom/browser/atom_browser_main_parts.h"
|
#include "atom/browser/atom_browser_main_parts.h"
|
||||||
#include "atom/browser/net/url_request_async_asar_job.h"
|
#include "atom/browser/net/url_request_async_asar_job.h"
|
||||||
#include "atom/browser/net/url_request_buffer_job.h"
|
#include "atom/browser/net/url_request_buffer_job.h"
|
||||||
|
@ -28,9 +27,10 @@ namespace atom {
|
||||||
namespace api {
|
namespace api {
|
||||||
|
|
||||||
Protocol::Protocol(v8::Isolate* isolate, AtomBrowserContext* browser_context)
|
Protocol::Protocol(v8::Isolate* isolate, AtomBrowserContext* browser_context)
|
||||||
: request_context_getter_(browser_context->GetRequestContext()),
|
: browser_context_(browser_context),
|
||||||
job_factory_(browser_context->job_factory()) {
|
request_context_getter_(browser_context->GetRequestContext()),
|
||||||
CHECK(job_factory_);
|
weak_factory_(this) {
|
||||||
|
CHECK(job_factory());
|
||||||
Init(isolate);
|
Init(isolate);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -48,19 +48,19 @@ void Protocol::UnregisterProtocol(
|
||||||
base::Bind(&Protocol::UnregisterProtocolInIO,
|
base::Bind(&Protocol::UnregisterProtocolInIO,
|
||||||
base::Unretained(this), scheme),
|
base::Unretained(this), scheme),
|
||||||
base::Bind(&Protocol::OnIOCompleted,
|
base::Bind(&Protocol::OnIOCompleted,
|
||||||
base::Unretained(this), callback));
|
GetWeakPtr(), callback));
|
||||||
}
|
}
|
||||||
|
|
||||||
Protocol::ProtocolError Protocol::UnregisterProtocolInIO(
|
Protocol::ProtocolError Protocol::UnregisterProtocolInIO(
|
||||||
const std::string& scheme) {
|
const std::string& scheme) {
|
||||||
if (!job_factory_->HasProtocolHandler(scheme))
|
if (!job_factory()->HasProtocolHandler(scheme))
|
||||||
return PROTOCOL_NOT_REGISTERED;
|
return PROTOCOL_NOT_REGISTERED;
|
||||||
job_factory_->SetProtocolHandler(scheme, nullptr);
|
job_factory()->SetProtocolHandler(scheme, nullptr);
|
||||||
return PROTOCOL_OK;
|
return PROTOCOL_OK;
|
||||||
}
|
}
|
||||||
|
|
||||||
void Protocol::IsProtocolHandled(const std::string& scheme,
|
void Protocol::IsProtocolHandled(const std::string& scheme,
|
||||||
const BooleanCallback& callback) {
|
const BooleanCallback& callback) {
|
||||||
content::BrowserThread::PostTaskAndReplyWithResult(
|
content::BrowserThread::PostTaskAndReplyWithResult(
|
||||||
content::BrowserThread::IO, FROM_HERE,
|
content::BrowserThread::IO, FROM_HERE,
|
||||||
base::Bind(&Protocol::IsProtocolHandledInIO,
|
base::Bind(&Protocol::IsProtocolHandledInIO,
|
||||||
|
@ -69,7 +69,7 @@ void Protocol::IsProtocolHandled(const std::string& scheme,
|
||||||
}
|
}
|
||||||
|
|
||||||
bool Protocol::IsProtocolHandledInIO(const std::string& scheme) {
|
bool Protocol::IsProtocolHandledInIO(const std::string& scheme) {
|
||||||
return job_factory_->IsHandledProtocol(scheme);
|
return job_factory()->IsHandledProtocol(scheme);
|
||||||
}
|
}
|
||||||
|
|
||||||
void Protocol::UninterceptProtocol(
|
void Protocol::UninterceptProtocol(
|
||||||
|
@ -81,15 +81,15 @@ void Protocol::UninterceptProtocol(
|
||||||
base::Bind(&Protocol::UninterceptProtocolInIO,
|
base::Bind(&Protocol::UninterceptProtocolInIO,
|
||||||
base::Unretained(this), scheme),
|
base::Unretained(this), scheme),
|
||||||
base::Bind(&Protocol::OnIOCompleted,
|
base::Bind(&Protocol::OnIOCompleted,
|
||||||
base::Unretained(this), callback));
|
GetWeakPtr(), callback));
|
||||||
}
|
}
|
||||||
|
|
||||||
Protocol::ProtocolError Protocol::UninterceptProtocolInIO(
|
Protocol::ProtocolError Protocol::UninterceptProtocolInIO(
|
||||||
const std::string& scheme) {
|
const std::string& scheme) {
|
||||||
if (!original_protocols_.contains(scheme))
|
if (!original_protocols_.contains(scheme))
|
||||||
return PROTOCOL_NOT_INTERCEPTED;
|
return PROTOCOL_NOT_INTERCEPTED;
|
||||||
job_factory_->ReplaceProtocol(scheme,
|
job_factory()->ReplaceProtocol(scheme,
|
||||||
original_protocols_.take_and_erase(scheme));
|
original_protocols_.take_and_erase(scheme));
|
||||||
return PROTOCOL_OK;
|
return PROTOCOL_OK;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -10,9 +10,11 @@
|
||||||
#include <vector>
|
#include <vector>
|
||||||
|
|
||||||
#include "atom/browser/api/trackable_object.h"
|
#include "atom/browser/api/trackable_object.h"
|
||||||
|
#include "atom/browser/atom_browser_context.h"
|
||||||
#include "atom/browser/net/atom_url_request_job_factory.h"
|
#include "atom/browser/net/atom_url_request_job_factory.h"
|
||||||
#include "base/callback.h"
|
#include "base/callback.h"
|
||||||
#include "base/containers/scoped_ptr_hash_map.h"
|
#include "base/containers/scoped_ptr_hash_map.h"
|
||||||
|
#include "base/memory/weak_ptr.h"
|
||||||
#include "content/public/browser/browser_thread.h"
|
#include "content/public/browser/browser_thread.h"
|
||||||
#include "native_mate/arguments.h"
|
#include "native_mate/arguments.h"
|
||||||
#include "native_mate/dictionary.h"
|
#include "native_mate/dictionary.h"
|
||||||
|
@ -29,9 +31,6 @@ class URLRequestContextGetter;
|
||||||
|
|
||||||
namespace atom {
|
namespace atom {
|
||||||
|
|
||||||
class AtomBrowserContext;
|
|
||||||
class AtomURLRequestJobFactory;
|
|
||||||
|
|
||||||
namespace api {
|
namespace api {
|
||||||
|
|
||||||
class Protocol : public mate::TrackableObject<Protocol> {
|
class Protocol : public mate::TrackableObject<Protocol> {
|
||||||
|
@ -50,6 +49,14 @@ class Protocol : public mate::TrackableObject<Protocol> {
|
||||||
protected:
|
protected:
|
||||||
Protocol(v8::Isolate* isolate, AtomBrowserContext* browser_context);
|
Protocol(v8::Isolate* isolate, AtomBrowserContext* browser_context);
|
||||||
|
|
||||||
|
AtomURLRequestJobFactory* job_factory() const {
|
||||||
|
return browser_context_->job_factory();
|
||||||
|
}
|
||||||
|
|
||||||
|
base::WeakPtr<Protocol> GetWeakPtr() {
|
||||||
|
return weak_factory_.GetWeakPtr();
|
||||||
|
}
|
||||||
|
|
||||||
private:
|
private:
|
||||||
// Possible errors.
|
// Possible errors.
|
||||||
enum ProtocolError {
|
enum ProtocolError {
|
||||||
|
@ -107,17 +114,17 @@ class Protocol : public mate::TrackableObject<Protocol> {
|
||||||
base::Bind(&Protocol::RegisterProtocolInIO<RequestJob>,
|
base::Bind(&Protocol::RegisterProtocolInIO<RequestJob>,
|
||||||
base::Unretained(this), scheme, handler),
|
base::Unretained(this), scheme, handler),
|
||||||
base::Bind(&Protocol::OnIOCompleted,
|
base::Bind(&Protocol::OnIOCompleted,
|
||||||
base::Unretained(this), callback));
|
GetWeakPtr(), callback));
|
||||||
}
|
}
|
||||||
template<typename RequestJob>
|
template<typename RequestJob>
|
||||||
ProtocolError RegisterProtocolInIO(const std::string& scheme,
|
ProtocolError RegisterProtocolInIO(const std::string& scheme,
|
||||||
const Handler& handler) {
|
const Handler& handler) {
|
||||||
if (job_factory_->IsHandledProtocol(scheme))
|
if (job_factory()->IsHandledProtocol(scheme))
|
||||||
return PROTOCOL_REGISTERED;
|
return PROTOCOL_REGISTERED;
|
||||||
std::unique_ptr<CustomProtocolHandler<RequestJob>> protocol_handler(
|
std::unique_ptr<CustomProtocolHandler<RequestJob>> protocol_handler(
|
||||||
new CustomProtocolHandler<RequestJob>(
|
new CustomProtocolHandler<RequestJob>(
|
||||||
isolate(), request_context_getter_, handler));
|
isolate(), request_context_getter_, handler));
|
||||||
if (job_factory_->SetProtocolHandler(scheme, std::move(protocol_handler)))
|
if (job_factory()->SetProtocolHandler(scheme, std::move(protocol_handler)))
|
||||||
return PROTOCOL_OK;
|
return PROTOCOL_OK;
|
||||||
else
|
else
|
||||||
return PROTOCOL_FAIL;
|
return PROTOCOL_FAIL;
|
||||||
|
@ -144,15 +151,15 @@ class Protocol : public mate::TrackableObject<Protocol> {
|
||||||
base::Bind(&Protocol::InterceptProtocolInIO<RequestJob>,
|
base::Bind(&Protocol::InterceptProtocolInIO<RequestJob>,
|
||||||
base::Unretained(this), scheme, handler),
|
base::Unretained(this), scheme, handler),
|
||||||
base::Bind(&Protocol::OnIOCompleted,
|
base::Bind(&Protocol::OnIOCompleted,
|
||||||
base::Unretained(this), callback));
|
GetWeakPtr(), callback));
|
||||||
}
|
}
|
||||||
template<typename RequestJob>
|
template<typename RequestJob>
|
||||||
ProtocolError InterceptProtocolInIO(const std::string& scheme,
|
ProtocolError InterceptProtocolInIO(const std::string& scheme,
|
||||||
const Handler& handler) {
|
const Handler& handler) {
|
||||||
if (!job_factory_->IsHandledProtocol(scheme))
|
if (!job_factory()->IsHandledProtocol(scheme))
|
||||||
return PROTOCOL_NOT_REGISTERED;
|
return PROTOCOL_NOT_REGISTERED;
|
||||||
// It is possible a protocol is handled but can not be intercepted.
|
// It is possible a protocol is handled but can not be intercepted.
|
||||||
if (!job_factory_->HasProtocolHandler(scheme))
|
if (!job_factory()->HasProtocolHandler(scheme))
|
||||||
return PROTOCOL_FAIL;
|
return PROTOCOL_FAIL;
|
||||||
if (ContainsKey(original_protocols_, scheme))
|
if (ContainsKey(original_protocols_, scheme))
|
||||||
return PROTOCOL_INTERCEPTED;
|
return PROTOCOL_INTERCEPTED;
|
||||||
|
@ -161,7 +168,7 @@ class Protocol : public mate::TrackableObject<Protocol> {
|
||||||
isolate(), request_context_getter_, handler));
|
isolate(), request_context_getter_, handler));
|
||||||
original_protocols_.set(
|
original_protocols_.set(
|
||||||
scheme,
|
scheme,
|
||||||
job_factory_->ReplaceProtocol(scheme, std::move(protocol_handler)));
|
job_factory()->ReplaceProtocol(scheme, std::move(protocol_handler)));
|
||||||
return PROTOCOL_OK;
|
return PROTOCOL_OK;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -175,6 +182,7 @@ class Protocol : public mate::TrackableObject<Protocol> {
|
||||||
// Convert error code to string.
|
// Convert error code to string.
|
||||||
std::string ErrorCodeToString(ProtocolError error);
|
std::string ErrorCodeToString(ProtocolError error);
|
||||||
|
|
||||||
|
scoped_refptr<AtomBrowserContext> browser_context_;
|
||||||
net::URLRequestContextGetter* request_context_getter_;
|
net::URLRequestContextGetter* request_context_getter_;
|
||||||
|
|
||||||
// Map that stores the original protocols of schemes.
|
// Map that stores the original protocols of schemes.
|
||||||
|
@ -183,7 +191,7 @@ class Protocol : public mate::TrackableObject<Protocol> {
|
||||||
std::unique_ptr<net::URLRequestJobFactory::ProtocolHandler>>;
|
std::unique_ptr<net::URLRequestJobFactory::ProtocolHandler>>;
|
||||||
OriginalProtocolsMap original_protocols_;
|
OriginalProtocolsMap original_protocols_;
|
||||||
|
|
||||||
AtomURLRequestJobFactory* job_factory_; // weak ref
|
base::WeakPtrFactory<Protocol> weak_factory_;
|
||||||
|
|
||||||
DISALLOW_COPY_AND_ASSIGN(Protocol);
|
DISALLOW_COPY_AND_ASSIGN(Protocol);
|
||||||
};
|
};
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue