cookie_fetcher.h revision 3345a6884c488ff3a535c2c9acdd33d74b37e311
1// Copyright (c) 2010 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#ifndef CHROME_BROWSER_CHROMEOS_LOGIN_COOKIE_FETCHER_H_
6#define CHROME_BROWSER_CHROMEOS_LOGIN_COOKIE_FETCHER_H_
7#pragma once
8
9#include <string>
10#include "base/scoped_ptr.h"
11#include "chrome/browser/chromeos/login/auth_response_handler.h"
12#include "chrome/browser/chromeos/login/client_login_response_handler.h"
13#include "chrome/browser/chromeos/login/issue_response_handler.h"
14#include "chrome/browser/profile.h"
15#include "chrome/common/net/url_fetcher.h"
16
17namespace chromeos {
18
19// Given a SID/LSID pair, this class will attempt to turn them into a
20// full-fledged set of Google AuthN cookies.
21//
22// A CookieFetcher manages its own lifecycle.  It deletes itself once it's
23// done attempting to fetch URLs.
24class CookieFetcher : public URLFetcher::Delegate {
25 public:
26  // This class is a very thin wrapper around posting a task to the UI thread
27  // to call LoginUtils::DoBrowserLaunch().  It's here to allow mocking.
28  //
29  // In normal usage, instances of this class are owned by a CookieFetcher.
30  class Delegate {
31   public:
32    Delegate() {}
33    virtual ~Delegate() {}
34    virtual void DoLaunch(Profile* profile) = 0;
35  };
36
37  // |profile| is the Profile whose cookie jar you want the cookies in.
38  explicit CookieFetcher(Profile* profile);
39
40  // |profile| is the Profile whose cookie jar you want the cookies in.
41  // Takes ownership of |cl_handler|, |i_handler|, and |launcher|.
42  CookieFetcher(Profile* profile,
43                AuthResponseHandler* cl_handler,
44                AuthResponseHandler* i_handler,
45                Delegate* launcher)
46      : profile_(profile),
47        client_login_handler_(cl_handler),
48        issue_handler_(i_handler),
49        launcher_(launcher) {
50  }
51
52  // Given a newline-delineated SID/LSID pair of Google cookies (like
53  // those that come back from ClientLogin), try to use them to fetch
54  // a full-fledged set of Google AuthN cookies.  These cookies will wind up
55  // stored in the cookie jar associated with |profile_|, if we get them.
56  // Either way, we end up by calling launcher_->DoLaunch()
57  void AttemptFetch(const std::string& credentials);
58
59  // Overloaded from URLFetcher::Delegate.
60  virtual void OnURLFetchComplete(const URLFetcher* source,
61                                  const GURL& url,
62                                  const URLRequestStatus& status,
63                                  int response_code,
64                                  const ResponseCookies& cookies,
65                                  const std::string& data);
66
67 private:
68  class DelegateImpl : public Delegate {
69   public:
70    DelegateImpl() {}
71    ~DelegateImpl() {}
72    void DoLaunch(Profile* profile);
73   private:
74    DISALLOW_COPY_AND_ASSIGN(DelegateImpl);
75  };
76
77  virtual ~CookieFetcher() {}
78
79  scoped_ptr<URLFetcher> fetcher_;
80  Profile* profile_;
81  scoped_ptr<AuthResponseHandler> client_login_handler_;
82  scoped_ptr<AuthResponseHandler> issue_handler_;
83  scoped_ptr<Delegate> launcher_;
84
85  DISALLOW_COPY_AND_ASSIGN(CookieFetcher);
86};
87
88}  // namespace chromeos
89
90#endif  // CHROME_BROWSER_CHROMEOS_LOGIN_COOKIE_FETCHER_H_
91