OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "chrome/browser/ui/crypto_module_password_dialog.h" | 5 #include "chrome/browser/ui/crypto_module_delegate_nss.h" |
6 | 6 |
7 #include "base/basictypes.h" | 7 #include "base/basictypes.h" |
8 #include "base/bind.h" | 8 #include "base/bind.h" |
9 #include "base/strings/utf_string_conversions.h" | |
10 #include "base/synchronization/waitable_event.h" | 9 #include "base/synchronization/waitable_event.h" |
| 10 #include "chrome/browser/net/nss_context.h" |
11 #include "content/public/browser/browser_thread.h" | 11 #include "content/public/browser/browser_thread.h" |
12 #include "crypto/crypto_module_blocking_password_delegate.h" | 12 #include "crypto/nss_crypto_module_delegate.h" |
13 #include "grit/generated_resources.h" | |
14 #include "ui/base/l10n/l10n_util.h" | |
15 #include "url/gurl.h" | |
16 | 13 |
17 using content::BrowserThread; | 14 using content::BrowserThread; |
18 | 15 |
19 namespace chrome { | 16 namespace chrome { |
20 | 17 |
21 namespace { | 18 namespace { |
22 | 19 |
23 class CryptoModuleBlockingDialogDelegate | 20 class ChromeNSSCryptoModuleDelegate |
24 : public crypto::CryptoModuleBlockingPasswordDelegate { | 21 : public crypto::NSSCryptoModuleDelegate { |
25 public: | 22 public: |
26 CryptoModuleBlockingDialogDelegate(CryptoModulePasswordReason reason, | 23 ChromeNSSCryptoModuleDelegate(content::ResourceContext* context, |
27 const std::string& server) | 24 CryptoModulePasswordReason reason, |
| 25 const std::string& server) |
28 : event_(false, false), | 26 : event_(false, false), |
| 27 context_(context), |
29 reason_(reason), | 28 reason_(reason), |
30 server_(server), | 29 server_(server), |
31 cancelled_(false) { | 30 cancelled_(false) {} |
| 31 |
| 32 virtual ~ChromeNSSCryptoModuleDelegate() {} |
| 33 |
| 34 // crypto::NSSCryptoModuleDelegate implementation. |
| 35 virtual void Initialize(const base::Closure& callback) OVERRIDE { |
| 36 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
| 37 |
| 38 content::ResourceContext* context = context_; |
| 39 context_ = NULL; |
| 40 OnPrivateNSSKeySlotForResourceContextReady( |
| 41 context, |
| 42 base::Bind(&ChromeNSSCryptoModuleDelegate::DidGetSlot, |
| 43 // Caller is responsible for keeping us alive until we run |
| 44 // the callback. |
| 45 base::Unretained(this), |
| 46 callback)); |
32 } | 47 } |
33 | 48 |
34 virtual ~CryptoModuleBlockingDialogDelegate() { | 49 // TODO(mattm): allow choosing which slot to generate and store the key. |
35 // Make sure we clear the password in memory. | 50 virtual crypto::ScopedPK11Slot RequestSlot() OVERRIDE { |
36 password_.replace(0, password_.size(), password_.size(), 0); | 51 return slot_.Pass(); |
37 } | 52 } |
38 | 53 |
39 // crypto::CryptoModuleBlockingDialogDelegate implementation. | 54 // crypto::CryptoModuleBlockingPasswordDelegate implementation. |
40 virtual std::string RequestPassword(const std::string& slot_name, | 55 virtual std::string RequestPassword(const std::string& slot_name, |
41 bool retry, | 56 bool retry, |
42 bool* cancelled) OVERRIDE { | 57 bool* cancelled) OVERRIDE { |
43 DCHECK(!BrowserThread::CurrentlyOn(BrowserThread::UI)); | 58 DCHECK(!BrowserThread::CurrentlyOn(BrowserThread::UI)); |
44 DCHECK(!event_.IsSignaled()); | 59 DCHECK(!event_.IsSignaled()); |
45 event_.Reset(); | 60 event_.Reset(); |
46 | 61 |
47 if (BrowserThread::PostTask( | 62 if (BrowserThread::PostTask( |
48 BrowserThread::UI, FROM_HERE, | 63 BrowserThread::UI, FROM_HERE, |
49 base::Bind(&CryptoModuleBlockingDialogDelegate::ShowDialog, | 64 base::Bind(&ChromeNSSCryptoModuleDelegate::ShowDialog, |
50 // We block on event_ until the task completes, so | 65 // We block on event_ until the task completes, so |
51 // there's no need to ref-count. | 66 // there's no need to ref-count. |
52 base::Unretained(this), | 67 base::Unretained(this), |
53 slot_name, | 68 slot_name, |
54 retry))) { | 69 retry))) { |
55 event_.Wait(); | 70 event_.Wait(); |
56 } | 71 } |
57 *cancelled = cancelled_; | 72 *cancelled = cancelled_; |
58 return password_; | 73 return password_; |
59 } | 74 } |
60 | 75 |
61 private: | 76 private: |
62 void ShowDialog(const std::string& slot_name, | 77 void ShowDialog(const std::string& slot_name, |
63 bool retry) { | 78 bool retry) { |
64 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 79 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
65 ShowCryptoModulePasswordDialog( | 80 ShowCryptoModulePasswordDialog( |
66 slot_name, | 81 slot_name, |
67 retry, | 82 retry, |
68 reason_, | 83 reason_, |
69 server_, | 84 server_, |
70 NULL, // TODO(mattm): Supply parent window. | 85 NULL, // TODO(mattm): Supply parent window. |
71 base::Bind(&CryptoModuleBlockingDialogDelegate::GotPassword, | 86 base::Bind(&ChromeNSSCryptoModuleDelegate::GotPassword, |
72 // We block on event_ until the task completes, so | 87 // We block on event_ until the task completes, so |
73 // there's no need to ref-count. | 88 // there's no need to ref-count. |
74 base::Unretained(this))); | 89 base::Unretained(this))); |
75 } | 90 } |
76 | 91 |
77 void GotPassword(const char* password) { | 92 void GotPassword(const char* password) { |
78 if (password) | 93 if (password) |
79 password_ = password; | 94 password_ = password; |
80 else | 95 else |
81 cancelled_ = true; | 96 cancelled_ = true; |
82 event_.Signal(); | 97 event_.Signal(); |
83 } | 98 } |
84 | 99 |
| 100 void DidGetSlot(const base::Closure& callback, crypto::ScopedPK11Slot slot) { |
| 101 slot_ = slot.Pass(); |
| 102 callback.Run(); |
| 103 } |
| 104 |
85 base::WaitableEvent event_; | 105 base::WaitableEvent event_; |
| 106 content::ResourceContext* context_; |
86 CryptoModulePasswordReason reason_; | 107 CryptoModulePasswordReason reason_; |
87 std::string server_; | 108 std::string server_; |
88 std::string password_; | 109 std::string password_; |
| 110 crypto::ScopedPK11Slot slot_; |
89 bool cancelled_; | 111 bool cancelled_; |
90 | 112 |
91 DISALLOW_COPY_AND_ASSIGN(CryptoModuleBlockingDialogDelegate); | 113 DISALLOW_COPY_AND_ASSIGN(ChromeNSSCryptoModuleDelegate); |
92 }; | 114 }; |
93 | 115 |
94 } // namespace | 116 } // namespace |
95 | 117 |
96 crypto::CryptoModuleBlockingPasswordDelegate* | 118 crypto::NSSCryptoModuleDelegate* CreateNSSCryptoModuleDelegate( |
97 NewCryptoModuleBlockingDialogDelegate(CryptoModulePasswordReason reason, | 119 content::ResourceContext* context, |
98 const std::string& server) { | 120 CryptoModulePasswordReason reason, |
99 return new CryptoModuleBlockingDialogDelegate(reason, server); | 121 const std::string& server) { |
| 122 return new ChromeNSSCryptoModuleDelegate(context, reason, server); |
100 } | 123 } |
101 | 124 |
| 125 |
102 } // namespace chrome | 126 } // namespace chrome |
OLD | NEW |