summaryrefslogtreecommitdiffstats
path: root/remoting/host/oauth_client.h
blob: a92077ac01951652cfc06bf31cde3feb1b3a95d0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
// Copyright (c) 2012 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
//
// This class implements the OAuth2 client for the Chromoting host.

#ifndef REMOTING_HOST_OAUTH_CLIENT_H_
#define REMOTING_HOST_OAUTH_CLIENT_H_

#include <string>

#include "base/memory/scoped_ptr.h"
#include "remoting/host/gaia_oauth_client.h"

namespace base {
class MessageLoopProxy;
}  // namespace base

namespace remoting {

class URLRequestContextGetter;

class OAuthClient : public GaiaOAuthClient::Delegate {
 public:
  class Delegate {
   public:
    virtual ~Delegate() { }
    // Invoked on a successful response to the RefreshToken request.
    virtual void OnRefreshTokenResponse(const std::string& access_token,
                                        int expires_in_seconds) = 0;
    // Invoked when there is an OAuth error with one of the requests.
    virtual void OnOAuthError() = 0;
  };

  OAuthClient();
  virtual ~OAuthClient();

  // Notify the delegate on the speficied message loop when an access token is
  // available. As long as the refresh is successful, another will be queued,
  // timed just before the access token expires. As soon as an error occurs,
  // this automatic refresh behaviour is cancelled.
  //
  // The delegate is accessed on the specified message loop, and must out-live
  // it.
  void Start(const scoped_refptr<URLRequestContextGetter>& url_context_,
             const std::string& refresh_token,
             Delegate* delegate,
             base::MessageLoopProxy* message_loop);

  // Overridden from GaiaOAuthClient::Delegate
  virtual void OnGetTokensResponse(const std::string& refresh_token,
                                   const std::string& access_token,
                                   int expires_in_seconds) OVERRIDE;
  virtual void OnRefreshTokenResponse(const std::string& access_token,
                                      int expires) OVERRIDE;
  virtual void OnOAuthError() OVERRIDE;
  virtual void OnNetworkError(int response_code) OVERRIDE;

 private:
  void RefreshToken();

  scoped_ptr<GaiaOAuthClient> gaia_oauth_client_;
  std::string refresh_token_;
  OAuthClient::Delegate* delegate_;
  scoped_refptr<base::MessageLoopProxy> message_loop_;

  DISALLOW_COPY_AND_ASSIGN(OAuthClient);
};

}  // namespace remoting

#endif  // REMOTING_HOST_OAUTH_CLIENT_H_