blob: bf15a3db49bf9ffadf05e5e445147b70a07d8f62 [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 Maunder012fe3292020-09-15 15:38:495#ifndef CHROME_BROWSER_ENDPOINT_FETCHER_ENDPOINT_FETCHER_H_
6#define CHROME_BROWSER_ENDPOINT_FETCHER_ENDPOINT_FETCHER_H_
David Maundere7daf772019-08-30 14:13:297
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 Cookfaf1c102020-02-27 15:23:2014#include "components/signin/public/identity_manager/scope_set.h"
David Maundere7daf772019-08-30 14:13:2915#include "net/traffic_annotation/network_traffic_annotation.h"
David Maunder78d44552020-05-16 15:24:1716#include "services/data_decoder/public/cpp/json_sanitizer.h"
David Maundere7daf772019-08-30 14:13:2917
18namespace network {
19struct ResourceRequest;
20class SimpleURLLoader;
21} // namespace network
22
23namespace signin {
24struct AccessTokenInfo;
25class IdentityManager;
26} // namespace signin
27
28class GoogleServiceAuthError;
29class GURL;
30class Profile;
31
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 Maundere7daf772019-08-30 14:13:2955 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 Maunder6c98fc32020-05-15 22:36:3464
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 Maunderb5a97742020-11-22 17:37:0272 const std::vector<std::string>& headers,
David Maunder6c98fc32020-05-15 22:36:3473 const net::NetworkTrafficAnnotationTag& annotation_tag);
74
Wei-Yin Chen (陳威尹)37d20b2a2020-07-08 02:24:3175 // Constructor if no authentication is needed.
76 EndpointFetcher(Profile* const profile,
77 const GURL& url,
78 const net::NetworkTrafficAnnotationTag& annotation_tag);
79
David Maundere7daf772019-08-30 14:13:2980 // 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 Liang21049f22021-07-17 00:13:3394 // This Constructor can be used in a background thread.
Mei Liangfe36b232021-05-10 22:25:4595 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 Liang1a848e12021-08-24 23:39:55101 const std::vector<std::string>& headers,
Mei Liangfe36b232021-05-10 22:25:45102 const net::NetworkTrafficAnnotationTag& annotation_tag,
Mei Liang21049f22021-07-17 00:13:33103 const scoped_refptr<network::SharedURLLoaderFactory>& url_loader_factory,
104 const bool is_oauth_fetch);
Mei Liangfe36b232021-05-10 22:25:45105
David Maundere7daf772019-08-30 14:13:29106 EndpointFetcher(const EndpointFetcher& endpoint_fetcher) = delete;
107
108 EndpointFetcher& operator=(const EndpointFetcher& endpoint_fetcher) = delete;
109
Mei Liangfe36b232021-05-10 22:25:45110 virtual ~EndpointFetcher();
David Maundere7daf772019-08-30 14:13:29111
112 // TODO(crbug.com/999256) enable cancellation support
113 void Fetch(EndpointFetcherCallback callback);
Mei Liangfe36b232021-05-10 22:25:45114 virtual void PerformRequest(EndpointFetcherCallback endpoint_fetcher_callback,
115 const char* key);
116
Mei Liang5a229db2021-08-27 18:54:52117 std::string GetUrlForTesting();
118
Mei Liangfe36b232021-05-10 22:25:45119 protected:
120 // Used for Mock only. see MockEndpointFetcher class.
121 explicit EndpointFetcher(
122 const net::NetworkTrafficAnnotationTag& annotation_tag);
David Maundere7daf772019-08-30 14:13:29123
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 Maunder78d44552020-05-16 15:24:17130 void OnSanitizationResult(EndpointFetcherCallback endpoint_fetcher_callback,
131 data_decoder::JsonSanitizer::Result result);
David Maundere7daf772019-08-30 14:13:29132
Wei-Yin Chen (陳威尹)37d20b2a2020-07-08 02:24:31133 enum AuthType { CHROME_API_KEY, OAUTH, NO_AUTH };
David Maunder6c98fc32020-05-15 22:36:34134 AuthType auth_type_;
135
David Maundere7daf772019-08-30 14:13:29136 // 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 Maunderb5a97742020-11-22 17:37:02144 const std::vector<std::string> headers_;
David Maundere7daf772019-08-30 14:13:29145 const net::NetworkTrafficAnnotationTag annotation_tag_;
James Cookfaf1c102020-02-27 15:23:20146 signin::ScopeSet oauth_scopes_;
David Maundere7daf772019-08-30 14:13:29147
148 // Members set in constructor
149 const scoped_refptr<network::SharedURLLoaderFactory> url_loader_factory_;
150 signin::IdentityManager* const identity_manager_;
Wei-Yin Chen (陳威尹)37d20b2a2020-07-08 02:24:31151 bool sanitize_response_;
David Maundere7daf772019-08-30 14:13:29152
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 Maunder012fe3292020-09-15 15:38:49161#endif // CHROME_BROWSER_ENDPOINT_FETCHER_ENDPOINT_FETCHER_H_