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/chromeos/login/profile_auth_data.h" | 5 #include "chrome/browser/chromeos/login/profile_auth_data.h" |
6 | 6 |
7 #include "base/bind.h" | 7 #include "base/bind.h" |
8 #include "base/bind_helpers.h" | 8 #include "base/bind_helpers.h" |
9 #include "base/callback.h" | 9 #include "base/callback.h" |
10 #include "base/location.h" | 10 #include "base/location.h" |
11 #include "base/logging.h" | 11 #include "base/logging.h" |
12 #include "base/memory/ref_counted.h" | 12 #include "base/memory/ref_counted.h" |
13 #include "content/public/browser/browser_context.h" | 13 #include "content/public/browser/browser_context.h" |
14 #include "content/public/browser/browser_thread.h" | 14 #include "content/public/browser/browser_thread.h" |
15 #include "net/cookies/canonical_cookie.h" | 15 #include "net/cookies/canonical_cookie.h" |
16 #include "net/cookies/cookie_monster.h" | 16 #include "net/cookies/cookie_monster.h" |
17 #include "net/cookies/cookie_store.h" | 17 #include "net/cookies/cookie_store.h" |
18 #include "net/http/http_auth_cache.h" | 18 #include "net/http/http_auth_cache.h" |
19 #include "net/http/http_network_session.h" | 19 #include "net/http/http_network_session.h" |
20 #include "net/http/http_transaction_factory.h" | 20 #include "net/http/http_transaction_factory.h" |
21 #include "net/ssl/server_bound_cert_service.h" | 21 #include "net/ssl/channel_id_service.h" |
22 #include "net/ssl/server_bound_cert_store.h" | 22 #include "net/ssl/channel_id_store.h" |
23 #include "net/url_request/url_request_context.h" | 23 #include "net/url_request/url_request_context.h" |
24 #include "net/url_request/url_request_context_getter.h" | 24 #include "net/url_request/url_request_context_getter.h" |
25 | 25 |
26 using content::BrowserThread; | 26 using content::BrowserThread; |
27 | 27 |
28 namespace chromeos { | 28 namespace chromeos { |
29 | 29 |
30 namespace { | 30 namespace { |
31 | 31 |
32 class ProfileAuthDataTransferer { | 32 class ProfileAuthDataTransferer { |
33 public: | 33 public: |
34 ProfileAuthDataTransferer( | 34 ProfileAuthDataTransferer( |
35 content::BrowserContext* from_context, | 35 content::BrowserContext* from_context, |
36 content::BrowserContext* to_context, | 36 content::BrowserContext* to_context, |
37 bool transfer_auth_cookies_and_server_bound_certs, | 37 bool transfer_auth_cookies_and_channel_ids, |
38 const base::Closure& completion_callback); | 38 const base::Closure& completion_callback); |
39 | 39 |
40 void BeginTransfer(); | 40 void BeginTransfer(); |
41 | 41 |
42 private: | 42 private: |
43 void BeginTransferOnIOThread(); | 43 void BeginTransferOnIOThread(); |
44 | 44 |
45 // Transfer the proxy auth cache from |from_context_| to |to_context_|. If | 45 // Transfer the proxy auth cache from |from_context_| to |to_context_|. If |
46 // the user was required to authenticate with a proxy during login, this | 46 // the user was required to authenticate with a proxy during login, this |
47 // authentication information will be transferred into the user's session. | 47 // authentication information will be transferred into the user's session. |
48 void TransferProxyAuthCache(); | 48 void TransferProxyAuthCache(); |
49 | 49 |
50 // Retrieve the contents of |from_context_|'s cookie jar. When the retrieval | 50 // Retrieve the contents of |from_context_|'s cookie jar. When the retrieval |
51 // finishes, OnCookiesToTransferRetrieved will be called with the result. | 51 // finishes, OnCookiesToTransferRetrieved will be called with the result. |
52 void RetrieveCookiesToTransfer(); | 52 void RetrieveCookiesToTransfer(); |
53 | 53 |
54 // Callback that receives the contents of |from_context_|'s cookie jar. Calls | 54 // Callback that receives the contents of |from_context_|'s cookie jar. Calls |
55 // MaybeTransferCookiesAndCerts() to try and perform the transfer. | 55 // MaybeTransferCookiesAndChannelIDs() to try and perform the transfer. |
56 void OnCookiesToTransferRetrieved(const net::CookieList& cookies_to_transfer); | 56 void OnCookiesToTransferRetrieved(const net::CookieList& cookies_to_transfer); |
57 | 57 |
58 // Retrieve |from_context_|'s server bound certs. When the retrieval finishes, | 58 // Retrieve |from_context_|'s channel IDs. When the retrieval finishes, |
59 // OnServerBoundCertsToTransferRetrieved will be called with the result. | 59 // OnChannelIDsToTransferRetrieved will be called with the result. |
60 void RetrieveServerBoundCertsToTransfer(); | 60 void RetrieveChannelIDsToTransfer(); |
61 | 61 |
62 // Callback that receives |from_context_|'s server bound certs. Calls | 62 // Callback that receives |from_context_|'s channel IDs. Calls |
63 // MaybeTransferCookiesAndCerts() to try and perform the transfer. | 63 // MaybeTransferCookiesAndChannelIDs() to try and perform the transfer. |
64 void OnServerBoundCertsToTransferRetrieved( | 64 void OnChannelIDsToTransferRetrieved( |
65 const net::ServerBoundCertStore::ServerBoundCertList& certs_to_transfer); | 65 const net::ChannelIDStore::ChannelIDList& channel_ids_to_transfer); |
66 | 66 |
67 // If both auth cookies and server bound certs have been retrieved from | 67 // If both auth cookies and channel IDs have been retrieved from |
68 // |from_context| already, retrieve the contents of |to_context|'s cookie jar | 68 // |from_context| already, retrieve the contents of |to_context|'s cookie jar |
69 // as well, allowing OnTargetCookieJarContentsRetrieved() to perform the | 69 // as well, allowing OnTargetCookieJarContentsRetrieved() to perform the |
70 // actual transfer. | 70 // actual transfer. |
71 void MaybeTransferCookiesAndCerts(); | 71 void MaybeTransferCookiesAndChannelIDs(); |
72 | 72 |
73 // Transfer auth cookies and server bound certificates to the user's | 73 // Transfer auth cookies and server bound certificates to the user's |
74 // |to_context_| if the user's cookie jar is empty. Call Finish() when done. | 74 // |to_context_| if the user's cookie jar is empty. Call Finish() when done. |
75 void OnTargetCookieJarContentsRetrieved( | 75 void OnTargetCookieJarContentsRetrieved( |
76 const net::CookieList& target_cookies); | 76 const net::CookieList& target_cookies); |
77 | 77 |
78 // Post the |completion_callback_| to the UI thread and delete |this|. | 78 // Post the |completion_callback_| to the UI thread and delete |this|. |
79 void Finish(); | 79 void Finish(); |
80 | 80 |
81 scoped_refptr<net::URLRequestContextGetter> from_context_; | 81 scoped_refptr<net::URLRequestContextGetter> from_context_; |
82 scoped_refptr<net::URLRequestContextGetter> to_context_; | 82 scoped_refptr<net::URLRequestContextGetter> to_context_; |
83 bool transfer_auth_cookies_and_server_bound_certs_; | 83 bool transfer_auth_cookies_and_channel_ids_; |
84 base::Closure completion_callback_; | 84 base::Closure completion_callback_; |
85 | 85 |
86 net::CookieList cookies_to_transfer_; | 86 net::CookieList cookies_to_transfer_; |
87 net::ServerBoundCertStore::ServerBoundCertList certs_to_transfer_; | 87 net::ChannelIDStore::ChannelIDList channel_ids_to_transfer_; |
88 | 88 |
89 bool got_cookies_; | 89 bool got_cookies_; |
90 bool got_server_bound_certs_; | 90 bool got_channel_ids_; |
91 }; | 91 }; |
92 | 92 |
93 ProfileAuthDataTransferer::ProfileAuthDataTransferer( | 93 ProfileAuthDataTransferer::ProfileAuthDataTransferer( |
94 content::BrowserContext* from_context, | 94 content::BrowserContext* from_context, |
95 content::BrowserContext* to_context, | 95 content::BrowserContext* to_context, |
96 bool transfer_auth_cookies_and_server_bound_certs, | 96 bool transfer_auth_cookies_and_channel_ids, |
97 const base::Closure& completion_callback) | 97 const base::Closure& completion_callback) |
98 : from_context_(from_context->GetRequestContext()), | 98 : from_context_(from_context->GetRequestContext()), |
99 to_context_(to_context->GetRequestContext()), | 99 to_context_(to_context->GetRequestContext()), |
100 transfer_auth_cookies_and_server_bound_certs_( | 100 transfer_auth_cookies_and_channel_ids_( |
101 transfer_auth_cookies_and_server_bound_certs), | 101 transfer_auth_cookies_and_channel_ids), |
102 completion_callback_(completion_callback), | 102 completion_callback_(completion_callback), |
103 got_cookies_(false), | 103 got_cookies_(false), |
104 got_server_bound_certs_(false) { | 104 got_channel_ids_(false) { |
105 } | 105 } |
106 | 106 |
107 void ProfileAuthDataTransferer::BeginTransfer() { | 107 void ProfileAuthDataTransferer::BeginTransfer() { |
108 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 108 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
109 // If we aren't transferring auth cookies or server bound certificates, post | 109 // If we aren't transferring auth cookies or server bound certificates, post |
110 // the completion callback immediately. Otherwise, it will be called when both | 110 // the completion callback immediately. Otherwise, it will be called when both |
111 // auth cookies and server bound certificates have been transferred. | 111 // auth cookies and server bound certificates have been transferred. |
112 if (!transfer_auth_cookies_and_server_bound_certs_) { | 112 if (!transfer_auth_cookies_and_channel_ids_) { |
113 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, completion_callback_); | 113 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, completion_callback_); |
114 // Null the callback so that when Finish is called, the callback won't be | 114 // Null the callback so that when Finish is called, the callback won't be |
115 // called again. | 115 // called again. |
116 completion_callback_.Reset(); | 116 completion_callback_.Reset(); |
117 } | 117 } |
118 BrowserThread::PostTask( | 118 BrowserThread::PostTask( |
119 BrowserThread::IO, FROM_HERE, | 119 BrowserThread::IO, FROM_HERE, |
120 base::Bind(&ProfileAuthDataTransferer::BeginTransferOnIOThread, | 120 base::Bind(&ProfileAuthDataTransferer::BeginTransferOnIOThread, |
121 base::Unretained(this))); | 121 base::Unretained(this))); |
122 } | 122 } |
123 | 123 |
124 void ProfileAuthDataTransferer::BeginTransferOnIOThread() { | 124 void ProfileAuthDataTransferer::BeginTransferOnIOThread() { |
125 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | 125 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
126 TransferProxyAuthCache(); | 126 TransferProxyAuthCache(); |
127 if (transfer_auth_cookies_and_server_bound_certs_) { | 127 if (transfer_auth_cookies_and_channel_ids_) { |
128 RetrieveCookiesToTransfer(); | 128 RetrieveCookiesToTransfer(); |
129 RetrieveServerBoundCertsToTransfer(); | 129 RetrieveChannelIDsToTransfer(); |
130 } else { | 130 } else { |
131 Finish(); | 131 Finish(); |
132 } | 132 } |
133 } | 133 } |
134 | 134 |
135 void ProfileAuthDataTransferer::TransferProxyAuthCache() { | 135 void ProfileAuthDataTransferer::TransferProxyAuthCache() { |
136 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | 136 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
137 net::HttpAuthCache* new_cache = to_context_->GetURLRequestContext()-> | 137 net::HttpAuthCache* new_cache = to_context_->GetURLRequestContext()-> |
138 http_transaction_factory()->GetSession()->http_auth_cache(); | 138 http_transaction_factory()->GetSession()->http_auth_cache(); |
139 new_cache->UpdateAllFrom(*from_context_->GetURLRequestContext()-> | 139 new_cache->UpdateAllFrom(*from_context_->GetURLRequestContext()-> |
140 http_transaction_factory()->GetSession()->http_auth_cache()); | 140 http_transaction_factory()->GetSession()->http_auth_cache()); |
141 } | 141 } |
142 | 142 |
143 void ProfileAuthDataTransferer::RetrieveCookiesToTransfer() { | 143 void ProfileAuthDataTransferer::RetrieveCookiesToTransfer() { |
144 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | 144 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
145 net::CookieStore* from_store = | 145 net::CookieStore* from_store = |
146 from_context_->GetURLRequestContext()->cookie_store(); | 146 from_context_->GetURLRequestContext()->cookie_store(); |
147 net::CookieMonster* from_monster = from_store->GetCookieMonster(); | 147 net::CookieMonster* from_monster = from_store->GetCookieMonster(); |
148 from_monster->SetKeepExpiredCookies(); | 148 from_monster->SetKeepExpiredCookies(); |
149 from_monster->GetAllCookiesAsync( | 149 from_monster->GetAllCookiesAsync( |
150 base::Bind(&ProfileAuthDataTransferer::OnCookiesToTransferRetrieved, | 150 base::Bind(&ProfileAuthDataTransferer::OnCookiesToTransferRetrieved, |
151 base::Unretained(this))); | 151 base::Unretained(this))); |
152 } | 152 } |
153 | 153 |
154 void ProfileAuthDataTransferer::OnCookiesToTransferRetrieved( | 154 void ProfileAuthDataTransferer::OnCookiesToTransferRetrieved( |
155 const net::CookieList& cookies_to_transfer) { | 155 const net::CookieList& cookies_to_transfer) { |
156 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | 156 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
157 got_cookies_ = true; | 157 got_cookies_ = true; |
158 cookies_to_transfer_ = cookies_to_transfer; | 158 cookies_to_transfer_ = cookies_to_transfer; |
159 MaybeTransferCookiesAndCerts(); | 159 MaybeTransferCookiesAndChannelIDs(); |
160 } | 160 } |
161 | 161 |
162 void ProfileAuthDataTransferer::RetrieveServerBoundCertsToTransfer() { | 162 void ProfileAuthDataTransferer::RetrieveChannelIDsToTransfer() { |
163 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | 163 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
164 net::ServerBoundCertService* from_service = | 164 net::ChannelIDService* from_service = |
165 from_context_->GetURLRequestContext()->server_bound_cert_service(); | 165 from_context_->GetURLRequestContext()->channel_id_service(); |
166 from_service->GetCertStore()->GetAllServerBoundCerts( | 166 from_service->GetChannelIDStore()->GetAllChannelIDs( |
167 base::Bind( | 167 base::Bind( |
168 &ProfileAuthDataTransferer::OnServerBoundCertsToTransferRetrieved, | 168 &ProfileAuthDataTransferer::OnChannelIDsToTransferRetrieved, |
169 base::Unretained(this))); | 169 base::Unretained(this))); |
170 } | 170 } |
171 | 171 |
172 void ProfileAuthDataTransferer::OnServerBoundCertsToTransferRetrieved( | 172 void ProfileAuthDataTransferer::OnChannelIDsToTransferRetrieved( |
173 const net::ServerBoundCertStore::ServerBoundCertList& certs_to_transfer) { | 173 const net::ChannelIDStore::ChannelIDList& channel_ids_to_transfer) { |
174 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | 174 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
175 certs_to_transfer_ = certs_to_transfer; | 175 channel_ids_to_transfer_ = channel_ids_to_transfer; |
176 got_server_bound_certs_ = true; | 176 got_channel_ids_ = true; |
177 MaybeTransferCookiesAndCerts(); | 177 MaybeTransferCookiesAndChannelIDs(); |
178 } | 178 } |
179 | 179 |
180 void ProfileAuthDataTransferer::MaybeTransferCookiesAndCerts() { | 180 void ProfileAuthDataTransferer::MaybeTransferCookiesAndChannelIDs() { |
181 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | 181 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
182 if (!(got_cookies_ && got_server_bound_certs_)) | 182 if (!(got_cookies_ && got_channel_ids_)) |
183 return; | 183 return; |
184 | 184 |
185 // Nothing to transfer over? | 185 // Nothing to transfer over? |
186 if (!cookies_to_transfer_.size()) { | 186 if (!cookies_to_transfer_.size()) { |
187 Finish(); | 187 Finish(); |
188 return; | 188 return; |
189 } | 189 } |
190 | 190 |
191 // Retrieve the contents of |to_context_|'s cookie jar. | 191 // Retrieve the contents of |to_context_|'s cookie jar. |
192 net::CookieStore* to_store = | 192 net::CookieStore* to_store = |
193 to_context_->GetURLRequestContext()->cookie_store(); | 193 to_context_->GetURLRequestContext()->cookie_store(); |
194 net::CookieMonster* to_monster = to_store->GetCookieMonster(); | 194 net::CookieMonster* to_monster = to_store->GetCookieMonster(); |
195 to_monster->GetAllCookiesAsync( | 195 to_monster->GetAllCookiesAsync( |
196 base::Bind(&ProfileAuthDataTransferer::OnTargetCookieJarContentsRetrieved, | 196 base::Bind(&ProfileAuthDataTransferer::OnTargetCookieJarContentsRetrieved, |
197 base::Unretained(this))); | 197 base::Unretained(this))); |
198 } | 198 } |
199 | 199 |
200 void ProfileAuthDataTransferer::OnTargetCookieJarContentsRetrieved( | 200 void ProfileAuthDataTransferer::OnTargetCookieJarContentsRetrieved( |
201 const net::CookieList& target_cookies) { | 201 const net::CookieList& target_cookies) { |
202 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | 202 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
203 if (target_cookies.empty()) { | 203 if (target_cookies.empty()) { |
204 net::CookieStore* to_store = | 204 net::CookieStore* to_store = |
205 to_context_->GetURLRequestContext()->cookie_store(); | 205 to_context_->GetURLRequestContext()->cookie_store(); |
206 net::CookieMonster* to_monster = to_store->GetCookieMonster(); | 206 net::CookieMonster* to_monster = to_store->GetCookieMonster(); |
207 to_monster->InitializeFrom(cookies_to_transfer_); | 207 to_monster->InitializeFrom(cookies_to_transfer_); |
208 net::ServerBoundCertService* to_cert_service = | 208 net::ChannelIDService* to_cert_service = |
209 to_context_->GetURLRequestContext()->server_bound_cert_service(); | 209 to_context_->GetURLRequestContext()->channel_id_service(); |
210 to_cert_service->GetCertStore()->InitializeFrom(certs_to_transfer_); | 210 to_cert_service->GetChannelIDStore()->InitializeFrom( |
| 211 channel_ids_to_transfer_); |
211 } | 212 } |
212 | 213 |
213 Finish(); | 214 Finish(); |
214 } | 215 } |
215 | 216 |
216 void ProfileAuthDataTransferer::Finish() { | 217 void ProfileAuthDataTransferer::Finish() { |
217 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | 218 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
218 if (!completion_callback_.is_null()) | 219 if (!completion_callback_.is_null()) |
219 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, completion_callback_); | 220 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, completion_callback_); |
220 delete this; | 221 delete this; |
221 } | 222 } |
222 | 223 |
223 } // namespace | 224 } // namespace |
224 | 225 |
225 void ProfileAuthData::Transfer( | 226 void ProfileAuthData::Transfer( |
226 content::BrowserContext* from_context, | 227 content::BrowserContext* from_context, |
227 content::BrowserContext* to_context, | 228 content::BrowserContext* to_context, |
228 bool transfer_auth_cookies_and_server_bound_certs, | 229 bool transfer_auth_cookies_and_channel_ids, |
229 const base::Closure& completion_callback) { | 230 const base::Closure& completion_callback) { |
230 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 231 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
231 (new ProfileAuthDataTransferer(from_context, | 232 (new ProfileAuthDataTransferer(from_context, |
232 to_context, | 233 to_context, |
233 transfer_auth_cookies_and_server_bound_certs, | 234 transfer_auth_cookies_and_channel_ids, |
234 completion_callback))->BeginTransfer(); | 235 completion_callback))->BeginTransfer(); |
235 } | 236 } |
236 | 237 |
237 } // namespace chromeos | 238 } // namespace chromeos |
OLD | NEW |