blob: 3843c8e4cf0d7ccf8b682d9afc59ba84563790f6 [file] [log] [blame]
David Maundere7daf772019-08-30 14:13:291// 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 Maunderc4455002022-06-03 20:27:475#ifndef COMPONENTS_ENDPOINT_FETCHER_ENDPOINT_FETCHER_H_
6#define COMPONENTS_ENDPOINT_FETCHER_ENDPOINT_FETCHER_H_
David Maundere7daf772019-08-30 14:13:297
8#include <string>
9#include <vector>
10
Keishi Hattori0e45c022021-11-27 09:25:5211#include "base/memory/raw_ptr.h"
David Maundere7daf772019-08-30 14:13:2912#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 Cookfaf1c102020-02-27 15:23:2015#include "components/signin/public/identity_manager/scope_set.h"
David Maundere7daf772019-08-30 14:13:2916#include "net/traffic_annotation/network_traffic_annotation.h"
David Maunder78d44552020-05-16 15:24:1717#include "services/data_decoder/public/cpp/json_sanitizer.h"
David Maundere7daf772019-08-30 14:13:2918
19namespace network {
20struct ResourceRequest;
21class SimpleURLLoader;
22} // namespace network
23
24namespace signin {
25struct AccessTokenInfo;
26class IdentityManager;
27} // namespace signin
28
29class GoogleServiceAuthError;
30class GURL;
David Maundere7daf772019-08-30 14:13:2931
32struct EndpointResponse {
33 std::string response;
34 // TODO(crbug.com/993393) Add more detailed error messaging
35};
36
37using 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).
51class EndpointFetcher {
52 public:
53 // Preferred constructor - forms identity_manager and url_loader_factory.
David Maunder6c98fc32020-05-15 22:36:3454 // OAUTH authentication is used for this constructor.
David Maunderc4455002022-06-03 20:27:4755 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 Maunder6c98fc32020-05-15 22:36:3466
67 // Constructor if Chrome API Key is used for authentication
David Maunderc4455002022-06-03 20:27:4768 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 Maunder6c98fc32020-05-15 22:36:3478
Wei-Yin Chen (陳威尹)37d20b2a2020-07-08 02:24:3179 // Constructor if no authentication is needed.
David Maunderc4455002022-06-03 20:27:4780 EndpointFetcher(
81 const scoped_refptr<network::SharedURLLoaderFactory>& url_loader_factory,
82 const GURL& url,
83 const net::NetworkTrafficAnnotationTag& annotation_tag);
Wei-Yin Chen (陳威尹)37d20b2a2020-07-08 02:24:3184
David Maundere7daf772019-08-30 14:13:2985 // 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 Liang21049f22021-07-17 00:13:3399 // This Constructor can be used in a background thread.
Mei Liangfe36b232021-05-10 22:25:45100 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 Liang1a848e12021-08-24 23:39:55106 const std::vector<std::string>& headers,
Mei Liang4380a3f2021-09-15 22:20:14107 const std::vector<std::string>& cors_exempt_headers,
Mei Liangfe36b232021-05-10 22:25:45108 const net::NetworkTrafficAnnotationTag& annotation_tag,
Mei Liang21049f22021-07-17 00:13:33109 const scoped_refptr<network::SharedURLLoaderFactory>& url_loader_factory,
110 const bool is_oauth_fetch);
Mei Liangfe36b232021-05-10 22:25:45111
David Maundere7daf772019-08-30 14:13:29112 EndpointFetcher(const EndpointFetcher& endpoint_fetcher) = delete;
113
114 EndpointFetcher& operator=(const EndpointFetcher& endpoint_fetcher) = delete;
115
Mei Liangfe36b232021-05-10 22:25:45116 virtual ~EndpointFetcher();
David Maundere7daf772019-08-30 14:13:29117
118 // TODO(crbug.com/999256) enable cancellation support
119 void Fetch(EndpointFetcherCallback callback);
Mei Liangfe36b232021-05-10 22:25:45120 virtual void PerformRequest(EndpointFetcherCallback endpoint_fetcher_callback,
121 const char* key);
122
Mei Liang5a229db2021-08-27 18:54:52123 std::string GetUrlForTesting();
124
Mei Liangfe36b232021-05-10 22:25:45125 protected:
126 // Used for Mock only. see MockEndpointFetcher class.
127 explicit EndpointFetcher(
128 const net::NetworkTrafficAnnotationTag& annotation_tag);
David Maundere7daf772019-08-30 14:13:29129
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 Maunder78d44552020-05-16 15:24:17136 void OnSanitizationResult(EndpointFetcherCallback endpoint_fetcher_callback,
137 data_decoder::JsonSanitizer::Result result);
David Maundere7daf772019-08-30 14:13:29138
Wei-Yin Chen (陳威尹)37d20b2a2020-07-08 02:24:31139 enum AuthType { CHROME_API_KEY, OAUTH, NO_AUTH };
David Maunder6c98fc32020-05-15 22:36:34140 AuthType auth_type_;
141
David Maundere7daf772019-08-30 14:13:29142 // 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 Maunderb5a97742020-11-22 17:37:02150 const std::vector<std::string> headers_;
Mei Liang4380a3f2021-09-15 22:20:14151 const std::vector<std::string> cors_exempt_headers_;
David Maundere7daf772019-08-30 14:13:29152 const net::NetworkTrafficAnnotationTag annotation_tag_;
James Cookfaf1c102020-02-27 15:23:20153 signin::ScopeSet oauth_scopes_;
David Maundere7daf772019-08-30 14:13:29154
155 // Members set in constructor
156 const scoped_refptr<network::SharedURLLoaderFactory> url_loader_factory_;
Keishi Hattori0e45c022021-11-27 09:25:52157 const raw_ptr<signin::IdentityManager> identity_manager_;
Wei-Yin Chen (陳威尹)37d20b2a2020-07-08 02:24:31158 bool sanitize_response_;
David Maunderc4455002022-06-03 20:27:47159 bool is_stable_channel_;
David Maundere7daf772019-08-30 14:13:29160
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 Maunderc4455002022-06-03 20:27:47169#endif // COMPONENTS_ENDPOINT_FETCHER_ENDPOINT_FETCHER_H_