From 9372d4df32450f839c48e9280d9536dc1d891b47 Mon Sep 17 00:00:00 2001 From: Cheng Zhao Date: Wed, 15 Jun 2016 17:12:45 +0900 Subject: [PATCH] Make sure BrowserContext is deleted after Protocol --- atom/browser/api/atom_api_protocol.cc | 24 ++++++++++++------------ atom/browser/api/atom_api_protocol.h | 30 +++++++++++++++++++----------- 2 files changed, 31 insertions(+), 23 deletions(-) diff --git a/atom/browser/api/atom_api_protocol.cc b/atom/browser/api/atom_api_protocol.cc index db8facf..6ec7f01 100644 --- a/atom/browser/api/atom_api_protocol.cc +++ b/atom/browser/api/atom_api_protocol.cc @@ -5,7 +5,6 @@ #include "atom/browser/api/atom_api_protocol.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/net/url_request_async_asar_job.h" #include "atom/browser/net/url_request_buffer_job.h" @@ -28,9 +27,10 @@ namespace atom { namespace api { Protocol::Protocol(v8::Isolate* isolate, AtomBrowserContext* browser_context) - : request_context_getter_(browser_context->GetRequestContext()), - job_factory_(browser_context->job_factory()) { - CHECK(job_factory_); + : browser_context_(browser_context), + request_context_getter_(browser_context->GetRequestContext()), + weak_factory_(this) { + CHECK(job_factory()); Init(isolate); } @@ -48,19 +48,19 @@ void Protocol::UnregisterProtocol( base::Bind(&Protocol::UnregisterProtocolInIO, base::Unretained(this), scheme), base::Bind(&Protocol::OnIOCompleted, - base::Unretained(this), callback)); + GetWeakPtr(), callback)); } Protocol::ProtocolError Protocol::UnregisterProtocolInIO( const std::string& scheme) { - if (!job_factory_->HasProtocolHandler(scheme)) + if (!job_factory()->HasProtocolHandler(scheme)) return PROTOCOL_NOT_REGISTERED; - job_factory_->SetProtocolHandler(scheme, nullptr); + job_factory()->SetProtocolHandler(scheme, nullptr); return PROTOCOL_OK; } void Protocol::IsProtocolHandled(const std::string& scheme, - const BooleanCallback& callback) { + const BooleanCallback& callback) { content::BrowserThread::PostTaskAndReplyWithResult( content::BrowserThread::IO, FROM_HERE, base::Bind(&Protocol::IsProtocolHandledInIO, @@ -69,7 +69,7 @@ void Protocol::IsProtocolHandled(const std::string& scheme, } bool Protocol::IsProtocolHandledInIO(const std::string& scheme) { - return job_factory_->IsHandledProtocol(scheme); + return job_factory()->IsHandledProtocol(scheme); } void Protocol::UninterceptProtocol( @@ -81,15 +81,15 @@ void Protocol::UninterceptProtocol( base::Bind(&Protocol::UninterceptProtocolInIO, base::Unretained(this), scheme), base::Bind(&Protocol::OnIOCompleted, - base::Unretained(this), callback)); + GetWeakPtr(), callback)); } Protocol::ProtocolError Protocol::UninterceptProtocolInIO( const std::string& scheme) { if (!original_protocols_.contains(scheme)) return PROTOCOL_NOT_INTERCEPTED; - job_factory_->ReplaceProtocol(scheme, - original_protocols_.take_and_erase(scheme)); + job_factory()->ReplaceProtocol(scheme, + original_protocols_.take_and_erase(scheme)); return PROTOCOL_OK; } diff --git a/atom/browser/api/atom_api_protocol.h b/atom/browser/api/atom_api_protocol.h index 734d179..bdcd4a9 100644 --- a/atom/browser/api/atom_api_protocol.h +++ b/atom/browser/api/atom_api_protocol.h @@ -10,9 +10,11 @@ #include #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 "base/callback.h" #include "base/containers/scoped_ptr_hash_map.h" +#include "base/memory/weak_ptr.h" #include "content/public/browser/browser_thread.h" #include "native_mate/arguments.h" #include "native_mate/dictionary.h" @@ -29,9 +31,6 @@ class URLRequestContextGetter; namespace atom { -class AtomBrowserContext; -class AtomURLRequestJobFactory; - namespace api { class Protocol : public mate::TrackableObject { @@ -50,6 +49,14 @@ class Protocol : public mate::TrackableObject { protected: Protocol(v8::Isolate* isolate, AtomBrowserContext* browser_context); + AtomURLRequestJobFactory* job_factory() const { + return browser_context_->job_factory(); + } + + base::WeakPtr GetWeakPtr() { + return weak_factory_.GetWeakPtr(); + } + private: // Possible errors. enum ProtocolError { @@ -107,17 +114,17 @@ class Protocol : public mate::TrackableObject { base::Bind(&Protocol::RegisterProtocolInIO, base::Unretained(this), scheme, handler), base::Bind(&Protocol::OnIOCompleted, - base::Unretained(this), callback)); + GetWeakPtr(), callback)); } template ProtocolError RegisterProtocolInIO(const std::string& scheme, const Handler& handler) { - if (job_factory_->IsHandledProtocol(scheme)) + if (job_factory()->IsHandledProtocol(scheme)) return PROTOCOL_REGISTERED; std::unique_ptr> protocol_handler( new CustomProtocolHandler( 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; else return PROTOCOL_FAIL; @@ -144,15 +151,15 @@ class Protocol : public mate::TrackableObject { base::Bind(&Protocol::InterceptProtocolInIO, base::Unretained(this), scheme, handler), base::Bind(&Protocol::OnIOCompleted, - base::Unretained(this), callback)); + GetWeakPtr(), callback)); } template ProtocolError InterceptProtocolInIO(const std::string& scheme, const Handler& handler) { - if (!job_factory_->IsHandledProtocol(scheme)) + if (!job_factory()->IsHandledProtocol(scheme)) return PROTOCOL_NOT_REGISTERED; // 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; if (ContainsKey(original_protocols_, scheme)) return PROTOCOL_INTERCEPTED; @@ -161,7 +168,7 @@ class Protocol : public mate::TrackableObject { isolate(), request_context_getter_, handler)); original_protocols_.set( scheme, - job_factory_->ReplaceProtocol(scheme, std::move(protocol_handler))); + job_factory()->ReplaceProtocol(scheme, std::move(protocol_handler))); return PROTOCOL_OK; } @@ -175,6 +182,7 @@ class Protocol : public mate::TrackableObject { // Convert error code to string. std::string ErrorCodeToString(ProtocolError error); + scoped_refptr browser_context_; net::URLRequestContextGetter* request_context_getter_; // Map that stores the original protocols of schemes. @@ -183,7 +191,7 @@ class Protocol : public mate::TrackableObject { std::unique_ptr>; OriginalProtocolsMap original_protocols_; - AtomURLRequestJobFactory* job_factory_; // weak ref + base::WeakPtrFactory weak_factory_; DISALLOW_COPY_AND_ASSIGN(Protocol); }; -- 2.7.4