File: performance_intervention_bubble.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 (161 lines) | stat: -rw-r--r-- 7,027 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
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
// Copyright 2024 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/performance_controls/performance_intervention_bubble.h"

#include <memory>
#include <string>
#include <utility>

#include "base/functional/bind.h"
#include "base/functional/callback_helpers.h"
#include "base/notreached.h"
#include "chrome/browser/ui/browser.h"
#include "chrome/browser/ui/performance_controls/performance_controls_metrics.h"
#include "chrome/browser/ui/performance_controls/performance_intervention_bubble_delegate.h"
#include "chrome/browser/ui/performance_controls/performance_intervention_bubble_observer.h"
#include "chrome/browser/ui/performance_controls/performance_intervention_button_controller.h"
#include "chrome/browser/ui/performance_controls/tab_list_model.h"
#include "chrome/browser/ui/views/performance_controls/performance_intervention_button.h"
#include "chrome/browser/ui/views/performance_controls/tab_list_view.h"
#include "chrome/grit/branded_strings.h"
#include "chrome/grit/generated_resources.h"
#include "components/performance_manager/public/features.h"
#include "components/strings/grit/components_strings.h"
#include "ui/base/interaction/element_identifier.h"
#include "ui/base/l10n/l10n_util.h"
#include "ui/base/models/dialog_model.h"
#include "ui/base/models/dialog_model_field.h"
#include "ui/views/bubble/bubble_dialog_model_host.h"
#include "ui/views/view_class_properties.h"
#include "ui/views/widget/widget.h"

namespace {
const char kViewClassName[] = "PerformanceInterventionBubble";
}

DEFINE_CLASS_ELEMENT_IDENTIFIER_VALUE(PerformanceInterventionBubble,
                                      kPerformanceInterventionDialogBody);
DEFINE_CLASS_ELEMENT_IDENTIFIER_VALUE(
    PerformanceInterventionBubble,
    kPerformanceInterventionDialogDismissButton);
DEFINE_CLASS_ELEMENT_IDENTIFIER_VALUE(
    PerformanceInterventionBubble,
    kPerformanceInterventionDialogDeactivateButton);
DEFINE_CLASS_ELEMENT_IDENTIFIER_VALUE(PerformanceInterventionBubble,
                                      kPerformanceInterventionTabList);

// static
views::BubbleDialogModelHost* PerformanceInterventionBubble::CreateBubble(
    Browser* browser,
    PerformanceInterventionButton* anchor_view,
    PerformanceInterventionButtonController* button_controller) {
  auto tab_list_model_unique =
      std::make_unique<TabListModel>(button_controller->actionable_cpu_tabs());
  TabListModel* const tab_list_model = tab_list_model_unique.get();

  RecordSuggestedTabShownCount(tab_list_model->count());
  auto bubble_delegate =
      std::make_unique<PerformanceInterventionBubbleDelegate>(
          browser, std::move(tab_list_model_unique), button_controller);

  const DialogStrings strings = GetStrings(tab_list_model->count());
  PerformanceInterventionBubbleDelegate* const delegate = bubble_delegate.get();
  auto dialog_model =
      ui::DialogModel::Builder(std::move(bubble_delegate))
          .SetInternalName(kViewClassName)
          .SetTitle(strings.title)
          .SetIsAlertDialog()
          .SetCloseActionCallback(base::BindOnce(
              &PerformanceInterventionBubbleDelegate::OnBubbleClosed,
              base::Unretained(delegate)))
          .AddParagraph(ui::DialogModelLabel(strings.body_text)
                            .set_is_secondary()
                            .set_allow_character_break(),
                        std::u16string(), kPerformanceInterventionDialogBody)
          .AddCustomField(
              std::make_unique<views::BubbleDialogModelHost::CustomView>(
                  std::make_unique<TabListView>(tab_list_model),
                  views::BubbleDialogModelHost::FieldType::kMenuItem),
              kPerformanceInterventionTabList)
          .AddOkButton(
              base::BindOnce(&PerformanceInterventionBubbleDelegate::
                                 OnDeactivateButtonClicked,
                             base::Unretained(delegate)),
              ui::DialogModel::Button::Params()
                  .SetLabel(strings.deactivate_tabs_button)
                  .SetId(kPerformanceInterventionDialogDeactivateButton))
          .AddCancelButton(
              base::BindOnce(&PerformanceInterventionBubbleDelegate::
                                 OnDismissButtonClicked,
                             base::Unretained(delegate)),
              ui::DialogModel::Button::Params()
                  .SetLabel(l10n_util::GetStringUTF16(
                      IDS_PERFORMANCE_INTERVENTION_DISMISS_BUTTON))
                  .SetId(kPerformanceInterventionDialogDismissButton))
          .Build();

  auto bubble_unique = std::make_unique<views::BubbleDialogModelHost>(
      std::move(dialog_model), anchor_view, views::BubbleBorder::TOP_RIGHT);
  auto* const bubble = bubble_unique.get();

  views::Widget* widget =
      views::BubbleDialogDelegate::CreateBubble(std::move(bubble_unique));
  widget->widget_delegate()->SetEnableArrowKeyTraversal(true);
  widget->Show();
  button_controller->OnBubbleShown();

  return bubble;
}

// static
void PerformanceInterventionBubble::CloseBubble(
    views::BubbleDialogModelHost* bubble_dialog) {
  CHECK(bubble_dialog);
  bubble_dialog->Close();
}

DialogStrings PerformanceInterventionBubble::GetStrings(int count) {
  const std::u16string body_text = l10n_util::GetStringUTF16(
      count > 1 ? IDS_PERFORMANCE_INTERVENTION_DIALOG_BODY_V1
                : IDS_PERFORMANCE_INTERVENTION_DIALOG_BODY_SINGULAR_V1);

  if (base::FeatureList::IsEnabled(
          performance_manager::features::
              kPerformanceInterventionNotificationStringImprovements)) {
    const int string_version =
        performance_manager::features::kNotificationStringVersion.Get();
    if (string_version == 1) {
      return {
          l10n_util::GetPluralStringFUTF16(
              IDS_PERFORMANCE_INTERVENTION_DIALOG_TITLE_UPDATED_V1, count),
          body_text,
          l10n_util::GetPluralStringFUTF16(
              IDS_PERFORMANCE_INTERVENTION_DEACTIVATE_TABS_BUTTON_UPDATED_V1,
              count)};
    } else if (string_version == 2) {
      return {
          l10n_util::GetPluralStringFUTF16(
              IDS_PERFORMANCE_INTERVENTION_DIALOG_TITLE_UPDATED_V2, count),
          body_text,
          l10n_util::GetPluralStringFUTF16(
              IDS_PERFORMANCE_INTERVENTION_DEACTIVATE_TABS_BUTTON_UPDATED_V2,
              count)};
    } else {
      return {
          l10n_util::GetPluralStringFUTF16(
              IDS_PERFORMANCE_INTERVENTION_DIALOG_TITLE_UPDATED_V3, count),
          body_text,
          l10n_util::GetPluralStringFUTF16(
              IDS_PERFORMANCE_INTERVENTION_DEACTIVATE_TABS_BUTTON_UPDATED_V2,
              count)};
    }
  } else {
    return {
        l10n_util::GetStringUTF16(IDS_PERFORMANCE_INTERVENTION_DIALOG_TITLE_V1),
        body_text,
        l10n_util::GetStringUTF16(
            IDS_PERFORMANCE_INTERVENTION_DEACTIVATE_TABS_BUTTON_V1)};
  }
}