File: feed_network.h

package info (click to toggle)
chromium 138.0.7204.183-1~deb12u1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm-proposed-updates
  • size: 6,080,960 kB
  • sloc: cpp: 34,937,079; ansic: 7,176,967; javascript: 4,110,704; python: 1,419,954; asm: 946,768; xml: 739,971; pascal: 187,324; sh: 89,623; perl: 88,663; objc: 79,944; sql: 50,304; cs: 41,786; fortran: 24,137; makefile: 21,811; 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 (275 lines) | stat: -rw-r--r-- 10,202 bytes parent folder | download | duplicates (6)
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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
// 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_FEED_CORE_V2_FEED_NETWORK_H_
#define COMPONENTS_FEED_CORE_V2_FEED_NETWORK_H_

#include <memory>
#include <string_view>

#include "base/functional/callback.h"
#include "components/feed/core/proto/v2/wire/consistency_token.pb.h"
#include "components/feed/core/proto/v2/wire/feed_query.pb.h"
#include "components/feed/core/proto/v2/wire/request.pb.h"
#include "components/feed/core/proto/v2/wire/response.pb.h"
#include "components/feed/core/proto/v2/wire/upload_actions_request.pb.h"
#include "components/feed/core/proto/v2/wire/upload_actions_response.pb.h"
#include "components/feed/core/proto/v2/wire/web_feeds.pb.h"
#include "components/feed/core/v2/enums.h"
#include "components/feed/core/v2/public/types.h"
#include "components/feed/core/v2/types.h"
#include "net/http/http_request_headers.h"

namespace feedwire {
class Request;
class Response;
}  // namespace feedwire

namespace feed {
struct AccountInfo;

// DiscoverApi types. Defines information about each discover API. For use with
// `FeedNetwork::SendApiRequest()`.
// Some APIs do not send request metadata because it is already included in the
// `feedwire::Request` proto and is therefore redundant.

struct QueryInteractiveFeedDiscoverApi {
  using Request = feedwire::Request;
  using Response = feedwire::Response;
  static constexpr NetworkRequestType kRequestType =
      NetworkRequestType::kQueryInteractiveFeed;
  static std::string_view Method() { return "POST"; }
  static std::string_view RequestPath(const Request&) {
    return "v1:queryInteractiveFeed";
  }
  static bool SendRequestMetadata() { return false; }
};

struct QueryBackgroundFeedDiscoverApi {
  using Request = feedwire::Request;
  using Response = feedwire::Response;
  static constexpr NetworkRequestType kRequestType =
      NetworkRequestType::kQueryBackgroundFeed;
  static std::string_view Method() { return "POST"; }
  static std::string_view RequestPath(const Request&) {
    return "v1:queryBackgroundFeed";
  }
  static bool SendRequestMetadata() { return false; }
};

struct QueryNextPageDiscoverApi {
  using Request = feedwire::Request;
  using Response = feedwire::Response;
  static constexpr NetworkRequestType kRequestType =
      NetworkRequestType::kQueryNextPage;
  static std::string_view Method() { return "POST"; }
  static std::string_view RequestPath(const Request&) {
    return "v1:queryNextPage";
  }
  static bool SendRequestMetadata() { return false; }
};

struct UploadActionsDiscoverApi {
  using Request = feedwire::UploadActionsRequest;
  using Response = feedwire::UploadActionsResponse;
  static constexpr NetworkRequestType kRequestType =
      NetworkRequestType::kUploadActions;
  static std::string_view Method() { return "POST"; }
  static std::string_view RequestPath(const Request&) {
    return "v1/actions:upload";
  }
  static bool SendRequestMetadata() { return true; }
};

struct ListWebFeedsDiscoverApi {
  using Request = feedwire::webfeed::ListWebFeedsRequest;
  using Response = feedwire::webfeed::ListWebFeedsResponse;
  static constexpr NetworkRequestType kRequestType =
      NetworkRequestType::kListWebFeeds;
  static std::string_view Method() { return "POST"; }
  static std::string_view RequestPath(const Request&) { return "v1/webFeeds"; }
  static bool SendRequestMetadata() { return true; }
};

struct ListRecommendedWebFeedDiscoverApi {
  using Request = feedwire::webfeed::ListRecommendedWebFeedsRequest;
  using Response = feedwire::webfeed::ListRecommendedWebFeedsResponse;
  static constexpr NetworkRequestType kRequestType =
      NetworkRequestType::kListRecommendedWebFeeds;
  static std::string_view Method() { return "POST"; }
  static std::string_view RequestPath(const Request&) {
    return "v1/recommendedWebFeeds";
  }
  static bool SendRequestMetadata() { return true; }
};

struct FollowWebFeedDiscoverApi {
  using Request = feedwire::webfeed::FollowWebFeedRequest;
  using Response = feedwire::webfeed::FollowWebFeedResponse;
  static constexpr NetworkRequestType kRequestType =
      NetworkRequestType::kFollowWebFeed;
  static std::string_view Method() { return "POST"; }
  static std::string_view RequestPath(const Request&) {
    return "v1:followWebFeed";
  }
  static bool SendRequestMetadata() { return true; }
};

struct UnfollowWebFeedDiscoverApi {
  using Request = feedwire::webfeed::UnfollowWebFeedRequest;
  using Response = feedwire::webfeed::UnfollowWebFeedResponse;
  static constexpr NetworkRequestType kRequestType =
      NetworkRequestType::kUnfollowWebFeed;
  static std::string_view Method() { return "POST"; }
  static std::string_view RequestPath(const Request&) {
    return "v1:unfollowWebFeed";
  }
  static bool SendRequestMetadata() { return true; }
};

struct WebFeedListContentsDiscoverApi {
  using Request = feedwire::Request;
  using Response = feedwire::Response;
  static constexpr NetworkRequestType kRequestType =
      NetworkRequestType::kWebFeedListContents;
  static std::string_view Method() { return "POST"; }
  static std::string_view RequestPath(const Request&) { return "v1/contents"; }
  static bool SendRequestMetadata() { return false; }
};

struct SingleWebFeedListContentsDiscoverApi {
  using Request = feedwire::Request;
  using Response = feedwire::Response;
  static constexpr NetworkRequestType kRequestType =
      NetworkRequestType::kSingleWebFeedListContents;
  static std::string_view Method() { return "POST"; }
  static std::string_view RequestPath(const Request&) { return "v1/contents"; }
  static bool SendRequestMetadata() { return false; }
};

struct QueryWebFeedDiscoverApi {
  using Request = feedwire::webfeed::QueryWebFeedRequest;
  using Response = feedwire::webfeed::QueryWebFeedResponse;
  static constexpr NetworkRequestType kRequestType =
      NetworkRequestType::kQueryWebFeed;
  static std::string_view Method() { return "POST"; }
  static std::string_view RequestPath(const Request&) {
    return "v1:queryWebFeed";
  }
  static bool SendRequestMetadata() { return true; }
};

class FeedNetwork {
 public:
  struct RawResponse {
    // HTTP response body.
    std::string response_bytes;
    NetworkResponseInfo response_info;
  };
  // Result of SendQueryRequest.
  struct QueryRequestResult {
    QueryRequestResult();
    ~QueryRequestResult();
    QueryRequestResult(QueryRequestResult&&);
    QueryRequestResult& operator=(QueryRequestResult&&);
    NetworkResponseInfo response_info;
    // Response body if one was received.
    std::unique_ptr<feedwire::Response> response_body;
    // Whether the request was signed in.
    bool was_signed_in;
  };

