Chromium Code Reviews| Index: chrome/browser/ui/crypto_module_delegate_nss.cc |
| diff --git a/chrome/browser/ui/crypto_module_delegate_nss.cc b/chrome/browser/ui/crypto_module_delegate_nss.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..55fffc7f35e0c957af97c6d8ad8bd51e483510d7 |
| --- /dev/null |
| +++ b/chrome/browser/ui/crypto_module_delegate_nss.cc |
| @@ -0,0 +1,106 @@ |
| +// Copyright (c) 2012 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +#include "chrome/browser/ui/crypto_module_delegate_nss.h" |
| + |
| +#include "base/basictypes.h" |
| +#include "base/bind.h" |
| +#include "chrome/browser/net/nss_context.h" |
| +#include "content/public/browser/browser_thread.h" |
| + |
| +using content::BrowserThread; |
| + |
| +ChromeNSSCryptoModuleDelegate::ChromeNSSCryptoModuleDelegate( |
| + chrome::CryptoModulePasswordReason reason, |
| + const std::string& server) |
| + : event_(false, false), |
| + reason_(reason), |
| + server_(server), |
| + cancelled_(false) {} |
| + |
| +ChromeNSSCryptoModuleDelegate::~ChromeNSSCryptoModuleDelegate() {} |
| + |
| +bool ChromeNSSCryptoModuleDelegate::InitializeSlot( |
| + content::ResourceContext* context, |
| + const base::Closure& initialization_complete_callback) { |
| + DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
| + base::Callback<void(crypto::ScopedPK11Slot)> get_slot_callback; |
| + if (!initialization_complete_callback.is_null()) |
| + get_slot_callback = base::Bind(&ChromeNSSCryptoModuleDelegate::DidGetSlot, |
| + base::Unretained(this), |
| + initialization_complete_callback); |
| + |
| + slot_ = GetPrivateNSSKeySlotForResourceContext(context, get_slot_callback); |
| + return slot_ != NULL; |
|
Ryan Sleevi
2013/12/05 00:23:19
"slot_.get() != NULL" or simply "return slot_"
mattm
2013/12/05 04:41:25
done
|
| +} |
| + |
| +// TODO(mattm): allow choosing which slot to generate and store the key. |
| +crypto::ScopedPK11Slot ChromeNSSCryptoModuleDelegate::RequestSlot() { |
| + return slot_.Pass(); |
| +} |
| + |
| +std::string ChromeNSSCryptoModuleDelegate::RequestPassword( |
| + const std::string& slot_name, |
| + bool retry, |
| + bool* cancelled) { |
| + DCHECK(!BrowserThread::CurrentlyOn(BrowserThread::UI)); |
|
Ryan Sleevi
2013/12/05 00:23:19
Is this necessary, given that event_.Wait() will c
mattm
2013/12/05 04:41:25
Good point. done.
|
| + DCHECK(!event_.IsSignaled()); |
| + event_.Reset(); |
| + |
| + if (BrowserThread::PostTask( |
| + BrowserThread::UI, |
| + FROM_HERE, |
| + base::Bind(&ChromeNSSCryptoModuleDelegate::ShowDialog, |
| + // We block on event_ until the task completes, so |
| + // there's no need to ref-count. |
|
Ryan Sleevi
2013/12/05 00:23:19
comment nit: pronouns
mattm
2013/12/05 04:41:25
Done.
|
| + base::Unretained(this), |
| + slot_name, |
| + retry))) { |
| + event_.Wait(); |
| + } |
| + *cancelled = cancelled_; |
| + return password_; |
| +} |
| + |
| +void ChromeNSSCryptoModuleDelegate::ShowDialog(const std::string& slot_name, |
| + bool retry) { |
| + DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| + ShowCryptoModulePasswordDialog( |
| + slot_name, |
| + retry, |
| + reason_, |
| + server_, |
| + NULL, // TODO(mattm): Supply parent window. |
| + base::Bind(&ChromeNSSCryptoModuleDelegate::GotPassword, |
| + // We block on event_ until the task completes, so |
| + // there's no need to ref-count. |
| + base::Unretained(this))); |
| +} |
| + |
| +void ChromeNSSCryptoModuleDelegate::GotPassword(const char* password) { |
| + if (password) |
| + password_ = password; |
| + else |
| + cancelled_ = true; |
| + event_.Signal(); |
| +} |
| + |
| +void ChromeNSSCryptoModuleDelegate::DidGetSlot(const base::Closure& callback, |
| + crypto::ScopedPK11Slot slot) { |
| + slot_ = slot.Pass(); |
| + callback.Run(); |
| +} |
| + |
| +namespace chrome { |
| + |
| +crypto::CryptoModuleBlockingPasswordDelegate* |
| +CreateCryptoModuleBlockingPasswordDelegate(CryptoModulePasswordReason reason, |
| + const std::string& server) { |
| + // Returns a ChromeNSSCryptoModuleDelegate without calling InitializeSlot. |
| + // Since it is only being used as a CreateCryptoModuleBlockingDialogDelegate, |
| + // initializing the slot handle is unnecessary. |
| + return new ChromeNSSCryptoModuleDelegate(reason, server); |
| +} |
| + |
| +} // namespace chrome |