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
|
// 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 "chrome/browser/ui/views/commerce/price_tracking_icon_view.h"
#include <string_view>
#include "base/metrics/user_metrics.h"
#include "base/timer/timer.h"
#include "chrome/browser/bookmarks/bookmark_model_factory.h"
#include "chrome/browser/commerce/shopping_service_factory.h"
#include "chrome/browser/feature_engagement/tracker_factory.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/browser/ui/bookmarks/bookmark_utils.h"
#include "chrome/browser/ui/browser.h"
#include "chrome/browser/ui/browser_element_identifiers.h"
#include "chrome/browser/ui/color/chrome_color_id.h"
#include "chrome/browser/ui/commerce/commerce_ui_tab_helper.h"
#include "chrome/browser/ui/tabs/public/tab_features.h"
#include "chrome/browser/ui/ui_features.h"
#include "chrome/browser/ui/views/commerce/price_tracking_bubble_dialog_view.h"
#include "chrome/browser/ui/views/frame/browser_view.h"
#include "chrome/browser/ui/views/page_action/page_action_icon_view.h"
#include "chrome/browser/ui/views/side_panel/side_panel_coordinator.h"
#include "chrome/browser/ui/views/side_panel/side_panel_entry.h"
#include "chrome/browser/ui/views/side_panel/side_panel_registry.h"
#include "chrome/common/pref_names.h"
#include "components/bookmarks/browser/bookmark_model.h"
#include "components/commerce/core/commerce_feature_list.h"
#include "components/commerce/core/metrics/metrics_utils.h"
#include "components/commerce/core/pref_names.h"
#include "components/commerce/core/price_tracking_utils.h"
#include "components/commerce/core/shopping_service.h"
#include "components/feature_engagement/public/feature_constants.h"
#include "components/feature_engagement/public/tracker.h"
#include "components/omnibox/browser/vector_icons.h"
#include "components/power_bookmarks/core/power_bookmark_utils.h"
#include "components/power_bookmarks/core/proto/power_bookmark_meta.pb.h"
#include "components/prefs/pref_service.h"
#include "components/strings/grit/components_strings.h"
#include "components/tabs/public/tab_interface.h"
#include "content/public/browser/web_contents.h"
#include "ui/base/l10n/l10n_util.h"
#include "ui/base/metadata/metadata_impl_macros.h"
#include "ui/base/models/image_model.h"
#include "ui/gfx/vector_icon_types.h"
#include "ui/views/accessibility/view_accessibility.h"
#include "ui/views/view_class_properties.h"
namespace {
// This will add the bookmark to the shopping collection if the feature is
// enabled, otherwise we save to "other bookmarks".
void AddIfNotBookmarkedToTheDefaultFolder(bookmarks::BookmarkModel* model,
content::WebContents* web_contents) {
GURL url;
std::u16string title;
if (chrome::GetURLAndTitleToBookmark(web_contents, &url, &title)) {
if (bookmarks::IsBookmarkedByUser(model, url)) {
return;
}
const bookmarks::BookmarkNode* parent =
commerce::GetShoppingCollectionBookmarkFolder(model, true);
model->AddNewURL(parent, parent->children().size(), title, url);
}
}
} // namespace
PriceTrackingIconView::PriceTrackingIconView(
IconLabelBubbleView::Delegate* parent_delegate,
Delegate* delegate,
Browser* browser)
: PageActionIconView(nullptr,
0,
parent_delegate,
delegate,
"PriceTracking"),
browser_(browser),
profile_(browser->profile()),
bubble_coordinator_(this),
icon_(&omnibox::kPriceTrackingDisabledRefreshIcon) {
SetUpForInOutAnimation();
SetProperty(views::kElementIdentifierKey, kPriceTrackingChipElementId);
GetViewAccessibility().SetName(
l10n_util::GetStringUTF16(IDS_OMNIBOX_TRACK_PRICE));
}
PriceTrackingIconView::~PriceTrackingIconView() = default;
views::BubbleDialogDelegate* PriceTrackingIconView::GetBubble() const {
return bubble_coordinator_.GetBubble();
}
void PriceTrackingIconView::OnExecuting(
PageActionIconView::ExecuteSource execute_source) {
if (AnimateOutTimer().IsRunning()) {
AnimateOutTimer().Stop();
}
auto* web_contents = GetWebContents();
DCHECK(web_contents);
auto* tab_helper = tabs::TabInterface::GetFromContents(web_contents)
->GetTabFeatures()
->commerce_ui_tab_helper();
CHECK(tab_helper);
const gfx::Image& product_image = tab_helper->GetProductImage();
tab_helper->OnPriceTrackingIconClicked();
DCHECK(!product_image.IsEmpty());
base::RecordAction(
base::UserMetricsAction("Commerce.PriceTracking.OmniboxChipClicked"));
bookmarks::BookmarkModel* bookmarkModel =
BookmarkModelFactory::GetForBrowserContext(profile_);
if (ShouldShowFirstUseExperienceBubble()) {
const bookmarks::BookmarkNode* bookmark =
bookmarkModel->GetMostRecentlyAddedUserNodeForURL(
GetWebContents()->GetLastCommittedURL());
bubble_coordinator_.Show(
GetWebContents(), profile_, GetWebContents()->GetLastCommittedURL(),
ui::ImageModel::FromImage(product_image),
base::BindOnce(&PriceTrackingIconView::EnablePriceTracking,
weak_ptr_factory_.GetWeakPtr()),
base::BindOnce(&PriceTrackingIconView::UnpauseAnimation,
weak_ptr_factory_.GetWeakPtr()),
PriceTrackingBubbleDialogView::Type::TYPE_FIRST_USE_EXPERIENCE,
bookmark ? std::optional<std::u16string>(bookmark->parent()->GetTitle())
: std::nullopt);
} else {
EnablePriceTracking(/*enable=*/true);
const bookmarks::BookmarkNode* bookmark =
bookmarkModel->GetMostRecentlyAddedUserNodeForURL(
GetWebContents()->GetLastCommittedURL());
bubble_coordinator_.Show(
GetWebContents(), profile_, GetWebContents()->GetLastCommittedURL(),
ui::ImageModel::FromImage(product_image),
base::BindOnce(&PriceTrackingIconView::EnablePriceTracking,
weak_ptr_factory_.GetWeakPtr()),
base::BindOnce(&PriceTrackingIconView::UnpauseAnimation,
weak_ptr_factory_.GetWeakPtr()),
PriceTrackingBubbleDialogView::Type::TYPE_NORMAL,
bookmark ? std::optional<std::u16string>(bookmark->parent()->GetTitle())
: std::nullopt);
}
}
const gfx::VectorIcon& PriceTrackingIconView::GetVectorIcon() const {
return *icon_;
}
bool PriceTrackingIconView::ShouldShow() {
if (delegate()->ShouldHidePageActionIcons()) {
return false;
}
auto* web_contents = GetWebContents();
if (!web_contents) {
return false;
}
auto* tab_helper = tabs::TabInterface::GetFromContents(web_contents)
->GetTabFeatures()
->commerce_ui_tab_helper();
return tab_helper && tab_helper->ShouldShowPriceTrackingIconView();
}
void PriceTrackingIconView::UpdateImpl() {
bool should_show = ShouldShow();
if (should_show) {
SetVisualState(IsPriceTracking());
if (!GetVisible()) {
base::RecordAction(
base::UserMetricsAction("Commerce.PriceTracking.OmniboxChipShown"));
}
MaybeShowPageActionLabel();
} else {
scoped_window_call_to_action_ptr_.reset();
HidePageActionLabel();
}
SetVisible(should_show);
}
void PriceTrackingIconView::AnimationProgressed(
const gfx::Animation* animation) {
PageActionIconView::AnimationProgressed(animation);
// When the label is fully revealed pause the animation for
// kLabelPersistDuration before resuming the animation and allowing the label
// to animate out. This is currently set to show for 12s including the in/out
// animation.
// TODO(crbug.com/40832707): This approach of inspecting the animation
// progress to extend the animation duration is quite hacky. This should be
// removed and the IconLabelBubbleView API expanded to support a finer level
// of control.
constexpr double kAnimationValueWhenLabelFullyShown = 0.5;
constexpr base::TimeDelta kLabelPersistDuration = base::Seconds(10.8);
if (should_extend_label_shown_duration_ &&
GetAnimationValue() >= kAnimationValueWhenLabelFullyShown) {
should_extend_label_shown_duration_ = false;
PauseAnimation();
AnimateOutTimer().Start(
FROM_HERE, kLabelPersistDuration,
base::BindOnce(&PriceTrackingIconView::UnpauseAnimation,
base::Unretained(this)));
}
}
void PriceTrackingIconView::ForceVisibleForTesting(bool is_tracking_price) {
SetVisible(true);
SetVisualState(is_tracking_price);
}
std::u16string_view PriceTrackingIconView::GetIconLabelForTesting() const {
return label()->GetText();
}
void PriceTrackingIconView::SetOneShotTimerForTesting(
base::OneShotTimer* timer) {
animate_out_timer_for_testing_ = timer;
}
void PriceTrackingIconView::EnablePriceTracking(bool enable) {
if (IsPriceTracking() == enable) {
return;
}
if (enable && ShouldShowFirstUseExperienceBubble()) {
profile_->GetPrefs()->SetBoolean(prefs::kShouldShowPriceTrackFUEBubble,
false);
}
bookmarks::BookmarkModel* const model =
BookmarkModelFactory::GetForBrowserContext(profile_);
const bookmarks::BookmarkNode* existing_node =
model->GetMostRecentlyAddedUserNodeForURL(
GetWebContents()->GetLastCommittedURL());
bool is_new_bookmark = existing_node == nullptr;
if (enable) {
AddIfNotBookmarkedToTheDefaultFolder(model, GetWebContents());
base::RecordAction(
base::UserMetricsAction("Commerce.PriceTracking.OmniboxChip.Tracked"));
commerce::MaybeEnableEmailNotifications(profile_->GetPrefs());
commerce::metrics::RecordShoppingActionUKM(
GetWebContents()->GetPrimaryMainFrame()->GetPageUkmSourceId(),
commerce::metrics::ShoppingAction::kPriceTracked);
}
auto* tab_helper = tabs::TabInterface::GetFromContents(GetWebContents())
->GetTabFeatures()
->commerce_ui_tab_helper();
CHECK(tab_helper);
tab_helper->SetPriceTrackingState(
enable, is_new_bookmark,
base::BindOnce(&PriceTrackingIconView::OnPriceTrackingServerStateUpdated,
weak_ptr_factory_.GetWeakPtr()));
SetVisualState(enable);
}
void PriceTrackingIconView::SetVisualState(bool enable) {
icon_ = enable ? &omnibox::kPriceTrackingEnabledRefreshIcon
: &omnibox::kPriceTrackingDisabledRefreshIcon;
// TODO(meiliang@): Confirm with UXW on the tooltip string. If this expected,
// we can return label()->GetText() instead.
GetViewAccessibility().SetName(l10n_util::GetStringUTF16(
enable ? IDS_OMNIBOX_TRACKING_PRICE : IDS_OMNIBOX_TRACK_PRICE));
SetLabel(l10n_util::GetStringUTF16(enable ? IDS_OMNIBOX_TRACKING_PRICE
: IDS_OMNIBOX_TRACK_PRICE));
SetBackgroundVisibility(BackgroundVisibility::kWithLabel);
UpdateIconImage();
}
void PriceTrackingIconView::OnPriceTrackingServerStateUpdated(bool success) {
// TODO(crbug.com/40865740): Handles error if |success| is false.
if (commerce::kRevertIconOnFailure.Get() && !success) {
bubble_coordinator_.Hide();
UpdateImpl();
}
}
bool PriceTrackingIconView::IsPriceTracking() const {
if (!GetWebContents()) {
return false;
}
auto* tab_helper = tabs::TabInterface::GetFromContents(GetWebContents())
->GetTabFeatures()
->commerce_ui_tab_helper();
CHECK(tab_helper);
return tab_helper->IsPriceTracking();
}
bool PriceTrackingIconView::ShouldShowFirstUseExperienceBubble() const {
return profile_->GetPrefs()->GetBoolean(
prefs::kShouldShowPriceTrackFUEBubble) &&
!profile_->GetPrefs()->HasPrefPath(
commerce::kPriceEmailNotificationsEnabled) &&
!IsPriceTracking();
}
void PriceTrackingIconView::MaybeShowPageActionLabel() {
auto* tab_helper = tabs::TabInterface::GetFromContents(GetWebContents())
->GetTabFeatures()
->commerce_ui_tab_helper();
if (!tab_helper || !tab_helper->ShouldExpandPageActionIcon(
PageActionIconType::kPriceTracking)) {
return;
}
if (!tabs::TabInterface::GetFromContents(GetWebContents())
->GetBrowserWindowInterface()
->CanShowCallToAction()) {
return;
}
scoped_window_call_to_action_ptr_ =
tabs::TabInterface::GetFromContents(GetWebContents())
->GetBrowserWindowInterface()
->ShowCallToAction();
should_extend_label_shown_duration_ = true;
AnimateIn(std::nullopt);
}
void PriceTrackingIconView::HidePageActionLabel() {
UnpauseAnimation();
ResetSlideAnimation(false);
}
base::OneShotTimer& PriceTrackingIconView::AnimateOutTimer() {
return animate_out_timer_for_testing_ ? *animate_out_timer_for_testing_
: animate_out_timer_;
}
BEGIN_METADATA(PriceTrackingIconView)
END_METADATA
|