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_
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"
17 class GoogleServiceAuthError
;
18 class OAuth2MintTokenFlowTest
;
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
29 class OAuth2ApiCallFlow
30 : public net::URLFetcherDelegate
,
31 public OAuth2AccessTokenConsumer
{
35 ~OAuth2ApiCallFlow() override
;
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
;
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;
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.
77 void EndApiCall(const net::URLFetcher
* source
);
80 scoped_ptr
<net::URLFetcher
> url_fetcher_
;
82 DISALLOW_COPY_AND_ASSIGN(OAuth2ApiCallFlow
);
85 #endif // GOOGLE_APIS_GAIA_OAUTH2_API_CALL_FLOW_H_