File: discount_infos_storage.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 (151 lines) | stat: -rw-r--r-- 5,682 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
// Copyright 2025 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/discount_infos_storage.h"

#include "base/check.h"
#include "base/functional/callback.h"
#include "base/metrics/histogram_functions.h"
#include "base/strings/string_util.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_types.h"
#include "components/commerce/core/commerce_utils.h"
#include "components/commerce/core/proto/discounts_db_content.pb.h"
#include "components/session_proto_db/session_proto_storage.h"

namespace commerce {

DiscountInfosStorage::DiscountInfosStorage(
    SessionProtoStorage<DiscountInfosContent>* discount_infos_proto_db,
    history::HistoryService* history_service)
    : proto_db_(discount_infos_proto_db) {
  if (history_service) {
    history_service_observation_.Observe(history_service);
  }
}
DiscountInfosStorage::~DiscountInfosStorage() = default;

void DiscountInfosStorage::LoadDiscountsWithPrefix(const GURL& url,
                                         DiscountInfoCallback callback) {
  proto_db_->LoadContentWithPrefix(
      url.spec(),
      base::BindOnce(&DiscountInfosStorage::OnLoadDiscounts,
                     weak_ptr_factory_.GetWeakPtr(), url, std::move(callback)));
}

void DiscountInfosStorage::SaveDiscounts(
    const GURL& url,
    const std::vector<DiscountInfo>& infos) {
  DiscountInfosContent proto;
  proto.set_key(url.spec());
  for (const DiscountInfo& info : infos) {
    if (info.expiry_time_sec.has_value() && info.discount_code.has_value()) {
      discount_infos_db::DiscountInfoContent* discount_proto =
          proto.add_discounts();
      discount_proto->set_id(info.id);
      discount_infos_db::DiscountInfoContent_Type type =
          discount_infos_db::DiscountInfoContent_Type_TYPE_UNSPECIFIED;
      if (info.type == DiscountType::kFreeListingWithCode) {
        type =
            discount_infos_db::DiscountInfoContent_Type_FREE_LISTING_WITH_CODE;
      } else if (info.type == DiscountType::kCrawledPromotion) {
        type = discount_infos_db::DiscountInfoContent_Type_CRAWLED_PROMOTION;
      }
      discount_proto->set_type(type);
      discount_proto->set_language_code(info.language_code);
      discount_proto->set_description_detail(info.description_detail);
      if (info.terms_and_conditions.has_value()) {
        discount_proto->set_terms_and_conditions(
            info.terms_and_conditions.value());
      }
      discount_proto->set_expiry_time_sec(info.expiry_time_sec.value());
      discount_proto->set_discount_code(info.discount_code.value());
      discount_proto->set_offer_id(info.offer_id);
    }
  }
  if (proto.discounts().size() > 0) {
    proto_db_->InsertContent(url.spec(), proto,
                             base::BindOnce([](bool succeeded) {}));
  } else {
    DeleteDiscountsForUrl(url.spec());
  }
}

void DiscountInfosStorage::DeleteDiscountsForUrl(const std::string& url) {
  proto_db_->DeleteOneEntry(url, base::BindOnce([](bool succeeded) {}));
}

void DiscountInfosStorage::OnLoadDiscounts(const GURL& url,
                                           DiscountInfoCallback callback,
                                           bool succeeded,
                                           DiscountInfosKeyAndValues data) {
  if (!succeeded || data.size() == 0) {
    std::move(callback).Run(url, {});
    return;
  }
  std::vector<DiscountInfo> unexpired_infos;
  for (const auto& [key, value] : data) {
    std::vector<DiscountInfo> valid_infos =
        GetUnexpiredDiscountsFromProto(value);

    if (valid_infos.size() == 0) {
      // Delete the entry in the db if no unexpired discounts found.
      DeleteDiscountsForUrl(key);
    } else {
      // Update local database if expired discounts found.
      if ((int)(valid_infos.size()) != value.discounts().size()) {
        SaveDiscounts(GURL(key), valid_infos);
      }
      unexpired_infos.insert(unexpired_infos.end(), valid_infos.begin(),
                             valid_infos.end());
    }
  }
  std::move(callback).Run(url, std::move(unexpired_infos));
}

std::vector<DiscountInfo> DiscountInfosStorage::GetUnexpiredDiscountsFromProto(
    const DiscountInfosContent& proto) {
  std::vector<DiscountInfo> infos;
  for (const discount_infos_db::DiscountInfoContent& content :
       proto.discounts()) {
    // First check whether the discount is expired.
    if ((base::Time::Now() - base::Time::UnixEpoch()).InSeconds() >
        content.expiry_time_sec()) {
      continue;
    }

    DiscountInfo info;
    info.id = content.id();
    info.type = DiscountType(content.type());
    info.language_code = content.language_code();
    info.description_detail = content.description_detail();
    if (content.has_terms_and_conditions()) {
      info.terms_and_conditions = content.terms_and_conditions();
    }
    info.expiry_time_sec = content.expiry_time_sec();
    if (content.has_discount_code()) {
      info.discount_code = content.discount_code();
    }
    info.offer_id = content.offer_id();
    infos.push_back(info);
  }

  return infos;
}

void DiscountInfosStorage::OnHistoryDeletions(
    history::HistoryService* history_service,
    const history::DeletionInfo& deletion_info) {
  if (deletion_info.IsAllHistory()) {
    proto_db_->DeleteAllContent(base::BindOnce([](bool succeeded) {}));
    return;
  }

  for (const history::URLRow& row : deletion_info.deleted_rows()) {
    DeleteDiscountsForUrl(row.url().spec());
  }
}
}  // namespace commerce