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 <string> | 7 #include <string> |
8 | 8 |
9 #include "base/bind.h" | 9 #include "base/bind.h" |
10 #include "base/bind_helpers.h" | 10 #include "base/bind_helpers.h" |
(...skipping 119 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
130 transfer_auth_cookies_and_channel_ids_on_first_login), | 130 transfer_auth_cookies_and_channel_ids_on_first_login), |
131 transfer_saml_auth_cookies_on_subsequent_login_( | 131 transfer_saml_auth_cookies_on_subsequent_login_( |
132 transfer_saml_auth_cookies_on_subsequent_login), | 132 transfer_saml_auth_cookies_on_subsequent_login), |
133 completion_callback_(completion_callback), | 133 completion_callback_(completion_callback), |
134 first_login_(false), | 134 first_login_(false), |
135 waiting_for_auth_cookies_(false), | 135 waiting_for_auth_cookies_(false), |
136 waiting_for_channel_ids_(false) { | 136 waiting_for_channel_ids_(false) { |
137 } | 137 } |
138 | 138 |
139 void ProfileAuthDataTransferer::BeginTransfer() { | 139 void ProfileAuthDataTransferer::BeginTransfer() { |
140 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 140 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
141 // If we aren't transferring auth cookies or channel IDs, post the completion | 141 // If we aren't transferring auth cookies or channel IDs, post the completion |
142 // callback immediately. Otherwise, it will be called when the transfer | 142 // callback immediately. Otherwise, it will be called when the transfer |
143 // finishes. | 143 // finishes. |
144 if (!transfer_auth_cookies_and_channel_ids_on_first_login_ && | 144 if (!transfer_auth_cookies_and_channel_ids_on_first_login_ && |
145 !transfer_saml_auth_cookies_on_subsequent_login_) { | 145 !transfer_saml_auth_cookies_on_subsequent_login_) { |
146 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, completion_callback_); | 146 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, completion_callback_); |
147 // Null the callback so that when Finish is called, the callback won't be | 147 // Null the callback so that when Finish is called, the callback won't be |
148 // called again. | 148 // called again. |
149 completion_callback_.Reset(); | 149 completion_callback_.Reset(); |
150 } | 150 } |
151 BrowserThread::PostTask( | 151 BrowserThread::PostTask( |
152 BrowserThread::IO, FROM_HERE, | 152 BrowserThread::IO, FROM_HERE, |
153 base::Bind(&ProfileAuthDataTransferer::BeginTransferOnIOThread, | 153 base::Bind(&ProfileAuthDataTransferer::BeginTransferOnIOThread, |
154 base::Unretained(this))); | 154 base::Unretained(this))); |
155 } | 155 } |
156 | 156 |
157 void ProfileAuthDataTransferer::BeginTransferOnIOThread() { | 157 void ProfileAuthDataTransferer::BeginTransferOnIOThread() { |
158 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | 158 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
159 TransferProxyAuthCache(); | 159 TransferProxyAuthCache(); |
160 if (transfer_auth_cookies_and_channel_ids_on_first_login_ || | 160 if (transfer_auth_cookies_and_channel_ids_on_first_login_ || |
161 transfer_saml_auth_cookies_on_subsequent_login_) { | 161 transfer_saml_auth_cookies_on_subsequent_login_) { |
162 // Retrieve the contents of |to_context_|'s cookie jar. | 162 // Retrieve the contents of |to_context_|'s cookie jar. |
163 net::CookieStore* to_store = | 163 net::CookieStore* to_store = |
164 to_context_->GetURLRequestContext()->cookie_store(); | 164 to_context_->GetURLRequestContext()->cookie_store(); |
165 net::CookieMonster* to_monster = to_store->GetCookieMonster(); | 165 net::CookieMonster* to_monster = to_store->GetCookieMonster(); |
166 to_monster->GetAllCookiesAsync( | 166 to_monster->GetAllCookiesAsync( |
167 base::Bind( | 167 base::Bind( |
168 &ProfileAuthDataTransferer::OnTargetCookieJarContentsRetrieved, | 168 &ProfileAuthDataTransferer::OnTargetCookieJarContentsRetrieved, |
169 base::Unretained(this))); | 169 base::Unretained(this))); |
170 } else { | 170 } else { |
171 Finish(); | 171 Finish(); |
172 } | 172 } |
173 } | 173 } |
174 | 174 |
175 void ProfileAuthDataTransferer::TransferProxyAuthCache() { | 175 void ProfileAuthDataTransferer::TransferProxyAuthCache() { |
176 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | 176 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
177 net::HttpAuthCache* new_cache = to_context_->GetURLRequestContext()-> | 177 net::HttpAuthCache* new_cache = to_context_->GetURLRequestContext()-> |
178 http_transaction_factory()->GetSession()->http_auth_cache(); | 178 http_transaction_factory()->GetSession()->http_auth_cache(); |
179 new_cache->UpdateAllFrom(*from_context_->GetURLRequestContext()-> | 179 new_cache->UpdateAllFrom(*from_context_->GetURLRequestContext()-> |
180 http_transaction_factory()->GetSession()->http_auth_cache()); | 180 http_transaction_factory()->GetSession()->http_auth_cache()); |
181 } | 181 } |
182 | 182 |
183 void ProfileAuthDataTransferer::OnTargetCookieJarContentsRetrieved( | 183 void ProfileAuthDataTransferer::OnTargetCookieJarContentsRetrieved( |
184 const net::CookieList& target_cookies) { | 184 const net::CookieList& target_cookies) { |
185 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | 185 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
186 first_login_ = target_cookies.empty(); | 186 first_login_ = target_cookies.empty(); |
187 if (first_login_) { | 187 if (first_login_) { |
188 // On first login, transfer all auth cookies and channel IDs if | 188 // On first login, transfer all auth cookies and channel IDs if |
189 // |transfer_auth_cookies_and_channel_ids_on_first_login_| is true. | 189 // |transfer_auth_cookies_and_channel_ids_on_first_login_| is true. |
190 waiting_for_auth_cookies_ = | 190 waiting_for_auth_cookies_ = |
191 transfer_auth_cookies_and_channel_ids_on_first_login_; | 191 transfer_auth_cookies_and_channel_ids_on_first_login_; |
192 waiting_for_channel_ids_ = | 192 waiting_for_channel_ids_ = |
193 transfer_auth_cookies_and_channel_ids_on_first_login_; | 193 transfer_auth_cookies_and_channel_ids_on_first_login_; |
194 } else { | 194 } else { |
195 // On subsequent login, transfer auth cookies set by the SAML IdP if | 195 // On subsequent login, transfer auth cookies set by the SAML IdP if |
196 // |transfer_saml_auth_cookies_on_subsequent_login_| is true. | 196 // |transfer_saml_auth_cookies_on_subsequent_login_| is true. |
197 waiting_for_auth_cookies_ = transfer_saml_auth_cookies_on_subsequent_login_; | 197 waiting_for_auth_cookies_ = transfer_saml_auth_cookies_on_subsequent_login_; |
198 } | 198 } |
199 | 199 |
200 if (!waiting_for_auth_cookies_ && !waiting_for_channel_ids_) { | 200 if (!waiting_for_auth_cookies_ && !waiting_for_channel_ids_) { |
201 Finish(); | 201 Finish(); |
202 return; | 202 return; |
203 } | 203 } |
204 | 204 |
205 if (waiting_for_auth_cookies_) | 205 if (waiting_for_auth_cookies_) |
206 RetrieveCookiesToTransfer(); | 206 RetrieveCookiesToTransfer(); |
207 if (waiting_for_channel_ids_) | 207 if (waiting_for_channel_ids_) |
208 RetrieveChannelIDsToTransfer(); | 208 RetrieveChannelIDsToTransfer(); |
209 } | 209 } |
210 | 210 |
211 void ProfileAuthDataTransferer::RetrieveCookiesToTransfer() { | 211 void ProfileAuthDataTransferer::RetrieveCookiesToTransfer() { |
212 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | 212 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
213 net::CookieStore* from_store = | 213 net::CookieStore* from_store = |
214 from_context_->GetURLRequestContext()->cookie_store(); | 214 from_context_->GetURLRequestContext()->cookie_store(); |
215 net::CookieMonster* from_monster = from_store->GetCookieMonster(); | 215 net::CookieMonster* from_monster = from_store->GetCookieMonster(); |
216 from_monster->SetKeepExpiredCookies(); | 216 from_monster->SetKeepExpiredCookies(); |
217 from_monster->GetAllCookiesAsync( | 217 from_monster->GetAllCookiesAsync( |
218 base::Bind(&ProfileAuthDataTransferer::OnCookiesToTransferRetrieved, | 218 base::Bind(&ProfileAuthDataTransferer::OnCookiesToTransferRetrieved, |
219 base::Unretained(this))); | 219 base::Unretained(this))); |
220 } | 220 } |
221 | 221 |
222 void ProfileAuthDataTransferer::OnCookiesToTransferRetrieved( | 222 void ProfileAuthDataTransferer::OnCookiesToTransferRetrieved( |
223 const net::CookieList& cookies_to_transfer) { | 223 const net::CookieList& cookies_to_transfer) { |
224 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | 224 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
225 waiting_for_auth_cookies_ = false; | 225 waiting_for_auth_cookies_ = false; |
226 cookies_to_transfer_ = cookies_to_transfer; | 226 cookies_to_transfer_ = cookies_to_transfer; |
227 | 227 |
228 // Look for cookies indicating the points in time at which redirects from GAIA | 228 // Look for cookies indicating the points in time at which redirects from GAIA |
229 // to SAML IdP and back occurred. These cookies are synthesized by | 229 // to SAML IdP and back occurred. These cookies are synthesized by |
230 // chrome/browser/resources/gaia_auth/background.js. If the cookies are found, | 230 // chrome/browser/resources/gaia_auth/background.js. If the cookies are found, |
231 // their creation times are stored in |saml_start_time_| and | 231 // their creation times are stored in |saml_start_time_| and |
232 // |cookies_to_transfer_| and the cookies are deleted. | 232 // |cookies_to_transfer_| and the cookies are deleted. |
233 for (net::CookieList::iterator it = cookies_to_transfer_.begin(); | 233 for (net::CookieList::iterator it = cookies_to_transfer_.begin(); |
234 it != cookies_to_transfer_.end(); ) { | 234 it != cookies_to_transfer_.end(); ) { |
235 if (it->Name() == kSAMLStartCookie) { | 235 if (it->Name() == kSAMLStartCookie) { |
236 saml_start_time_ = it->CreationDate(); | 236 saml_start_time_ = it->CreationDate(); |
237 it = cookies_to_transfer_.erase(it); | 237 it = cookies_to_transfer_.erase(it); |
238 } else if (it->Name() == kSAMLEndCookie) { | 238 } else if (it->Name() == kSAMLEndCookie) { |
239 saml_end_time_ = it->CreationDate(); | 239 saml_end_time_ = it->CreationDate(); |
240 it = cookies_to_transfer_.erase(it); | 240 it = cookies_to_transfer_.erase(it); |
241 } else { | 241 } else { |
242 ++it; | 242 ++it; |
243 } | 243 } |
244 } | 244 } |
245 | 245 |
246 MaybeTransferCookiesAndChannelIDs(); | 246 MaybeTransferCookiesAndChannelIDs(); |
247 } | 247 } |
248 | 248 |
249 void ProfileAuthDataTransferer::RetrieveChannelIDsToTransfer() { | 249 void ProfileAuthDataTransferer::RetrieveChannelIDsToTransfer() { |
250 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | 250 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
251 net::ChannelIDService* from_service = | 251 net::ChannelIDService* from_service = |
252 from_context_->GetURLRequestContext()->channel_id_service(); | 252 from_context_->GetURLRequestContext()->channel_id_service(); |
253 from_service->GetChannelIDStore()->GetAllChannelIDs( | 253 from_service->GetChannelIDStore()->GetAllChannelIDs( |
254 base::Bind( | 254 base::Bind( |
255 &ProfileAuthDataTransferer::OnChannelIDsToTransferRetrieved, | 255 &ProfileAuthDataTransferer::OnChannelIDsToTransferRetrieved, |
256 base::Unretained(this))); | 256 base::Unretained(this))); |
257 } | 257 } |
258 | 258 |
259 void ProfileAuthDataTransferer::OnChannelIDsToTransferRetrieved( | 259 void ProfileAuthDataTransferer::OnChannelIDsToTransferRetrieved( |
260 const net::ChannelIDStore::ChannelIDList& channel_ids_to_transfer) { | 260 const net::ChannelIDStore::ChannelIDList& channel_ids_to_transfer) { |
261 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | 261 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
262 channel_ids_to_transfer_ = channel_ids_to_transfer; | 262 channel_ids_to_transfer_ = channel_ids_to_transfer; |
263 waiting_for_channel_ids_ = false; | 263 waiting_for_channel_ids_ = false; |
264 MaybeTransferCookiesAndChannelIDs(); | 264 MaybeTransferCookiesAndChannelIDs(); |
265 } | 265 } |
266 | 266 |
267 bool ProfileAuthDataTransferer::IsGAIACookie( | 267 bool ProfileAuthDataTransferer::IsGAIACookie( |
268 const net::CanonicalCookie& cookie) { | 268 const net::CanonicalCookie& cookie) { |
269 const base::Time& creation_date = cookie.CreationDate(); | 269 const base::Time& creation_date = cookie.CreationDate(); |
270 if (creation_date < saml_start_time_) | 270 if (creation_date < saml_start_time_) |
271 return true; | 271 return true; |
272 if (!saml_end_time_.is_null() && creation_date > saml_end_time_) | 272 if (!saml_end_time_.is_null() && creation_date > saml_end_time_) |
273 return true; | 273 return true; |
274 | 274 |
275 const std::string& domain = cookie.Domain(); | 275 const std::string& domain = cookie.Domain(); |
276 return domain.find("google") != std::string::npos || | 276 return domain.find("google") != std::string::npos || |
277 domain.find("youtube") != std::string::npos; | 277 domain.find("youtube") != std::string::npos; |
278 } | 278 } |
279 | 279 |
280 void ProfileAuthDataTransferer::MaybeTransferCookiesAndChannelIDs() { | 280 void ProfileAuthDataTransferer::MaybeTransferCookiesAndChannelIDs() { |
281 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | 281 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
282 if (waiting_for_auth_cookies_ || waiting_for_channel_ids_) | 282 if (waiting_for_auth_cookies_ || waiting_for_channel_ids_) |
283 return; | 283 return; |
284 | 284 |
285 net::CookieStore* to_store = | 285 net::CookieStore* to_store = |
286 to_context_->GetURLRequestContext()->cookie_store(); | 286 to_context_->GetURLRequestContext()->cookie_store(); |
287 net::CookieMonster* to_monster = to_store->GetCookieMonster(); | 287 net::CookieMonster* to_monster = to_store->GetCookieMonster(); |
288 if (first_login_) { | 288 if (first_login_) { |
289 to_monster->ImportCookies(cookies_to_transfer_); | 289 to_monster->ImportCookies(cookies_to_transfer_); |
290 net::ChannelIDService* to_cert_service = | 290 net::ChannelIDService* to_cert_service = |
291 to_context_->GetURLRequestContext()->channel_id_service(); | 291 to_context_->GetURLRequestContext()->channel_id_service(); |
292 to_cert_service->GetChannelIDStore()->InitializeFrom( | 292 to_cert_service->GetChannelIDStore()->InitializeFrom( |
293 channel_ids_to_transfer_); | 293 channel_ids_to_transfer_); |
294 } else { | 294 } else { |
295 net::CookieList non_gaia_cookies; | 295 net::CookieList non_gaia_cookies; |
296 for (net::CookieList::const_iterator it = cookies_to_transfer_.begin(); | 296 for (net::CookieList::const_iterator it = cookies_to_transfer_.begin(); |
297 it != cookies_to_transfer_.end(); ++it) { | 297 it != cookies_to_transfer_.end(); ++it) { |
298 if (!IsGAIACookie(*it)) | 298 if (!IsGAIACookie(*it)) |
299 non_gaia_cookies.push_back(*it); | 299 non_gaia_cookies.push_back(*it); |
300 } | 300 } |
301 to_monster->ImportCookies(non_gaia_cookies); | 301 to_monster->ImportCookies(non_gaia_cookies); |
302 } | 302 } |
303 | 303 |
304 Finish(); | 304 Finish(); |
305 } | 305 } |
306 | 306 |
307 void ProfileAuthDataTransferer::Finish() { | 307 void ProfileAuthDataTransferer::Finish() { |
308 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | 308 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
309 if (!completion_callback_.is_null()) | 309 if (!completion_callback_.is_null()) |
310 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, completion_callback_); | 310 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, completion_callback_); |
311 base::MessageLoop::current()->DeleteSoon(FROM_HERE, this); | 311 base::MessageLoop::current()->DeleteSoon(FROM_HERE, this); |
312 } | 312 } |
313 | 313 |
314 } // namespace | 314 } // namespace |
315 | 315 |
316 void ProfileAuthData::Transfer( | 316 void ProfileAuthData::Transfer( |
317 content::BrowserContext* from_context, | 317 content::BrowserContext* from_context, |
318 content::BrowserContext* to_context, | 318 content::BrowserContext* to_context, |
319 bool transfer_auth_cookies_and_channel_ids_on_first_login, | 319 bool transfer_auth_cookies_and_channel_ids_on_first_login, |
320 bool transfer_saml_auth_cookies_on_subsequent_login, | 320 bool transfer_saml_auth_cookies_on_subsequent_login, |
321 const base::Closure& completion_callback) { | 321 const base::Closure& completion_callback) { |
322 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 322 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
323 (new ProfileAuthDataTransferer( | 323 (new ProfileAuthDataTransferer( |
324 from_context, | 324 from_context, |
325 to_context, | 325 to_context, |
326 transfer_auth_cookies_and_channel_ids_on_first_login, | 326 transfer_auth_cookies_and_channel_ids_on_first_login, |
327 transfer_saml_auth_cookies_on_subsequent_login, | 327 transfer_saml_auth_cookies_on_subsequent_login, |
328 completion_callback))->BeginTransfer(); | 328 completion_callback))->BeginTransfer(); |
329 } | 329 } |
330 | 330 |
331 } // namespace chromeos | 331 } // namespace chromeos |
OLD | NEW |