1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135
|
// Copyright 2020 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifndef COMPONENTS_TRUSTED_VAULT_TRUSTED_VAULT_REQUEST_H_
#define COMPONENTS_TRUSTED_VAULT_TRUSTED_VAULT_REQUEST_H_
#include <memory>
#include <optional>
#include <string>
#include "base/functional/callback.h"
#include "base/functional/callback_forward.h"
#include "base/memory/weak_ptr.h"
#include "base/time/time.h"
#include "components/trusted_vault/trusted_vault_access_token_fetcher.h"
#include "components/trusted_vault/trusted_vault_connection.h"
#include "components/trusted_vault/trusted_vault_histograms.h"
#include "components/trusted_vault/trusted_vault_server_constants.h"
#include "google_apis/gaia/core_account_id.h"
#include "net/base/backoff_entry.h"
#include "url/gurl.h"
namespace network {
class SharedURLLoaderFactory;
class SimpleURLLoader;
} // namespace network
namespace trusted_vault {
// Allows calling VaultService API using proto-over-http.
class TrustedVaultRequest : public TrustedVaultConnection::Request {
public:
enum class HttpStatus {
// Reported when server returns http status code 200 or 204.
kSuccess,
// Reported when server returns http status code 400 (bad request).
kBadRequest,
// Reported when server returns http status code 404 (not found).
kNotFound,
// Reported when server returns http status code 409 (conflict).
kConflict,
// Reported when access token fetch attempt was failed due to transient auth
// error.
kTransientAccessTokenFetchError,
// Reported when access token fetch attempt failed due to permanent auth
// error.
kPersistentAccessTokenFetchError,
// Reported when access token fetch attempt was cancelled due to primary
// account change.
kPrimaryAccountChangeAccessTokenFetchError,
// Reported when network error occurs.
kNetworkError,
// Reported when other http errors occur.
kOtherError
};
enum class HttpMethod { kGet, kPost, kPatch };
using CompletionCallback =
base::OnceCallback<void(HttpStatus status,
const std::string& response_body)>;
using RecordFetchStatusCallback =
base::RepeatingCallback<void(int http_status, int net_error)>;
// |callback| will be run upon completion and it's allowed to delete this
// object upon |callback| call. For |GET| requests, |serialized_request_proto|
// must be null. For |POST| and |PATCH| requests, it can be either way
// (optional payload). |url_loader_factory| must not be null.
// |max_retry_duration| specifies for how long the request can be retried in
// case of transient errors. There will be no retries when it is set to zero.
// |record_fetch_status_callback| may be used to record fetch outcomes in a
// histogram metric.
TrustedVaultRequest(
const SecurityDomainId& security_domain_id,
const CoreAccountId& account_id,
HttpMethod http_method,
const GURL& request_url,
const std::optional<std::string>& serialized_request_proto,
base::TimeDelta max_retry_duration,
scoped_refptr<network::SharedURLLoaderFactory> url_loader_factory,
std::unique_ptr<TrustedVaultAccessTokenFetcher> access_token_fetcher,
RecordFetchStatusCallback record_fetch_status_callback);
TrustedVaultRequest(const TrustedVaultRequest& other) = delete;
TrustedVaultRequest& operator=(const TrustedVaultRequest& other) = delete;
~TrustedVaultRequest() override;
// Attempts to fetch access token and sends the request if fetch was
// successful or populates error into ResultCallback otherwise. Should be
// called at most once.
void FetchAccessTokenAndSendRequest(CompletionCallback callback);
private:
void OnAccessTokenFetched(
TrustedVaultAccessTokenFetcher::AccessTokenInfoOrError
access_token_info_or_error);
void OnURLLoadComplete(std::optional<std::string> response_body);
std::unique_ptr<network::SimpleURLLoader> CreateURLLoader(
const std::string& access_token) const;
bool CanRetry() const;
void ScheduleRetry();
void Retry();
// Running |completion_callback_| may cause destroying of this object, so all
// callers of this method must not run any code afterwards.
void RunCompletionCallbackAndMaybeDestroySelf(
HttpStatus status,
const std::string& response_body);
const SecurityDomainId security_domain_id_;
const CoreAccountId account_id_;
const HttpMethod http_method_;
const GURL request_url_;
const std::optional<std::string> serialized_request_proto_;
const scoped_refptr<network::SharedURLLoaderFactory> url_loader_factory_;
const std::unique_ptr<TrustedVaultAccessTokenFetcher> access_token_fetcher_;
const RecordFetchStatusCallback record_fetch_status_callback_;
const base::TimeTicks max_retry_time_;
net::BackoffEntry backoff_entry_;
CompletionCallback completion_callback_;
// Initialized lazily upon successful access token fetch.
std::unique_ptr<network::SimpleURLLoader> url_loader_;
base::WeakPtrFactory<TrustedVaultRequest> weak_ptr_factory_{this};
};
} // namespace trusted_vault
#endif // COMPONENTS_TRUSTED_VAULT_TRUSTED_VAULT_REQUEST_H_
|