File: subscriptions_server_proxy.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 (452 lines) | stat: -rw-r--r-- 19,436 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
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
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
// Copyright 2022 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/subscriptions/subscriptions_server_proxy.h"

#include <queue>
#include <string>
#include <unordered_map>

#include "base/check.h"
#include "base/feature_list.h"
#include "base/functional/callback.h"
#include "base/json/json_reader.h"
#include "base/json/json_writer.h"
#include "base/json/values_util.h"
#include "base/strings/string_number_conversions.h"
#include "base/time/time.h"
#include "components/commerce/core/commerce_constants.h"
#include "components/commerce/core/commerce_feature_list.h"
#include "components/commerce/core/commerce_utils.h"
#include "components/commerce/core/subscriptions/commerce_subscription.h"
#include "components/endpoint_fetcher/endpoint_fetcher.h"
#include "components/signin/public/base/consent_level.h"
#include "components/signin/public/identity_manager/identity_manager.h"
#include "components/sync/base/features.h"
#include "net/http/http_status_code.h"
#include "net/traffic_annotation/network_traffic_annotation.h"
#include "services/network/public/cpp/shared_url_loader_factory.h"

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

namespace {

// For creating endpoint fetcher.
const int kDefaultTimeoutMs = 5000;
const char kTimeoutParam[] = "subscriptions_server_request_timeout";
constexpr base::FeatureParam<int> kTimeoutMs{&commerce::kShoppingList,
                                             kTimeoutParam, kDefaultTimeoutMs};

const char kDefaultServiceBaseUrl[] =
    "https://memex-pa.googleapis.com/v1/shopping/subscriptions";
const char kBaseUrlParam[] = "subscriptions_service_base_url";
constexpr base::FeatureParam<std::string> kServiceBaseUrl{
    &commerce::kShoppingList, kBaseUrlParam, kDefaultServiceBaseUrl};

const char kGetQueryParams[] = "?requestParams.subscriptionType=";
const char kManageQueryParams[] = "?requestSnapshotParams.subscriptionType=";
const char kPriceTrackGetParam[] = "PRICE_TRACK";

// For generating server requests and deserializing the responses.
const char kSubscriptionsKey[] = "subscriptions";
const char kCreateRequestParamsKey[] = "createShoppingSubscriptionsParams";
const char kEventTimestampsKey[] = "eventTimestampMicros";
const char kDeleteRequestParamsKey[] = "removeShoppingSubscriptionsParams";
const char kStatusKey[] = "status";
const char kStatusCodeKey[] = "code";
const int kBackendCanonicalCodeSuccess = 0;

// For (de)serializing subscription.
const char kSubscriptionTypeKey[] = "type";
const char kSubscriptionIdTypeKey[] = "identifierType";
const char kSubscriptionIdKey[] = "identifier";
const char kSubscriptionManagementTypeKey[] = "managementType";
const char kSubscriptionTimestampKey[] = "eventTimestampMicros";
const char kSubscriptionSeenOfferKey[] = "userSeenOffer";
const char kSeenOfferIdKey[] = "offerId";
const char kSeenOfferPriceKey[] = "seenPriceMicros";
const char kSeenOfferCountryKey[] = "countryCode";
const char kSeenOfferLocaleKey[] = "languageCode";

}  // namespace

