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