  template <typename RESPONSE_MESSAGE>
  struct ApiResult {
    ApiResult() = default;
    ~ApiResult() = default;
    ApiResult(ApiResult&&) = default;
    ApiResult& operator=(ApiResult&&) = default;

    NetworkResponseInfo response_info;
    // Response body if one was received.
    std::unique_ptr<RESPONSE_MESSAGE> response_body;
  };

  virtual ~FeedNetwork();

  // Send functions. These send a request and call the callback with the result.
  // If |CancelRequests()| is called, the result callback may never be called.

  // Send a feedwire::Request, and receive the response in |callback|.
  virtual void SendQueryRequest(
      NetworkRequestType request_type,
      const feedwire::Request& request,
      const AccountInfo& account_info,
      base::OnceCallback<void(QueryRequestResult)> callback) = 0;

  // Send a Discover API request. Usage:
  // SendApiRequest<UploadActionsDiscoverApi>(request_message, callback).
  template <typename API>
  void SendApiRequest(
      const typename API::Request& request,
      const AccountInfo& account_info,
      RequestMetadata request_metadata,
      base::OnceCallback<void(ApiResult<typename API::Response>)> callback) {
    std::string binary_proto;
    request.SerializeToString(&binary_proto);
    std::optional<RequestMetadata> optional_request_metadata;
    if (API::SendRequestMetadata()) {
      optional_request_metadata =
          std::make_optional(std::move(request_metadata));
    }

    SendDiscoverApiRequest(
        API::kRequestType, API::RequestPath(request), API::Method(),
        std::move(binary_proto), account_info,
        std::move(optional_request_metadata),
        base::BindOnce(&ParseAndForwardApiResponse<API>, std::move(callback)));
  }

  virtual void SendAsyncDataRequest(
      const GURL& url,
      std::string_view request_method,
      net::HttpRequestHeaders request_headers,
      std::string request_body,
      const AccountInfo& account_info,
      base::OnceCallback<void(RawResponse)> callback) = 0;

  // Cancels all pending requests immediately. This could be used, for example,
  // if there are pending requests for a user who just signed out.
  virtual void CancelRequests() = 0;

 protected:
  static void ParseAndForwardApiResponseStarted(
      NetworkRequestType request_type,
      const RawResponse& raw_response);
  virtual void SendDiscoverApiRequest(
      NetworkRequestType request_type,
      std::string_view api_path,
      std::string_view method,
      std::string request_bytes,
      const AccountInfo& account_info,
      std::optional<RequestMetadata> request_metadata,
      base::OnceCallback<void(RawResponse)> callback) = 0;

  template <typename API>
  static void ParseAndForwardApiResponse(
      base::OnceCallback<void(FeedNetwork::ApiResult<typename API::Response>)>
          result_callback,
      RawResponse raw_response) {
    ParseAndForwardApiResponseStarted(API::kRequestType, raw_response);
    FeedNetwork::ApiResult<typename API::Response> result;
    result.response_info = raw_response.response_info;
    if (result.response_info.status_code == 200) {
      auto response_message = std::make_unique<typename API::Response>();
      if (response_message->ParseFromString(raw_response.response_bytes)) {
        result.response_body = std::move(response_message);
      }
    }
    std::move(result_callback).Run(std::move(result));
  }
};

}  // namespace feed

#endif  // COMPONENTS_FEED_CORE_V2_FEED_NETWORK_H_