File: plus_address_http_client_impl.h

package info (click to toggle)
chromium 138.0.7204.157-1
  • links: PTS, VCS
  • area: main
  • in suites: sid, trixie
  • size: 6,071,864 kB
  • sloc: cpp: 34,936,859; ansic: 7,176,967; javascript: 4,110,704; python: 1,419,953; asm: 946,768; xml: 739,967; pascal: 187,324; sh: 89,623; perl: 88,663; objc: 79,944; sql: 50,304; cs: 41,786; fortran: 24,137; makefile: 21,806; php: 13,980; tcl: 13,166; yacc: 8,925; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (156 lines) | stat: -rw-r--r-- 6,353 bytes parent folder | download | duplicates (3)
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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
// Copyright 2023 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_PLUS_ADDRESSES_PLUS_ADDRESS_HTTP_CLIENT_IMPL_H_
#define COMPONENTS_PLUS_ADDRESSES_PLUS_ADDRESS_HTTP_CLIENT_IMPL_H_

#include <list>
#include <optional>
#include <string>
#include <string_view>

#include "base/containers/queue.h"
#include "base/functional/callback_forward.h"
#include "base/memory/scoped_refptr.h"
#include "base/sequence_checker.h"
#include "components/plus_addresses/plus_address_http_client.h"
#include "components/plus_addresses/plus_address_types.h"
#include "components/signin/public/identity_manager/scope_set.h"
#include "url/gurl.h"

class GoogleServiceAuthError;

namespace network {
class SharedURLLoaderFactory;
class SimpleURLLoader;
}  // namespace network

namespace signin {
struct AccessTokenInfo;
class IdentityManager;
class PrimaryAccountAccessTokenFetcher;
}  // namespace signin

namespace plus_addresses {

// This endpoint is used for most plus-address operations.
inline constexpr std::string_view kServerPlusProfileEndpoint = "v1/profiles";
inline constexpr std::string_view kServerReservePlusAddressEndpoint =
    "v1/profiles/reserve";
inline constexpr std::string_view kServerCreatePlusAddressEndpoint =
    "v1/profiles/create";
inline constexpr std::string_view kServerPreallocatePlusAddressEndpoint =
    "v1/emailAddresses/reserve";

class PlusAddressHttpClientImpl : public PlusAddressHttpClient {
 public:
  PlusAddressHttpClientImpl(
      signin::IdentityManager* identity_manager,
      scoped_refptr<network::SharedURLLoaderFactory> url_loader_factory);
  PlusAddressHttpClientImpl(const PlusAddressHttpClientImpl&) = delete;
  PlusAddressHttpClientImpl(PlusAddressHttpClientImpl&&) = delete;
  PlusAddressHttpClientImpl& operator=(const PlusAddressHttpClientImpl&) =
      delete;
  PlusAddressHttpClientImpl& operator=(PlusAddressHttpClientImpl&&) = delete;
  ~PlusAddressHttpClientImpl() override;

  // PlusAddressHttpClient:
  void ReservePlusAddress(const url::Origin& origin,
                          bool refresh,
                          PlusAddressRequestCallback on_completed) override;
  void ConfirmPlusAddress(const url::Origin& origin,
                          const PlusAddress& plus_address,
                          PlusAddressRequestCallback on_completed) override;
  void PreallocatePlusAddresses(
      PreallocatePlusAddressesCallback callback) override;
  void Reset() override;

 private:
  friend class PlusAddressHttpClientImplTestApi;

  using UrlLoaderList = std::list<std::unique_ptr<network::SimpleURLLoader>>;
  using TokenReadyCallback =
      base::OnceCallback<void(std::optional<std::string>)>;

  // Initiates a request for a new OAuth token. If the request succeeds, this
  // runs `on_fetched` with the retrieved token. Must be run on the UI thread.
  void GetAuthToken(TokenReadyCallback on_fetched);

  // The actual implementations of the interface methods. The overridden
  // interface methods (`ReservePlusAddress`, ...) call `GetAuthToken` with the
  // methods below passed in as the `TokenReadyCallback`.
  void ReservePlusAddressInternal(const url::Origin& origin,
                                  bool refresh,
                                  PlusAddressRequestCallback on_completed,
                                  std::optional<std::string> auth_token);
  void ConfirmPlusAddressInternal(const url::Origin& origin,
                                  const PlusAddress& plus_address,
                                  PlusAddressRequestCallback on_completed,
                                  std::optional<std::string> auth_token);
  void PreallocatePlusAddressesInternal(
      PreallocatePlusAddressesCallback callback,
      std::optional<std::string> auth_token);

  // Removes the UrlLoader at `it` from the internal loaders, and, after
  // verifying that the response did not time out or return an error, emits a
  // metric for the response size.
  // If either the client timed out or there was an error on the server, it
  // returns with error.
  base::expected<void, PlusAddressRequestError> ProcessNetworkResponse(
      UrlLoaderList::iterator it,
      PlusAddressNetworkRequestType type,
      base::TimeTicks request_start,
      base::optional_ref<const std::string> response);

  // This is shared by the `ReservePlusAddress` and `ConfirmPlusAddress` methods
  // since they both use `loaders_for_creation_` and have the same return type.
  void OnReserveOrConfirmPlusAddressComplete(
      UrlLoaderList::iterator it,
      PlusAddressNetworkRequestType type,
      base::TimeTicks request_start,
      PlusAddressRequestCallback on_completed,
      std::unique_ptr<std::string> response);

  void OnPreallocationComplete(UrlLoaderList::iterator it,
                               base::TimeTicks request_start,
                               PreallocatePlusAddressesCallback on_completed,
                               std::unique_ptr<std::string> response);

  // Runs callback and any pending_callbacks_ blocked on the token.
  void OnTokenFetched(TokenReadyCallback callback,
                      GoogleServiceAuthError error,
                      signin::AccessTokenInfo access_token_info);

  // Creates a resource request for a given `endpoint`, `method` and
  // `auth_token`.
  std::unique_ptr<network::ResourceRequest> CreateRequest(
      std::string_view endpoint,
      std::string_view method,
      std::string_view auth_token) const;

  // The IdentityManager instance for the current profile.
  const raw_ref<signin::IdentityManager> identity_manager_;

  // Used to make HTTP requests.
  const scoped_refptr<network::SharedURLLoaderFactory> url_loader_factory_;

  const std::optional<GURL> server_url_;

  const signin::ScopeSet scopes_;

  std::unique_ptr<signin::PrimaryAccountAccessTokenFetcher>
      access_token_fetcher_ GUARDED_BY_CONTEXT(sequence_checker_);

  // Stores callbacks that raced to get an auth token to run them once ready.
  base::queue<TokenReadyCallback> pending_callbacks_;

  // Loaders used for Create, Reserve, and Preallocate calls.
  UrlLoaderList loaders_;

  SEQUENCE_CHECKER(sequence_checker_);
};

}  // namespace plus_addresses

#endif  // COMPONENTS_PLUS_ADDRESSES_PLUS_ADDRESS_HTTP_CLIENT_IMPL_H_