File: per_user_topic_subscription_request.cc

package info (click to toggle)
chromium 139.0.7258.127-1
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 6,122,068 kB
  • sloc: cpp: 35,100,771; ansic: 7,163,530; javascript: 4,103,002; python: 1,436,920; asm: 946,517; xml: 746,709; pascal: 187,653; perl: 88,691; sh: 88,436; objc: 79,953; sql: 51,488; cs: 44,583; fortran: 24,137; makefile: 22,147; tcl: 15,277; php: 13,980; yacc: 8,984; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (370 lines) | stat: -rw-r--r-- 13,017 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
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
// Copyright 2018 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "components/invalidation/impl/per_user_topic_subscription_request.h"

#include <memory>

#include "base/functional/bind.h"
#include "base/json/json_writer.h"
#include "base/memory/ptr_util.h"
#include "base/metrics/histogram_functions.h"
#include "base/strings/stringprintf.h"
#include "base/values.h"
#include "net/http/http_status_code.h"
#include "services/network/public/cpp/resource_request.h"
#include "services/network/public/mojom/fetch_api.mojom-shared.h"
#include "services/network/public/mojom/url_response_head.mojom.h"

using net::HttpRequestHeaders;

namespace invalidation {

namespace {

const char kPublicTopicNameKey[] = "publicTopicName";
const char kPrivateTopicNameKey[] = "privateTopicName";

const std::string* GetTopicName(const base::Value& value) {
  if (!value.is_dict())
    return nullptr;
  const base::Value::Dict& dict = value.GetDict();
  if (dict.FindBool("isPublic").value_or(false)) {
    return dict.FindString(kPublicTopicNameKey);
  }
  return dict.FindString(kPrivateTopicNameKey);
}

bool IsNetworkError(int net_error) {
  // Note: ERR_HTTP_RESPONSE_CODE_FAILURE isn't a real network error - it
  // indicates that the network request itself succeeded, but we received an
  // HTTP error. The alternative to special-casing this error would be to call
  // SetAllowHttpErrorResults(true) on the SimpleUrlLoader, but that also means
  // we'd download the response body in case of HTTP errors, which we don't
  // need.
  return net_error != net::OK &&
         net_error != net::ERR_HTTP_RESPONSE_CODE_FAILURE;
}

// Subscription status values for UMA_HISTOGRAM.
enum class SubscriptionStatus {
  kSuccess = 0,
  kNetworkFailure = 1,
  kHttpFailure = 2,
  kParsingFailure = 3,
  kFailure = 4,
  kMaxValue = kFailure,
};

void RecordRequestStatus(SubscriptionStatus status,
                         PerUserTopicSubscriptionRequest::RequestType type,
                         const std::string& topic,
                         int net_error = net::OK,
                         int response_code = 200) {
  switch (type) {
    case PerUserTopicSubscriptionRequest::RequestType::kSubscribe: {
      base::UmaHistogramEnumeration(
          "FCMInvalidations.SubscriptionRequestStatus", status);
      break;
    }
    case PerUserTopicSubscriptionRequest::RequestType::kUnsubscribe: {
      base::UmaHistogramEnumeration(
          "FCMInvalidations.UnsubscriptionRequestStatus", status);
      break;
    }
  }
  if (type != PerUserTopicSubscriptionRequest::RequestType::kSubscribe) {
    return;
  }

  if (IsNetworkError(net_error)) {
    // Tracks the cases, when request fails due to network error.
    base::UmaHistogramSparse("FCMInvalidations.FailedSubscriptionsErrorCode",
                             -net_error);
    DVLOG(1) << "Subscription request failed with error: " << net_error << ": "
             << net::ErrorToString(net_error);
  } else {
    // Log a histogram to track response success vs. failure rates.
    base::UmaHistogramSparse("FCMInvalidations.SubscriptionResponseCode",
                             response_code);
  }
}

}  // namespace

PerUserTopicSubscriptionRequest::PerUserTopicSubscriptionRequest() = default;

PerUserTopicSubscriptionRequest::~PerUserTopicSubscriptionRequest() = default;

void PerUserTopicSubscriptionRequest::Start(
    CompletedCallback callback,
    network::mojom::URLLoaderFactory* loader_factory) {
  DCHECK(request_completed_callback_.is_null()) << "Request already running!";
  request_completed_callback_ = std::move(callback);

  simple_loader_->DownloadToStringOfUnboundedSizeUntilCrashAndDie(
      loader_factory,
      base::BindOnce(&PerUserTopicSubscriptionRequest::OnURLFetchComplete,
                     weak_ptr_factory_.GetWeakPtr()));
}

void PerUserTopicSubscriptionRequest::OnURLFetchComplete(
    std::unique_ptr<std::string> response_body) {
  int response_code = 0;
  if (simple_loader_->ResponseInfo() &&
      simple_loader_->ResponseInfo()->headers) {
    response_code = simple_loader_->ResponseInfo()->headers->response_code();
  }
  OnURLFetchCompleteInternal(simple_loader_->NetError(), response_code,
                             std::move(response_body));
  // Potentially dead after the above invocation; nothing to do except return.
}

void PerUserTopicSubscriptionRequest::OnURLFetchCompleteInternal(
    int net_error,
    int response_code,
    std::unique_ptr<std::string> response_body) {
  if (IsNetworkError(net_error)) {
    RecordRequestStatus(SubscriptionStatus::kNetworkFailure, type_, topic_,
                        net_error, response_code);
    RunCompletedCallbackAndMaybeDie(Status(StatusCode::FAILED, "Network Error"),
                                    std::string());
    // Potentially dead after the above invocation; nothing to do except return.
    return;
  }

  if (response_code != net::HTTP_OK) {
    StatusCode status = StatusCode::FAILED;
    if (response_code == net::HTTP_UNAUTHORIZED) {
      status = StatusCode::AUTH_FAILURE;
    } else if (response_code >= 400 && response_code <= 499) {
      status = StatusCode::FAILED_NON_RETRIABLE;
    }
    RecordRequestStatus(SubscriptionStatus::kHttpFailure, type_, topic_,
                        net_error, response_code);
    RunCompletedCallbackAndMaybeDie(
        Status(status, base::StringPrintf("HTTP Error: %d", response_code)),
        std::string());
    // Potentially dead after the above invocation; nothing to do except return.
    return;
  }

  if (type_ == RequestType::kUnsubscribe) {
    // No response body expected for DELETE requests.
    RecordRequestStatus(SubscriptionStatus::kSuccess, type_, topic_, net_error,
                        response_code);
    RunCompletedCallbackAndMaybeDie(Status(StatusCode::SUCCESS, std::string()),
                                    std::string());
    // Potentially dead after the above invocation; nothing to do except return.
    return;
  }

  if (!response_body || response_body->empty()) {
    RecordRequestStatus(SubscriptionStatus::kParsingFailure, type_, topic_,
                        net_error, response_code);
    RunCompletedCallbackAndMaybeDie(Status(StatusCode::FAILED, "Body missing"),
                                    std::string());
    // Potentially dead after the above invocation; nothing to do except return.
    return;
  }

  data_decoder::DataDecoder::ParseJsonIsolated(
      *response_body,
      base::BindOnce(&PerUserTopicSubscriptionRequest::OnJsonParse,
                     weak_ptr_factory_.GetWeakPtr()));
}

void PerUserTopicSubscriptionRequest::OnJsonParse(
    data_decoder::DataDecoder::ValueOrError result) {
  if (const auto topic_name = result.transform(GetTopicName);
      topic_name.has_value() && *topic_name) {
    RecordRequestStatus(SubscriptionStatus::kSuccess, type_, topic_);
    RunCompletedCallbackAndMaybeDie(Status(StatusCode::SUCCESS, std::string()),
                                    **topic_name);
    // Potentially dead after the above invocation; nothing to do except return.
    return;
  }
  RecordRequestStatus(SubscriptionStatus::kParsingFailure, type_, topic_);
  RunCompletedCallbackAndMaybeDie(
      Status(StatusCode::FAILED,
             result.has_value() ? "Missing topic name" : "Body parse error"),
      std::string());
  // Potentially dead after the above invocation; nothing to do except return.
}

void PerUserTopicSubscriptionRequest::RunCompletedCallbackAndMaybeDie(
    Status status,
    std::string topic_name) {
  std::move(request_completed_callback_)
      .Run(std::move(status), std::move(topic_name));
}

PerUserTopicSubscriptionRequest::Builder::Builder() = default;
PerUserTopicSubscriptionRequest::Builder::~Builder() = default;

std::unique_ptr<PerUserTopicSubscriptionRequest>
PerUserTopicSubscriptionRequest::Builder::Build() const {
  DCHECK(!scope_.empty());
  auto request = base::WrapUnique(new PerUserTopicSubscriptionRequest());

  std::string url;
  switch (type_) {
    case RequestType::kSubscribe:
      url = base::StringPrintf(
          "%s/v1/perusertopics/%s/rel/topics/?subscriber_token=%s",
          scope_.c_str(), project_id_.c_str(), instance_id_token_.c_str());
      break;
    case RequestType::kUnsubscribe:
      std::string public_param;
      if (topic_is_public_) {
        public_param = "subscription.is_public=true&";
      }
      url = base::StringPrintf(
          "%s/v1/perusertopics/%s/rel/topics/%s?%ssubscriber_token=%s",
          scope_.c_str(), project_id_.c_str(), topic_.c_str(),
          public_param.c_str(), instance_id_token_.c_str());
      break;
  }
  GURL full_url(url);

  DCHECK(full_url.is_valid());

  request->url_ = full_url;
  request->type_ = type_;
  request->topic_ = topic_;

  std::string body;
  if (type_ == RequestType::kSubscribe) {
    body = BuildBody();
  }
  net::HttpRequestHeaders headers = BuildHeaders();
  request->simple_loader_ = BuildURLFetcher(headers, body, full_url);

  // Log the request for debugging network issues.
  DVLOG(1) << "Building a subscription request to " << full_url << ":\n"
           << headers.ToString() << "\n"
           << body;
  return request;
}

PerUserTopicSubscriptionRequest::Builder&
PerUserTopicSubscriptionRequest::Builder::SetInstanceIdToken(
    const std::string& token) {
  instance_id_token_ = token;
  return *this;
}

PerUserTopicSubscriptionRequest::Builder&
PerUserTopicSubscriptionRequest::Builder::SetScope(const std::string& scope) {
  scope_ = scope;
  return *this;
}

PerUserTopicSubscriptionRequest::Builder&
PerUserTopicSubscriptionRequest::Builder::SetAuthenticationHeader(
    const std::string& auth_header) {
  auth_header_ = auth_header;
  return *this;
}

PerUserTopicSubscriptionRequest::Builder&
PerUserTopicSubscriptionRequest::Builder::SetPublicTopicName(
    const Topic& topic) {
  topic_ = topic;
  return *this;
}

PerUserTopicSubscriptionRequest::Builder&
PerUserTopicSubscriptionRequest::Builder::SetProjectId(
    const std::string& project_id) {
  project_id_ = project_id;
  return *this;
}

PerUserTopicSubscriptionRequest::Builder&
PerUserTopicSubscriptionRequest::Builder::SetType(RequestType type) {
  type_ = type;
  return *this;
}

PerUserTopicSubscriptionRequest::Builder&
PerUserTopicSubscriptionRequest::Builder::SetTopicIsPublic(
    bool topic_is_public) {
  topic_is_public_ = topic_is_public;
  return *this;
}

HttpRequestHeaders PerUserTopicSubscriptionRequest::Builder::BuildHeaders()
    const {
  HttpRequestHeaders headers;
  if (!auth_header_.empty()) {
    headers.SetHeader(HttpRequestHeaders::kAuthorization, auth_header_);
  }
  return headers;
}

std::string PerUserTopicSubscriptionRequest::Builder::BuildBody() const {
  base::Value::Dict request;

  request.Set("public_topic_name", topic_);
  if (topic_is_public_)
    request.Set("is_public", true);

  std::string request_json;
  bool success = base::JSONWriter::Write(request, &request_json);
  DCHECK(success);
  return request_json;
}

std::unique_ptr<network::SimpleURLLoader>
PerUserTopicSubscriptionRequest::Builder::BuildURLFetcher(
    const HttpRequestHeaders& headers,
    const std::string& body,
    const GURL& url) const {
  net::NetworkTrafficAnnotationTag traffic_annotation =
      net::DefineNetworkTrafficAnnotation("per_user_topic_registration_request",
                                          R"(
        semantics {
          sender:
            "Subscribe for listening to the specific user topic"
          description:
            "This request subscribes the client for receiving FCM messages for"
            "the concrete user topic."
          trigger:
            "Subscription takes place only once per profile per topic. "
          data:
            "An OAuth2 token is sent as an authorization header."
          destination: GOOGLE_OWNED_SERVICE
        }
        policy {
          cookies_allowed: NO
          setting:
            "This feature can not be disabled by settings now"
          policy_exception_justification:
            "This feature is required to deliver core user experiences and "
            "cannot be disabled by policy."
        })");

  auto request = std::make_unique<network::ResourceRequest>();
  switch (type_) {
    case PerUserTopicSubscriptionRequest::RequestType::kSubscribe:
      request->method = "POST";
      break;
    case PerUserTopicSubscriptionRequest::RequestType::kUnsubscribe:
      request->method = "DELETE";
      break;
  }
  request->url = url;
  request->headers = headers;
  // Disable cookies for this request.
  request->credentials_mode = network::mojom::CredentialsMode::kOmit;

  std::unique_ptr<network::SimpleURLLoader> url_loader =
      network::SimpleURLLoader::Create(std::move(request), traffic_annotation);
  url_loader->AttachStringForUpload(body, "application/json; charset=UTF-8");

  return url_loader;
}

}  // namespace invalidation