David Maunder | e7daf77 | 2019-08-30 14:13:29 | [diff] [blame] | 1 | // Copyright 2019 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. |
| 4 | |
David Maunder | 012fe329 | 2020-09-15 15:38:49 | [diff] [blame] | 5 | #ifndef CHROME_BROWSER_ENDPOINT_FETCHER_ENDPOINT_FETCHER_H_ |
| 6 | #define CHROME_BROWSER_ENDPOINT_FETCHER_ENDPOINT_FETCHER_H_ |
David Maunder | e7daf77 | 2019-08-30 14:13:29 | [diff] [blame] | 7 | |
| 8 | #include <string> |
| 9 | #include <vector> |
| 10 | |
| 11 | #include "base/memory/scoped_refptr.h" |
| 12 | #include "base/memory/weak_ptr.h" |
| 13 | #include "components/signin/public/identity_manager/primary_account_access_token_fetcher.h" |
James Cook | faf1c10 | 2020-02-27 15:23:20 | [diff] [blame] | 14 | #include "components/signin/public/identity_manager/scope_set.h" |
David Maunder | e7daf77 | 2019-08-30 14:13:29 | [diff] [blame] | 15 | #include "net/traffic_annotation/network_traffic_annotation.h" |
David Maunder | 78d4455 | 2020-05-16 15:24:17 | [diff] [blame] | 16 | #include "services/data_decoder/public/cpp/json_sanitizer.h" |
David Maunder | e7daf77 | 2019-08-30 14:13:29 | [diff] [blame] | 17 | |
| 18 | namespace network { |
| 19 | struct ResourceRequest; |
| 20 | class SimpleURLLoader; |
| 21 | } // namespace network |
| 22 | |
| 23 | namespace signin { |
| 24 | struct AccessTokenInfo; |
| 25 | class IdentityManager; |
| 26 | } // namespace signin |
| 27 | |
| 28 | class GoogleServiceAuthError; |
| 29 | class GURL; |
| 30 | class Profile; |
| 31 | |
| 32 | struct EndpointResponse { |
| 33 | std::string response; |
| 34 | // TODO(crbug.com/993393) Add more detailed error messaging |
| 35 | }; |
| 36 | |
| 37 | using EndpointFetcherCallback = |
| 38 | base::OnceCallback<void(std::unique_ptr<EndpointResponse>)>; |
| 39 | |
| 40 | // EndpointFetcher calls an endpoint and returns the response. |
| 41 | // EndpointFetcher is not thread safe and it is up to the caller |
| 42 | // to wait until the callback function passed to Fetch() completes |
| 43 | // before invoking Fetch() again. |
| 44 | // Destroying an EndpointFetcher will result in the in-flight request being |
| 45 | // cancelled. |
| 46 | // EndpointFetcher performs authentication via the signed in user to |
| 47 | // Chrome. |
| 48 | // If the request times out an empty response will be returned. There will also |
| 49 | // be an error code indicating timeout once more detailed error messaging is |
| 50 | // added TODO(crbug.com/993393). |
| 51 | class EndpointFetcher { |
| 52 | public: |
| 53 | // Preferred constructor - forms identity_manager and url_loader_factory. |
David Maunder | 6c98fc3 | 2020-05-15 22:36:34 | [diff] [blame] | 54 | // OAUTH authentication is used for this constructor. |
David Maunder | e7daf77 | 2019-08-30 14:13:29 | [diff] [blame] | 55 | EndpointFetcher(Profile* const profile, |
| 56 | const std::string& oauth_consumer_name, |
| 57 | const GURL& url, |
| 58 | const std::string& http_method, |
| 59 | const std::string& content_type, |
| 60 | const std::vector<std::string>& scopes, |
| 61 | int64_t timeout_ms, |
| 62 | const std::string& post_data, |
| 63 | const net::NetworkTrafficAnnotationTag& annotation_tag); |
David Maunder | 6c98fc3 | 2020-05-15 22:36:34 | [diff] [blame] | 64 | |
| 65 | // Constructor if Chrome API Key is used for authentication |
| 66 | EndpointFetcher(Profile* const profile, |
| 67 | const GURL& url, |
| 68 | const std::string& http_method, |
| 69 | const std::string& content_type, |
| 70 | int64_t timeout_ms, |
| 71 | const std::string& post_data, |
David Maunder | b5a9774 | 2020-11-22 17:37:02 | [diff] [blame] | 72 | const std::vector<std::string>& headers, |
David Maunder | 6c98fc3 | 2020-05-15 22:36:34 | [diff] [blame] | 73 | const net::NetworkTrafficAnnotationTag& annotation_tag); |
| 74 | |
Wei-Yin Chen (陳威尹) | 37d20b2a | 2020-07-08 02:24:31 | [diff] [blame] | 75 | // Constructor if no authentication is needed. |
| 76 | EndpointFetcher(Profile* const profile, |
| 77 | const GURL& url, |
| 78 | const net::NetworkTrafficAnnotationTag& annotation_tag); |
| 79 | |
David Maunder | e7daf77 | 2019-08-30 14:13:29 | [diff] [blame] | 80 | // Used for tests. Can be used if caller constructs their own |
| 81 | // url_loader_factory and identity_manager. |
| 82 | EndpointFetcher( |
| 83 | const std::string& oauth_consumer_name, |
| 84 | const GURL& url, |
| 85 | const std::string& http_method, |
| 86 | const std::string& content_type, |
| 87 | const std::vector<std::string>& scopes, |
| 88 | int64_t timeout_ms, |
| 89 | const std::string& post_data, |
| 90 | const net::NetworkTrafficAnnotationTag& annotation_tag, |
| 91 | const scoped_refptr<network::SharedURLLoaderFactory>& url_loader_factory, |
| 92 | signin::IdentityManager* const identity_manager); |
| 93 | |
Mei Liang | 21049f2 | 2021-07-17 00:13:33 | [diff] [blame] | 94 | // This Constructor can be used in a background thread. |
Mei Liang | fe36b23 | 2021-05-10 22:25:45 | [diff] [blame] | 95 | EndpointFetcher( |
| 96 | const GURL& url, |
| 97 | const std::string& http_method, |
| 98 | const std::string& content_type, |
| 99 | int64_t timeout_ms, |
| 100 | const std::string& post_data, |
Mei Liang | 1a848e1 | 2021-08-24 23:39:55 | [diff] [blame] | 101 | const std::vector<std::string>& headers, |
Mei Liang | fe36b23 | 2021-05-10 22:25:45 | [diff] [blame] | 102 | const net::NetworkTrafficAnnotationTag& annotation_tag, |
Mei Liang | 21049f2 | 2021-07-17 00:13:33 | [diff] [blame] | 103 | const scoped_refptr<network::SharedURLLoaderFactory>& url_loader_factory, |
| 104 | const bool is_oauth_fetch); |
Mei Liang | fe36b23 | 2021-05-10 22:25:45 | [diff] [blame] | 105 | |
David Maunder | e7daf77 | 2019-08-30 14:13:29 | [diff] [blame] | 106 | EndpointFetcher(const EndpointFetcher& endpoint_fetcher) = delete; |
| 107 | |
| 108 | EndpointFetcher& operator=(const EndpointFetcher& endpoint_fetcher) = delete; |
| 109 | |
Mei Liang | fe36b23 | 2021-05-10 22:25:45 | [diff] [blame] | 110 | virtual ~EndpointFetcher(); |
David Maunder | e7daf77 | 2019-08-30 14:13:29 | [diff] [blame] | 111 | |
| 112 | // TODO(crbug.com/999256) enable cancellation support |
| 113 | void Fetch(EndpointFetcherCallback callback); |
Mei Liang | fe36b23 | 2021-05-10 22:25:45 | [diff] [blame] | 114 | virtual void PerformRequest(EndpointFetcherCallback endpoint_fetcher_callback, |
| 115 | const char* key); |
| 116 | |
Mei Liang | 5a229db | 2021-08-27 18:54:52 | [diff] [blame^] | 117 | std::string GetUrlForTesting(); |
| 118 | |
Mei Liang | fe36b23 | 2021-05-10 22:25:45 | [diff] [blame] | 119 | protected: |
| 120 | // Used for Mock only. see MockEndpointFetcher class. |
| 121 | explicit EndpointFetcher( |
| 122 | const net::NetworkTrafficAnnotationTag& annotation_tag); |
David Maunder | e7daf77 | 2019-08-30 14:13:29 | [diff] [blame] | 123 | |
| 124 | private: |
| 125 | void OnAuthTokenFetched(EndpointFetcherCallback callback, |
| 126 | GoogleServiceAuthError error, |
| 127 | signin::AccessTokenInfo access_token_info); |
| 128 | void OnResponseFetched(EndpointFetcherCallback callback, |
| 129 | std::unique_ptr<std::string> response_body); |
David Maunder | 78d4455 | 2020-05-16 15:24:17 | [diff] [blame] | 130 | void OnSanitizationResult(EndpointFetcherCallback endpoint_fetcher_callback, |
| 131 | data_decoder::JsonSanitizer::Result result); |
David Maunder | e7daf77 | 2019-08-30 14:13:29 | [diff] [blame] | 132 | |
Wei-Yin Chen (陳威尹) | 37d20b2a | 2020-07-08 02:24:31 | [diff] [blame] | 133 | enum AuthType { CHROME_API_KEY, OAUTH, NO_AUTH }; |
David Maunder | 6c98fc3 | 2020-05-15 22:36:34 | [diff] [blame] | 134 | AuthType auth_type_; |
| 135 | |
David Maunder | e7daf77 | 2019-08-30 14:13:29 | [diff] [blame] | 136 | // Members set in constructor to be passed to network::ResourceRequest or |
| 137 | // network::SimpleURLLoader. |
| 138 | const std::string oauth_consumer_name_; |
| 139 | const GURL url_; |
| 140 | const std::string http_method_; |
| 141 | const std::string content_type_; |
| 142 | int64_t timeout_ms_; |
| 143 | const std::string post_data_; |
David Maunder | b5a9774 | 2020-11-22 17:37:02 | [diff] [blame] | 144 | const std::vector<std::string> headers_; |
David Maunder | e7daf77 | 2019-08-30 14:13:29 | [diff] [blame] | 145 | const net::NetworkTrafficAnnotationTag annotation_tag_; |
James Cook | faf1c10 | 2020-02-27 15:23:20 | [diff] [blame] | 146 | signin::ScopeSet oauth_scopes_; |
David Maunder | e7daf77 | 2019-08-30 14:13:29 | [diff] [blame] | 147 | |
| 148 | // Members set in constructor |
| 149 | const scoped_refptr<network::SharedURLLoaderFactory> url_loader_factory_; |
| 150 | signin::IdentityManager* const identity_manager_; |
Wei-Yin Chen (陳威尹) | 37d20b2a | 2020-07-08 02:24:31 | [diff] [blame] | 151 | bool sanitize_response_; |
David Maunder | e7daf77 | 2019-08-30 14:13:29 | [diff] [blame] | 152 | |
| 153 | // Members set in Fetch |
| 154 | std::unique_ptr<const signin::PrimaryAccountAccessTokenFetcher> |
| 155 | access_token_fetcher_; |
| 156 | std::unique_ptr<network::SimpleURLLoader> simple_url_loader_; |
| 157 | |
| 158 | base::WeakPtrFactory<EndpointFetcher> weak_ptr_factory_{this}; |
| 159 | }; |
| 160 | |
David Maunder | 012fe329 | 2020-09-15 15:38:49 | [diff] [blame] | 161 | #endif // CHROME_BROWSER_ENDPOINT_FETCHER_ENDPOINT_FETCHER_H_ |