feat: promisify dialog.showCertificateTrustDialog() (#17181)
* feat: promisify dialog.showCertificateTrustDialog() * update promisification doc
This commit is contained in:
parent
f15d0b1ed7
commit
961c9a88a8
6 changed files with 77 additions and 53 deletions
|
@ -7,7 +7,7 @@
|
|||
|
||||
#include <string>
|
||||
|
||||
#include "base/callback_forward.h"
|
||||
#include "atom/common/promise_util.h"
|
||||
#include "base/memory/ref_counted.h"
|
||||
#include "net/cert/x509_certificate.h"
|
||||
|
||||
|
@ -17,12 +17,10 @@ class NativeWindow;
|
|||
|
||||
namespace certificate_trust {
|
||||
|
||||
typedef base::Callback<void(void)> ShowTrustCallback;
|
||||
|
||||
void ShowCertificateTrust(atom::NativeWindow* parent_window,
|
||||
const scoped_refptr<net::X509Certificate>& cert,
|
||||
const std::string& message,
|
||||
const ShowTrustCallback& callback);
|
||||
v8::Local<v8::Promise> ShowCertificateTrust(
|
||||
atom::NativeWindow* parent_window,
|
||||
const scoped_refptr<net::X509Certificate>& cert,
|
||||
const std::string& message);
|
||||
|
||||
} // namespace certificate_trust
|
||||
|
||||
|
|
|
@ -15,7 +15,7 @@
|
|||
|
||||
@interface TrustDelegate : NSObject {
|
||||
@private
|
||||
certificate_trust::ShowTrustCallback callback_;
|
||||
std::unique_ptr<atom::util::Promise> promise_;
|
||||
SFCertificateTrustPanel* panel_;
|
||||
scoped_refptr<net::X509Certificate> cert_;
|
||||
SecTrustRef trust_;
|
||||
|
@ -23,12 +23,12 @@
|
|||
SecPolicyRef sec_policy_;
|
||||
}
|
||||
|
||||
- (id)initWithCallback:(const certificate_trust::ShowTrustCallback&)callback
|
||||
panel:(SFCertificateTrustPanel*)panel
|
||||
cert:(const scoped_refptr<net::X509Certificate>&)cert
|
||||
trust:(SecTrustRef)trust
|
||||
certChain:(CFArrayRef)certChain
|
||||
secPolicy:(SecPolicyRef)secPolicy;
|
||||
- (id)initWithPromise:(atom::util::Promise)promise
|
||||
panel:(SFCertificateTrustPanel*)panel
|
||||
cert:(const scoped_refptr<net::X509Certificate>&)cert
|
||||
trust:(SecTrustRef)trust
|
||||
certChain:(CFArrayRef)certChain
|
||||
secPolicy:(SecPolicyRef)secPolicy;
|
||||
|
||||
- (void)panelDidEnd:(NSWindow*)sheet
|
||||
returnCode:(int)returnCode
|
||||
|
@ -47,14 +47,14 @@
|
|||
[super dealloc];
|
||||
}
|
||||
|
||||
- (id)initWithCallback:(const certificate_trust::ShowTrustCallback&)callback
|
||||
panel:(SFCertificateTrustPanel*)panel
|
||||
cert:(const scoped_refptr<net::X509Certificate>&)cert
|
||||
trust:(SecTrustRef)trust
|
||||
certChain:(CFArrayRef)certChain
|
||||
secPolicy:(SecPolicyRef)secPolicy {
|
||||
- (id)initWithPromise:(atom::util::Promise)promise
|
||||
panel:(SFCertificateTrustPanel*)panel
|
||||
cert:(const scoped_refptr<net::X509Certificate>&)cert
|
||||
trust:(SecTrustRef)trust
|
||||
certChain:(CFArrayRef)certChain
|
||||
secPolicy:(SecPolicyRef)secPolicy {
|
||||
if ((self = [super init])) {
|
||||
callback_ = callback;
|
||||
promise_.reset(new atom::util::Promise(std::move(promise)));
|
||||
panel_ = panel;
|
||||
cert_ = cert;
|
||||
trust_ = trust;
|
||||
|
@ -73,8 +73,7 @@
|
|||
// now.
|
||||
cert_db->NotifyObserversCertDBChanged();
|
||||
|
||||
callback_.Run();
|
||||
|
||||
promise_->Resolve();
|
||||
[self autorelease];
|
||||
}
|
||||
|
||||
|
@ -82,10 +81,14 @@
|
|||
|
||||
namespace certificate_trust {
|
||||
|
||||
void ShowCertificateTrust(atom::NativeWindow* parent_window,
|
||||
const scoped_refptr<net::X509Certificate>& cert,
|
||||
const std::string& message,
|
||||
const ShowTrustCallback& callback) {
|
||||
v8::Local<v8::Promise> ShowCertificateTrust(
|
||||
atom::NativeWindow* parent_window,
|
||||
const scoped_refptr<net::X509Certificate>& cert,
|
||||
const std::string& message) {
|
||||
v8::Isolate* isolate = v8::Isolate::GetCurrent();
|
||||
atom::util::Promise promise(isolate);
|
||||
v8::Local<v8::Promise> handle = promise.GetHandle();
|
||||
|
||||
auto* sec_policy = SecPolicyCreateBasicX509();
|
||||
auto cert_chain =
|
||||
net::x509_util::CreateSecCertificateArrayForX509Certificate(cert.get());
|
||||
|
@ -98,18 +101,20 @@ void ShowCertificateTrust(atom::NativeWindow* parent_window,
|
|||
auto msg = base::SysUTF8ToNSString(message);
|
||||
|
||||
auto panel = [[SFCertificateTrustPanel alloc] init];
|
||||
auto delegate = [[TrustDelegate alloc] initWithCallback:callback
|
||||
panel:panel
|
||||
cert:cert
|
||||
trust:trust
|
||||
certChain:cert_chain
|
||||
secPolicy:sec_policy];
|
||||
auto delegate = [[TrustDelegate alloc] initWithPromise:std::move(promise)
|
||||
panel:panel
|
||||
cert:cert
|
||||
trust:trust
|
||||
certChain:cert_chain
|
||||
secPolicy:sec_policy];
|
||||
[panel beginSheetForWindow:window
|
||||
modalDelegate:delegate
|
||||
didEndSelector:@selector(panelDidEnd:returnCode:contextInfo:)
|
||||
contextInfo:nil
|
||||
trust:trust
|
||||
message:msg];
|
||||
|
||||
return handle;
|
||||
}
|
||||
|
||||
} // namespace certificate_trust
|
||||
|
|
|
@ -8,7 +8,6 @@
|
|||
|
||||
#include <wincrypt.h>
|
||||
|
||||
#include "base/callback.h"
|
||||
#include "net/cert/cert_database.h"
|
||||
#include "net/cert/x509_util_win.h"
|
||||
|
||||
|
@ -57,14 +56,16 @@ CERT_CHAIN_PARA GetCertificateChainParameters() {
|
|||
return params;
|
||||
}
|
||||
|
||||
void ShowCertificateTrust(atom::NativeWindow* parent_window,
|
||||
const scoped_refptr<net::X509Certificate>& cert,
|
||||
const std::string& message,
|
||||
const ShowTrustCallback& callback) {
|
||||
v8::Local<v8::Promise> ShowCertificateTrust(
|
||||
atom::NativeWindow* parent_window,
|
||||
const scoped_refptr<net::X509Certificate>& cert,
|
||||
const std::string& message) {
|
||||
v8::Isolate* isolate = v8::Isolate::GetCurrent();
|
||||
atom::util::Promise promise(isolate);
|
||||
v8::Local<v8::Promise> handle = promise.GetHandle();
|
||||
|
||||
PCCERT_CHAIN_CONTEXT chain_context;
|
||||
|
||||
auto cert_context = net::x509_util::CreateCertContextWithChain(cert.get());
|
||||
|
||||
auto params = GetCertificateChainParameters();
|
||||
|
||||
if (CertGetCertificateChain(NULL, cert_context.get(), NULL, NULL, ¶ms,
|
||||
|
@ -79,7 +80,8 @@ void ShowCertificateTrust(atom::NativeWindow* parent_window,
|
|||
CertFreeCertificateChain(chain_context);
|
||||
}
|
||||
|
||||
callback.Run();
|
||||
promise.Resolve();
|
||||
return handle;
|
||||
}
|
||||
|
||||
} // namespace certificate_trust
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue