File: trigger.cc

package info (click to toggle)
chromium 138.0.7204.183-1~deb12u1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm-proposed-updates
  • size: 6,080,960 kB
  • sloc: cpp: 34,937,079; ansic: 7,176,967; javascript: 4,110,704; python: 1,419,954; asm: 946,768; xml: 739,971; pascal: 187,324; sh: 89,623; perl: 88,663; objc: 79,944; sql: 50,304; cs: 41,786; fortran: 24,137; makefile: 21,811; php: 13,980; tcl: 13,166; yacc: 8,925; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (109 lines) | stat: -rw-r--r-- 3,909 bytes parent folder | download | duplicates (5)
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
// 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/tabs/organization/trigger.h"

#include "base/time/default_tick_clock.h"
#include "chrome/browser/enterprise/browser_management/management_service_factory.h"
#include "chrome/browser/ui/browser.h"
#include "chrome/browser/ui/tabs/organization/tab_data.h"
#include "chrome/browser/ui/tabs/organization/tab_organization_service.h"
#include "chrome/browser/ui/tabs/organization/tab_organization_service_factory.h"
#include "chrome/browser/ui/tabs/organization/trigger_policies.h"
#include "chrome/browser/ui/tabs/tab_group_model.h"
#include "chrome/browser/ui/tabs/tab_strip_model.h"
#include "chrome/browser/ui/ui_features.h"
#include "tab_sensitivity_cache.h"

namespace {

// Just counts the number of tabs in the browser.
float ScoringFunction(float sensitivity_threshold, TabStripModel* const model) {
  // Feature may be disabled in tests, in which case GetForProfile will CHECK.
  const TabOrganizationService* const service =
      base::FeatureList::IsEnabled(features::kTabOrganization)
          ? TabOrganizationServiceFactory::GetForProfile(model->profile())
          : nullptr;

  int num_eligible_tabs = 0;
  for (int i = 0; i < model->count(); i++) {
    const TabData tab = TabData(model->GetTabAtIndex(i));
    if (service) {
      const std::optional<float> score =
          service->tab_sensitivity_cache()->GetScore(tab.original_url());
      if (score && score.value() > sensitivity_threshold) {
        continue;
      }
    }
    if (!tab.IsValidForOrganizing()) {
      continue;
    }
    num_eligible_tabs++;
  }
  return num_eligible_tabs;
}
}  // namespace

TabOrganizationTrigger::TabOrganizationTrigger(
    TriggerScoringFunction scoring_function,
    float score_threshold,
    std::unique_ptr<TriggerPolicy> policy)
    : scoring_function_(scoring_function),
      score_threshold_(score_threshold),
      policy_(std::move(policy)) {}

TabOrganizationTrigger::~TabOrganizationTrigger() = default;

bool TabOrganizationTrigger::ShouldTrigger(
    TabStripModel* const tab_strip_model) const {
  const float score = scoring_function_.Run(tab_strip_model);
  if (score < score_threshold_) {
    return false;
  }

  return policy_->ShouldTrigger(score);
}

TriggerScoringFunction GetTriggerScoringFunction() {
  return base::BindRepeating(&ScoringFunction, GetSensitivityThreshold());
}

float GetTriggerScoreThreshold() {
  return features::kTabOrganizationTriggerThreshold.Get();
}

float GetSensitivityThreshold() {
  return features::kTabOrganizationTriggerSensitivityThreshold.Get();
}

std::unique_ptr<TriggerPolicy> GetTriggerPolicy(
    BackoffLevelProvider* backoff_level_provider,
    Profile* profile) {
  if (features::KTabOrganizationTriggerDemoMode.Get()) {
    return std::make_unique<DemoTriggerPolicy>();
  }

  auto* management_service =
      policy::ManagementServiceFactory::GetForProfile(profile);
  if (!base::FeatureList::IsEnabled(
          features::kTabOrganizationEnableNudgeForEnterprise) &&
      policy::ManagementServiceFactory::GetForPlatform()->IsManaged() &&
      management_service && management_service->IsManaged()) {
    return std::make_unique<NeverTriggerPolicy>();
  }

  return std::make_unique<TargetFrequencyTriggerPolicy>(
      std::make_unique<UsageTickClock>(base::DefaultTickClock::GetInstance()),
      features::kTabOrganizationTriggerPeriod.Get(),
      features::kTabOrganizationTriggerBackoffBase.Get(),
      std::move(backoff_level_provider));
}

std::unique_ptr<TabOrganizationTrigger> MakeTrigger(
    BackoffLevelProvider* backoff_level_provider,
    Profile* profile) {
  return std::make_unique<TabOrganizationTrigger>(
      GetTriggerScoringFunction(), GetTriggerScoreThreshold(),
      GetTriggerPolicy(backoff_level_provider, profile));
}