Add testing/scripts/OWNERS
[chromium-blink-merge.git] / google_apis / gaia / oauth2_api_call_flow.h
blob164e2d15eff4a76f6f86674a165fbb6b26921e6d
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 "google_apis/gaia/oauth2_access_token_consumer.h"
13 #include "google_apis/gaia/oauth2_access_token_fetcher.h"
14 #include "net/url_request/url_fetcher_delegate.h"
15 #include "url/gurl.h"
17 class GoogleServiceAuthError;
18 class OAuth2MintTokenFlowTest;
20 namespace net {
21 class URLFetcher;
22 class URLRequestContextGetter;
25 // Base class for all classes that implement a flow to call OAuth2 enabled APIs,
26 // given an access token to the service. This class abstracts the basic steps
27 // and exposes template methods for sub-classes to implement for API specific
28 // details.
29 class OAuth2ApiCallFlow
30 : public net::URLFetcherDelegate,
31 public OAuth2AccessTokenConsumer {
32 public:
33 OAuth2ApiCallFlow();
35 ~OAuth2ApiCallFlow() override;
37 // Start the flow.
38 virtual void Start(net::URLRequestContextGetter* context,
39 const std::string& access_token);
41 // net::URLFetcherDelegate implementation.
42 void OnURLFetchComplete(const net::URLFetcher* source) override;
44 protected:
45 // Template methods for sub-classes.
47 // Methods to help create the API request.
48 virtual GURL CreateApiCallUrl() = 0;
49 virtual std::string CreateApiCallBody() = 0;
50 virtual std::string CreateApiCallBodyContentType();
52 // Sub-classes can expose an appropriate observer interface by implementing
53 // these template methods.
54 // Called when the API call finished successfully.
55 virtual void ProcessApiCallSuccess(const net::URLFetcher* source) = 0;
56 // Called when the API call failed.
57 virtual void ProcessApiCallFailure(const net::URLFetcher* source) = 0;
59 private:
60 enum State {
61 INITIAL,
62 API_CALL_STARTED,
63 API_CALL_DONE,
64 ERROR_STATE
67 // Creates an instance of URLFetcher that does not send or save cookies.
68 // Template method CreateApiCallUrl is used to get the URL.
69 // Template method CreateApiCallBody is used to get the body.
70 // The URLFether's method will be GET if body is empty, POST otherwise.
71 // Caller owns the returned instance.
72 net::URLFetcher* CreateURLFetcher(net::URLRequestContextGetter* context,
73 const std::string& access_token);
75 // Helper methods to implement the state machine for the flow.
76 void BeginApiCall();
77 void EndApiCall(const net::URLFetcher* source);
79 State state_;
80 scoped_ptr<net::URLFetcher> url_fetcher_;
82 DISALLOW_COPY_AND_ASSIGN(OAuth2ApiCallFlow);
85 #endif // GOOGLE_APIS_GAIA_OAUTH2_API_CALL_FLOW_H_