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 "net/url_request/url_fetcher_delegate.h"
15 class GoogleServiceAuthError
;
16 class OAuth2MintTokenFlowTest
;
20 class URLRequestContextGetter
;
23 // Base class for all classes that implement a flow to call OAuth2 enabled APIs,
24 // given an access token to the service. This class abstracts the basic steps
25 // and exposes template methods for sub-classes to implement for API specific
27 class OAuth2ApiCallFlow
: public net::URLFetcherDelegate
{
31 ~OAuth2ApiCallFlow() override
;
34 virtual void Start(net::URLRequestContextGetter
* context
,
35 const std::string
& access_token
);
37 // net::URLFetcherDelegate implementation.
38 void OnURLFetchComplete(const net::URLFetcher
* source
) override
;
41 // Template methods for sub-classes.
43 // Methods to help create the API request.
44 virtual GURL
CreateApiCallUrl() = 0;
45 virtual std::string
CreateApiCallBody() = 0;
46 virtual std::string
CreateApiCallBodyContentType();
48 // Sub-classes can expose an appropriate observer interface by implementing
49 // these template methods.
50 // Called when the API call finished successfully.
51 virtual void ProcessApiCallSuccess(const net::URLFetcher
* source
) = 0;
52 // Called when the API call failed.
53 virtual void ProcessApiCallFailure(const net::URLFetcher
* source
) = 0;
63 // Creates an instance of URLFetcher that does not send or save cookies.
64 // Template method CreateApiCallUrl is used to get the URL.
65 // Template method CreateApiCallBody is used to get the body.
66 // The URLFether's method will be GET if body is empty, POST otherwise.
67 // Caller owns the returned instance.
68 net::URLFetcher
* CreateURLFetcher(net::URLRequestContextGetter
* context
,
69 const std::string
& access_token
);
71 // Helper methods to implement the state machine for the flow.
73 void EndApiCall(const net::URLFetcher
* source
);
76 scoped_ptr
<net::URLFetcher
> url_fetcher_
;
78 DISALLOW_COPY_AND_ASSIGN(OAuth2ApiCallFlow
);
81 #endif // GOOGLE_APIS_GAIA_OAUTH2_API_CALL_FLOW_H_