This source file includes following definitions.
- got_server_bound_certs_
- BeginTransfer
- BeginTransferOnIOThread
- MaybeDoCookieAndCertTransfer
- Finish
- OnTransferCookiesIfEmptyJar
- OnGetCookiesToTransfer
- RetrieveDefaultCookies
- OnGetServerBoundCertsToTransfer
- RetrieveDefaultServerBoundCerts
- TransferDefaultAuthCache
- Transfer
#include "chrome/browser/chromeos/login/profile_auth_data.h"
#include "chrome/browser/profiles/profile.h"
#include "content/public/browser/browser_thread.h"
#include "net/cookies/cookie_monster.h"
#include "net/cookies/cookie_store.h"
#include "net/http/http_auth_cache.h"
#include "net/http/http_network_session.h"
#include "net/http/http_transaction_factory.h"
#include "net/ssl/server_bound_cert_service.h"
#include "net/ssl/server_bound_cert_store.h"
#include "net/url_request/url_request_context.h"
#include "net/url_request/url_request_context_getter.h"
using content::BrowserThread;
namespace chromeos {
namespace {
class ProfileAuthDataTransferer {
public:
ProfileAuthDataTransferer(
Profile* from_profile,
Profile* to_profile,
bool transfer_cookies,
const base::Closure& completion_callback);
void BeginTransfer();
private:
void BeginTransferOnIOThread();
void MaybeDoCookieAndCertTransfer();
void Finish();
void OnTransferCookiesIfEmptyJar(const net::CookieList& cookies_in_jar);
void OnGetCookiesToTransfer(const net::CookieList& cookies_to_transfer);
void RetrieveDefaultCookies();
void OnGetServerBoundCertsToTransfer(
const net::ServerBoundCertStore::ServerBoundCertList& certs);
void RetrieveDefaultServerBoundCerts();
void TransferDefaultAuthCache();
scoped_refptr<net::URLRequestContextGetter> from_context_;
scoped_refptr<net::URLRequestContextGetter> to_context_;
bool transfer_cookies_;
base::Closure completion_callback_;
net::CookieList cookies_to_transfer_;
net::ServerBoundCertStore::ServerBoundCertList certs_to_transfer_;
bool got_cookies_;
bool got_server_bound_certs_;
};
ProfileAuthDataTransferer::ProfileAuthDataTransferer(
Profile* from_profile,
Profile* to_profile,
bool transfer_cookies,
const base::Closure& completion_callback)
: from_context_(from_profile->GetRequestContext()),
to_context_(to_profile->GetRequestContext()),
transfer_cookies_(transfer_cookies),
completion_callback_(completion_callback),
got_cookies_(false),
got_server_bound_certs_(false) {
}
void ProfileAuthDataTransferer::BeginTransfer() {
DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
if (!transfer_cookies_) {
BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, completion_callback_);
completion_callback_.Reset();
}
BrowserThread::PostTask(
BrowserThread::IO, FROM_HERE,
base::Bind(&ProfileAuthDataTransferer::BeginTransferOnIOThread,
base::Unretained(this)));
}
void ProfileAuthDataTransferer::BeginTransferOnIOThread() {
DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
TransferDefaultAuthCache();
if (transfer_cookies_) {
RetrieveDefaultCookies();
RetrieveDefaultServerBoundCerts();
} else {
Finish();
}
}
void ProfileAuthDataTransferer::MaybeDoCookieAndCertTransfer() {
DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
if (!(got_cookies_ && got_server_bound_certs_))
return;
if (!cookies_to_transfer_.size()) {
Finish();
return;
}
net::CookieStore* to_store =
to_context_->GetURLRequestContext()->cookie_store();
net::CookieMonster* to_monster = to_store->GetCookieMonster();
to_monster->GetAllCookiesAsync(
base::Bind(&ProfileAuthDataTransferer::OnTransferCookiesIfEmptyJar,
base::Unretained(this)));
}
void ProfileAuthDataTransferer::Finish() {
DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
if (!completion_callback_.is_null())
BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, completion_callback_);
delete this;
}
void ProfileAuthDataTransferer::OnTransferCookiesIfEmptyJar(
const net::CookieList& cookies_in_jar) {
DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
if (!cookies_in_jar.size()) {
net::CookieStore* to_store =
to_context_->GetURLRequestContext()->cookie_store();
net::CookieMonster* to_monster = to_store->GetCookieMonster();
to_monster->InitializeFrom(cookies_to_transfer_);
net::ServerBoundCertService* to_cert_service =
to_context_->GetURLRequestContext()->server_bound_cert_service();
to_cert_service->GetCertStore()->InitializeFrom(certs_to_transfer_);
}
Finish();
}
void ProfileAuthDataTransferer::OnGetCookiesToTransfer(
const net::CookieList& cookies_to_transfer) {
DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
got_cookies_ = true;
cookies_to_transfer_ = cookies_to_transfer;
MaybeDoCookieAndCertTransfer();
}
void ProfileAuthDataTransferer::RetrieveDefaultCookies() {
DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
net::CookieStore* from_store =
from_context_->GetURLRequestContext()->cookie_store();
net::CookieMonster* from_monster = from_store->GetCookieMonster();
from_monster->SetKeepExpiredCookies();
from_monster->GetAllCookiesAsync(
base::Bind(&ProfileAuthDataTransferer::OnGetCookiesToTransfer,
base::Unretained(this)));
}
void ProfileAuthDataTransferer::OnGetServerBoundCertsToTransfer(
const net::ServerBoundCertStore::ServerBoundCertList& certs) {
DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
certs_to_transfer_ = certs;
got_server_bound_certs_ = true;
MaybeDoCookieAndCertTransfer();
}
void ProfileAuthDataTransferer::RetrieveDefaultServerBoundCerts() {
DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
net::ServerBoundCertService* from_service =
from_context_->GetURLRequestContext()->server_bound_cert_service();
from_service->GetCertStore()->GetAllServerBoundCerts(
base::Bind(&ProfileAuthDataTransferer::OnGetServerBoundCertsToTransfer,
base::Unretained(this)));
}
void ProfileAuthDataTransferer::TransferDefaultAuthCache() {
DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
net::HttpAuthCache* new_cache = to_context_->GetURLRequestContext()->
http_transaction_factory()->GetSession()->http_auth_cache();
new_cache->UpdateAllFrom(*from_context_->GetURLRequestContext()->
http_transaction_factory()->GetSession()->http_auth_cache());
}
}
void ProfileAuthData::Transfer(
Profile* from_profile,
Profile* to_profile,
bool transfer_cookies,
const base::Closure& completion_callback) {
DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
(new ProfileAuthDataTransferer(from_profile, to_profile, transfer_cookies,
completion_callback))->BeginTransfer();
}
}