iOS gtest launcher: retry tests in one big batch
[chromium-blink-merge.git] / google_apis / gaia / oauth2_api_call_flow.h
blobfd4c660732baedb2d627df8e74563bf0176c117a
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_delegate.h"
13 #include "url/gurl.h"
15 class GoogleServiceAuthError;
16 class OAuth2MintTokenFlowTest;
18 namespace net {
19 class URLFetcher;
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
26 // details.
27 class OAuth2ApiCallFlow : public net::URLFetcherDelegate {
28 public:
29 OAuth2ApiCallFlow();
31 ~OAuth2ApiCallFlow() override;
33 // Start the flow.
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;
40 protected:
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;
55 private:
56 enum State {
57 INITIAL,
58 API_CALL_STARTED,
59 API_CALL_DONE,
60 ERROR_STATE
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.
72 void BeginApiCall();
73 void EndApiCall(const net::URLFetcher* source);
75 State state_;
76 scoped_ptr<net::URLFetcher> url_fetcher_;
78 DISALLOW_COPY_AND_ASSIGN(OAuth2ApiCallFlow);
81 #endif // GOOGLE_APIS_GAIA_OAUTH2_API_CALL_FLOW_H_