File: cluster_server_proxy.cc

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 (182 lines) | stat: -rw-r--r-- 6,851 bytes parent folder | download | duplicates (2)
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
// Copyright 2024 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/commerce/core/compare/cluster_server_proxy.h"

#include <utility>

#include "base/json/json_reader.h"
#include "base/json/values_util.h"
#include "base/strings/string_number_conversions.h"
#include "components/commerce/core/account_checker.h"
#include "components/commerce/core/commerce_constants.h"
#include "components/commerce/core/commerce_feature_list.h"
#include "components/commerce/core/compare/compare_utils.h"
#include "components/commerce/core/feature_utils.h"
#include "components/signin/public/identity_manager/identity_manager.h"
#include "net/http/http_request_headers.h"
#include "net/http/http_status_code.h"
#include "net/traffic_annotation/network_traffic_annotation.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"

using endpoint_fetcher::EndpointFetcher;
using endpoint_fetcher::EndpointResponse;

namespace commerce {
namespace {
// RPC timeout duration.
const int kTimeoutMs = 5000;

// URL to get compare result.
const char kDefaultServiceBaseUrl[] =
    "https://memex-pa.googleapis.com/v1/shopping/products:related";
const char kBaseUrlParam[] = "cluster_service_base_url";
constexpr base::FeatureParam<std::string> kServiceBaseUrl{
    &commerce::kProductSpecifications, kBaseUrlParam, kDefaultServiceBaseUrl};

// lowerCamelCase JSON proto message keys.
const char kProductIdsKey[] = "productIds";
const char kTypeKey[] = "type";
const char kGPCTypeName[] = "GLOBAL_PRODUCT_CLUSTER_ID";
const char kIdentifierKey[] = "identifier";

constexpr net::NetworkTrafficAnnotationTag
    kGetComparableProductsTrafficAnnotation =
        net::DefineNetworkTrafficAnnotation(
            "chrome_commerce_comparable_products",
            R"(
          semantics {
            sender: "Chrome Shopping"
            description:
              "Ask server for comparable products from a list of product Ids."
            trigger:
              "A Chrome-initiated request that requires user enabling the "
              "Tab Compare feature. The request is sent after Chrome detects "
              "several open tabs has similar products, before showing the user"
              "a button to allow them to compare some of the products."
            user_data {
              type: OTHER
              type: ACCESS_TOKEN
            }
            data:
              "Product cluster IDs obtained previously from the Google server "
              "for product pages user opened."
            destination: GOOGLE_OWNED_SERVICE
            internal {
              contacts { email: "chrome-shopping-eng@google.com" }
            }
            last_reviewed: "2024-06-10"
          }
          policy {
            cookies_allowed: NO
            setting:
              "This fetch is enabled for any user that is eligible to use this "
              "feature based on things like country, locale, and whether the "
              "user is signed in. The request is enabled with the Tab Compare "
              "feature."
            chrome_policy {
              TabCompareSettings {
                policy_options {mode: MANDATORY}
                TabCompareSettings: 2
              }
            }
          }
        )");

// Deserializes a product cluster ID from JSON.
std::optional<uint64_t> Deserialize(const base::Value& value) {
  if (!value.is_dict()) {
    return std::nullopt;
  }

  const base::Value::Dict& value_dict = value.GetDict();
  auto* type = value_dict.FindString(kTypeKey);
  if (!type || *type != kGPCTypeName) {
    return std::nullopt;
  }

  auto* identifier = value_dict.FindString(kIdentifierKey);
  uint64_t product_cluster_id;
  if (!identifier || !base::StringToUint64(*identifier, &product_cluster_id)) {
    return std::nullopt;
  }

  return product_cluster_id;
}

}  // namespace

ClusterServerProxy::ClusterServerProxy(
    signin::IdentityManager* identity_manager,
    const scoped_refptr<network::SharedURLLoaderFactory>& url_loader_factory,
    AccountChecker* account_checker)
    : identity_manager_(identity_manager),
      url_loader_factory_(url_loader_factory),
      account_checker_(account_checker) {}

ClusterServerProxy::~ClusterServerProxy() = default;

void ClusterServerProxy::GetComparableProducts(
    const std::vector<uint64_t>& product_cluster_ids,
    GetComparableProductsCallback callback) {
  if (!commerce::CanFetchProductSpecificationsData(account_checker_)) {
    std::move(callback).Run(std::vector<uint64_t>());
    return;
  }
  auto fetcher = CreateEndpointFetcher(
      GURL(kServiceBaseUrl.Get()),
      GetJsonStringForProductClusterIds(product_cluster_ids));
  auto* const fetcher_ptr = fetcher.get();

  fetcher_ptr->Fetch(base::BindOnce(&ClusterServerProxy::HandleCompareResponse,
                                    weak_ptr_factory_.GetWeakPtr(),
                                    std::move(callback), std::move(fetcher)));
}

std::unique_ptr<EndpointFetcher> ClusterServerProxy::CreateEndpointFetcher(
    const GURL& url,
    const std::string& post_data) {
  return std::make_unique<EndpointFetcher>(
      url_loader_factory_, kOAuthName, url, kPostHttpMethod, kContentType,
      std::vector<std::string>{kOAuthScope}, base::Milliseconds(kTimeoutMs),
      post_data, kGetComparableProductsTrafficAnnotation, identity_manager_,
      signin::ConsentLevel::kSync);
}

void ClusterServerProxy::HandleCompareResponse(
    GetComparableProductsCallback callback,
    std::unique_ptr<EndpointFetcher> endpoint_fetcher,
    std::unique_ptr<EndpointResponse> response) {
  if (response->http_status_code != net::HTTP_OK || response->error_type) {
    VLOG(1) << "Got bad response (" << response->http_status_code
            << ") for comparable products!";
    std::move(callback).Run(std::vector<uint64_t>());
    return;
  }

  data_decoder::DataDecoder::ParseJsonIsolated(
      response->response,
      base::BindOnce(&ClusterServerProxy::OnResponseJsonParsed,
                     weak_ptr_factory_.GetWeakPtr(), std::move(callback)));
}

void ClusterServerProxy::OnResponseJsonParsed(
    GetComparableProductsCallback callback,
    data_decoder::DataDecoder::ValueOrError result) {
  std::vector<uint64_t> product_cluster_ids;
  if (result.has_value() && result->is_dict()) {
    if (auto* response_json = result->GetDict().FindList(kProductIdsKey)) {
      for (const auto& product_id_json : *response_json) {
        if (auto product_id = Deserialize(product_id_json)) {
          product_cluster_ids.push_back(*product_id);
        }
      }
    }
  }
  std::move(callback).Run(std::move(product_cluster_ids));
}

}  // namespace commerce