OLD | NEW |
(Empty) | |
| 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 |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "chrome/browser/ui/crypto_module_delegate_nss.h" |
| 6 |
| 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" |
| 11 |
| 12 using content::BrowserThread; |
| 13 |
| 14 ChromeNSSCryptoModuleDelegate::ChromeNSSCryptoModuleDelegate( |
| 15 chrome::CryptoModulePasswordReason reason, |
| 16 const std::string& server) |
| 17 : event_(false, false), |
| 18 reason_(reason), |
| 19 server_(server), |
| 20 cancelled_(false) {} |
| 21 |
| 22 ChromeNSSCryptoModuleDelegate::~ChromeNSSCryptoModuleDelegate() {} |
| 23 |
| 24 bool ChromeNSSCryptoModuleDelegate::InitializeSlot( |
| 25 content::ResourceContext* context, |
| 26 const base::Closure& initialization_complete_callback) { |
| 27 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
| 28 base::Callback<void(crypto::ScopedPK11Slot)> get_slot_callback; |
| 29 if (!initialization_complete_callback.is_null()) |
| 30 get_slot_callback = base::Bind(&ChromeNSSCryptoModuleDelegate::DidGetSlot, |
| 31 base::Unretained(this), |
| 32 initialization_complete_callback); |
| 33 |
| 34 slot_ = GetPrivateNSSKeySlotForResourceContext(context, get_slot_callback); |
| 35 return slot_.get() != NULL; |
| 36 } |
| 37 |
| 38 // TODO(mattm): allow choosing which slot to generate and store the key. |
| 39 crypto::ScopedPK11Slot ChromeNSSCryptoModuleDelegate::RequestSlot() { |
| 40 return slot_.Pass(); |
| 41 } |
| 42 |
| 43 std::string ChromeNSSCryptoModuleDelegate::RequestPassword( |
| 44 const std::string& slot_name, |
| 45 bool retry, |
| 46 bool* cancelled) { |
| 47 DCHECK(!event_.IsSignaled()); |
| 48 event_.Reset(); |
| 49 |
| 50 if (BrowserThread::PostTask( |
| 51 BrowserThread::UI, |
| 52 FROM_HERE, |
| 53 base::Bind(&ChromeNSSCryptoModuleDelegate::ShowDialog, |
| 54 // This method blocks on |event_| until the task completes, |
| 55 // so there's no need to ref-count. |
| 56 base::Unretained(this), |
| 57 slot_name, |
| 58 retry))) { |
| 59 event_.Wait(); |
| 60 } |
| 61 *cancelled = cancelled_; |
| 62 return password_; |
| 63 } |
| 64 |
| 65 void ChromeNSSCryptoModuleDelegate::ShowDialog(const std::string& slot_name, |
| 66 bool retry) { |
| 67 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 68 ShowCryptoModulePasswordDialog( |
| 69 slot_name, |
| 70 retry, |
| 71 reason_, |
| 72 server_, |
| 73 NULL, // TODO(mattm): Supply parent window. |
| 74 base::Bind(&ChromeNSSCryptoModuleDelegate::GotPassword, |
| 75 // RequestPassword is blocked on |event_| until GotPassword is |
| 76 // called, so there's no need to ref-count. |
| 77 base::Unretained(this))); |
| 78 } |
| 79 |
| 80 void ChromeNSSCryptoModuleDelegate::GotPassword(const char* password) { |
| 81 if (password) |
| 82 password_ = password; |
| 83 else |
| 84 cancelled_ = true; |
| 85 event_.Signal(); |
| 86 } |
| 87 |
| 88 void ChromeNSSCryptoModuleDelegate::DidGetSlot(const base::Closure& callback, |
| 89 crypto::ScopedPK11Slot slot) { |
| 90 slot_ = slot.Pass(); |
| 91 callback.Run(); |
| 92 } |
| 93 |
| 94 crypto::CryptoModuleBlockingPasswordDelegate* |
| 95 CreateCryptoModuleBlockingPasswordDelegate( |
| 96 chrome::CryptoModulePasswordReason reason, |
| 97 const std::string& server) { |
| 98 // Returns a ChromeNSSCryptoModuleDelegate without calling InitializeSlot. |
| 99 // Since it is only being used as a CreateCryptoModuleBlockingDialogDelegate, |
| 100 // initializing the slot handle is unnecessary. |
| 101 return new ChromeNSSCryptoModuleDelegate(reason, server); |
| 102 } |
OLD | NEW |