diff --git a/chromium_src/chrome/browser/certificate_manager_model.cc b/chromium_src/chrome/browser/certificate_manager_model.cc index 4766578bc846..c8f580fa4baa 100644 --- a/chromium_src/chrome/browser/certificate_manager_model.cc +++ b/chromium_src/chrome/browser/certificate_manager_model.cc @@ -69,12 +69,12 @@ net::NSSCertDatabase* GetNSSCertDatabaseForResourceContext( // static void CertificateManagerModel::Create(content::BrowserContext* browser_context, - const CreationCallback& callback) { + CreationCallback callback) { DCHECK_CURRENTLY_ON(BrowserThread::UI); - base::PostTask( - FROM_HERE, {BrowserThread::IO}, - base::BindOnce(&CertificateManagerModel::GetCertDBOnIOThread, - browser_context->GetResourceContext(), callback)); + base::PostTask(FROM_HERE, {BrowserThread::IO}, + base::BindOnce(&CertificateManagerModel::GetCertDBOnIOThread, + browser_context->GetResourceContext(), + std::move(callback))); } CertificateManagerModel::CertificateManagerModel( @@ -129,17 +129,17 @@ bool CertificateManagerModel::Delete(CERTCertificate* cert) { void CertificateManagerModel::DidGetCertDBOnUIThread( net::NSSCertDatabase* cert_db, bool is_user_db_available, - const CreationCallback& callback) { + CreationCallback callback) { DCHECK_CURRENTLY_ON(BrowserThread::UI); std::unique_ptr model( new CertificateManagerModel(cert_db, is_user_db_available)); - callback.Run(std::move(model)); + std::move(callback).Run(std::move(model)); } // static void CertificateManagerModel::DidGetCertDBOnIOThread( - const CreationCallback& callback, + CreationCallback callback, net::NSSCertDatabase* cert_db) { DCHECK_CURRENTLY_ON(BrowserThread::IO); @@ -147,17 +147,24 @@ void CertificateManagerModel::DidGetCertDBOnIOThread( base::PostTask( FROM_HERE, {BrowserThread::UI}, base::BindOnce(&CertificateManagerModel::DidGetCertDBOnUIThread, cert_db, - is_user_db_available, callback)); + is_user_db_available, std::move(callback))); } // static void CertificateManagerModel::GetCertDBOnIOThread( content::ResourceContext* context, - const CreationCallback& callback) { + CreationCallback callback) { DCHECK_CURRENTLY_ON(BrowserThread::IO); - net::NSSCertDatabase* cert_db = GetNSSCertDatabaseForResourceContext( - context, base::BindOnce(&CertificateManagerModel::DidGetCertDBOnIOThread, - callback)); + + auto did_get_cert_db_callback = base::AdaptCallbackForRepeating( + base::BindOnce(&CertificateManagerModel::DidGetCertDBOnIOThread, + std::move(callback))); + + net::NSSCertDatabase* cert_db = + GetNSSCertDatabaseForResourceContext(context, did_get_cert_db_callback); + + // If the NSS database was already available, |cert_db| is non-null and + // |did_get_cert_db_callback| has not been called. Call it explicitly. if (cert_db) - DidGetCertDBOnIOThread(callback, cert_db); + did_get_cert_db_callback.Run(cert_db); } diff --git a/chromium_src/chrome/browser/certificate_manager_model.h b/chromium_src/chrome/browser/certificate_manager_model.h index fc166fc43eda..b68a650e24b1 100644 --- a/chromium_src/chrome/browser/certificate_manager_model.h +++ b/chromium_src/chrome/browser/certificate_manager_model.h @@ -24,14 +24,14 @@ class ResourceContext; // manager dialog, and processes changes from the view. class CertificateManagerModel { public: - typedef base::Callback)> - CreationCallback; + using CreationCallback = + base::OnceCallback)>; // Creates a CertificateManagerModel. The model will be passed to the callback // when it is ready. The caller must ensure the model does not outlive the // |browser_context|. static void Create(content::BrowserContext* browser_context, - const CreationCallback& callback); + CreationCallback callback); ~CertificateManagerModel(); @@ -100,11 +100,11 @@ class CertificateManagerModel { // file for details. static void DidGetCertDBOnUIThread(net::NSSCertDatabase* cert_db, bool is_user_db_available, - const CreationCallback& callback); - static void DidGetCertDBOnIOThread(const CreationCallback& callback, + CreationCallback callback); + static void DidGetCertDBOnIOThread(CreationCallback callback, net::NSSCertDatabase* cert_db); static void GetCertDBOnIOThread(content::ResourceContext* context, - const CreationCallback& callback); + CreationCallback callback); net::NSSCertDatabase* cert_db_; // Whether the certificate database has a public slot associated with the diff --git a/shell/browser/api/electron_api_app.cc b/shell/browser/api/electron_api_app.cc index 95c1f81ef7e6..d5ac93194fde 100644 --- a/shell/browser/api/electron_api_app.cc +++ b/shell/browser/api/electron_api_app.cc @@ -497,14 +497,19 @@ void OnClientCertificateSelected( } #if defined(USE_NSS_CERTS) -int ImportIntoCertStore(CertificateManagerModel* model, - const base::DictionaryValue& options) { +int ImportIntoCertStore(CertificateManagerModel* model, base::Value options) { std::string file_data, cert_path; base::string16 password; net::ScopedCERTCertificateList imported_certs; int rv = -1; - options.GetString("certificate", &cert_path); - options.GetString("password", &password); + + std::string* cert_path_ptr = options.FindStringKey("certificate"); + if (cert_path_ptr) + cert_path = *cert_path_ptr; + + std::string* pwd = options.FindStringKey("password"); + if (pwd) + password = base::UTF8ToUTF16(*pwd); if (!cert_path.empty()) { if (base::ReadFileToString(base::FilePath(cert_path), &file_data)) { @@ -1071,31 +1076,36 @@ Browser::LoginItemSettings App::GetLoginItemSettings( } #if defined(USE_NSS_CERTS) -void App::ImportCertificate(const base::DictionaryValue& options, - net::CompletionRepeatingCallback callback) { - auto browser_context = ElectronBrowserContext::From("", false); - if (!certificate_manager_model_) { - auto copy = base::DictionaryValue::From( - base::Value::ToUniquePtrValue(options.Clone())); - CertificateManagerModel::Create( - browser_context.get(), - base::BindRepeating(&App::OnCertificateManagerModelCreated, - base::Unretained(this), base::Passed(©), - callback)); +void App::ImportCertificate(gin_helper::ErrorThrower thrower, + base::Value options, + net::CompletionOnceCallback callback) { + if (!options.is_dict()) { + thrower.ThrowTypeError("Expected options to be an object"); return; } - int rv = ImportIntoCertStore(certificate_manager_model_.get(), options); + auto browser_context = ElectronBrowserContext::From("", false); + if (!certificate_manager_model_) { + CertificateManagerModel::Create( + browser_context.get(), + base::BindOnce(&App::OnCertificateManagerModelCreated, + base::Unretained(this), std::move(options), + std::move(callback))); + return; + } + + int rv = + ImportIntoCertStore(certificate_manager_model_.get(), std::move(options)); std::move(callback).Run(rv); } void App::OnCertificateManagerModelCreated( - std::unique_ptr options, + base::Value options, net::CompletionOnceCallback callback, std::unique_ptr model) { certificate_manager_model_ = std::move(model); int rv = - ImportIntoCertStore(certificate_manager_model_.get(), *(options.get())); + ImportIntoCertStore(certificate_manager_model_.get(), std::move(options)); std::move(callback).Run(rv); } #endif diff --git a/shell/browser/api/electron_api_app.h b/shell/browser/api/electron_api_app.h index 6ca93737e263..2e9413d9ef30 100644 --- a/shell/browser/api/electron_api_app.h +++ b/shell/browser/api/electron_api_app.h @@ -62,7 +62,7 @@ class App : public ElectronBrowserClient::Delegate, #if defined(USE_NSS_CERTS) void OnCertificateManagerModelCreated( - std::unique_ptr options, + base::Value options, net::CompletionOnceCallback callback, std::unique_ptr model); #endif @@ -184,8 +184,9 @@ class App : public ElectronBrowserClient::Delegate, bool enabled); Browser::LoginItemSettings GetLoginItemSettings(gin_helper::Arguments* args); #if defined(USE_NSS_CERTS) - void ImportCertificate(const base::DictionaryValue& options, - net::CompletionRepeatingCallback callback); + void ImportCertificate(gin_helper::ErrorThrower thrower, + base::Value options, + net::CompletionOnceCallback callback); #endif v8::Local GetFileIcon(const base::FilePath& path, gin_helper::Arguments* args); diff --git a/shell/browser/electron_javascript_dialog_manager.cc b/shell/browser/electron_javascript_dialog_manager.cc index 1aa1247d1b71..ff3c7d6aa945 100644 --- a/shell/browser/electron_javascript_dialog_manager.cc +++ b/shell/browser/electron_javascript_dialog_manager.cc @@ -107,8 +107,7 @@ void ElectronJavaScriptDialogManager::RunJavaScriptDialog( electron::ShowMessageBox( settings, base::BindOnce(&ElectronJavaScriptDialogManager::OnMessageBoxCallback, - base::Unretained(this), base::Passed(std::move(callback)), - origin)); + base::Unretained(this), std::move(callback), origin)); } void ElectronJavaScriptDialogManager::RunBeforeUnloadDialog(