Roll src/third_party/WebKit 9f7fb92:f103b33 (svn 202621:202622)
[chromium-blink-merge.git] / components / signin / core / browser / signin_manager_cookie_helper.cc
blob846ba67c2fa84f75d160004dde5fb2d07cfac41d
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"
7 #include <vector>
9 #include "base/location.h"
10 #include "base/single_thread_task_runner.h"
11 #include "google_apis/gaia/gaia_urls.h"
12 #include "net/cookies/cookie_monster.h"
13 #include "net/url_request/url_request_context.h"
15 SigninManagerCookieHelper::SigninManagerCookieHelper(
16 net::URLRequestContextGetter* request_context_getter,
17 scoped_refptr<base::SingleThreadTaskRunner> ui_thread,
18 scoped_refptr<base::SingleThreadTaskRunner> io_thread)
19 : request_context_getter_(request_context_getter),
20 ui_thread_(ui_thread),
21 io_thread_(io_thread) {
22 DCHECK(ui_thread_->BelongsToCurrentThread());
25 SigninManagerCookieHelper::~SigninManagerCookieHelper() {
28 void SigninManagerCookieHelper::StartFetchingGaiaCookiesOnUIThread(
29 const base::Callback<void(const net::CookieList& cookies)>& callback) {
30 StartFetchingCookiesOnUIThread(
31 GaiaUrls::GetInstance()->gaia_url(), callback);
34 void SigninManagerCookieHelper::StartFetchingCookiesOnUIThread(
35 const GURL& url,
36 const base::Callback<void(const net::CookieList& cookies)>& callback) {
37 DCHECK(ui_thread_->BelongsToCurrentThread());
38 DCHECK(!callback.is_null());
39 DCHECK(completion_callback_.is_null());
41 completion_callback_ = callback;
42 io_thread_->PostTask(FROM_HERE,
43 base::Bind(&SigninManagerCookieHelper::FetchCookiesOnIOThread,
44 this,
45 url));
48 void SigninManagerCookieHelper::FetchCookiesOnIOThread(const GURL& url) {
49 DCHECK(io_thread_->BelongsToCurrentThread());
51 scoped_refptr<net::CookieMonster> cookie_monster =
52 request_context_getter_->GetURLRequestContext()->
53 cookie_store()->GetCookieMonster();
54 if (cookie_monster.get()) {
55 cookie_monster->GetAllCookiesForURLAsync(
56 url, base::Bind(&SigninManagerCookieHelper::OnCookiesFetched, this));
57 } else {
58 OnCookiesFetched(net::CookieList());
62 void SigninManagerCookieHelper::OnCookiesFetched(
63 const net::CookieList& cookies) {
64 DCHECK(io_thread_->BelongsToCurrentThread());
65 ui_thread_->PostTask(FROM_HERE,
66 base::Bind(&SigninManagerCookieHelper::NotifyOnUIThread, this, cookies));
69 void SigninManagerCookieHelper::NotifyOnUIThread(
70 const net::CookieList& cookies) {
71 DCHECK(ui_thread_->BelongsToCurrentThread());
72 base::ResetAndReturn(&completion_callback_).Run(cookies);