NaCl docs: add sanitizers to GSoC ideas
[chromium-blink-merge.git] / chrome / browser / ui / crypto_module_delegate_nss.cc
blob5812e5491eafe72163eea83e005b3faaa032ba66
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"
8 #include "base/bind.h"
9 #include "chrome/browser/net/nss_context.h"
10 #include "content/public/browser/browser_thread.h"
12 using content::BrowserThread;
14 namespace {
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) {
21 if (!slot) {
22 callback.Run(scoped_ptr<ChromeNSSCryptoModuleDelegate>());
23 return;
25 callback.Run(scoped_ptr<ChromeNSSCryptoModuleDelegate>(
26 new ChromeNSSCryptoModuleDelegate(reason, server, slot.Pass())));
29 } // namespace
31 ChromeNSSCryptoModuleDelegate::ChromeNSSCryptoModuleDelegate(
32 chrome::CryptoModulePasswordReason reason,
33 const net::HostPortPair& server,
34 crypto::ScopedPK11Slot slot)
35 : reason_(reason),
36 server_(server),
37 event_(false, false),
38 cancelled_(false),
39 slot_(slot.Pass()) {
42 ChromeNSSCryptoModuleDelegate::~ChromeNSSCryptoModuleDelegate() {}
44 // static
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>)>&
50 callback) {
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);
59 if (slot)
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() {
65 return slot_.Pass();
68 std::string ChromeNSSCryptoModuleDelegate::RequestPassword(
69 const std::string& slot_name,
70 bool retry,
71 bool* cancelled) {
72 DCHECK(!event_.IsSignaled());
73 event_.Reset();
75 if (BrowserThread::PostTask(
76 BrowserThread::UI,
77 FROM_HERE,
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),
82 slot_name,
83 retry))) {
84 event_.Wait();
86 *cancelled = cancelled_;
87 return password_;
90 void ChromeNSSCryptoModuleDelegate::ShowDialog(const std::string& slot_name,
91 bool retry) {
92 DCHECK_CURRENTLY_ON(BrowserThread::UI);
93 ShowCryptoModulePasswordDialog(
94 slot_name,
95 retry,
96 reason_,
97 server_.host(),
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;
108 else
109 cancelled_ = true;
110 event_.Signal();
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
119 // unnecessary.
120 return new ChromeNSSCryptoModuleDelegate(
121 reason, server, crypto::ScopedPK11Slot());