signin_manager_cookie_helper.cc revision effb81e5f8246d0db0270817048dc992db66e9fb
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
14SigninManagerCookieHelper::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
24SigninManagerCookieHelper::~SigninManagerCookieHelper() {
25}
26
27void SigninManagerCookieHelper::StartFetchingGaiaCookiesOnUIThread(
28    const base::Callback<void(const net::CookieList& cookies)>& callback) {
29  StartFetchingCookiesOnUIThread(
30      GaiaUrls::GetInstance()->gaia_url(), callback);
31}
32
33void 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
47void 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
61void 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
68void SigninManagerCookieHelper::NotifyOnUIThread(
69    const net::CookieList& cookies) {
70  DCHECK(ui_thread_->BelongsToCurrentThread());
71  base::ResetAndReturn(&completion_callback_).Run(cookies);
72}
73