Roll WebRTC 9745:9761, Libjingle 9742:9761
[chromium-blink-merge.git] / google_apis / gaia / oauth2_api_call_flow.h
blobd8e6941943462527ad6536ce9b8c99e2866842ff
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.
5 #ifndef GOOGLE_APIS_GAIA_OAUTH2_API_CALL_FLOW_H_
6 #define GOOGLE_APIS_GAIA_OAUTH2_API_CALL_FLOW_H_
8 #include <string>
10 #include "base/gtest_prod_util.h"
11 #include "base/memory/scoped_ptr.h"
12 #include "net/url_request/url_fetcher.h"
13 #include "net/url_request/url_fetcher_delegate.h"
14 #include "url/gurl.h"
16 class GoogleServiceAuthError;
17 class OAuth2MintTokenFlowTest;
19 namespace net {
20 class URLFetcher;
21 class URLRequestContextGetter;
24 // Base class for all classes that implement a flow to call OAuth2 enabled APIs,
25 // given an access token to the service. This class abstracts the basic steps
26 // and exposes template methods for sub-classes to implement for API specific
27 // details.
28 class OAuth2ApiCallFlow : public net::URLFetcherDelegate {
29 public:
30 OAuth2ApiCallFlow();
32 ~OAuth2ApiCallFlow() override;
34 // Start the flow.
35 virtual void Start(net::URLRequestContextGetter* context,
36 const std::string& access_token);
38 // net::URLFetcherDelegate implementation.
39 void OnURLFetchComplete(const net::URLFetcher* source) override;
41 protected:
42 // Template methods for sub-classes.
44 // Methods to help create the API request.
45 virtual GURL CreateApiCallUrl() = 0;
46 virtual std::string CreateApiCallBody() = 0;
47 virtual std::string CreateApiCallBodyContentType();
49 // Returns the request type (e.g. GET, POST) for the |body| that will be sent
50 // with the request.
51 virtual net::URLFetcher::RequestType GetRequestTypeForBody(
52 const std::string& body);
54 // Sub-classes can expose an appropriate observer interface by implementing
55 // these template methods.
56 // Called when the API call finished successfully.
57 virtual void ProcessApiCallSuccess(const net::URLFetcher* source) = 0;
58 // Called when the API call failed.
59 virtual void ProcessApiCallFailure(const net::URLFetcher* source) = 0;
61 private:
62 enum State {
63 INITIAL,
64 API_CALL_STARTED,
65 API_CALL_DONE,
66 ERROR_STATE
69 // Creates an instance of URLFetcher that does not send or save cookies.
70 // Template method CreateApiCallUrl is used to get the URL.
71 // Template method CreateApiCallBody is used to get the body.
72 // The URLFether's method will be GET if body is empty, POST otherwise.
73 scoped_ptr<net::URLFetcher> CreateURLFetcher(
74 net::URLRequestContextGetter* context,
75 const std::string& access_token);
77 // Helper methods to implement the state machine for the flow.
78 void BeginApiCall();
79 void EndApiCall(const net::URLFetcher* source);
81 State state_;
82 scoped_ptr<net::URLFetcher> url_fetcher_;
84 DISALLOW_COPY_AND_ASSIGN(OAuth2ApiCallFlow);
87 #endif // GOOGLE_APIS_GAIA_OAUTH2_API_CALL_FLOW_H_