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 typedef std::vector<base::Callback<void(net::NSSCertDatabase*)> > |
| 48 ReadyCallbackList; |
| 49 |
| 50 void DidGetPrivateSlot(crypto::ScopedPK11Slot private_slot) { |
| 51 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::IO)); |
| 52 nss_cert_database_.reset(new net::NSSCertDatabaseChromeOS( |
| 53 crypto::GetPublicSlotForChromeOSUser(username_hash_), |
| 54 private_slot.Pass())); |
| 55 |
| 56 ReadyCallbackList callback_list; |
| 57 callback_list.swap(ready_callback_list_); |
| 58 for (ReadyCallbackList::iterator i = callback_list.begin(); |
| 59 i != callback_list.end(); |
| 60 ++i) { |
| 61 (*i).Run(nss_cert_database_.get()); |
| 62 } |
| 63 } |
| 64 |
| 65 std::string username_hash_; |
| 66 scoped_ptr<net::NSSCertDatabaseChromeOS> nss_cert_database_; |
| 67 ReadyCallbackList ready_callback_list_; |
| 68 base::WeakPtrFactory<NSSCertDatabaseChromeOSManager> weak_ptr_factory_; |
| 69 |
| 70 DISALLOW_COPY_AND_ASSIGN(NSSCertDatabaseChromeOSManager); |
| 71 }; |
| 72 |
12 std::string GetUsername(content::ResourceContext* context) { | 73 std::string GetUsername(content::ResourceContext* context) { |
13 return ProfileIOData::FromResourceContext(context)->username_hash(); | 74 return ProfileIOData::FromResourceContext(context)->username_hash(); |
14 } | 75 } |
| 76 |
15 } // namespace | 77 } // namespace |
16 | 78 |
17 crypto::ScopedPK11Slot GetPublicNSSKeySlotForResourceContext( | 79 crypto::ScopedPK11Slot GetPublicNSSKeySlotForResourceContext( |
18 content::ResourceContext* context) { | 80 content::ResourceContext* context) { |
19 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::IO)); | 81 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::IO)); |
20 return crypto::GetPublicSlotForChromeOSUser(GetUsername(context)); | 82 return crypto::GetPublicSlotForChromeOSUser(GetUsername(context)); |
21 } | 83 } |
22 | 84 |
23 crypto::ScopedPK11Slot GetPrivateNSSKeySlotForResourceContext( | 85 crypto::ScopedPK11Slot GetPrivateNSSKeySlotForResourceContext( |
24 content::ResourceContext* context, | 86 content::ResourceContext* context, |
25 const base::Callback<void(crypto::ScopedPK11Slot)>& callback) { | 87 const base::Callback<void(crypto::ScopedPK11Slot)>& callback) { |
26 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::IO)); | 88 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::IO)); |
27 return crypto::GetPrivateSlotForChromeOSUser(GetUsername(context), callback); | 89 return crypto::GetPrivateSlotForChromeOSUser(GetUsername(context), callback); |
28 } | 90 } |
29 | 91 |
| 92 net::NSSCertDatabase* GetNSSCertDatabaseForResourceContext( |
| 93 content::ResourceContext* context, |
| 94 const base::Callback<void(net::NSSCertDatabase*)>& callback) { |
| 95 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::IO)); |
| 96 NSSCertDatabaseChromeOSManager* manager = |
| 97 static_cast<NSSCertDatabaseChromeOSManager*>( |
| 98 context->GetUserData(kDatabaseManagerKey)); |
| 99 if (!manager) { |
| 100 manager = new NSSCertDatabaseChromeOSManager(GetUsername(context)); |
| 101 context->SetUserData(kDatabaseManagerKey, manager); |
| 102 } |
| 103 return manager->GetNSSCertDatabase(callback); |
| 104 } |
OLD | NEW |