| 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" | 7 #include "base/memory/weak_ptr.h" |
| 8 #include "base/supports_user_data.h" | 8 #include "base/supports_user_data.h" |
| 9 #include "chrome/browser/profiles/profile_io_data.h" | 9 #include "chrome/browser/profiles/profile_io_data.h" |
| 10 #include "content/public/browser/browser_thread.h" | 10 #include "content/public/browser/browser_thread.h" |
| 11 #include "crypto/nss_util_internal.h" | 11 #include "crypto/nss_util_internal.h" |
| 12 #include "net/cert/nss_cert_database_chromeos.h" | 12 #include "net/cert/nss_cert_database_chromeos.h" |
| 13 | 13 |
| 14 namespace { | 14 namespace { |
| 15 | 15 |
| 16 void* kDatabaseManagerKey = &kDatabaseManagerKey; | 16 void* kDatabaseManagerKey = &kDatabaseManagerKey; |
| 17 | 17 |
| 18 class NSSCertDatabaseChromeOSManager : public base::SupportsUserData::Data { | 18 class NSSCertDatabaseChromeOSManager : public base::SupportsUserData::Data { |
| 19 public: | 19 public: |
| 20 typedef base::Callback<void(net::NSSCertDatabaseChromeOS*)> | 20 typedef base::Callback<void(net::NSSCertDatabaseChromeOS*)> |
| 21 GetNSSCertDatabaseCallback; | 21 GetNSSCertDatabaseCallback; |
| 22 explicit NSSCertDatabaseChromeOSManager(const std::string& username_hash) | 22 explicit NSSCertDatabaseChromeOSManager(const std::string& username_hash) |
| 23 : username_hash_(username_hash), weak_ptr_factory_(this) { | 23 : username_hash_(username_hash), weak_ptr_factory_(this) { |
| 24 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::IO)); | 24 DCHECK_CURRENTLY_ON(content::BrowserThread::IO); |
| 25 crypto::ScopedPK11Slot private_slot(crypto::GetPrivateSlotForChromeOSUser( | 25 crypto::ScopedPK11Slot private_slot(crypto::GetPrivateSlotForChromeOSUser( |
| 26 username_hash, | 26 username_hash, |
| 27 base::Bind(&NSSCertDatabaseChromeOSManager::DidGetPrivateSlot, | 27 base::Bind(&NSSCertDatabaseChromeOSManager::DidGetPrivateSlot, |
| 28 weak_ptr_factory_.GetWeakPtr()))); | 28 weak_ptr_factory_.GetWeakPtr()))); |
| 29 if (private_slot) | 29 if (private_slot) |
| 30 DidGetPrivateSlot(private_slot.Pass()); | 30 DidGetPrivateSlot(private_slot.Pass()); |
| 31 } | 31 } |
| 32 | 32 |
| 33 ~NSSCertDatabaseChromeOSManager() override { | 33 ~NSSCertDatabaseChromeOSManager() override { |
| 34 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::IO)); | 34 DCHECK_CURRENTLY_ON(content::BrowserThread::IO); |
| 35 } | 35 } |
| 36 | 36 |
| 37 net::NSSCertDatabaseChromeOS* GetNSSCertDatabase( | 37 net::NSSCertDatabaseChromeOS* GetNSSCertDatabase( |
| 38 const GetNSSCertDatabaseCallback& callback) { | 38 const GetNSSCertDatabaseCallback& callback) { |
| 39 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::IO)); | 39 DCHECK_CURRENTLY_ON(content::BrowserThread::IO); |
| 40 | 40 |
| 41 if (nss_cert_database_) | 41 if (nss_cert_database_) |
| 42 return nss_cert_database_.get(); | 42 return nss_cert_database_.get(); |
| 43 | 43 |
| 44 ready_callback_list_.push_back(callback); | 44 ready_callback_list_.push_back(callback); |
| 45 return NULL; | 45 return NULL; |
| 46 } | 46 } |
| 47 | 47 |
| 48 private: | 48 private: |
| 49 typedef std::vector<GetNSSCertDatabaseCallback> ReadyCallbackList; | 49 typedef std::vector<GetNSSCertDatabaseCallback> ReadyCallbackList; |
| 50 | 50 |
| 51 void DidGetPrivateSlot(crypto::ScopedPK11Slot private_slot) { | 51 void DidGetPrivateSlot(crypto::ScopedPK11Slot private_slot) { |
| 52 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::IO)); | 52 DCHECK_CURRENTLY_ON(content::BrowserThread::IO); |
| 53 nss_cert_database_.reset(new net::NSSCertDatabaseChromeOS( | 53 nss_cert_database_.reset(new net::NSSCertDatabaseChromeOS( |
| 54 crypto::GetPublicSlotForChromeOSUser(username_hash_), | 54 crypto::GetPublicSlotForChromeOSUser(username_hash_), |
| 55 private_slot.Pass())); | 55 private_slot.Pass())); |
| 56 | 56 |
| 57 ReadyCallbackList callback_list; | 57 ReadyCallbackList callback_list; |
| 58 callback_list.swap(ready_callback_list_); | 58 callback_list.swap(ready_callback_list_); |
| 59 for (ReadyCallbackList::iterator i = callback_list.begin(); | 59 for (ReadyCallbackList::iterator i = callback_list.begin(); |
| 60 i != callback_list.end(); | 60 i != callback_list.end(); |
| 61 ++i) { | 61 ++i) { |
| 62 (*i).Run(nss_cert_database_.get()); | 62 (*i).Run(nss_cert_database_.get()); |
| 63 } | 63 } |
| 64 } | 64 } |
| 65 | 65 |
| 66 std::string username_hash_; | 66 std::string username_hash_; |
| 67 scoped_ptr<net::NSSCertDatabaseChromeOS> nss_cert_database_; | 67 scoped_ptr<net::NSSCertDatabaseChromeOS> nss_cert_database_; |
| 68 ReadyCallbackList ready_callback_list_; | 68 ReadyCallbackList ready_callback_list_; |
| 69 base::WeakPtrFactory<NSSCertDatabaseChromeOSManager> weak_ptr_factory_; | 69 base::WeakPtrFactory<NSSCertDatabaseChromeOSManager> weak_ptr_factory_; |
| 70 | 70 |
| 71 DISALLOW_COPY_AND_ASSIGN(NSSCertDatabaseChromeOSManager); | 71 DISALLOW_COPY_AND_ASSIGN(NSSCertDatabaseChromeOSManager); |
| 72 }; | 72 }; |
| 73 | 73 |
| 74 std::string GetUsername(content::ResourceContext* context) { | 74 std::string GetUsername(content::ResourceContext* context) { |
| 75 return ProfileIOData::FromResourceContext(context)->username_hash(); | 75 return ProfileIOData::FromResourceContext(context)->username_hash(); |
| 76 } | 76 } |
| 77 | 77 |
| 78 net::NSSCertDatabaseChromeOS* GetNSSCertDatabaseChromeOS( | 78 net::NSSCertDatabaseChromeOS* GetNSSCertDatabaseChromeOS( |
| 79 content::ResourceContext* context, | 79 content::ResourceContext* context, |
| 80 const NSSCertDatabaseChromeOSManager::GetNSSCertDatabaseCallback& | 80 const NSSCertDatabaseChromeOSManager::GetNSSCertDatabaseCallback& |
| 81 callback) { | 81 callback) { |
| 82 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::IO)); | 82 DCHECK_CURRENTLY_ON(content::BrowserThread::IO); |
| 83 NSSCertDatabaseChromeOSManager* manager = | 83 NSSCertDatabaseChromeOSManager* manager = |
| 84 static_cast<NSSCertDatabaseChromeOSManager*>( | 84 static_cast<NSSCertDatabaseChromeOSManager*>( |
| 85 context->GetUserData(kDatabaseManagerKey)); | 85 context->GetUserData(kDatabaseManagerKey)); |
| 86 if (!manager) { | 86 if (!manager) { |
| 87 manager = new NSSCertDatabaseChromeOSManager(GetUsername(context)); | 87 manager = new NSSCertDatabaseChromeOSManager(GetUsername(context)); |
| 88 context->SetUserData(kDatabaseManagerKey, manager); | 88 context->SetUserData(kDatabaseManagerKey, manager); |
| 89 } | 89 } |
| 90 return manager->GetNSSCertDatabase(callback); | 90 return manager->GetNSSCertDatabase(callback); |
| 91 } | 91 } |
| 92 | 92 |
| (...skipping 16 matching lines...) Expand all Loading... |
| 109 net::NSSCertDatabaseChromeOS* db = | 109 net::NSSCertDatabaseChromeOS* db = |
| 110 GetNSSCertDatabaseChromeOS(context, callback); | 110 GetNSSCertDatabaseChromeOS(context, callback); |
| 111 if (db) | 111 if (db) |
| 112 callback.Run(db); | 112 callback.Run(db); |
| 113 } | 113 } |
| 114 | 114 |
| 115 } // namespace | 115 } // namespace |
| 116 | 116 |
| 117 crypto::ScopedPK11Slot GetPublicNSSKeySlotForResourceContext( | 117 crypto::ScopedPK11Slot GetPublicNSSKeySlotForResourceContext( |
| 118 content::ResourceContext* context) { | 118 content::ResourceContext* context) { |
| 119 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::IO)); | 119 DCHECK_CURRENTLY_ON(content::BrowserThread::IO); |
| 120 return crypto::GetPublicSlotForChromeOSUser(GetUsername(context)); | 120 return crypto::GetPublicSlotForChromeOSUser(GetUsername(context)); |
| 121 } | 121 } |
| 122 | 122 |
| 123 crypto::ScopedPK11Slot GetPrivateNSSKeySlotForResourceContext( | 123 crypto::ScopedPK11Slot GetPrivateNSSKeySlotForResourceContext( |
| 124 content::ResourceContext* context, | 124 content::ResourceContext* context, |
| 125 const base::Callback<void(crypto::ScopedPK11Slot)>& callback) { | 125 const base::Callback<void(crypto::ScopedPK11Slot)>& callback) { |
| 126 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::IO)); | 126 DCHECK_CURRENTLY_ON(content::BrowserThread::IO); |
| 127 return crypto::GetPrivateSlotForChromeOSUser(GetUsername(context), callback); | 127 return crypto::GetPrivateSlotForChromeOSUser(GetUsername(context), callback); |
| 128 } | 128 } |
| 129 | 129 |
| 130 net::NSSCertDatabase* GetNSSCertDatabaseForResourceContext( | 130 net::NSSCertDatabase* GetNSSCertDatabaseForResourceContext( |
| 131 content::ResourceContext* context, | 131 content::ResourceContext* context, |
| 132 const base::Callback<void(net::NSSCertDatabase*)>& callback) { | 132 const base::Callback<void(net::NSSCertDatabase*)>& callback) { |
| 133 return GetNSSCertDatabaseChromeOS( | 133 return GetNSSCertDatabaseChromeOS( |
| 134 context, base::Bind(&CallWithNSSCertDatabase, callback)); | 134 context, base::Bind(&CallWithNSSCertDatabase, callback)); |
| 135 } | 135 } |
| 136 | 136 |
| 137 void EnableNSSSystemKeySlotForResourceContext( | 137 void EnableNSSSystemKeySlotForResourceContext( |
| 138 content::ResourceContext* context) { | 138 content::ResourceContext* context) { |
| 139 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::IO)); | 139 DCHECK_CURRENTLY_ON(content::BrowserThread::IO); |
| 140 base::Callback<void(crypto::ScopedPK11Slot)> callback = | 140 base::Callback<void(crypto::ScopedPK11Slot)> callback = |
| 141 base::Bind(&SetSystemSlotOfDBForResourceContext, context); | 141 base::Bind(&SetSystemSlotOfDBForResourceContext, context); |
| 142 crypto::ScopedPK11Slot system_slot = crypto::GetSystemNSSKeySlot(callback); | 142 crypto::ScopedPK11Slot system_slot = crypto::GetSystemNSSKeySlot(callback); |
| 143 if (system_slot) | 143 if (system_slot) |
| 144 callback.Run(system_slot.Pass()); | 144 callback.Run(system_slot.Pass()); |
| 145 } | 145 } |
| OLD | NEW |