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.h"
13 #include "net/url_request/url_fetcher_delegate.h"
16 class GoogleServiceAuthError
;
17 class OAuth2MintTokenFlowTest
;
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
28 class OAuth2ApiCallFlow
: public net::URLFetcherDelegate
{
32 ~OAuth2ApiCallFlow() override
;
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
;
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
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;
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.
79 void EndApiCall(const net::URLFetcher
* source
);
82 scoped_ptr
<net::URLFetcher
> url_fetcher_
;
84 DISALLOW_COPY_AND_ASSIGN(OAuth2ApiCallFlow
);
87 #endif // GOOGLE_APIS_GAIA_OAUTH2_API_CALL_FLOW_H_