File: commerce_ui_tab_helper.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 (804 lines) | stat: -rw-r--r-- 29,136 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
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
// 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/commerce/commerce_ui_tab_helper.h"

#include "base/check_is_test.h"
#include "base/functional/bind.h"
#include "base/metrics/histogram_functions.h"
#include "base/metrics/user_metrics.h"
#include "base/metrics/user_metrics_action.h"
#include "base/strings/string_number_conversions.h"
#include "base/time/time.h"
#include "chrome/browser/feature_engagement/tracker_factory.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/browser/ui/browser_tabstrip.h"
#include "chrome/browser/ui/browser_window.h"
#include "chrome/browser/ui/browser_window/public/browser_window_features.h"
#include "chrome/browser/ui/browser_window/public/browser_window_interface.h"
#include "chrome/browser/ui/commerce/commerce_page_action_controller.h"
#include "chrome/browser/ui/commerce/discounts_page_action_controller.h"
#include "chrome/browser/ui/commerce/price_tracking_page_action_controller.h"
#include "chrome/browser/ui/commerce/product_specifications_entry_point_controller.h"
#include "chrome/browser/ui/commerce/product_specifications_page_action_controller.h"
#include "chrome/browser/ui/tabs/public/tab_features.h"
#include "chrome/browser/ui/views/chrome_layout_provider.h"
#include "chrome/browser/ui/views/commerce/price_insights_icon_view.h"
#include "chrome/browser/ui/views/commerce/price_insights_page_action_view_controller.h"
#include "chrome/browser/ui/views/frame/browser_view.h"
#include "chrome/browser/ui/views/frame/toolbar_button_provider.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/browser/ui/views/side_panel/side_panel_ui.h"
#include "chrome/browser/ui/views/side_panel/side_panel_web_ui_view.h"
#include "chrome/browser/ui/webui/commerce/shopping_insights_side_panel_ui.h"
#include "chrome/common/pref_names.h"
#include "components/bookmarks/browser/bookmark_model.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/feature_utils.h"
#include "components/commerce/core/metrics/discounts_metric_collector.h"
#include "components/commerce/core/metrics/metrics_utils.h"
#include "components/commerce/core/price_tracking_utils.h"
#include "components/image_fetcher/core/image_fetcher.h"
#include "components/strings/grit/components_strings.h"
#include "components/vector_icons/vector_icons.h"
#include "content/public/browser/browser_task_traits.h"
#include "content/public/browser/browser_thread.h"
#include "content/public/browser/navigation_details.h"
#include "content/public/browser/navigation_handle.h"
#include "content/public/browser/web_contents.h"
#include "services/metrics/public/cpp/ukm_builders.h"
#include "services/metrics/public/cpp/ukm_recorder.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/views/vector_icons.h"
#include "ui/views/view_class_properties.h"
#include "url/gurl.h"

using SidePanelWebUIViewT_ShoppingInsightsSidePanelUI =
    SidePanelWebUIViewT<ShoppingInsightsSidePanelUI>;
using commerce::metrics::ShoppingContextualFeature;
BEGIN_TEMPLATE_METADATA(SidePanelWebUIViewT_ShoppingInsightsSidePanelUI,
                        SidePanelWebUIViewT)
END_METADATA

