Extract SIGPIPE ignoring code to a common place.
[chromium-blink-merge.git] / google_apis / gaia / oauth2_mint_token_fetcher.h
blob4995c7aa9cb32edd13e34770d0390d76d4d2547f
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_MINT_TOKEN_FETCHER_H_
6 #define GOOGLE_APIS_GAIA_OAUTH2_MINT_TOKEN_FETCHER_H_
8 #include <string>
9 #include <vector>
11 #include "base/gtest_prod_util.h"
12 #include "base/memory/scoped_ptr.h"
13 #include "google_apis/gaia/oauth2_mint_token_consumer.h"
14 #include "googleurl/src/gurl.h"
15 #include "net/url_request/url_fetcher_delegate.h"
17 class OAuth2MintTokenFetcherTest;
19 namespace net {
20 class URLFetcher;
21 class URLRequestContextGetter;
22 class URLRequestStatus;
25 // Abstracts the details to mint new OAuth2 tokens from OAuth2 login scoped
26 // token.
28 // This class should be used on a single thread, but it can be whichever thread
29 // that you like.
30 // Also, do not reuse the same instance. Once Start() is called, the instance
31 // should not be reused.
33 // Usage:
34 // * Create an instance with a consumer.
35 // * Call Start()
36 // * The consumer passed in the constructor will be called on the same
37 // thread Start was called with the results.
39 // This class can handle one request at a time. To parallelize requests,
40 // create multiple instances.
41 class OAuth2MintTokenFetcher : public net::URLFetcherDelegate {
42 public:
43 OAuth2MintTokenFetcher(OAuth2MintTokenConsumer* consumer,
44 net::URLRequestContextGetter* getter,
45 const std::string& source);
46 virtual ~OAuth2MintTokenFetcher();
48 // Start the flow.
49 virtual void Start(const std::string& oauth_login_access_token,
50 const std::string& client_id,
51 const std::vector<std::string>& scopes,
52 const std::string& origin);
54 void CancelRequest();
56 // Implementation of net::URLFetcherDelegate
57 virtual void OnURLFetchComplete(const net::URLFetcher* source) OVERRIDE;
59 private:
60 enum State {
61 INITIAL,
62 MINT_TOKEN_STARTED,
63 MINT_TOKEN_DONE,
64 ERROR_STATE,
67 // Helper methods for the flow.
68 void StartMintToken();
69 void EndMintToken(const net::URLFetcher* source);
71 // Helper methods for reporting back results.
72 void OnMintTokenSuccess(const std::string& access_token);
73 void OnMintTokenFailure(const GoogleServiceAuthError& error);
75 // Other helpers.
76 static GURL MakeMintTokenUrl();
77 static std::string MakeMintTokenHeader(const std::string& access_token);
78 static std::string MakeMintTokenBody(const std::string& client_id,
79 const std::vector<std::string>& scopes,
80 const std::string& origin);
81 static bool ParseMintTokenResponse(const net::URLFetcher* source,
82 std::string* access_token);
84 // State that is set during construction.
85 OAuth2MintTokenConsumer* const consumer_;
86 net::URLRequestContextGetter* const getter_;
87 std::string source_;
88 State state_;
90 // While a fetch is in progress.
91 scoped_ptr<net::URLFetcher> fetcher_;
92 std::string oauth_login_access_token_;
93 std::string client_id_;
94 std::vector<std::string> scopes_;
95 std::string origin_;
97 friend class OAuth2MintTokenFetcherTest;
98 FRIEND_TEST_ALL_PREFIXES(OAuth2MintTokenFetcherTest,
99 ParseMintTokenResponse);
101 DISALLOW_COPY_AND_ASSIGN(OAuth2MintTokenFetcher);
104 #endif // GOOGLE_APIS_GAIA_OAUTH2_MINT_TOKEN_FETCHER_H_