Home | History | Annotate | Download | only in signin
      1 // Copyright (c) 2012 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 "chrome/browser/signin/signin_manager_cookie_helper.h"
      6 
      7 #include <vector>
      8 
      9 #include "content/public/browser/browser_thread.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 
     14 using content::BrowserThread;
     15 
     16 SigninManagerCookieHelper::SigninManagerCookieHelper(
     17     net::URLRequestContextGetter* request_context_getter)
     18     : request_context_getter_(request_context_getter) {
     19   DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
     20 }
     21 
     22 SigninManagerCookieHelper::~SigninManagerCookieHelper() {
     23 }
     24 
     25 void SigninManagerCookieHelper::StartFetchingGaiaCookiesOnUIThread(
     26     const base::Callback<void(const net::CookieList& cookies)>& callback) {
     27   DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
     28   DCHECK(!callback.is_null());
     29   DCHECK(completion_callback_.is_null());
     30 
     31   completion_callback_ = callback;
     32   BrowserThread::PostTask(
     33       BrowserThread::IO, FROM_HERE,
     34       base::Bind(&SigninManagerCookieHelper::FetchGaiaCookiesOnIOThread, this));
     35 }
     36 
     37 void SigninManagerCookieHelper::FetchGaiaCookiesOnIOThread() {
     38   DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
     39 
     40   scoped_refptr<net::CookieMonster> cookie_monster =
     41       request_context_getter_->GetURLRequestContext()->
     42       cookie_store()->GetCookieMonster();
     43   if (cookie_monster.get()) {
     44     cookie_monster->GetAllCookiesForURLAsync(
     45         GaiaUrls::GetInstance()->gaia_url(),
     46         base::Bind(&SigninManagerCookieHelper::OnGaiaCookiesFetched, this));
     47   } else {
     48     OnGaiaCookiesFetched(net::CookieList());
     49   }
     50 }
     51 
     52 void SigninManagerCookieHelper::OnGaiaCookiesFetched(
     53     const net::CookieList& cookies) {
     54   DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
     55   BrowserThread::PostTask(
     56       BrowserThread::UI, FROM_HERE,
     57       base::Bind(&SigninManagerCookieHelper::NotifyOnUIThread, this, cookies));
     58 }
     59 
     60 void SigninManagerCookieHelper::NotifyOnUIThread(
     61     const net::CookieList& cookies) {
     62   DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
     63   base::ResetAndReturn(&completion_callback_).Run(cookies);
     64 }
     65