1 // Copyright 2013 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "chrome/browser/ui/crypto_module_delegate_nss.h"
7 #include "base/basictypes.h"
9 #include "chrome/browser/net/nss_context.h"
10 #include "content/public/browser/browser_thread.h"
12 using content::BrowserThread
;
14 ChromeNSSCryptoModuleDelegate::ChromeNSSCryptoModuleDelegate(
15 chrome::CryptoModulePasswordReason reason
,
16 const net::HostPortPair
& server
)
22 ChromeNSSCryptoModuleDelegate::~ChromeNSSCryptoModuleDelegate() {}
24 bool ChromeNSSCryptoModuleDelegate::InitializeSlot(
25 content::ResourceContext
* context
,
26 const base::Closure
& initialization_complete_callback
) {
27 DCHECK_CURRENTLY_ON(BrowserThread::IO
);
29 base::Callback
<void(crypto::ScopedPK11Slot
)> get_slot_callback
;
30 if (!initialization_complete_callback
.is_null())
31 get_slot_callback
= base::Bind(&ChromeNSSCryptoModuleDelegate::DidGetSlot
,
32 // Caller is responsible for keeping |this|
33 // alive until the callback is run.
34 base::Unretained(this),
35 initialization_complete_callback
);
37 slot_
= GetPrivateNSSKeySlotForResourceContext(context
, get_slot_callback
);
38 return slot_
.get() != NULL
;
41 // TODO(mattm): allow choosing which slot to generate and store the key.
42 crypto::ScopedPK11Slot
ChromeNSSCryptoModuleDelegate::RequestSlot() {
46 std::string
ChromeNSSCryptoModuleDelegate::RequestPassword(
47 const std::string
& slot_name
,
50 DCHECK(!event_
.IsSignaled());
53 if (BrowserThread::PostTask(
56 base::Bind(&ChromeNSSCryptoModuleDelegate::ShowDialog
,
57 // This method blocks on |event_| until the task completes,
58 // so there's no need to ref-count.
59 base::Unretained(this),
64 *cancelled
= cancelled_
;
68 void ChromeNSSCryptoModuleDelegate::ShowDialog(const std::string
& slot_name
,
70 DCHECK_CURRENTLY_ON(BrowserThread::UI
);
71 ShowCryptoModulePasswordDialog(
76 NULL
, // TODO(mattm): Supply parent window.
77 base::Bind(&ChromeNSSCryptoModuleDelegate::GotPassword
,
78 // RequestPassword is blocked on |event_| until GotPassword is
79 // called, so there's no need to ref-count.
80 base::Unretained(this)));
83 void ChromeNSSCryptoModuleDelegate::GotPassword(const std::string
& password
) {
84 if (!password
.empty())
91 void ChromeNSSCryptoModuleDelegate::DidGetSlot(const base::Closure
& callback
,
92 crypto::ScopedPK11Slot slot
) {
98 crypto::CryptoModuleBlockingPasswordDelegate
*
99 CreateCryptoModuleBlockingPasswordDelegate(
100 chrome::CryptoModulePasswordReason reason
,
101 const net::HostPortPair
& server
) {
102 // Returns a ChromeNSSCryptoModuleDelegate without calling InitializeSlot.
103 // Since it is only being used as a CreateCryptoModuleBlockingDialogDelegate,
104 // initializing the slot handle is unnecessary.
105 return new ChromeNSSCryptoModuleDelegate(reason
, server
);