Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2013 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/net/nss_context.h" | 5 #include "chrome/browser/net/nss_context.h" |
| 6 | 6 |
| 7 #include "base/memory/weak_ptr.h" | |
| 8 #include "base/supports_user_data.h" | |
| 7 #include "chrome/browser/profiles/profile_io_data.h" | 9 #include "chrome/browser/profiles/profile_io_data.h" |
| 8 #include "content/public/browser/browser_thread.h" | 10 #include "content/public/browser/browser_thread.h" |
| 9 #include "crypto/nss_util_internal.h" | 11 #include "crypto/nss_util_internal.h" |
| 12 #include "net/cert/nss_cert_database_chromeos.h" | |
| 10 | 13 |
| 11 namespace { | 14 namespace { |
| 15 | |
| 16 void* kDatabaseManagerKey = &kDatabaseManagerKey; | |
| 17 | |
| 18 class NSSCertDatabaseChromeOSManager : public base::SupportsUserData::Data { | |
| 19 public: | |
| 20 explicit NSSCertDatabaseChromeOSManager(const std::string& username_hash) | |
| 21 : username_hash_(username_hash), weak_ptr_factory_(this) { | |
| 22 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::IO)); | |
| 23 crypto::ScopedPK11Slot private_slot(crypto::GetPrivateSlotForChromeOSUser( | |
| 24 username_hash, | |
| 25 base::Bind(&NSSCertDatabaseChromeOSManager::DidGetPrivateSlot, | |
| 26 weak_ptr_factory_.GetWeakPtr()))); | |
| 27 if (private_slot) | |
| 28 DidGetPrivateSlot(private_slot.Pass()); | |
| 29 } | |
| 30 | |
| 31 virtual ~NSSCertDatabaseChromeOSManager() { | |
| 32 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::IO)); | |
| 33 } | |
| 34 | |
| 35 net::NSSCertDatabase* GetNSSCertDatabase( | |
| 36 const base::Callback<void(net::NSSCertDatabase*)>& callback) { | |
| 37 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::IO)); | |
| 38 | |
| 39 if (nss_cert_database_) | |
| 40 return nss_cert_database_.get(); | |
| 41 | |
| 42 ready_callback_list_.push_back(callback); | |
| 43 return NULL; | |
| 44 } | |
| 45 | |
| 46 private: | |
| 47 void DidGetPrivateSlot(crypto::ScopedPK11Slot private_slot) { | |
| 48 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::IO)); | |
| 49 nss_cert_database_.reset(new net::NSSCertDatabaseChromeOS( | |
| 50 crypto::GetPublicSlotForChromeOSUser(username_hash_), | |
| 51 private_slot.Pass())); | |
| 52 | |
| 53 ReadyCallbackList callback_list; | |
| 54 callback_list.swap(ready_callback_list_); | |
| 55 for (ReadyCallbackList::iterator i = callback_list.begin(); | |
| 56 i != callback_list.end(); | |
| 57 ++i) { | |
| 58 (*i).Run(nss_cert_database_.get()); | |
|
Ryan Sleevi
2013/12/18 21:28:32
Is there risk here of any of these callbacks causi
mattm
2013/12/19 22:35:00
After thinking about it more, I believe that can't
| |
| 59 } | |
| 60 } | |
| 61 | |
| 62 std::string username_hash_; | |
| 63 scoped_ptr<net::NSSCertDatabaseChromeOS> nss_cert_database_; | |
| 64 | |
| 65 typedef std::vector<base::Callback<void(net::NSSCertDatabase*)> > | |
| 66 ReadyCallbackList; | |
|
Ryan Sleevi
2013/12/18 21:28:32
Should be before the function - per http://google-
mattm
2013/12/19 22:35:00
Done.
| |
| 67 ReadyCallbackList ready_callback_list_; | |
| 68 | |
| 69 base::WeakPtrFactory<NSSCertDatabaseChromeOSManager> weak_ptr_factory_; | |
| 70 | |
| 71 DISALLOW_COPY_AND_ASSIGN(NSSCertDatabaseChromeOSManager); | |
| 72 }; | |
| 73 | |
| 12 std::string GetUsername(content::ResourceContext* context) { | 74 std::string GetUsername(content::ResourceContext* context) { |
| 13 return ProfileIOData::FromResourceContext(context)->username_hash(); | 75 return ProfileIOData::FromResourceContext(context)->username_hash(); |
| 14 } | 76 } |
| 77 | |
| 15 } // namespace | 78 } // namespace |
| 16 | 79 |
| 17 crypto::ScopedPK11Slot GetPublicNSSKeySlotForResourceContext( | 80 crypto::ScopedPK11Slot GetPublicNSSKeySlotForResourceContext( |
| 18 content::ResourceContext* context) { | 81 content::ResourceContext* context) { |
| 19 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::IO)); | 82 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::IO)); |
| 20 return crypto::GetPublicSlotForChromeOSUser(GetUsername(context)); | 83 return crypto::GetPublicSlotForChromeOSUser(GetUsername(context)); |
| 21 } | 84 } |
| 22 | 85 |
| 23 crypto::ScopedPK11Slot GetPrivateNSSKeySlotForResourceContext( | 86 crypto::ScopedPK11Slot GetPrivateNSSKeySlotForResourceContext( |
| 24 content::ResourceContext* context, | 87 content::ResourceContext* context, |
| 25 const base::Callback<void(crypto::ScopedPK11Slot)>& callback) { | 88 const base::Callback<void(crypto::ScopedPK11Slot)>& callback) { |
| 26 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::IO)); | 89 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::IO)); |
| 27 return crypto::GetPrivateSlotForChromeOSUser(GetUsername(context), callback); | 90 return crypto::GetPrivateSlotForChromeOSUser(GetUsername(context), callback); |
| 28 } | 91 } |
| 29 | 92 |
| 93 net::NSSCertDatabase* GetNSSCertDatabaseForResourceContext( | |
| 94 content::ResourceContext* context, | |
| 95 const base::Callback<void(net::NSSCertDatabase*)>& callback) { | |
| 96 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::IO)); | |
| 97 NSSCertDatabaseChromeOSManager* manager = | |
| 98 static_cast<NSSCertDatabaseChromeOSManager*>( | |
| 99 context->GetUserData(kDatabaseManagerKey)); | |
| 100 if (!manager) { | |
| 101 manager = new NSSCertDatabaseChromeOSManager(GetUsername(context)); | |
| 102 context->SetUserData(kDatabaseManagerKey, manager); | |
| 103 } | |
| 104 return manager->GetNSSCertDatabase(callback); | |
| 105 } | |
| OLD | NEW |