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.
4
5 #include "components/signin/core/browser/signin_manager_cookie_helper.h"
6
7 #include <vector>
8
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"
13
SigninManagerCookieHelper(net::URLRequestContextGetter * request_context_getter,scoped_refptr<base::MessageLoopProxy> ui_thread,scoped_refptr<base::MessageLoopProxy> io_thread)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());
22 }
23
~SigninManagerCookieHelper()24 SigninManagerCookieHelper::~SigninManagerCookieHelper() {
25 }
26
StartFetchingGaiaCookiesOnUIThread(const base::Callback<void (const net::CookieList & cookies)> & callback)27 void SigninManagerCookieHelper::StartFetchingGaiaCookiesOnUIThread(
28 const base::Callback<void(const net::CookieList& cookies)>& callback) {
29 StartFetchingCookiesOnUIThread(
30 GaiaUrls::GetInstance()->gaia_url(), callback);
31 }
32
StartFetchingCookiesOnUIThread(const GURL & url,const base::Callback<void (const net::CookieList & cookies)> & callback)33 void SigninManagerCookieHelper::StartFetchingCookiesOnUIThread(
34 const GURL& url,
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());
39
40 completion_callback_ = callback;
41 io_thread_->PostTask(FROM_HERE,
42 base::Bind(&SigninManagerCookieHelper::FetchCookiesOnIOThread,
43 this,
44 url));
45 }
46
FetchCookiesOnIOThread(const GURL & url)47 void SigninManagerCookieHelper::FetchCookiesOnIOThread(const GURL& url) {
48 DCHECK(io_thread_->BelongsToCurrentThread());
49
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));
56 } else {
57 OnCookiesFetched(net::CookieList());
58 }
59 }
60
OnCookiesFetched(const net::CookieList & cookies)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));
66 }
67
NotifyOnUIThread(const net::CookieList & cookies)68 void SigninManagerCookieHelper::NotifyOnUIThread(
69 const net::CookieList& cookies) {
70 DCHECK(ui_thread_->BelongsToCurrentThread());
71 base::ResetAndReturn(&completion_callback_).Run(cookies);
72 }
73