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;
16 void CreateWithSlot(chrome::CryptoModulePasswordReason reason,
17 const net::HostPortPair& server,
18 const base::Callback<void(
19 scoped_ptr<ChromeNSSCryptoModuleDelegate>)>& callback,
20 crypto::ScopedPK11Slot slot) {
22 callback.Run(scoped_ptr<ChromeNSSCryptoModuleDelegate>());
25 callback.Run(scoped_ptr<ChromeNSSCryptoModuleDelegate>(
26 new ChromeNSSCryptoModuleDelegate(reason, server, slot.Pass())));
31 ChromeNSSCryptoModuleDelegate::ChromeNSSCryptoModuleDelegate(
32 chrome::CryptoModulePasswordReason reason,
33 const net::HostPortPair& server,
34 crypto::ScopedPK11Slot slot)
42 ChromeNSSCryptoModuleDelegate::~ChromeNSSCryptoModuleDelegate() {}
45 void ChromeNSSCryptoModuleDelegate::CreateForResourceContext(
46 chrome::CryptoModulePasswordReason reason,
47 const net::HostPortPair& server,
48 content::ResourceContext* context,
49 const base::Callback<void(scoped_ptr<ChromeNSSCryptoModuleDelegate>)>&
51 DCHECK_CURRENTLY_ON(BrowserThread::IO);
52 DCHECK(!callback.is_null());
54 base::Callback<void(crypto::ScopedPK11Slot)> get_slot_callback =
55 base::Bind(&CreateWithSlot, reason, server, callback);
57 crypto::ScopedPK11Slot slot =
58 GetPrivateNSSKeySlotForResourceContext(context, get_slot_callback);
60 get_slot_callback.Run(slot.Pass());
63 // TODO(mattm): allow choosing which slot to generate and store the key.
64 crypto::ScopedPK11Slot ChromeNSSCryptoModuleDelegate::RequestSlot() {
68 std::string ChromeNSSCryptoModuleDelegate::RequestPassword(
69 const std::string& slot_name,
72 DCHECK(!event_.IsSignaled());
75 if (BrowserThread::PostTask(
78 base::Bind(&ChromeNSSCryptoModuleDelegate::ShowDialog,
79 // This method blocks on |event_| until the task completes,
80 // so there's no need to ref-count.
81 base::Unretained(this),
86 *cancelled = cancelled_;
90 void ChromeNSSCryptoModuleDelegate::ShowDialog(const std::string& slot_name,
92 DCHECK_CURRENTLY_ON(BrowserThread::UI);
93 ShowCryptoModulePasswordDialog(
98 NULL, // TODO(mattm): Supply parent window.
99 base::Bind(&ChromeNSSCryptoModuleDelegate::GotPassword,
100 // RequestPassword is blocked on |event_| until GotPassword is
101 // called, so there's no need to ref-count.
102 base::Unretained(this)));
105 void ChromeNSSCryptoModuleDelegate::GotPassword(const std::string& password) {
106 if (!password.empty())
107 password_ = password;
113 crypto::CryptoModuleBlockingPasswordDelegate*
114 CreateCryptoModuleBlockingPasswordDelegate(
115 chrome::CryptoModulePasswordReason reason,
116 const net::HostPortPair& server) {
117 // Returns a ChromeNSSCryptoModuleDelegate without Pk11Slot. Since it is only
118 // being used as a CryptoModuleBlockingDialogDelegate, using a slot handle is
120 return new ChromeNSSCryptoModuleDelegate(
121 reason, server, crypto::ScopedPK11Slot());