namespace commerce {

SubscriptionsServerProxy::SubscriptionsServerProxy(
    signin::IdentityManager* identity_manager,
    scoped_refptr<network::SharedURLLoaderFactory> url_loader_factory)
    : url_loader_factory_(std::move(url_loader_factory)),
      identity_manager_(identity_manager),
      weak_ptr_factory_(this) {}
SubscriptionsServerProxy::~SubscriptionsServerProxy() = default;

void SubscriptionsServerProxy::Create(
    std::unique_ptr<std::vector<CommerceSubscription>> subscriptions,
    ManageSubscriptionsFetcherCallback callback) {
  CHECK(subscriptions->size() > 0);
  std::string service_url = kServiceBaseUrl.Get() + kManageQueryParams;
  if ((*subscriptions)[0].type == SubscriptionType::kPriceTrack) {
    service_url += kPriceTrackGetParam;
  } else {
    VLOG(1) << "Unsupported type for Create query";
    std::move(callback).Run(
        SubscriptionsRequestStatus::kInvalidArgument,
        std::make_unique<std::vector<CommerceSubscription>>());
    return;
  }

  base::Value::List subscriptions_list;
  for (const auto& subscription : *subscriptions) {
    subscriptions_list.Append(Serialize(subscription));
  }
  base::Value::Dict subscriptions_json;
  subscriptions_json.Set(kSubscriptionsKey, std::move(subscriptions_list));
  base::Value::Dict request_json;
  request_json.Set(kCreateRequestParamsKey, std::move(subscriptions_json));
  std::string post_data;
  base::JSONWriter::Write(request_json, &post_data);

  net::NetworkTrafficAnnotationTag traffic_annotation =
      net::DefineNetworkTrafficAnnotation(
          "chrome_commerce_subscriptions_create", R"(
        semantics {
          sender: "Chrome Shopping"
          description:
            "Create new shopping subscriptions containing the product offers "
            "for tracking prices. These subscriptions will be stored on the"
            "server."
          trigger:
            "A user-initiated request is sent when the user explicitly tracks "
            "the product via the product page menu. A Chrome-initiated request "
            "is automatically sent on Chrome startup after the user has opted "
            "in to the tab-based price tracking feature from the tab switcher "
            "menu."
          data:
            "The list of subscriptions to be added, each of which contains a "
            "subscription type, a subscription id, the user seen offer price "
            "and offer locale. The request also includes an OAuth2 token "
            "authenticating the user."
          destination: GOOGLE_OWNED_SERVICE
        }
        policy {
          cookies_allowed: NO
          setting:
            "This feature is only enabled for signed-in users. User-initiated "
            "subscriptions can be managed in the user's Bookmarks. "
            "Chrome-initiated subscriptions can be removed when the user opts "
            "out of the tab-based price tracking feature from the tab switcher "
            "menu."
          chrome_policy {
            BrowserSignin {
              policy_options {mode: MANDATORY}
              BrowserSignin: 0
            }
          }
        })");

  auto fetcher = CreateEndpointFetcher(GURL(service_url),
                                       endpoint_fetcher::HttpMethod::kPost,
                                       post_data, traffic_annotation);
  auto* const fetcher_ptr = fetcher.get();
  fetcher_ptr->Fetch(base::BindOnce(
      &SubscriptionsServerProxy::HandleManageSubscriptionsResponses,
      weak_ptr_factory_.GetWeakPtr(), std::move(callback), std::move(fetcher)));
}

void SubscriptionsServerProxy::Delete(
    std::unique_ptr<std::vector<CommerceSubscription>> subscriptions,
    ManageSubscriptionsFetcherCallback callback) {
  CHECK(subscriptions->size() > 0);
  std::string service_url = kServiceBaseUrl.Get() + kManageQueryParams;
  if ((*subscriptions)[0].type == SubscriptionType::kPriceTrack) {
    service_url += kPriceTrackGetParam;
  } else {
    VLOG(1) << "Unsupported type for Delete query";
    std::move(callback).Run(
        SubscriptionsRequestStatus::kInvalidArgument,
        std::make_unique<std::vector<CommerceSubscription>>());
    return;
  }

  base::Value::List deletions_list;
  for (const auto& subscription : *subscriptions) {
    if (subscription.timestamp != kUnknownSubscriptionTimestamp)
      deletions_list.Append(base::Int64ToValue(subscription.timestamp));
  }
  base::Value::Dict deletions_json;
  deletions_json.Set(kEventTimestampsKey, std::move(deletions_list));
  base::Value::Dict request_json;
  request_json.Set(kDeleteRequestParamsKey, std::move(deletions_json));
  std::string post_data;
  base::JSONWriter::Write(request_json, &post_data);

  net::NetworkTrafficAnnotationTag traffic_annotation =
      net::DefineNetworkTrafficAnnotation(
          "chrome_commerce_subscriptions_delete", R"(
        semantics {
          sender: "Chrome Shopping"
          description:
            "Delete one or more shopping subscriptions. These subscriptions "
            "were stored on the server previously for tracking prices."
          trigger:
            "A user-initiated request is sent when the user explicitly "
            "untracks the product via the product page menu. A "
            "Chrome-initiated request is automatically sent when the user "
            "navigates away from product pages if the user has opted in to the "
            "tab-based price tracking feature from the tab switcher menu."
          data:
            "The list of subscriptions to be deleted, each of which contains "
            "the subscription's creation timestamp. The request also includes "
            "an OAuth2 token authenticating the user."
          destination: GOOGLE_OWNED_SERVICE
        }
        policy {
          cookies_allowed: NO
          setting:
            "This feature is only enabled for signed-in users. User-initiated "
            "subscriptions can be managed in the user's Bookmarks. "
            "Chrome-initiated subscriptions can be removed when the user opts "
            "out of the tab-based price tracking feature from the tab switcher "
            "menu."
          chrome_policy {
            BrowserSignin {
              policy_options {mode: MANDATORY}
              BrowserSignin: 0
            }
          }
        })");

  auto fetcher = CreateEndpointFetcher(GURL(service_url),
                                       endpoint_fetcher::HttpMethod::kPost,
                                       post_data, traffic_annotation);
  auto* const fetcher_ptr = fetcher.get();
  fetcher_ptr->Fetch(base::BindOnce(
      &SubscriptionsServerProxy::HandleManageSubscriptionsResponses,
      weak_ptr_factory_.GetWeakPtr(), std::move(callback), std::move(fetcher)));
}

void SubscriptionsServerProxy::Get(SubscriptionType type,
                                   GetSubscriptionsFetcherCallback callback) {
  std::string service_url = kServiceBaseUrl.Get() + kGetQueryParams;
  if (type == SubscriptionType::kPriceTrack) {
    service_url += kPriceTrackGetParam;
  } else {
    VLOG(1) << "Unsupported type for Get query";
    std::move(callback).Run(
        SubscriptionsRequestStatus::kInvalidArgument,
        std::make_unique<std::vector<CommerceSubscription>>());
    return;
  }

  net::NetworkTrafficAnnotationTag traffic_annotation =
      net::DefineNetworkTrafficAnnotation("chrome_commerce_subscriptions_get",
                                          R"(
        semantics {
          sender: "Chrome Shopping"
          description:
            "Retrieve all shopping subscriptions of a user for a specified "
            "type. These subscriptions will be stored locally for later query."
          trigger:
            "On Chrome startup, or after the user changes their primary "
            "account."
          data:
            "The request includes a subscription type to be retrieved and an "
            "OAuth2 token authenticating the user. The response includes a "
            "list of subscriptions, each of which contains a subscription type,"
            " a subscription id, and the subscription's creation timestamp."
          destination: GOOGLE_OWNED_SERVICE
        }
        policy {
          cookies_allowed: NO
          setting:
            "This feature is only enabled for signed-in users. User-initiated "
            "subscriptions can be managed in the user's Bookmarks. "
            "Chrome-initiated subscriptions can be removed when the user opts "
            "out of the tab-based price tracking feature from the tab switcher "
            "menu."
          chrome_policy {
            BrowserSignin {
              policy_options {mode: MANDATORY}
              BrowserSignin: 0
            }
          }
        })");

  auto fetcher = CreateEndpointFetcher(GURL(service_url),
                                       endpoint_fetcher::HttpMethod::kGet,
                                       kEmptyPostData, traffic_annotation);
  auto* const fetcher_ptr = fetcher.get();
  fetcher_ptr->Fetch(base::BindOnce(
      &SubscriptionsServerProxy::HandleGetSubscriptionsResponses,
      weak_ptr_factory_.GetWeakPtr(), std::move(callback), std::move(fetcher)));
}

std::unique_ptr<EndpointFetcher>
SubscriptionsServerProxy::CreateEndpointFetcher(
    const GURL& url,
    const endpoint_fetcher::HttpMethod http_method,
    const std::string& post_data,
    const net::NetworkTrafficAnnotationTag& annotation_tag) {
  // If ReplaceSyncPromosWithSignInPromos is enabled - ConsentLevel::kSync is no
  // longer attainable. See crbug.com/1503156 for details.
  signin::ConsentLevel consent_level =
      base::FeatureList::IsEnabled(syncer::kReplaceSyncPromosWithSignInPromos)
          ? signin::ConsentLevel::kSignin
          : signin::ConsentLevel::kSync;
  EndpointFetcher::RequestParams::Builder request_params =
      EndpointFetcher::RequestParams::Builder(http_method, annotation_tag);
  request_params.SetUrl(url)
      .SetContentType(kContentType)
      .SetAuthType(endpoint_fetcher::OAUTH)
      .SetOauthScopes(std::vector<std::string>{kOAuthScope})
      .SetConsentLevel(consent_level)
      .SetTimeout(base::Milliseconds(kTimeoutMs.Get()))
      .SetOauthConsumerName(kOAuthName)
      .SetPostData(post_data);
  MaybeUseAlternateShoppingServer(request_params);
  return std::make_unique<EndpointFetcher>(
      url_loader_factory_, identity_manager_, request_params.Build());
}

void SubscriptionsServerProxy::HandleManageSubscriptionsResponses(
    ManageSubscriptionsFetcherCallback callback,
    std::unique_ptr<EndpointFetcher> endpoint_fetcher,
    std::unique_ptr<EndpointResponse> responses) {
  if (responses->http_status_code != net::HTTP_OK || responses->error_type) {
    VLOG(1) << "Server failed to parse manage-subscriptions request";
    std::move(callback).Run(
        SubscriptionsRequestStatus::kServerParseError,
        std::make_unique<std::vector<CommerceSubscription>>());
    return;
  }
  data_decoder::DataDecoder::ParseJsonIsolated(
      responses->response,
      base::BindOnce(&SubscriptionsServerProxy::OnManageSubscriptionsJsonParsed,
                     weak_ptr_factory_.GetWeakPtr(), std::move(callback)));
}

void SubscriptionsServerProxy::OnManageSubscriptionsJsonParsed(
    ManageSubscriptionsFetcherCallback callback,
    data_decoder::DataDecoder::ValueOrError result) {
  if (result.has_value() && result->is_dict()) {
    if (auto* status_value = result->GetDict().FindDict(kStatusKey)) {
      if (auto status_code = status_value->FindInt(kStatusCodeKey)) {
        if (*status_code == kBackendCanonicalCodeSuccess) {
          std::move(callback).Run(SubscriptionsRequestStatus::kSuccess,
                                  GetSubscriptionsFromParsedJson(result));
        } else {
          std::move(callback).Run(
              SubscriptionsRequestStatus::kServerInternalError,
              std::make_unique<std::vector<CommerceSubscription>>());
        }
        return;
      }
    }
  }

  VLOG(1) << "Fail to get status code from response";
  std::move(callback).Run(
      SubscriptionsRequestStatus::kServerInternalError,
      std::make_unique<std::vector<CommerceSubscription>>());
}

void SubscriptionsServerProxy::HandleGetSubscriptionsResponses(
    GetSubscriptionsFetcherCallback callback,
    std::unique_ptr<EndpointFetcher> endpoint_fetcher,
    std::unique_ptr<EndpointResponse> responses) {
  if (responses->http_status_code != net::HTTP_OK || responses->error_type) {
    VLOG(1) << "Server failed to parse get-subscriptions request";
    std::move(callback).Run(
        SubscriptionsRequestStatus::kServerParseError,
        std::make_unique<std::vector<CommerceSubscription>>());
    return;
  }
  data_decoder::DataDecoder::ParseJsonIsolated(
      responses->response,
      base::BindOnce(&SubscriptionsServerProxy::OnGetSubscriptionsJsonParsed,
                     weak_ptr_factory_.GetWeakPtr(), std::move(callback)));
}

void SubscriptionsServerProxy::OnGetSubscriptionsJsonParsed(
    GetSubscriptionsFetcherCallback callback,
    data_decoder::DataDecoder::ValueOrError result) {
  auto subscriptions = GetSubscriptionsFromParsedJson(result);
  if (subscriptions->size() == 0) {
    VLOG(1) << "User has no subscriptions";
  }
  std::move(callback).Run(SubscriptionsRequestStatus::kSuccess,
                          std::move(subscriptions));
}

std::unique_ptr<std::vector<CommerceSubscription>>
SubscriptionsServerProxy::GetSubscriptionsFromParsedJson(
    const data_decoder::DataDecoder::ValueOrError& result) {
  auto subscriptions = std::make_unique<std::vector<CommerceSubscription>>();
  if (result.has_value() && result->is_dict()) {
    if (auto* subscriptions_json =
            result->GetDict().FindList(kSubscriptionsKey)) {
      for (const auto& subscription_json : *subscriptions_json) {
        if (auto subscription = Deserialize(subscription_json))
          subscriptions->push_back(*subscription);
      }
    }
  }
  return subscriptions;
}

bool SubscriptionsServerProxy::IsPriceTrackingLocaleKeyEnabled() {
  return base::FeatureList::IsEnabled(kPriceTrackingSubscriptionServiceLocaleKey);
}

base::Value::Dict SubscriptionsServerProxy::Serialize(
    const CommerceSubscription& subscription) {
  base::Value::Dict subscription_json;
  subscription_json.Set(kSubscriptionTypeKey,
                        SubscriptionTypeToString(subscription.type));
  subscription_json.Set(kSubscriptionIdTypeKey,
                        SubscriptionIdTypeToString(subscription.id_type));
  subscription_json.Set(kSubscriptionIdKey, subscription.id);
  subscription_json.Set(
      kSubscriptionManagementTypeKey,
      SubscriptionManagementTypeToString(subscription.management_type));
  if (auto seen_offer = subscription.user_seen_offer) {
    base::Value::Dict seen_offer_json;
    seen_offer_json.Set(kSeenOfferIdKey, seen_offer->offer_id);
    seen_offer_json.Set(kSeenOfferPriceKey,
                        base::NumberToString(seen_offer->user_seen_price));
    seen_offer_json.Set(kSeenOfferCountryKey, seen_offer->country_code);
    if (IsPriceTrackingLocaleKeyEnabled()) {
      seen_offer_json.Set(kSeenOfferLocaleKey, seen_offer->locale);
    }
    subscription_json.Set(kSubscriptionSeenOfferKey,
                          std::move(seen_offer_json));
  }
  return subscription_json;
}

std::optional<CommerceSubscription> SubscriptionsServerProxy::Deserialize(
    const base::Value& value) {
  if (value.is_dict()) {
    const base::Value::Dict& value_dict = value.GetDict();
    auto* type = value_dict.FindString(kSubscriptionTypeKey);
    auto* id_type = value_dict.FindString(kSubscriptionIdTypeKey);
    auto* id = value_dict.FindString(kSubscriptionIdKey);
    auto* management_type =
        value_dict.FindString(kSubscriptionManagementTypeKey);
    auto timestamp =
        base::ValueToInt64(value_dict.Find(kSubscriptionTimestampKey));
    if (type && id_type && id && management_type && timestamp) {
      return std::make_optional<CommerceSubscription>(
          StringToSubscriptionType(*type), StringToSubscriptionIdType(*id_type),
          *id, StringToSubscriptionManagementType(*management_type),
          *timestamp);
    }
  }

  VLOG(1) << "Subscription in response is not valid";
  return std::nullopt;
}

}  // namespace commerce