File: tab_event_tracker_impl.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 (128 lines) | stat: -rw-r--r-- 4,609 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
// 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/visited_url_ranking/internal/url_grouping/tab_event_tracker_impl.h"

#include "components/visited_url_ranking/public/features.h"

namespace visited_url_ranking {

namespace {

const base::TimeDelta kSelectionTimeWindow = base::Minutes(10);

}  // namespace

const char TabEventTrackerImpl::kAndroidNativeNewTabPageURL[] =
    "chrome-native://newtab/";

TabEventTrackerImpl::TabEventTrackerImpl(
    OnNewEventCallback trigger_event_callback,
    OnNewEventCallback invalidate_cache_callback)
    : trigger_event_callback_(trigger_event_callback),
      invalidate_cache_callback_(invalidate_cache_callback),
      tab_switcher_trigger_only_(
          features::kGroupSuggestionEnableTabSwitcherOnly.Get()),
      trigger_on_navigation_(
          features::kGroupSuggestionEnableRecentlyOpened.Get() ||
          features::kGroupSuggestionEnableSameOrigin.Get() ||
          features::kGroupSuggestionTriggerCalculationOnPageLoad.Get()) {}
TabEventTrackerImpl::~TabEventTrackerImpl() = default;

TabEventTrackerImpl::TabSelection::TabSelection() = default;
TabEventTrackerImpl::TabSelection::TabSelection(
    int tab_id,
    TabSelectionType tab_selection_type,
    base::Time time)
    : tab_id(tab_id), tab_selection_type(tab_selection_type), time(time) {}
TabEventTrackerImpl::TabSelection::~TabSelection() = default;

void TabEventTrackerImpl::DidAddTab(int tab_id, int tab_launch_type) {
  // The cache could have suggestions without the opened tab, which may need
  // to be included.
  invalidate_cache_callback_.Run();
  if (!tab_switcher_trigger_only_) {
    trigger_event_callback_.Run();
  }
}

void TabEventTrackerImpl::DidSelectTab(int tab_id,
                                       const GURL& url,
                                       TabSelectionType tab_selection_type,
                                       int last_tab_id) {
  current_selection_ =
      TabSelection(tab_id, tab_selection_type, base::Time::Now());
  if ((tab_selection_type != TabSelectionType::kFromUser &&
       tab_selection_type != TabSelectionType::kFromOmnibox) ||
      last_tab_id == tab_id || url.spec() == kAndroidNativeNewTabPageURL) {
    return;
  }
  current_selection_->committed = true;
  tab_id_selection_map_[tab_id].emplace_back(*current_selection_);
  if (!tab_switcher_trigger_only_) {
    trigger_event_callback_.Run();
  }
}

void TabEventTrackerImpl::WillCloseTab(int tab_id) {
  closing_tabs_.insert(tab_id);
}

void TabEventTrackerImpl::TabClosureUndone(int tab_id) {
  closing_tabs_.erase(tab_id);
  // The cache could have suggestions without the reopened tab, which may need
  // to be included.
  invalidate_cache_callback_.Run();
}

void TabEventTrackerImpl::TabClosureCommitted(int tab_id) {
  closing_tabs_.erase(tab_id);
  tab_id_selection_map_.erase(tab_id);
}

void TabEventTrackerImpl::DidMoveTab(int tab_id,
                                     int new_index,
                                     int current_index) {}

void TabEventTrackerImpl::OnDidFinishNavigation(
    int tab_id,
    ui::PageTransition page_transition) {
  if (!ui::PageTransitionCoreTypeIs(page_transition,
                                    ui::PAGE_TRANSITION_LINK) &&
      !ui::PageTransitionCoreTypeIs(page_transition,
                                    ui::PAGE_TRANSITION_TYPED) &&
      !ui::PageTransitionCoreTypeIs(page_transition,
                                    ui::PAGE_TRANSITION_AUTO_BOOKMARK) &&
      (page_transition & ui::PAGE_TRANSITION_FROM_ADDRESS_BAR) == 0) {
    return;
  }
  DCHECK(current_selection_.has_value());
  if (current_selection_->tab_id == tab_id && !current_selection_->committed) {
    current_selection_->committed = true;
    tab_id_selection_map_[tab_id].emplace_back(*current_selection_);
  }
  if (!tab_switcher_trigger_only_ && trigger_on_navigation_) {
    trigger_event_callback_.Run();
  }
}

void TabEventTrackerImpl::DidEnterTabSwitcher() {
  if (tab_switcher_trigger_only_) {
    trigger_event_callback_.Run();
  }
}

int TabEventTrackerImpl::GetSelectedCount(int tab_id) const {
  if (!closing_tabs_.contains(tab_id) &&
      tab_id_selection_map_.contains(tab_id)) {
    std::vector<TabSelection> selection_list = tab_id_selection_map_.at(tab_id);
    std::erase_if(selection_list, [&](TabSelection selection) {
      return base::Time::Now() - selection.time > kSelectionTimeWindow;
    });
    return selection_list.size();
  }
  return 0;
}

}  // namespace visited_url_ranking