1 // Copyright 2014 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "components/signin/core/browser/signin_manager_cookie_helper.h"
9 #include "base/message_loop/message_loop_proxy.h"
10 #include "google_apis/gaia/gaia_urls.h"
11 #include "net/cookies/cookie_monster.h"
12 #include "net/url_request/url_request_context.h"
14 SigninManagerCookieHelper::SigninManagerCookieHelper(
15 net::URLRequestContextGetter
* request_context_getter
,
16 scoped_refptr
<base::MessageLoopProxy
> ui_thread
,
17 scoped_refptr
<base::MessageLoopProxy
> io_thread
)
18 : request_context_getter_(request_context_getter
),
19 ui_thread_(ui_thread
),
20 io_thread_(io_thread
) {
21 DCHECK(ui_thread_
->BelongsToCurrentThread());
24 SigninManagerCookieHelper::~SigninManagerCookieHelper() {
27 void SigninManagerCookieHelper::StartFetchingGaiaCookiesOnUIThread(
28 const base::Callback
<void(const net::CookieList
& cookies
)>& callback
) {
29 StartFetchingCookiesOnUIThread(
30 GaiaUrls::GetInstance()->gaia_url(), callback
);
33 void SigninManagerCookieHelper::StartFetchingCookiesOnUIThread(
35 const base::Callback
<void(const net::CookieList
& cookies
)>& callback
) {
36 DCHECK(ui_thread_
->BelongsToCurrentThread());
37 DCHECK(!callback
.is_null());
38 DCHECK(completion_callback_
.is_null());
40 completion_callback_
= callback
;
41 io_thread_
->PostTask(FROM_HERE
,
42 base::Bind(&SigninManagerCookieHelper::FetchCookiesOnIOThread
,
47 void SigninManagerCookieHelper::FetchCookiesOnIOThread(const GURL
& url
) {
48 DCHECK(io_thread_
->BelongsToCurrentThread());
50 scoped_refptr
<net::CookieMonster
> cookie_monster
=
51 request_context_getter_
->GetURLRequestContext()->
52 cookie_store()->GetCookieMonster();
53 if (cookie_monster
.get()) {
54 cookie_monster
->GetAllCookiesForURLAsync(
55 url
, base::Bind(&SigninManagerCookieHelper::OnCookiesFetched
, this));
57 OnCookiesFetched(net::CookieList());
61 void SigninManagerCookieHelper::OnCookiesFetched(
62 const net::CookieList
& cookies
) {
63 DCHECK(io_thread_
->BelongsToCurrentThread());
64 ui_thread_
->PostTask(FROM_HERE
,
65 base::Bind(&SigninManagerCookieHelper::NotifyOnUIThread
, this, cookies
));
68 void SigninManagerCookieHelper::NotifyOnUIThread(
69 const net::CookieList
& cookies
) {
70 DCHECK(ui_thread_
->BelongsToCurrentThread());
71 base::ResetAndReturn(&completion_callback_
).Run(cookies
);