File: trial_group_checker.cc

package info (click to toggle)
chromium 138.0.7204.183-1
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 6,071,908 kB
  • sloc: cpp: 34,937,088; ansic: 7,176,967; javascript: 4,110,704; python: 1,419,953; 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,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 (133 lines) | stat: -rw-r--r-- 4,295 bytes parent folder | download | duplicates (7)
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
// Copyright 2019 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "chromeos/ash/components/trial_group/trial_group_checker.h"

#include <optional>

#include "base/functional/bind.h"
#include "base/json/json_reader.h"
#include "base/json/json_writer.h"
#include "base/values.h"
#include "net/base/load_flags.h"
#include "services/network/public/cpp/resource_request.h"
#include "services/network/public/cpp/shared_url_loader_factory.h"
#include "services/network/public/cpp/simple_url_loader.h"
#include "services/network/public/mojom/url_response_head.mojom.h"

namespace {

constexpr int kIsMember = 1;
constexpr char kServerUrl[] =
    "https://crosdogpack-pa.googleapis.com/v1/isMember";

}  // namespace

namespace ash {
namespace trial_group {

TrialGroupChecker::TrialGroupChecker(GroupId group_id)
    : server_url_(GURL(kServerUrl)), group_id_(group_id) {}

TrialGroupChecker::~TrialGroupChecker() = default;

void TrialGroupChecker::SetServerUrl(GURL server_url) {
  server_url_ = server_url;
}

void TrialGroupChecker::OnRequestComplete(
    std::unique_ptr<std::string> response_body) {
  const int net_error = loader_->NetError();

  int response_code = 0;
  if (loader_->ResponseInfo()) {
    response_code = loader_->ResponseInfo()->headers->response_code();
  }
  loader_.reset();

  const bool server_error =
      net_error != net::OK || (response_code >= 500 && response_code < 600);
  if (server_error || response_body->empty()) {
    std::move(callback_).Run(false);
    return;
  }

  auto membership_info = base::JSONReader::ReadAndReturnValueWithError(
      *response_body, base::JSON_PARSE_RFC);
  if (!membership_info.has_value() || !membership_info->is_dict()) {
    std::move(callback_).Run(false);
    return;
  }

  std::optional<int> member_status =
      membership_info->GetDict().FindInt("membership_info");
  if (!member_status) {
    std::move(callback_).Run(false);
    return;
  }

  bool is_member = (member_status.value() == kIsMember);
  std::move(callback_).Run(is_member);
}

TrialGroupChecker::Status TrialGroupChecker::LookUpMembership(
    scoped_refptr<network::SharedURLLoaderFactory> url_loader_factory,
    base::OnceCallback<void(bool is_member)> callback) {
  // OnRequestComplete has not completed from a previous call, so exit.
  if (loader_)
    return TrialGroupChecker::PREVIOUS_CALL_RUNNING;

  callback_ = std::move(callback);

  std::string upload_data;
  {
    base::Value::Dict request;
    request.Set("group", static_cast<int>(group_id_));
    base::JSONWriter::Write(request, &upload_data);
  }

  net::NetworkTrafficAnnotationTag traffic_annotation =
      net::DefineNetworkTrafficAnnotation("trial_group_lookup",
                                          R"(
          semantics {
            sender: "Trial Group Lookup"
            description:
              "Obtains whether user is in the Google group for a dogfood trial."
            trigger: "On boot."
            data: "Dogfood enum identifier and credentials."
            destination: GOOGLE_OWNED_SERVICE
          }
          policy {
            cookies_allowed: NO
            policy_exception_justification:
              "Only relevant for internal testing by Google employees. "
              "Opt-out is not possible on ChromeOS currently per "
              "go/finch-dogfood#chrome-os."
          }
      )");

  auto resource_request = std::make_unique<network::ResourceRequest>();
  resource_request->url = server_url_;

  DCHECK(resource_request->url.is_valid());
  resource_request->method = "POST";
  resource_request->load_flags =
      net::LOAD_BYPASS_CACHE | net::LOAD_DISABLE_CACHE;
  resource_request->credentials_mode =
      network::mojom::CredentialsMode::kInclude;

  loader_ = network::SimpleURLLoader::Create(std::move(resource_request),
                                             traffic_annotation);

  loader_->AttachStringForUpload(upload_data, "application/json");
  loader_->DownloadToString(
      url_loader_factory.get(),
      base::BindOnce(&TrialGroupChecker::OnRequestComplete,
                     weak_factory_.GetWeakPtr()),
      1024 /* 1 kiB */);
  return TrialGroupChecker::OK;
}

}  // namespace trial_group
}  // namespace ash