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 | c445500 | 2022-06-03 20:27:47 | [diff] [blame] | 5 | #ifndef COMPONENTS_ENDPOINT_FETCHER_ENDPOINT_FETCHER_H_ |
| 6 | #define COMPONENTS_ENDPOINT_FETCHER_ENDPOINT_FETCHER_H_ |
David Maunder | e7daf77 | 2019-08-30 14:13:29 | [diff] [blame] | 7 | |
| 8 | #include <string> |
| 9 | #include <vector> |
| 10 | |
Keishi Hattori | 0e45c02 | 2021-11-27 09:25:52 | [diff] [blame] | 11 | #include "base/memory/raw_ptr.h" |
David Maunder | e7daf77 | 2019-08-30 14:13:29 | [diff] [blame] | 12 | #include "base/memory/scoped_refptr.h" |
| 13 | #include "base/memory/weak_ptr.h" |
| 14 | #include "components/signin/public/identity_manager/primary_account_access_token_fetcher.h" |
James Cook | faf1c10 | 2020-02-27 15:23:20 | [diff] [blame] | 15 | #include "components/signin/public/identity_manager/scope_set.h" |
David Maunder | e7daf77 | 2019-08-30 14:13:29 | [diff] [blame] | 16 | #include "net/traffic_annotation/network_traffic_annotation.h" |
David Maunder | 78d4455 | 2020-05-16 15:24:17 | [diff] [blame] | 17 | #include "services/data_decoder/public/cpp/json_sanitizer.h" |
David Maunder | e7daf77 | 2019-08-30 14:13:29 | [diff] [blame] | 18 | |
| 19 | namespace network { |
| 20 | struct ResourceRequest; |
| 21 | class SimpleURLLoader; |
| 22 | } // namespace network |
| 23 | |
| 24 | namespace signin { |
| 25 | struct AccessTokenInfo; |
| 26 | class IdentityManager; |
| 27 | } // namespace signin |
| 28 | |
| 29 | class GoogleServiceAuthError; |
| 30 | class GURL; |
David Maunder | e7daf77 | 2019-08-30 14:13:29 | [diff] [blame] | 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 | c445500 | 2022-06-03 20:27:47 | [diff] [blame] | 55 | EndpointFetcher( |
| 56 | const scoped_refptr<network::SharedURLLoaderFactory>& url_loader_factory, |
| 57 | const std::string& oauth_consumer_name, |
| 58 | const GURL& url, |
| 59 | const std::string& http_method, |
| 60 | const std::string& content_type, |
| 61 | const std::vector<std::string>& scopes, |
| 62 | int64_t timeout_ms, |
| 63 | const std::string& post_data, |
| 64 | const net::NetworkTrafficAnnotationTag& annotation_tag, |
| 65 | signin::IdentityManager* const identity_manager); |
David Maunder | 6c98fc3 | 2020-05-15 22:36:34 | [diff] [blame] | 66 | |
| 67 | // Constructor if Chrome API Key is used for authentication |
David Maunder | c445500 | 2022-06-03 20:27:47 | [diff] [blame] | 68 | EndpointFetcher( |
| 69 | const scoped_refptr<network::SharedURLLoaderFactory>& url_loader_factory, |
| 70 | const GURL& url, |
| 71 | const std::string& http_method, |
| 72 | const std::string& content_type, |
| 73 | int64_t timeout_ms, |
| 74 | const std::string& post_data, |
| 75 | const std::vector<std::string>& headers, |
| 76 | const net::NetworkTrafficAnnotationTag& annotation_tag, |
| 77 | bool is_stable_channel); |
David Maunder | 6c98fc3 | 2020-05-15 22:36:34 | [diff] [blame] | 78 | |
Wei-Yin Chen (陳威尹) | 37d20b2a | 2020-07-08 02:24:31 | [diff] [blame] | 79 | // Constructor if no authentication is needed. |
David Maunder | c445500 | 2022-06-03 20:27:47 | [diff] [blame] | 80 | EndpointFetcher( |
| 81 | const scoped_refptr<network::SharedURLLoaderFactory>& url_loader_factory, |
| 82 | const GURL& url, |
| 83 | const net::NetworkTrafficAnnotationTag& annotation_tag); |
Wei-Yin Chen (陳威尹) | 37d20b2a | 2020-07-08 02:24:31 | [diff] [blame] | 84 | |
David Maunder | e7daf77 | 2019-08-30 14:13:29 | [diff] [blame] | 85 | // Used for tests. Can be used if caller constructs their own |
| 86 | // url_loader_factory and identity_manager. |
| 87 | EndpointFetcher( |
| 88 | const std::string& oauth_consumer_name, |
| 89 | const GURL& url, |
| 90 | const std::string& http_method, |
| 91 | const std::string& content_type, |
| 92 | const std::vector<std::string>& scopes, |
| 93 | int64_t timeout_ms, |
| 94 | const std::string& post_data, |
| 95 | const net::NetworkTrafficAnnotationTag& annotation_tag, |
| 96 | const scoped_refptr<network::SharedURLLoaderFactory>& url_loader_factory, |
| 97 | signin::IdentityManager* const identity_manager); |
| 98 | |
Mei Liang | 21049f2 | 2021-07-17 00:13:33 | [diff] [blame] | 99 | // This Constructor can be used in a background thread. |
Mei Liang | fe36b23 | 2021-05-10 22:25:45 | [diff] [blame] | 100 | EndpointFetcher( |
| 101 | const GURL& url, |
| 102 | const std::string& http_method, |
| 103 | const std::string& content_type, |
| 104 | int64_t timeout_ms, |
| 105 | const std::string& post_data, |
Mei Liang | 1a848e1 | 2021-08-24 23:39:55 | [diff] [blame] | 106 | const std::vector<std::string>& headers, |
Mei Liang | 4380a3f | 2021-09-15 22:20:14 | [diff] [blame] | 107 | const std::vector<std::string>& cors_exempt_headers, |
Mei Liang | fe36b23 | 2021-05-10 22:25:45 | [diff] [blame] | 108 | const net::NetworkTrafficAnnotationTag& annotation_tag, |
Mei Liang | 21049f2 | 2021-07-17 00:13:33 | [diff] [blame] | 109 | const scoped_refptr<network::SharedURLLoaderFactory>& url_loader_factory, |
| 110 | const bool is_oauth_fetch); |
Mei Liang | fe36b23 | 2021-05-10 22:25:45 | [diff] [blame] | 111 | |
David Maunder | e7daf77 | 2019-08-30 14:13:29 | [diff] [blame] | 112 | EndpointFetcher(const EndpointFetcher& endpoint_fetcher) = delete; |
| 113 | |
| 114 | EndpointFetcher& operator=(const EndpointFetcher& endpoint_fetcher) = delete; |
| 115 | |
Mei Liang | fe36b23 | 2021-05-10 22:25:45 | [diff] [blame] | 116 | virtual ~EndpointFetcher(); |
David Maunder | e7daf77 | 2019-08-30 14:13:29 | [diff] [blame] | 117 | |
| 118 | // TODO(crbug.com/999256) enable cancellation support |
| 119 | void Fetch(EndpointFetcherCallback callback); |
Mei Liang | fe36b23 | 2021-05-10 22:25:45 | [diff] [blame] | 120 | virtual void PerformRequest(EndpointFetcherCallback endpoint_fetcher_callback, |
| 121 | const char* key); |
| 122 | |
Mei Liang | 5a229db | 2021-08-27 18:54:52 | [diff] [blame] | 123 | std::string GetUrlForTesting(); |
| 124 | |
Mei Liang | fe36b23 | 2021-05-10 22:25:45 | [diff] [blame] | 125 | protected: |
| 126 | // Used for Mock only. see MockEndpointFetcher class. |
| 127 | explicit EndpointFetcher( |
| 128 | const net::NetworkTrafficAnnotationTag& annotation_tag); |
David Maunder | e7daf77 | 2019-08-30 14:13:29 | [diff] [blame] | 129 | |
| 130 | private: |
| 131 | void OnAuthTokenFetched(EndpointFetcherCallback callback, |
| 132 | GoogleServiceAuthError error, |
| 133 | signin::AccessTokenInfo access_token_info); |
| 134 | void OnResponseFetched(EndpointFetcherCallback callback, |
| 135 | std::unique_ptr<std::string> response_body); |
David Maunder | 78d4455 | 2020-05-16 15:24:17 | [diff] [blame] | 136 | void OnSanitizationResult(EndpointFetcherCallback endpoint_fetcher_callback, |
| 137 | data_decoder::JsonSanitizer::Result result); |
David Maunder | e7daf77 | 2019-08-30 14:13:29 | [diff] [blame] | 138 | |
Wei-Yin Chen (陳威尹) | 37d20b2a | 2020-07-08 02:24:31 | [diff] [blame] | 139 | enum AuthType { CHROME_API_KEY, OAUTH, NO_AUTH }; |
David Maunder | 6c98fc3 | 2020-05-15 22:36:34 | [diff] [blame] | 140 | AuthType auth_type_; |
| 141 | |
David Maunder | e7daf77 | 2019-08-30 14:13:29 | [diff] [blame] | 142 | // Members set in constructor to be passed to network::ResourceRequest or |
| 143 | // network::SimpleURLLoader. |
| 144 | const std::string oauth_consumer_name_; |
| 145 | const GURL url_; |
| 146 | const std::string http_method_; |
| 147 | const std::string content_type_; |
| 148 | int64_t timeout_ms_; |
| 149 | const std::string post_data_; |
David Maunder | b5a9774 | 2020-11-22 17:37:02 | [diff] [blame] | 150 | const std::vector<std::string> headers_; |
Mei Liang | 4380a3f | 2021-09-15 22:20:14 | [diff] [blame] | 151 | const std::vector<std::string> cors_exempt_headers_; |
David Maunder | e7daf77 | 2019-08-30 14:13:29 | [diff] [blame] | 152 | const net::NetworkTrafficAnnotationTag annotation_tag_; |
James Cook | faf1c10 | 2020-02-27 15:23:20 | [diff] [blame] | 153 | signin::ScopeSet oauth_scopes_; |
David Maunder | e7daf77 | 2019-08-30 14:13:29 | [diff] [blame] | 154 | |
| 155 | // Members set in constructor |
| 156 | const scoped_refptr<network::SharedURLLoaderFactory> url_loader_factory_; |
Keishi Hattori | 0e45c02 | 2021-11-27 09:25:52 | [diff] [blame] | 157 | const raw_ptr<signin::IdentityManager> identity_manager_; |
Wei-Yin Chen (陳威尹) | 37d20b2a | 2020-07-08 02:24:31 | [diff] [blame] | 158 | bool sanitize_response_; |
David Maunder | c445500 | 2022-06-03 20:27:47 | [diff] [blame] | 159 | bool is_stable_channel_; |
David Maunder | e7daf77 | 2019-08-30 14:13:29 | [diff] [blame] | 160 | |
| 161 | // Members set in Fetch |
| 162 | std::unique_ptr<const signin::PrimaryAccountAccessTokenFetcher> |
| 163 | access_token_fetcher_; |
| 164 | std::unique_ptr<network::SimpleURLLoader> simple_url_loader_; |
| 165 | |
| 166 | base::WeakPtrFactory<EndpointFetcher> weak_ptr_factory_{this}; |
| 167 | }; |
| 168 | |
David Maunder | c445500 | 2022-06-03 20:27:47 | [diff] [blame] | 169 | #endif // COMPONENTS_ENDPOINT_FETCHER_ENDPOINT_FETCHER_H_ |