use delegate to notify verification requests

This commit is contained in:
Robo 2015-11-16 18:58:37 +05:30
parent 37e6e6fab7
commit 92c3ee8e16
9 changed files with 38 additions and 53 deletions

View file

@ -13,7 +13,6 @@
#include "atom/browser/api/save_page_handler.h" #include "atom/browser/api/save_page_handler.h"
#include "atom/browser/atom_browser_context.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/browser.h"
#include "atom/common/native_mate_converters/callback.h" #include "atom/common/native_mate_converters/callback.h"
#include "atom/common/native_mate_converters/gurl_converter.h" #include "atom/common/native_mate_converters/gurl_converter.h"
#include "atom/common/native_mate_converters/file_path_converter.h" #include "atom/common/native_mate_converters/file_path_converter.h"
@ -253,9 +252,7 @@ void PassVerificationResult(
Session::Session(AtomBrowserContext* browser_context) Session::Session(AtomBrowserContext* browser_context)
: browser_context_(browser_context) { : browser_context_(browser_context) {
AttachAsUserData(browser_context); AttachAsUserData(browser_context);
browser_context->cert_verifier()->SetDelegate(this);
// Observe Browser to get certificate verification notification.
Browser::Get()->AddObserver(this);
// Observe DownloadManger to get download notifications. // Observe DownloadManger to get download notifications.
content::BrowserContext::GetDownloadManager(browser_context)-> content::BrowserContext::GetDownloadManager(browser_context)->
@ -265,11 +262,10 @@ Session::Session(AtomBrowserContext* browser_context)
Session::~Session() { Session::~Session() {
content::BrowserContext::GetDownloadManager(browser_context())-> content::BrowserContext::GetDownloadManager(browser_context())->
RemoveObserver(this); RemoveObserver(this);
Browser::Get()->RemoveObserver(this);
Destroy(); Destroy();
} }
void Session::OnCertVerification( void Session::RequestCertVerification(
const scoped_refptr<AtomCertVerifier::CertVerifyRequest>& request) { const scoped_refptr<AtomCertVerifier::CertVerifyRequest>& request) {
bool prevent_default = Emit( bool prevent_default = Emit(
"verify-certificate", "verify-certificate",

View file

@ -9,7 +9,6 @@
#include "atom/browser/api/trackable_object.h" #include "atom/browser/api/trackable_object.h"
#include "atom/browser/atom_cert_verifier.h" #include "atom/browser/atom_cert_verifier.h"
#include "atom/browser/browser_observer.h"
#include "content/public/browser/download_manager.h" #include "content/public/browser/download_manager.h"
#include "native_mate/handle.h" #include "native_mate/handle.h"
#include "net/base/completion_callback.h" #include "net/base/completion_callback.h"
@ -36,7 +35,7 @@ class AtomBrowserContext;
namespace api { namespace api {
class Session: public mate::TrackableObject<Session>, class Session: public mate::TrackableObject<Session>,
public BrowserObserver, public AtomCertVerifier::Delegate,
public content::DownloadManager::Observer { public content::DownloadManager::Observer {
public: public:
using ResolveProxyCallback = base::Callback<void(std::string)>; using ResolveProxyCallback = base::Callback<void(std::string)>;
@ -55,8 +54,8 @@ class Session: public mate::TrackableObject<Session>,
explicit Session(AtomBrowserContext* browser_context); explicit Session(AtomBrowserContext* browser_context);
~Session(); ~Session();
// BrowserObserver: // AtomCertVerifier::Delegate:
void OnCertVerification( void RequestCertVerification(
const scoped_refptr<AtomCertVerifier::CertVerifyRequest>&) override; const scoped_refptr<AtomCertVerifier::CertVerifyRequest>&) override;
// content::DownloadManager::Observer: // content::DownloadManager::Observer:

View file

@ -61,6 +61,7 @@ std::string RemoveWhitespace(const std::string& str) {
AtomBrowserContext::AtomBrowserContext(const std::string& partition, AtomBrowserContext::AtomBrowserContext(const std::string& partition,
bool in_memory) bool in_memory)
: brightray::BrowserContext(partition, in_memory), : brightray::BrowserContext(partition, in_memory),
cert_verifier_(new AtomCertVerifier),
job_factory_(new AtomURLRequestJobFactory), job_factory_(new AtomURLRequestJobFactory),
allow_ntlm_everywhere_(false) { allow_ntlm_everywhere_(false) {
} }
@ -160,7 +161,7 @@ content::BrowserPluginGuestManager* AtomBrowserContext::GetGuestManager() {
} }
net::CertVerifier* AtomBrowserContext::CreateCertVerifier() { net::CertVerifier* AtomBrowserContext::CreateCertVerifier() {
return new AtomCertVerifier; return cert_verifier_;
} }
net::SSLConfigService* AtomBrowserContext::CreateSSLConfigService() { net::SSLConfigService* AtomBrowserContext::CreateSSLConfigService() {

View file

@ -12,6 +12,7 @@
namespace atom { namespace atom {
class AtomDownloadManagerDelegate; class AtomDownloadManagerDelegate;
class AtomCertVerifier;
class AtomURLRequestJobFactory; class AtomURLRequestJobFactory;
class WebViewManager; class WebViewManager;
@ -40,6 +41,8 @@ class AtomBrowserContext : public brightray::BrowserContext {
void AllowNTLMCredentialsForAllDomains(bool should_allow); void AllowNTLMCredentialsForAllDomains(bool should_allow);
AtomCertVerifier* cert_verifier() const { return cert_verifier_; }
AtomURLRequestJobFactory* job_factory() const { return job_factory_; } AtomURLRequestJobFactory* job_factory() const { return job_factory_; }
private: private:
@ -47,6 +50,7 @@ class AtomBrowserContext : public brightray::BrowserContext {
scoped_ptr<WebViewManager> guest_manager_; scoped_ptr<WebViewManager> guest_manager_;
// Managed by brightray::BrowserContext. // Managed by brightray::BrowserContext.
AtomCertVerifier* cert_verifier_;
AtomURLRequestJobFactory* job_factory_; AtomURLRequestJobFactory* job_factory_;
bool allow_ntlm_everywhere_; bool allow_ntlm_everywhere_;

View file

@ -6,7 +6,6 @@
#include "atom/browser/browser.h" #include "atom/browser/browser.h"
#include "atom/common/native_mate_converters/net_converter.h" #include "atom/common/native_mate_converters/net_converter.h"
#include "base/callback_helpers.h"
#include "base/sha1.h" #include "base/sha1.h"
#include "base/stl_util.h" #include "base/stl_util.h"
#include "content/public/browser/browser_thread.h" #include "content/public/browser/browser_thread.h"
@ -55,7 +54,6 @@ void AtomCertVerifier::CertVerifyRequest::RunResult(int result) {
for (auto& callback : callbacks_) for (auto& callback : callbacks_)
callback.Run(result); callback.Run(result);
cert_verifier_->RemoveRequest(this); cert_verifier_->RemoveRequest(this);
Release();
} }
void AtomCertVerifier::CertVerifyRequest::DelegateToDefaultVerifier() { void AtomCertVerifier::CertVerifyRequest::DelegateToDefaultVerifier() {
@ -70,15 +68,11 @@ void AtomCertVerifier::CertVerifyRequest::DelegateToDefaultVerifier() {
verify_result_, verify_result_,
base::Bind(&CertVerifyRequest::RunResult, base::Bind(&CertVerifyRequest::RunResult,
weak_ptr_factory_.GetWeakPtr()), weak_ptr_factory_.GetWeakPtr()),
&new_out_req_, out_req_,
net_log_); net_log_);
if (rv != net::ERR_IO_PENDING && !callbacks_.empty()) { if (rv != net::ERR_IO_PENDING)
for (auto& callback : callbacks_) RunResult(rv);
callback.Run(rv);
cert_verifier_->RemoveRequest(this);
Release();
}
} }
void AtomCertVerifier::CertVerifyRequest::ContinueWithResult(int result) { void AtomCertVerifier::CertVerifyRequest::ContinueWithResult(int result) {
@ -103,7 +97,8 @@ void AtomCertVerifier::CertVerifyRequest::ContinueWithResult(int result) {
weak_ptr_factory_.GetWeakPtr())); weak_ptr_factory_.GetWeakPtr()));
} }
AtomCertVerifier::AtomCertVerifier() { AtomCertVerifier::AtomCertVerifier()
: delegate_(nullptr) {
default_cert_verifier_.reset(net::CertVerifier::CreateDefault()); default_cert_verifier_.reset(net::CertVerifier::CreateDefault());
} }
@ -122,7 +117,7 @@ int AtomCertVerifier::Verify(
const net::BoundNetLog& net_log) { const net::BoundNetLog& net_log) {
DCHECK_CURRENTLY_ON(BrowserThread::IO); DCHECK_CURRENTLY_ON(BrowserThread::IO);
if (callback.is_null() || !verify_result || hostname.empty()) if (callback.is_null() || !verify_result || hostname.empty() || !delegate_)
return net::ERR_INVALID_ARGUMENT; return net::ERR_INVALID_ARGUMENT;
const RequestParams key(cert->fingerprint(), const RequestParams key(cert->fingerprint(),
@ -144,8 +139,8 @@ int AtomCertVerifier::Verify(
requests_.insert(make_scoped_refptr(request)); requests_.insert(make_scoped_refptr(request));
BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, BrowserThread::PostTask(BrowserThread::UI, FROM_HERE,
base::Bind(&Browser::RequestCertVerification, base::Bind(&Delegate::RequestCertVerification,
base::Unretained(Browser::Get()), base::Unretained(delegate_),
make_scoped_refptr(request))); make_scoped_refptr(request)));
} }

View file

@ -38,8 +38,7 @@ class AtomCertVerifier : public net::CertVerifier {
}; };
class CertVerifyRequest class CertVerifyRequest
: public net::CertVerifier::Request, : public base::RefCountedThreadSafe<CertVerifyRequest> {
public base::RefCountedThreadSafe<CertVerifyRequest> {
public: public:
CertVerifyRequest( CertVerifyRequest(
AtomCertVerifier* cert_verifier, AtomCertVerifier* cert_verifier,
@ -58,12 +57,6 @@ class AtomCertVerifier : public net::CertVerifier {
net_log_(net_log), net_log_(net_log),
handled_(false), handled_(false),
weak_ptr_factory_(this) { weak_ptr_factory_(this) {
out_req_->reset(this);
new_out_req_.reset(new net::CertVerifier::Request());
}
~CertVerifyRequest() {
out_req_->reset();
} }
void RunResult(int result); void RunResult(int result);
@ -84,6 +77,7 @@ class AtomCertVerifier : public net::CertVerifier {
private: private:
friend class base::RefCountedThreadSafe<CertVerifyRequest>; friend class base::RefCountedThreadSafe<CertVerifyRequest>;
~CertVerifyRequest() {}
AtomCertVerifier* cert_verifier_; AtomCertVerifier* cert_verifier_;
const RequestParams key_; const RequestParams key_;
@ -92,7 +86,6 @@ class AtomCertVerifier : public net::CertVerifier {
scoped_refptr<net::CRLSet> crl_set_; scoped_refptr<net::CRLSet> crl_set_;
net::CertVerifyResult* verify_result_; net::CertVerifyResult* verify_result_;
scoped_ptr<Request>* out_req_; scoped_ptr<Request>* out_req_;
scoped_ptr<Request> new_out_req_;
const net::BoundNetLog net_log_; const net::BoundNetLog net_log_;
std::vector<net::CompletionCallback> callbacks_; std::vector<net::CompletionCallback> callbacks_;
@ -103,8 +96,22 @@ class AtomCertVerifier : public net::CertVerifier {
DISALLOW_COPY_AND_ASSIGN(CertVerifyRequest); DISALLOW_COPY_AND_ASSIGN(CertVerifyRequest);
}; };
class Delegate {
public:
Delegate() {}
virtual ~Delegate() {}
// Called on UI thread.
virtual void RequestCertVerification(
const scoped_refptr<CertVerifyRequest>& request) {}
};
AtomCertVerifier(); AtomCertVerifier();
~AtomCertVerifier() override; virtual ~AtomCertVerifier();
void SetDelegate(Delegate* delegate) {
delegate_ = delegate;
}
protected: protected:
// net::CertVerifier: // net::CertVerifier:
@ -146,6 +153,8 @@ class AtomCertVerifier : public net::CertVerifier {
CertVerifyRequestComparator>; CertVerifyRequestComparator>;
ActiveRequestSet requests_; ActiveRequestSet requests_;
Delegate* delegate_;
scoped_ptr<net::CertVerifier> default_cert_verifier_; scoped_ptr<net::CertVerifier> default_cert_verifier_;
DISALLOW_COPY_AND_ASSIGN(AtomCertVerifier); DISALLOW_COPY_AND_ASSIGN(AtomCertVerifier);

View file

@ -7,7 +7,6 @@
#include <string> #include <string>
#include "atom/browser/atom_browser_main_parts.h" #include "atom/browser/atom_browser_main_parts.h"
#include "atom/browser/atom_cert_verifier.h"
#include "atom/browser/native_window.h" #include "atom/browser/native_window.h"
#include "atom/browser/window_list.h" #include "atom/browser/window_list.h"
#include "base/message_loop/message_loop.h" #include "base/message_loop/message_loop.h"
@ -157,13 +156,6 @@ void Browser::RequestLogin(LoginHandler* login_handler) {
FOR_EACH_OBSERVER(BrowserObserver, observers_, OnLogin(login_handler)); FOR_EACH_OBSERVER(BrowserObserver, observers_, OnLogin(login_handler));
} }
void Browser::RequestCertVerification(
const scoped_refptr<AtomCertVerifier::CertVerifyRequest>& request) {
FOR_EACH_OBSERVER(BrowserObserver,
observers_,
OnCertVerification(request));
}
void Browser::NotifyAndShutdown() { void Browser::NotifyAndShutdown() {
if (is_shutdown_) if (is_shutdown_)
return; return;

View file

@ -29,7 +29,6 @@ class MenuModel;
namespace atom { namespace atom {
class AtomCertVerifier;
class LoginHandler; class LoginHandler;
// This class is used for control application-wide operations. // This class is used for control application-wide operations.
@ -136,10 +135,6 @@ class Browser : public WindowListObserver {
// Request basic auth login. // Request basic auth login.
void RequestLogin(LoginHandler* login_handler); void RequestLogin(LoginHandler* login_handler);
// Request Server Certificate Verification.
void RequestCertVerification(
const scoped_refptr<AtomCertVerifier::CertVerifyRequest>& request);
void AddObserver(BrowserObserver* obs) { void AddObserver(BrowserObserver* obs) {
observers_.AddObserver(obs); observers_.AddObserver(obs);
} }

View file

@ -7,7 +7,6 @@
#include <string> #include <string>
#include "atom/browser/atom_cert_verifier.h"
#include "base/memory/scoped_ptr.h" #include "base/memory/scoped_ptr.h"
#include "content/public/browser/client_certificate_delegate.h" #include "content/public/browser/client_certificate_delegate.h"
@ -17,7 +16,6 @@ class WebContents;
namespace net { namespace net {
class SSLCertRequestInfo; class SSLCertRequestInfo;
class X509Certificate;
} }
namespace atom { namespace atom {
@ -64,10 +62,6 @@ class BrowserObserver {
// The browser requests HTTP login. // The browser requests HTTP login.
virtual void OnLogin(LoginHandler* login_handler) {} virtual void OnLogin(LoginHandler* login_handler) {}
// The browser requests Server Certificate Verification.
virtual void OnCertVerification(
const scoped_refptr<AtomCertVerifier::CertVerifyRequest>& request) {}
protected: protected:
virtual ~BrowserObserver() {} virtual ~BrowserObserver() {}
}; };