File: storage_pressure_bubble_view.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 (142 lines) | stat: -rw-r--r-- 5,232 bytes parent folder | download | duplicates (4)
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
// Copyright 2019 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/storage/storage_pressure_bubble_view.h"

#include "base/auto_reset.h"
#include "base/feature_list.h"
#include "base/metrics/histogram_functions.h"
#include "base/metrics/histogram_macros.h"
#include "chrome/browser/ui/browser_list.h"
#include "chrome/browser/ui/browser_navigator.h"
#include "chrome/browser/ui/views/chrome_layout_provider.h"
#include "chrome/browser/ui/views/frame/app_menu_button.h"
#include "chrome/browser/ui/views/frame/browser_view.h"
#include "chrome/browser/ui/views/frame/toolbar_button_provider.h"
#include "chrome/grit/generated_resources.h"
#include "components/url_formatter/url_formatter.h"
#include "content/public/common/content_features.h"
#include "ui/base/l10n/l10n_util.h"
#include "ui/base/metadata/metadata_impl_macros.h"
#include "ui/base/mojom/dialog_button.mojom.h"
#include "ui/views/layout/box_layout.h"

namespace {

const char kAllSitesContentSettingsUrl[] =
    "chrome://settings/content/all?sort=data-stored";

// These values are persisted to logs. Entries should not be renumbered and
// numeric values should never be reused.
enum class StoragePressureBubbleHistogramValue {
  kShown = 0,
  kIgnored = 1,
  kOpenedAllSites = 2,
  kMaxValue = kOpenedAllSites,
};

void RecordBubbleHistogramValue(StoragePressureBubbleHistogramValue value) {
  base::UmaHistogramEnumeration("Storage.StoragePressure.Bubble", value);
}

}  // namespace

// static
void ShowStoragePressureBubble(const url::Origin& origin) {
  StoragePressureBubbleView::ShowBubble(origin);
}

void StoragePressureBubbleView::ShowBubble(const url::Origin& origin) {
  Browser* browser = BrowserList::GetInstance()->GetLastActive();
  if (!browser) {
    return;
  }

  StoragePressureBubbleView* bubble = new StoragePressureBubbleView(
      BrowserView::GetBrowserViewForBrowser(browser)
          ->toolbar_button_provider()
          ->GetAppMenuButton(),
      browser, origin);
  views::BubbleDialogDelegateView::CreateBubble(bubble)->Show();

  RecordBubbleHistogramValue(StoragePressureBubbleHistogramValue::kShown);
}

StoragePressureBubbleView::StoragePressureBubbleView(views::View* anchor_view,
                                                     Browser* browser,
                                                     const url::Origin& origin)
    : BubbleDialogDelegateView(anchor_view, views::BubbleBorder::TOP_RIGHT),
      browser_(browser),
      origin_(origin),
      ignored_(true) {
  SetButtons(static_cast<int>(ui::mojom::DialogButton::kOk));
  SetTitle(IDS_SETTINGS_STORAGE_PRESSURE_BUBBLE_VIEW_TITLE);
  SetButtonLabel(ui::mojom::DialogButton::kOk,
                 l10n_util::GetStringUTF16(
                     IDS_SETTINGS_STORAGE_PRESSURE_BUBBLE_VIEW_BUTTON_LABEL));
  SetAcceptCallback(base::BindOnce(&StoragePressureBubbleView::OnDialogAccepted,
                                   base::Unretained(this)));
  set_close_on_deactivate(false);
}

StoragePressureBubbleView::~StoragePressureBubbleView() {
  CHECK(!in_accept_);
  if (ignored_) {
    RecordBubbleHistogramValue(StoragePressureBubbleHistogramValue::kIgnored);
  }
}

void StoragePressureBubbleView::OnDialogAccepted() {
  base::AutoReset reset_in_accept(&in_accept_, true);
  auto weak_this = weak_ptr_factory_.GetWeakPtr();

  ignored_ = false;
  RecordBubbleHistogramValue(
      StoragePressureBubbleHistogramValue::kOpenedAllSites);
  // TODO(ellyjones): What is this doing here? The widget's about to close
  // anyway?
  GetWidget()->Close();

  CHECK(weak_this);
  CHECK(browser_);
  CHECK(browser_->profile());

  const GURL all_sites_gurl(kAllSitesContentSettingsUrl);
  NavigateParams params(browser_, all_sites_gurl,
                        ui::PAGE_TRANSITION_AUTO_TOPLEVEL);
  params.disposition = WindowOpenDisposition::NEW_FOREGROUND_TAB;
  Navigate(&params);
}

void StoragePressureBubbleView::Init() {
  ChromeLayoutProvider* provider = ChromeLayoutProvider::Get();
  SetLayoutManager(std::make_unique<views::BoxLayout>(
      views::BoxLayout::Orientation::kVertical, gfx::Insets(),
      provider->GetDistanceMetric(views::DISTANCE_UNRELATED_CONTROL_VERTICAL)));

  // Description text label.
  auto origin_string = url_formatter::FormatUrl(
      origin_.GetURL(),
      url_formatter::kFormatUrlOmitDefaults |
          url_formatter::kFormatUrlOmitTrivialSubdomains |
          url_formatter::kFormatUrlOmitHTTPS |
          url_formatter::kFormatUrlOmitTrailingSlashOnBareHostname,
      base::UnescapeRule::NONE, nullptr, nullptr, nullptr);
  auto text_label = std::make_unique<views::Label>(l10n_util::GetStringFUTF16(
      IDS_SETTINGS_STORAGE_PRESSURE_BUBBLE_VIEW_MESSAGE, origin_string));
  text_label->SetMultiLine(true);
  text_label->SetLineHeight(20);
  text_label->SetHorizontalAlignment(gfx::ALIGN_LEFT);
  text_label->SizeToFit(
      provider->GetDistanceMetric(views::DISTANCE_BUBBLE_PREFERRED_WIDTH) -
      margins().width());
  AddChildView(std::move(text_label));
}

bool StoragePressureBubbleView::ShouldShowCloseButton() const {
  return true;
}

BEGIN_METADATA(StoragePressureBubbleView)
END_METADATA