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
|
// Copyright 2023 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "chrome/browser/ui/commerce/price_tracking_page_action_controller.h"
#include "base/check_is_test.h"
#include "base/metrics/histogram_functions.h"
#include "base/strings/string_number_conversions.h"
#include "components/commerce/core/price_tracking_utils.h"
#include "components/commerce/core/shopping_service.h"
#include "components/feature_engagement/public/tracker.h"
#include "components/image_fetcher/core/image_fetcher.h"
namespace commerce {
namespace {
constexpr net::NetworkTrafficAnnotationTag kShoppingListTrafficAnnotation =
net::DefineNetworkTrafficAnnotation("shopping_list_ui_image_fetcher",
R"(
semantics {
sender: "Product image fetcher for the shopping list feature."
description:
"Retrieves the image for a product that is displayed on the active "
"web page. This will be shown to the user as part of the "
"bookmarking or price tracking action."
trigger:
"On navigation, if the URL of the page is determined to be a "
"product that can be price tracked, we will attempt to fetch the "
"image for it."
user_data {
type: NONE
}
data: "No user data."
internal {
contacts {
email: "chrome-shopping@google.com"
}
}
destination: GOOGLE_OWNED_SERVICE
last_reviewed: "2024-01-11"
}
policy {
cookies_allowed: NO
setting:
"This fetch is enabled for any user with the 'Shopping List' "
"feature enabled."
chrome_policy {
ShoppingListEnabled {
policy_options {mode: MANDATORY}
ShoppingListEnabled: false
}
}
})");
constexpr char kImageFetcherUmaClient[] = "ShoppingList";
// The minimum price that the price tracking UI always wants to expand at.
constexpr int64_t kAlwaysExpandChipPriceMicros = 100000000L;
} // namespace
PriceTrackingPageActionController::PriceTrackingPageActionController(
base::RepeatingCallback<void()> notify_callback,
ShoppingService* shopping_service,
image_fetcher::ImageFetcher* image_fetcher,
feature_engagement::Tracker* tracker)
: CommercePageActionController(std::move(notify_callback)),
shopping_service_(shopping_service),
image_fetcher_(image_fetcher),
tracker_(tracker) {
if (shopping_service_) {
scoped_observation_.Observe(shopping_service_);
shopping_service_->WaitForReady(base::BindOnce(
[](base::WeakPtr<PriceTrackingPageActionController> controller,
ShoppingService* service) {
if (!controller || !service) {
return;
}
if (service->IsShoppingListEligible()) {
// Fetching the image may have been blocked by the eligibility
// check, retry.
controller->MaybeDoProductImageFetch(
controller->product_info_for_page_);
controller->NotifyHost();
}
},
weak_ptr_factory_.GetWeakPtr()));
} else {
CHECK_IS_TEST();
}
}
PriceTrackingPageActionController::~PriceTrackingPageActionController() {
// Recording this in the destructor corresponds to the tab being closed or the
// browser shutting down.
MaybeRecordPriceTrackingIconMetrics(/*from_icon_use=*/false);
}
std::optional<bool>
PriceTrackingPageActionController::ShouldShowForNavigation() {
// If the user isn't eligible for the feature, don't block.
if (!shopping_service_ || !shopping_service_->IsShoppingListEligible()) {
return false;
}
// If we got a response from the shopping service but the response was empty,
// we don't need to wait for the image or subscription status.
if (got_product_response_for_page_ && !product_info_for_page_.has_value()) {
return false;
}
// If the page is determined to be a product page, we're "undecided" until we
// can show the current subscription state and have an image for the UI.
if (!got_initial_subscription_status_for_page_ ||
!got_image_response_for_page_) {
return std::nullopt;
}
return !last_fetched_image_.IsEmpty();
}
bool PriceTrackingPageActionController::WantsExpandedUi() {
if (!product_info_for_page_.has_value()) {
return false;
}
CommerceSubscription sub(
SubscriptionType::kPriceTrack, IdentifierType::kProductClusterId,
base::NumberToString(product_info_for_page_->product_cluster_id.value()),
ManagementType::kUserManaged);
bool already_subscribed = shopping_service_->IsSubscribedFromCache(sub);
// Don't expand the chip if the user is already subscribed to the product.
if (!already_subscribed) {
if (tracker_ &&
tracker_->ShouldTriggerHelpUI(
feature_engagement::kIPHPriceTrackingPageActionIconLabelFeature)) {
tracker_->Dismissed(
feature_engagement::kIPHPriceTrackingPageActionIconLabelFeature);
expanded_ui_for_page_ = true;
return true;
}
// If none of the above cases expanded a chip, expand the price tracking
// chip if the product price is > $100.
if (product_info_for_page_->amount_micros > kAlwaysExpandChipPriceMicros &&
product_info_for_page_->product_cluster_id.has_value()) {
expanded_ui_for_page_ = true;
return true;
}
}
return false;
}
void PriceTrackingPageActionController::ResetForNewNavigation(const GURL& url) {
if (!shopping_service_->IsShoppingListEligible()) {
return;
}
// The page action icon may not have been used for the last page load. If
// that's the case, make sure we record it.
MaybeRecordPriceTrackingIconMetrics(/*from_icon_use=*/false);
// Cancel any pending callbacks.
weak_ptr_factory_.InvalidateWeakPtrs();
is_cluster_id_tracked_by_user_ = false;
last_fetched_image_ = gfx::Image();
last_fetched_image_url_ = GURL();
current_url_ = url;
got_product_response_for_page_ = false;
got_image_response_for_page_ = false;
got_initial_subscription_status_for_page_ = false;
expanded_ui_for_page_ = false;
icon_use_recorded_for_page_ = false;
// Initiate an update for the icon on navigation since we may not have product
// info.
NotifyHost();
shopping_service_->GetProductInfoForUrl(
url, base::BindOnce(
&PriceTrackingPageActionController::HandleProductInfoResponse,
weak_ptr_factory_.GetWeakPtr()));
}
void PriceTrackingPageActionController::OnIconClicked() {
MaybeRecordPriceTrackingIconMetrics(/*from_icon_use=*/true);
}
void PriceTrackingPageActionController::MaybeRecordPriceTrackingIconMetrics(
bool from_icon_use) {
// Ignore cases where these is no cluster ID or the metric was already
// recorded for the page.
if (!cluster_id_for_page_.has_value() || icon_use_recorded_for_page_) {
return;
}
icon_use_recorded_for_page_ = true;
// Ignore any instances where the product is already tracked. This will not
// stop cases where the icon is being used to newly track a product since
// this logic will run prior to subscriptions updating.
if (is_cluster_id_tracked_by_user_) {
return;
}
std::string histogram_name = "Commerce.PriceTracking.IconInteractionState";
// Clicking the icon for a product that is already tracked does not
// immediately untrack the product. If we made it this far, we can assume the
// interaction was to track a product, otherwise we would have been blocked
// above.
if (from_icon_use) {
if (expanded_ui_for_page_) {
base::UmaHistogramEnumeration(
histogram_name, PageActionIconInteractionState::kClickedExpanded);
} else {
base::UmaHistogramEnumeration(histogram_name,
PageActionIconInteractionState::kClicked);
}
} else {
if (expanded_ui_for_page_) {
base::UmaHistogramEnumeration(
histogram_name, PageActionIconInteractionState::kNotClickedExpanded);
} else {
base::UmaHistogramEnumeration(
histogram_name, PageActionIconInteractionState::kNotClicked);
}
}
}
void PriceTrackingPageActionController::HandleProductInfoResponse(
const GURL& url,
const std::optional<const ProductInfo>& info) {
if (url != current_url_ || !info.has_value()) {
got_product_response_for_page_ = true;
NotifyHost();
return;
}
product_info_for_page_ = info;
got_product_response_for_page_ = true;
if (shopping_service_->IsShoppingListEligible() && CanTrackPrice(info) &&
!info->image_url.is_empty()) {
cluster_id_for_page_.emplace(info->product_cluster_id.value());
UpdatePriceTrackingStateFromSubscriptions();
MaybeDoProductImageFetch(info);
}
}
void PriceTrackingPageActionController::MaybeDoProductImageFetch(
const std::optional<ProductInfo>& info) {
if (!shopping_service_->IsShoppingListEligible() || !CanTrackPrice(info) ||
info->image_url.is_empty() || !this->last_fetched_image_.IsEmpty()) {
return;
}
// TODO(crbug.com/40863328): Delay this fetch by possibly waiting until page
// load has
// finished.
image_fetcher_->FetchImage(
info.value().image_url,
base::BindOnce(
&PriceTrackingPageActionController::HandleImageFetcherResponse,
weak_ptr_factory_.GetWeakPtr(), info.value().image_url),
image_fetcher::ImageFetcherParams(kShoppingListTrafficAnnotation,
kImageFetcherUmaClient));
}
void PriceTrackingPageActionController::
UpdatePriceTrackingStateFromSubscriptions() {
if (!cluster_id_for_page_.has_value()) {
return;
}
shopping_service_->IsSubscribed(
BuildUserSubscriptionForClusterId(cluster_id_for_page_.value()),
base::BindOnce(
[](base::WeakPtr<PriceTrackingPageActionController> controller,
bool is_tracked) {
if (!controller) {
return;
}
controller->is_cluster_id_tracked_by_user_ = is_tracked;
controller->got_initial_subscription_status_for_page_ = true;
controller->NotifyHost();
},
weak_ptr_factory_.GetWeakPtr()));
}
void PriceTrackingPageActionController::OnSubscribe(
const CommerceSubscription& subscription,
bool succeeded) {
HandleSubscriptionChange(subscription);
}
void PriceTrackingPageActionController::OnUnsubscribe(
const CommerceSubscription& subscription,
bool succeeded) {
HandleSubscriptionChange(subscription);
}
void PriceTrackingPageActionController::HandleSubscriptionChange(
const CommerceSubscription& sub) {
if (sub.id_type == IdentifierType::kProductClusterId &&
sub.id == base::NumberToString(
cluster_id_for_page_.value_or(kInvalidSubscriptionId))) {
UpdatePriceTrackingStateFromSubscriptions();
NotifyHost();
}
}
void PriceTrackingPageActionController::HandleImageFetcherResponse(
const GURL image_url,
const gfx::Image& image,
const image_fetcher::RequestMetadata& request_metadata) {
got_image_response_for_page_ = true;
if (!image.IsEmpty()) {
last_fetched_image_url_ = image_url;
last_fetched_image_ = image;
}
NotifyHost();
}
const gfx::Image& PriceTrackingPageActionController::GetLastFetchedImage() {
return last_fetched_image_;
}
const GURL& PriceTrackingPageActionController::GetLastFetchedImageUrl() {
return last_fetched_image_url_;
}
bool PriceTrackingPageActionController::IsPriceTrackingCurrentProduct() {
return is_cluster_id_tracked_by_user_;
}
void PriceTrackingPageActionController::SetImageFetcherForTesting(
image_fetcher::ImageFetcher* image_fetcher) {
image_fetcher_ = image_fetcher;
}
} // namespace commerce
|