namespace commerce {

CommerceUiTabHelper::CommerceUiTabHelper(
    tabs::TabInterface& tab_interface,
    ShoppingService* shopping_service,
    bookmarks::BookmarkModel* model,
    image_fetcher::ImageFetcher* image_fetcher,
    SidePanelRegistry* side_panel_registry)
    : ContentsObservingTabFeature(tab_interface),
      shopping_service_(shopping_service),
      bookmark_model_(model),
      image_fetcher_(image_fetcher),
      side_panel_registry_(side_panel_registry),
      price_insights_label_type_(PriceInsightsIconLabelType::kNone) {
  if (!image_fetcher_) {
    CHECK_IS_TEST();
  }

  if (shopping_service_) {
    shopping_service_->WaitForReady(
        base::BindOnce(&CommerceUiTabHelper::UpdateUiForShoppingServiceReady,
                       weak_ptr_factory_.GetWeakPtr()));
  } else {
    CHECK_IS_TEST();
  }

  auto* tracker = feature_engagement::TrackerFactory::GetForBrowserContext(
      web_contents()->GetBrowserContext());

  price_tracking_controller_ =
      std::make_unique<PriceTrackingPageActionController>(
          GetPageActionControllerNotificationCallback(base::BindRepeating(
              &CommerceUiTabHelper::UpdatePriceTrackingIconView,
              weak_ptr_factory_.GetWeakPtr())),
          shopping_service_, image_fetcher_, tracker);

  product_specifications_controller_ =
      std::make_unique<ProductSpecificationsPageActionController>(
          GetPageActionControllerNotificationCallback(base::BindRepeating(
              &CommerceUiTabHelper::UpdateProductSpecificationsIconView,
              weak_ptr_factory_.GetWeakPtr())),
          shopping_service_);

  discounts_page_action_controller_ =
      std::make_unique<DiscountsPageActionController>(
          GetPageActionControllerNotificationCallback(
              base::BindRepeating(&CommerceUiTabHelper::UpdateDiscountsIconView,
                                  weak_ptr_factory_.GetWeakPtr())),
          shopping_service_);
}

CommerceUiTabHelper::~CommerceUiTabHelper() = default;

// static
void CommerceUiTabHelper::RegisterProfilePrefs(PrefRegistrySimple* registry) {
  registry->RegisterBooleanPref(prefs::kShouldShowPriceTrackFUEBubble, true);
}

void CommerceUiTabHelper::UpdateUiForShoppingServiceReady(
    ShoppingService* service) {
  // This will happen in tests that don't pass CHECK_IS_TEST.
  if (!service) {
    return;
  }

  if (commerce::IsPriceInsightsEligible(service->GetAccountChecker())) {
    UpdatePriceInsightsIconView();
  }
}

void CommerceUiTabHelper::DidFinishNavigation(
    content::NavigationHandle* navigation_handle) {
  if (!navigation_handle->IsInPrimaryMainFrame() ||
      ShouldIgnoreSameUrlNavigation() ||
      IsSameDocumentWithSameCommittedUrl(navigation_handle)) {
    is_initial_navigation_committed_ = true;
    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.
  RecordPriceInsightsIconMetrics(/*from_icon_use=*/false);

  previous_main_frame_url_ = navigation_handle->GetURL();
  product_info_for_page_.reset();
  is_page_action_expansion_computed_for_page_ = false;
  got_discounts_response_for_page_ = false;
  got_insights_response_for_page_ = false;
  page_has_discounts_ = false;
  page_action_to_expand_ = std::nullopt;
  page_action_expanded_ = std::nullopt;
  pending_tracking_state_.reset();
  price_insights_info_.reset();
  icon_use_recorded_for_page_.clear();
  price_insights_label_type_ = PriceInsightsIconLabelType::kNone;

  MakeShoppingInsightsSidePanelUnavailable();

  if (!shopping_service_) {
    return;
  }

  page_action_icon_compute_start_time_ = base::TimeTicks::Now();

  discounts_page_action_controller_->ResetForNewNavigation(
      web_contents()->GetLastCommittedURL());

  // This value should not be a class member variable as it might change within
  // a browser session.
  bool is_price_insights_eligible =
      commerce::IsPriceInsightsEligible(shopping_service_->GetAccountChecker());
  if (is_price_insights_eligible) {
    UpdatePriceInsightsIconView();
  }

  price_tracking_controller_->ResetForNewNavigation(
      web_contents()->GetLastCommittedURL());

  product_specifications_controller_->ResetForNewNavigation(
      web_contents()->GetLastCommittedURL());

  if (is_price_insights_eligible) {
    // Price insights needs product info to get the product cluster title.
    shopping_service_->GetProductInfoForUrl(
        web_contents()->GetLastCommittedURL(),
        base::BindOnce(&CommerceUiTabHelper::HandleProductInfoResponse,
                       weak_ptr_factory_.GetWeakPtr()));
  }

  if (BrowserWindowInterface* bwi = tab().GetBrowserWindowInterface()) {
    auto* product_specifications_entry_point_controller =
        commerce::ProductSpecificationsEntryPointController::From(bwi);
    if (product_specifications_entry_point_controller) {
      product_specifications_entry_point_controller->DidFinishNavigation(
          web_contents());
    }
  }
}

bool CommerceUiTabHelper::ShouldIgnoreSameUrlNavigation() {
  return previous_main_frame_url_ == web_contents()->GetLastCommittedURL() &&
         is_initial_navigation_committed_;
}

bool CommerceUiTabHelper::IsSameDocumentWithSameCommittedUrl(
    content::NavigationHandle* navigation_handle) {
  return previous_main_frame_url_ == web_contents()->GetLastCommittedURL() &&
         navigation_handle->IsSameDocument();
}

void CommerceUiTabHelper::WebContentsDestroyed() {
  // If the tab or browser is closed, try recording whether the price tracking
  // icon was used.
  RecordPriceInsightsIconMetrics(/*from_icon_use=*/false);
}

void CommerceUiTabHelper::TriggerUpdateForIconView() {
  if (!shopping_service_) {
    return;
  }

  if (commerce::IsPriceInsightsEligible(
          shopping_service_->GetAccountChecker())) {
    UpdatePriceInsightsIconView();
  }
  UpdatePriceTrackingIconView();
}

void CommerceUiTabHelper::UpdatePriceInsightsIconView() {
  if (IsPageActionMigrated(PageActionIconType::kPriceInsights)) {
    tab()
        .GetTabFeatures()
        ->commerce_price_insights_page_action_view_controller()
        ->UpdatePageActionIcon(
            ShouldShowPriceInsightsIconView(),
            ShouldExpandPageActionIcon(PageActionIconType::kPriceInsights),
            GetPriceInsightsIconLabelTypeForPage());
    return;
  }

  UpdatePageActionIconView(PageActionIconType::kPriceInsights);
}

void CommerceUiTabHelper::SetImageFetcherForTesting(
    image_fetcher::ImageFetcher* image_fetcher) {
  CHECK_IS_TEST();
  image_fetcher_ = image_fetcher;
}

bool CommerceUiTabHelper::ShouldShowDiscountsIconView() {
  return discounts_page_action_controller_->ShouldShowForNavigation().value_or(
      false);
}

bool CommerceUiTabHelper::ShouldShowPriceTrackingIconView() {
  return price_tracking_controller_->ShouldShowForNavigation().value_or(false);
}

bool CommerceUiTabHelper::ShouldShowPriceInsightsIconView() {
  return shopping_service_ &&
         commerce::IsPriceInsightsEligible(
             shopping_service_->GetAccountChecker()) &&
         price_insights_info_.has_value();
}

bool CommerceUiTabHelper::ShouldShowProductSpecificationsIconView() {
  return product_specifications_controller_->ShouldShowForNavigation().value_or(
      false);
}

void CommerceUiTabHelper::HandleProductInfoResponse(
    const GURL& url,
    const std::optional<const ProductInfo>& info) {
  if (url != web_contents()->GetLastCommittedURL() || !info.has_value()) {
    // Price insights is depended on ProductInfo, it's impossible to get
    // insights without it.
    got_insights_response_for_page_ = true;
    MaybeComputePageActionToExpand();
    return;
  }

  product_info_for_page_ = info;

  if (commerce::IsPriceInsightsEligible(
          shopping_service_->GetAccountChecker())) {
    if (!info->product_cluster_title.empty()) {
      shopping_service_->GetPriceInsightsInfoForUrl(
          url,
          base::BindOnce(&CommerceUiTabHelper::HandlePriceInsightsInfoResponse,
                         weak_ptr_factory_.GetWeakPtr()));
    } else {
      // If we were blocked because of the title, consider it a response of
      // false.
      got_insights_response_for_page_ = true;
    }
  }
}

void CommerceUiTabHelper::HandlePriceInsightsInfoResponse(
    const GURL& url,
    const std::optional<PriceInsightsInfo>& info) {
  got_insights_response_for_page_ = true;

  if (url != web_contents()->GetLastCommittedURL() || !info.has_value()) {
    MaybeComputePageActionToExpand();
    return;
  }

  price_insights_info_.emplace(info.value());
  MaybeComputePageActionToExpand();
  MakeShoppingInsightsSidePanelAvailable();
  TriggerUpdateForIconView();
}

void CommerceUiTabHelper::MaybeComputePageActionToExpand() {
  if (!shopping_service_) {
    return;
  }

  // Make sure we have responses from all the relevant features first.
  if (!discounts_page_action_controller_->ShouldShowForNavigation()
           .has_value()) {
    return;
  }

    if (commerce::IsPriceInsightsEligible(
            shopping_service_->GetAccountChecker()) &&
        !got_insights_response_for_page_) {
      return;
    }

  if (!price_tracking_controller_->ShouldShowForNavigation().has_value()) {
    return;
  }

  if (!product_specifications_controller_->ShouldShowForNavigation()
           .has_value()) {
    return;
  }

  if (is_page_action_expansion_computed_for_page_) {
    return;
  }

  ComputePageActionToExpand();

  is_page_action_expansion_computed_for_page_ = true;

  if (ShouldShowPriceInsightsIconView()) {
    base::UmaHistogramEnumeration("Commerce.PriceInsights.OmniboxIconShown",
                                  price_insights_label_type_);
  }

  UpdateProductSpecificationsIconView();
  UpdateDiscountsIconView();
  UpdatePriceTrackingIconView();
  UpdatePriceInsightsIconView();

  if (ShouldShowDiscountsIconView()) {
    commerce::metrics::DiscountsMetricCollector::
        RecordDiscountsPageActionIconExpandState(
            IsPageActionIconExpanded(PageActionIconType::kDiscounts),
            GetDiscounts());
  }
}

void CommerceUiTabHelper::SetPriceTrackingState(
    bool enable,
    bool is_new_bookmark,
    base::OnceCallback<void(bool)> callback) {
  const bookmarks::BookmarkNode* node =
      bookmark_model_->GetMostRecentlyAddedUserNodeForURL(
          web_contents()->GetLastCommittedURL());

  base::OnceCallback<void(bool)> wrapped_callback = base::BindOnce(
      [](base::WeakPtr<CommerceUiTabHelper> helper,
         base::OnceCallback<void(bool)> callback, bool is_tracked,
         bool success) {
        if (helper) {
          helper->pending_tracking_state_.reset();
        }

        std::move(callback).Run(success);
      },
      weak_ptr_factory_.GetWeakPtr(), std::move(callback), enable);

  pending_tracking_state_.emplace(enable);

  if (node) {
    commerce::SetPriceTrackingStateForBookmark(
        shopping_service_.get(), bookmark_model_.get(), node, enable,
        std::move(wrapped_callback), enable && is_new_bookmark);
  } else {
    DCHECK(!enable);
    std::optional<commerce::ProductInfo> info =
        shopping_service_->GetAvailableProductInfoForUrl(
            web_contents()->GetLastCommittedURL());
    if (info.has_value()) {
      commerce::SetPriceTrackingStateForClusterId(
          shopping_service_.get(), bookmark_model_,
          info->product_cluster_id.value(), enable,
          std::move(wrapped_callback));
    }
  }
}

void CommerceUiTabHelper::OnPriceInsightsIconClicked() {
  auto* side_panel_ui = GetSidePanelUI();
  DCHECK(side_panel_ui &&
         side_panel_registry_->GetEntryForKey(
             SidePanelEntry::Key(SidePanelEntry::Id::kShoppingInsights)));

  if (side_panel_ui->IsSidePanelShowing() &&
      side_panel_ui->GetCurrentEntryId() ==
          SidePanelEntry::Id::kShoppingInsights) {
    side_panel_ui->Close();
  } else {
    side_panel_ui->Show(SidePanelEntryId::kShoppingInsights);
    if (price_insights_info_.has_value()) {
      base::UmaHistogramEnumeration("Commerce.PriceInsights.OmniboxIconClicked",
                                    price_insights_label_type_);
      base::UmaHistogramBoolean(
          "Commerce.PriceInsights.SidePanelOpenWithMultipleCatalogs",
          price_insights_info_->has_multiple_catalogs);
      commerce::metrics::RecordShoppingActionUKM(
          web_contents()->GetPrimaryMainFrame()->GetPageUkmSourceId(),
          commerce::metrics::ShoppingAction::kPriceInsightsOpened);
    }
  }
  RecordPriceInsightsIconMetrics(true);
}

const gfx::Image& CommerceUiTabHelper::GetProductImage() {
  return price_tracking_controller_->GetLastFetchedImage();
}

const GURL& CommerceUiTabHelper::GetProductImageURL() {
  return price_tracking_controller_->GetLastFetchedImageUrl();
}

bool CommerceUiTabHelper::IsPriceTracking() {
  return pending_tracking_state_.value_or(
      price_tracking_controller_->IsPriceTrackingCurrentProduct());
}

bool CommerceUiTabHelper::IsInRecommendedSet() {
  return product_specifications_controller_->IsInRecommendedSet();
}

GURL CommerceUiTabHelper::GetComparisonTableURL() {
  return product_specifications_controller_->GetComparisonTableURL();
}

void CommerceUiTabHelper::OnOpenComparePageClicked() {
  auto* tab_strip_model = tab().GetBrowserWindowInterface()->GetTabStripModel();
  GURL comparison_table_url = GetComparisonTableURL();

  for (int index = 0; index < tab_strip_model->count(); index++) {
    auto* tab_web_contents = tab_strip_model->GetWebContentsAt(index);
    if (tab_web_contents->GetLastCommittedURL() == comparison_table_url) {
      tab_strip_model->ActivateTabAt(index);
      return;
    }
  }

  int active_index = tab_strip_model->active_index();
  chrome::AddTabAt(
      tab().GetBrowserWindowInterface()->GetBrowserForMigrationOnly(),
      comparison_table_url, active_index + 1, true,
      tab_strip_model->GetTabGroupForTab(active_index));
}

std::u16string CommerceUiTabHelper::GetComparisonSetName() {
  return product_specifications_controller_->GetComparisonSetName();
}

std::u16string CommerceUiTabHelper::GetProductSpecificationsLabel(
    bool is_added) {
  return product_specifications_controller_->GetProductSpecificationsLabel(
      is_added);
}

const std::vector<DiscountInfo>& CommerceUiTabHelper::GetDiscounts() {
  return discounts_page_action_controller_->GetDiscounts();
}

void CommerceUiTabHelper::UpdatePriceTrackingIconView() {
  UpdatePageActionIconView(PageActionIconType::kPriceTracking);
}

void CommerceUiTabHelper::UpdateProductSpecificationsIconView() {
  UpdatePageActionIconView(PageActionIconType::kProductSpecifications);
}

void CommerceUiTabHelper::MakeShoppingInsightsSidePanelAvailable() {
  auto entry = std::make_unique<SidePanelEntry>(
      SidePanelEntry::Key(SidePanelEntry::Id::kShoppingInsights),
      base::BindRepeating(&CommerceUiTabHelper::CreateShoppingInsightsWebView,
                          base::Unretained(this)),
      SidePanelEntry::kSidePanelDefaultContentWidth);
  side_panel_registry_->Register(std::move(entry));
}

void CommerceUiTabHelper::MakeShoppingInsightsSidePanelUnavailable() {
  auto* side_panel_ui = GetSidePanelUI();
  if (side_panel_ui && side_panel_ui->IsSidePanelShowing() &&
      side_panel_ui->GetCurrentEntryId() ==
          SidePanelEntry::Id::kShoppingInsights) {
    side_panel_ui->Close();
    base::RecordAction(base::UserMetricsAction(
        "Commerce.PriceInsights.NavigationClosedSidePanel"));
  }

  side_panel_registry_->Deregister(
      SidePanelEntry::Key(SidePanelEntry::Id::kShoppingInsights));
}

std::unique_ptr<views::View> CommerceUiTabHelper::CreateShoppingInsightsWebView(
    SidePanelEntryScope& scope) {
  auto shopping_insights_web_view =
      std::make_unique<SidePanelWebUIViewT<ShoppingInsightsSidePanelUI>>(
          scope, base::RepeatingClosure(), base::RepeatingClosure(),
          std::make_unique<WebUIContentsWrapperT<ShoppingInsightsSidePanelUI>>(
              GURL(kChromeUIShoppingInsightsSidePanelUrl),
              Profile::FromBrowserContext(web_contents()->GetBrowserContext()),
              IDS_SHOPPING_INSIGHTS_SIDE_PANEL_TITLE,
              /*esc_closes_ui=*/false));
  // Call ShowUI() to make the UI ready, this doesn't really open/switch the
  // side panel.
  shopping_insights_web_view->ShowUI();

  return shopping_insights_web_view;
}

SidePanelUI* CommerceUiTabHelper::GetSidePanelUI() {
  if (BrowserWindowInterface* bwi = tab().GetBrowserWindowInterface()) {
    return bwi->GetFeatures().side_panel_ui();
  }

  return nullptr;
}

const std::optional<bool>&
CommerceUiTabHelper::GetPendingTrackingStateForTesting() {
  return pending_tracking_state_;
}

const std::optional<PriceInsightsInfo>&
CommerceUiTabHelper::GetPriceInsightsInfo() {
  return price_insights_info_;
}

void CommerceUiTabHelper::UpdateDiscountsIconView() {
  UpdatePageActionIconView(PageActionIconType::kDiscounts);
}

void CommerceUiTabHelper::ComputePageActionToExpand() {
  if (!page_action_icon_compute_start_time_.is_null()) {
    base::UmaHistogramTimes(
        "Commerce.IconComputationTime",
        base::TimeTicks::Now() - page_action_icon_compute_start_time_);
    page_action_to_expand_ = std::nullopt;
  }

  page_action_icon_compute_start_time_ = base::TimeTicks();

  if (!web_contents() || !web_contents()->GetBrowserContext()) {
    page_action_to_expand_ = std::nullopt;
    return;
  }

  auto* tracker = feature_engagement::TrackerFactory::GetForBrowserContext(
      web_contents()->GetBrowserContext());

  // TODO(b:301440117): Splitting the triggering logic for each icon into
  //                    delegates would make this much easier to test.
  if (discounts_page_action_controller_->WantsExpandedUi()) {
    page_action_to_expand_ = PageActionIconType::kDiscounts;
    MaybeRecordShoppingInformationUKM(PageActionIconType::kDiscounts);
    return;
  }

  if (ShouldShowProductSpecificationsIconView()) {
    page_action_to_expand_ = PageActionIconType::kProductSpecifications;
    return;
  }

  // Prioritize the price insights icon.
  if (ShouldShowPriceInsightsIconView()) {
    PriceInsightsIconLabelType label_type =
        GetPriceInsightsIconLabelTypeForPage();
    bool icon_has_label = label_type != PriceInsightsIconLabelType::kNone;

    if (icon_has_label && tracker &&
        tracker->ShouldTriggerHelpUI(
            feature_engagement::kIPHPriceInsightsPageActionIconLabelFeature)) {
      // Note that `Dismiss()` in these cases does not dismiss the UI. It's
      // telling the FE backend that the promo is done so that other promos can
      // run. Showing the label should not block other promos from displaying.
      tracker->Dismissed(
          feature_engagement::kIPHPriceInsightsPageActionIconLabelFeature);
      page_action_to_expand_ = PageActionIconType::kPriceInsights;
      MaybeRecordShoppingInformationUKM(PageActionIconType::kPriceInsights);
      price_insights_label_type_ = label_type;
      return;
    }
  }

  if (price_tracking_controller_->WantsExpandedUi()) {
    page_action_to_expand_ = PageActionIconType::kPriceTracking;
    MaybeRecordShoppingInformationUKM(PageActionIconType::kPriceTracking);
    return;
  }
  MaybeRecordShoppingInformationUKM(std::nullopt);
}

PriceInsightsIconLabelType
CommerceUiTabHelper::GetPriceInsightsIconLabelTypeForPage() {
  auto& price_insights_info = GetPriceInsightsInfo();

  if (!price_insights_info.has_value() ||
      !price_insights_info->typical_low_price_micros.has_value() ||
      !price_insights_info->typical_high_price_micros.has_value() ||
      price_insights_info->catalog_history_prices.empty()) {
    return PriceInsightsIconLabelType::kNone;
  } else if (price_insights_info->price_bucket ==
             commerce::PriceBucket::kLowPrice) {
    return PriceInsightsIconLabelType::kPriceIsLow;
  } else if (price_insights_info->price_bucket ==
                 commerce::PriceBucket::kHighPrice &&
             commerce::kPriceInsightsChipLabelExpandOnHighPrice.Get()) {
    return PriceInsightsIconLabelType::kPriceIsHigh;
  } else {
    return PriceInsightsIconLabelType::kNone;
  }
}

bool CommerceUiTabHelper::ShouldExpandPageActionIcon(PageActionIconType type) {
  // Only allow the requesting icon to expand once. This prevents the icon from
  // expanding multiple times per page load.
  if (page_action_to_expand_.has_value() &&
      type == page_action_to_expand_.value()) {
    page_action_expanded_ = page_action_to_expand_.value();
    page_action_to_expand_ = std::nullopt;
    return true;
  }
  return false;
}

bool CommerceUiTabHelper::IsPageActionIconExpanded(PageActionIconType type) {
  return page_action_expanded_.has_value() &&
         type == page_action_expanded_.value();
}

void CommerceUiTabHelper::OnPriceTrackingIconClicked() {
  price_tracking_controller_->OnIconClicked();
}

void CommerceUiTabHelper::OnProductSpecificationsIconClicked() {
  product_specifications_controller_->OnIconClicked();
}

void CommerceUiTabHelper::OnDiscountsCouponCodeCopied() {
  discounts_page_action_controller_->CouponCodeCopied();
}

bool CommerceUiTabHelper::IsDiscountsCouponCodeCopied() {
  return discounts_page_action_controller_->IsCouponCodeCopied();
}

bool CommerceUiTabHelper::ShouldAutoShowDiscountsBubble(uint64_t discount_id,
                                                        bool is_merchant_wide) {
  return discounts_page_action_controller_->ShouldAutoShowBubble(
      discount_id, is_merchant_wide);
}

void CommerceUiTabHelper::DiscountsBubbleShown(uint64_t discount_id) {
  discounts_page_action_controller_->DiscountsBubbleShown(discount_id);
}

void CommerceUiTabHelper::RecordIconMetrics(PageActionIconType page_action,
                                            bool from_icon_use) {
  if (icon_use_recorded_for_page_.contains(page_action)) {
    return;
  }
  icon_use_recorded_for_page_.insert(page_action);

  std::string histogram_name;
  switch (page_action) {
    case PageActionIconType::kPriceInsights:
      histogram_name = "Commerce.PriceInsights.IconInteractionState";
      break;
    default:
      return;
  }

  bool expanded = page_action_expanded_.has_value() &&
                  page_action_expanded_.value() == page_action;

  if (from_icon_use) {
    if (expanded) {
      base::UmaHistogramEnumeration(
          histogram_name, PageActionIconInteractionState::kClickedExpanded);
    } else {
      base::UmaHistogramEnumeration(histogram_name,
                                    PageActionIconInteractionState::kClicked);
    }
  } else {
    if (expanded) {
      base::UmaHistogramEnumeration(
          histogram_name, PageActionIconInteractionState::kNotClickedExpanded);
    } else {
      base::UmaHistogramEnumeration(
          histogram_name, PageActionIconInteractionState::kNotClicked);
    }
  }
}

void CommerceUiTabHelper::RecordPriceInsightsIconMetrics(bool from_icon_use) {
  if (ShouldShowPriceInsightsIconView()) {
    RecordIconMetrics(PageActionIconType::kPriceInsights, from_icon_use);
  }
}

void CommerceUiTabHelper::MaybeRecordShoppingInformationUKM(
    std::optional<PageActionIconType> page_action_type) {
  // This is our current definition of shopping content.
  if (!product_info_for_page_.has_value()) {
    return;
  }

  auto ukm_builder = ukm::builders::Shopping_ShoppingInformation(
      web_contents()->GetPrimaryMainFrame()->GetPageUkmSourceId());

  if (page_action_type.has_value()) {
    int64_t promoted_feature = 0;
    if (page_action_type == PageActionIconType::kDiscounts) {
      promoted_feature =
          static_cast<int64_t>(ShoppingContextualFeature::kDiscounts);
    } else if (page_action_type == PageActionIconType::kPriceInsights) {
      promoted_feature =
          static_cast<int64_t>(ShoppingContextualFeature::kPriceInsights);
    } else if (page_action_type == PageActionIconType::kPriceTracking) {
      promoted_feature =
          static_cast<int64_t>(ShoppingContextualFeature::kPriceTracking);
    } else {
      NOTREACHED();
    }
    ukm_builder.SetPromotedFeature(promoted_feature);
  }

  ukm_builder.SetHasPriceInsights(price_insights_info_.has_value())
      .SetHasDiscount(
          discounts_page_action_controller_->ShouldShowForNavigation().value_or(
              false))
      .SetIsPriceTrackable(true)
      .SetIsShoppingContent(true)
      .Record(ukm::UkmRecorder::Get());
}

PriceTrackingPageActionController*
CommerceUiTabHelper::GetPriceTrackingControllerForTesting() {
  return price_tracking_controller_.get();
}

void CommerceUiTabHelper::OnPageActionControllerNotification(
    base::RepeatingClosure page_action_icon_update_callback) {
  MaybeComputePageActionToExpand();
  page_action_icon_update_callback.Run();
}

base::RepeatingClosure
CommerceUiTabHelper::GetPageActionControllerNotificationCallback(
    base::RepeatingClosure page_action_icon_update_callback) {
  return base::BindRepeating(
      &CommerceUiTabHelper::OnPageActionControllerNotification,
      weak_ptr_factory_.GetWeakPtr(),
      std::move(page_action_icon_update_callback));
}

void CommerceUiTabHelper::SetPriceTrackingControllerForTesting(
    std::unique_ptr<PriceTrackingPageActionController> controller) {
  price_tracking_controller_.reset(controller.release());
}

void CommerceUiTabHelper::UpdatePageActionIconView(PageActionIconType type) {
  BrowserWindowInterface* bwi = tab().GetBrowserWindowInterface();
  if (!bwi) {
    return;
  }

  bwi->GetBrowserForMigrationOnly()->window()->UpdatePageActionIcon(type);
}

}  // namespace commerce