File: page_info_bubble_view.cc

package info (click to toggle)
chromium 139.0.7258.138-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 6,120,676 kB
  • sloc: cpp: 35,100,869; 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 (403 lines) | stat: -rw-r--r-- 16,757 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
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
// Copyright 2021 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/page_info/page_info_bubble_view.h"

#include <memory>

#include "chrome/browser/page_info/page_info_features.h"
#include "chrome/browser/platform_util.h"
#include "chrome/browser/ui/browser.h"
#include "chrome/browser/ui/browser_window.h"
#include "chrome/browser/ui/page_info/chrome_page_info_delegate.h"
#include "chrome/browser/ui/page_info/chrome_page_info_ui_delegate.h"
#include "chrome/browser/ui/page_info/page_info_dialog.h"
#include "chrome/browser/ui/views/bubble_anchor_util_views.h"
#include "chrome/browser/ui/views/chrome_layout_provider.h"
#include "chrome/browser/ui/views/controls/page_switcher_view.h"
#include "chrome/browser/ui/views/page_info/page_info_main_view.h"
#include "chrome/browser/ui/views/page_info/page_info_merchant_trust_content_view.h"
#include "chrome/browser/ui/views/page_info/page_info_merchant_trust_coordinator.h"
#include "chrome/browser/ui/views/page_info/page_info_security_content_view.h"
#include "chrome/browser/ui/views/page_info/page_info_view_factory.h"
#include "components/dom_distiller/core/url_constants.h"
#include "components/dom_distiller/core/url_utils.h"
#include "components/page_info/core/features.h"
#include "components/privacy_sandbox/privacy_sandbox_features.h"
#include "components/strings/grit/components_branded_strings.h"
#include "components/strings/grit/components_strings.h"
#include "components/strings/grit/privacy_sandbox_strings.h"
#include "content/public/common/url_constants.h"
#include "extensions/common/constants.h"
#include "ui/base/l10n/l10n_util.h"
#include "ui/base/metadata/metadata_impl_macros.h"
#include "ui/views/accessibility/view_accessibility.h"
#include "ui/views/layout/box_layout.h"

using bubble_anchor_util::AnchorConfiguration;
using bubble_anchor_util::GetPageInfoAnchorConfiguration;
using bubble_anchor_util::GetPageInfoAnchorRect;

// The regular PageInfoBubbleView is not supported for internal Chrome pages and
// extension pages. Instead of the |PageInfoBubbleView|, the
// |InternalPageInfoBubbleView| is displayed.
class InternalPageInfoBubbleView : public PageInfoBubbleViewBase {
  METADATA_HEADER(InternalPageInfoBubbleView, PageInfoBubbleViewBase)

 public:
  // If |anchor_view| is nullptr, or has no Widget, |parent_window| may be
  // provided to ensure this bubble is closed when the parent closes.
  InternalPageInfoBubbleView(views::View* anchor_view,
                             const gfx::Rect& anchor_rect,
                             gfx::NativeView parent_window,
                             content::WebContents* web_contents,
                             const GURL& url);
  InternalPageInfoBubbleView(const InternalPageInfoBubbleView&) = delete;
  InternalPageInfoBubbleView& operator=(const InternalPageInfoBubbleView&) =
      delete;
  ~InternalPageInfoBubbleView() override;
};

////////////////////////////////////////////////////////////////////////////////
// InternalPageInfoBubbleView
////////////////////////////////////////////////////////////////////////////////

InternalPageInfoBubbleView::InternalPageInfoBubbleView(
    views::View* anchor_view,
    const gfx::Rect& anchor_rect,
    gfx::NativeView parent_window,
    content::WebContents* web_contents,
    const GURL& url)
    : PageInfoBubbleViewBase(anchor_view,
                             anchor_rect,
                             parent_window,
                             PageInfoBubbleViewBase::BUBBLE_INTERNAL_PAGE,
                             web_contents) {
  int text = IDS_PAGE_INFO_INTERNAL_PAGE;
  if (url.SchemeIs(extensions::kExtensionScheme)) {
    text = IDS_PAGE_INFO_EXTENSION_PAGE;
  } else if (url.SchemeIs(content::kViewSourceScheme)) {
    text = IDS_PAGE_INFO_VIEW_SOURCE_PAGE;
  } else if (url.SchemeIs(url::kFileScheme)) {
    text = IDS_PAGE_INFO_FILE_PAGE;
  } else if (url.SchemeIs(content::kChromeDevToolsScheme)) {
    text = IDS_PAGE_INFO_DEVTOOLS_PAGE;
  } else if (url.SchemeIs(dom_distiller::kDomDistillerScheme)) {
    if (dom_distiller::url_utils::GetOriginalUrlFromDistillerUrl(url).SchemeIs(
            url::kHttpsScheme)) {
      text = IDS_PAGE_INFO_READER_MODE_PAGE_SECURE;
    } else {
      text = IDS_PAGE_INFO_READER_MODE_PAGE;
    }
  } else {
    CHECK(url.SchemeIs(content::kChromeUIScheme));
  }

  // Title insets assume there is content (and thus have no bottom padding). Use
  // dialog insets to get the bottom margin back.
  set_title_margins(
      ChromeLayoutProvider::Get()->GetInsetsMetric(views::INSETS_DIALOG));
  set_margins(gfx::Insets());

  set_fixed_width(ChromeLayoutProvider::Get()->GetDistanceMetric(
      views::DISTANCE_BUBBLE_PREFERRED_WIDTH));

  SetTitle(text);

  views::BubbleDialogDelegateView::CreateBubble(this);

  SetLayoutManager(std::make_unique<views::BoxLayout>(
      views::BoxLayout::Orientation::kVertical, gfx::Insets(),
      ChromeLayoutProvider::Get()->GetDistanceMetric(
          views::DISTANCE_RELATED_CONTROL_VERTICAL)));

  // Use a normal label's style for the title since there is no content.
  views::Label* title_label =
      static_cast<views::Label*>(GetBubbleFrameView()->title());
  title_label->SetFontList(views::Label::GetDefaultFontList());
  title_label->SetMultiLine(true);
  title_label->SetElideBehavior(gfx::NO_ELIDE);

  // TODO(crbug.com/343325197) Remove this SizeToContents() once this bug is
  // fixed.
  SizeToContents();
}

InternalPageInfoBubbleView::~InternalPageInfoBubbleView() = default;

BEGIN_METADATA(InternalPageInfoBubbleView)
END_METADATA

PageInfoBubbleView::PageInfoBubbleView(
    views::View* anchor_view,
    const gfx::Rect& anchor_rect,
    gfx::NativeView parent_window,
    content::WebContents* associated_web_contents,
    const GURL& url,
    base::OnceClosure initialized_callback,
    PageInfoClosingCallback closing_callback,
    bool allow_extended_site_info)
    : PageInfoBubbleViewBase(anchor_view,
                             anchor_rect,
                             parent_window,
                             PageInfoBubbleViewBase::BUBBLE_PAGE_INFO,
                             associated_web_contents),
      closing_callback_(std::move(closing_callback)) {
  DCHECK(closing_callback_);
  DCHECK(web_contents());
  ChromeLayoutProvider* layout_provider = ChromeLayoutProvider::Get();

  // Only set the top margin. The bottom margin is set for each subpage
  // separately, depending the content.
  const int top_margin =
      layout_provider->GetInsetsMetric(views::INSETS_DIALOG).top();
  set_margins(gfx::Insets().set_top(top_margin));
  ui_delegate_ =
      std::make_unique<ChromePageInfoUiDelegate>(web_contents(), url);
  presenter_ = std::make_unique<PageInfo>(
      std::make_unique<ChromePageInfoDelegate>(web_contents()), web_contents(),
      url);
  if (base::FeatureList::IsEnabled(page_info::kPageInfoHistoryDesktop)) {
    history_controller_ =
        std::make_unique<PageInfoHistoryController>(web_contents(), url);
  }
  view_factory_ = std::make_unique<PageInfoViewFactory>(
      presenter_.get(), ui_delegate_.get(), this, history_controller_.get(),
      allow_extended_site_info);

  SetShowTitle(false);
  SetShowCloseButton(false);
  // The title isn't visible, it is set for a11y purposes and the actual visible
  // title is a custom label in the content view.
  SetTitle(presenter_->GetSubjectNameForDisplay());

  SetLayoutManager(std::make_unique<views::BoxLayout>(
      views::BoxLayout::Orientation::kVertical));
  std::unique_ptr<views::View> main_page_view =
      view_factory_->CreateMainPageView(std::move(initialized_callback));
  main_page_view->SetID(PageInfoViewFactory::VIEW_ID_PAGE_INFO_CURRENT_VIEW);
  page_container_ = AddChildView(
      std::make_unique<PageSwitcherView>(std::move(main_page_view)));

  if (page_info::IsMerchantTrustFeatureEnabled()) {
    merchant_trust_coordinator_ =
        std::make_unique<PageInfoMerchantTrustCoordinator>(web_contents());
  }

  views::BubbleDialogDelegateView::CreateBubble(this);
}

PageInfoBubbleView::~PageInfoBubbleView() {
  page_container_ = nullptr;
  RemoveAllChildViews();
}

// static
views::BubbleDialogDelegateView* PageInfoBubbleView::CreatePageInfoBubble(
    views::View* anchor_view,
    const gfx::Rect& anchor_rect,
    gfx::NativeWindow parent_window,
    content::WebContents* web_contents,
    const GURL& url,
    base::OnceClosure initialized_callback,
    PageInfoClosingCallback closing_callback,
    bool allow_extended_site_info,
    std::optional<ContentSettingsType> type,
    bool open_merchant_trust_page) {
  DCHECK(web_contents);
  gfx::NativeView parent_view = platform_util::GetViewForWindow(parent_window);

  if (PageInfo::IsFileOrInternalPage(url) ||
      url.SchemeIs(extensions::kExtensionScheme) ||
      url.SchemeIs(dom_distiller::kDomDistillerScheme)) {
    return new InternalPageInfoBubbleView(anchor_view, anchor_rect, parent_view,
                                          web_contents, url);
  }

  PageInfoBubbleView* bubble = new PageInfoBubbleView(
      anchor_view, anchor_rect, parent_view, web_contents, url,
      std::move(initialized_callback), std::move(closing_callback),
      allow_extended_site_info);
  if (type) {
    CHECK(!open_merchant_trust_page);
    bubble->OpenPermissionPage(*type);
  }
  if (open_merchant_trust_page) {
    CHECK(!type);
    bubble->OpenMerchantTrustPage(
        page_info::MerchantBubbleOpenReferrer::kLocationBarChip);
  }
  return bubble;
}

void PageInfoBubbleView::OpenMainPage(base::OnceClosure initialized_callback) {
  std::unique_ptr<views::View> main_page_view =
      view_factory_->CreateMainPageView(std::move(initialized_callback));
  main_page_view->SetID(PageInfoViewFactory::VIEW_ID_PAGE_INFO_CURRENT_VIEW);
  page_container_->SwitchToPage(std::move(main_page_view));

  auto* close_button = page_container_->GetViewByID(
      PageInfoViewFactory::VIEW_ID_PAGE_INFO_CLOSE_BUTTON);
  close_button->RequestFocus();
}

void PageInfoBubbleView::OpenSecurityPage() {
  presenter_->RecordPageInfoAction(
      page_info::PAGE_INFO_SECURITY_DETAILS_OPENED);
  std::unique_ptr<views::View> security_page_view =
      view_factory_->CreateSecurityPageView();
  security_page_view->SetID(
      PageInfoViewFactory::VIEW_ID_PAGE_INFO_CURRENT_VIEW);
  page_container_->SwitchToPage(std::move(security_page_view));
  AnnouncePageOpened(
      l10n_util::GetStringUTF16(IDS_PAGE_INFO_SECURITY_SUBPAGE_HEADER));
}

void PageInfoBubbleView::OpenPermissionPage(ContentSettingsType type) {
  presenter_->RecordPageInfoAction(
      page_info::PAGE_INFO_PERMISSION_DIALOG_OPENED);
  std::unique_ptr<views::View> permissions_page_view =
      view_factory_->CreatePermissionPageView(type, web_contents());
  permissions_page_view->SetID(
      PageInfoViewFactory::VIEW_ID_PAGE_INFO_CURRENT_VIEW);
  page_container_->SwitchToPage(std::move(permissions_page_view));
  AnnouncePageOpened(PageInfoUI::PermissionTypeToUIString(type));
}

void PageInfoBubbleView::OpenAdPersonalizationPage() {
  presenter_->RecordPageInfoAction(
      page_info::PAGE_INFO_AD_PERSONALIZATION_PAGE_OPENED);
  std::unique_ptr<views::View> ad_personalization_page_view =
      view_factory_->CreateAdPersonalizationPageView();
  ad_personalization_page_view->SetID(
      PageInfoViewFactory::VIEW_ID_PAGE_INFO_CURRENT_VIEW);
  page_container_->SwitchToPage(std::move(ad_personalization_page_view));
  AnnouncePageOpened(
      l10n_util::GetStringUTF16(IDS_PAGE_INFO_AD_PRIVACY_HEADER));
}

void PageInfoBubbleView::OpenCookiesPage() {
  presenter_->OnCookiesPageOpened();
  std::unique_ptr<views::View> cookies_page_view =
      view_factory_->CreateCookiesPageView();
  cookies_page_view->SetID(PageInfoViewFactory::VIEW_ID_PAGE_INFO_CURRENT_VIEW);
  page_container_->SwitchToPage(std::move(cookies_page_view));
  AnnouncePageOpened(l10n_util::GetStringUTF16(IDS_PAGE_INFO_COOKIES));
}

void PageInfoBubbleView::OpenPrivacyAndSiteDataPage() {
  std::unique_ptr<views::View> privacy_and_site_data_page_view =
      view_factory_->CreatePrivacyAndSiteDataPageView();
  privacy_and_site_data_page_view->SetID(
      PageInfoViewFactory::VIEW_ID_PAGE_INFO_CURRENT_VIEW);
  page_container_->SwitchToPage(std::move(privacy_and_site_data_page_view));
  AnnouncePageOpened(
      l10n_util::GetStringUTF16(IDS_PAGE_INFO_PRIVACY_SITE_DATA_HEADER));
}

void PageInfoBubbleView::OpenMerchantTrustPage(
    page_info::MerchantBubbleOpenReferrer referrer) {
  CHECK(merchant_trust_coordinator_);
  auto title = l10n_util::GetStringUTF16(IDS_PAGE_INFO_MERCHANT_TRUST_HEADER);
  auto page_content = merchant_trust_coordinator_->CreatePageContent();
  // TODO(crbug.com/390370438): Remove this call after the presenter is
  // refactored since merchant trust page doesn't implement any of the
  // PageInfoUI methods.
  presenter_->InitializeUiState(page_content.get(), base::DoNothing());
  auto page_view =
      view_factory_->CreatePageView(title, std::move(page_content));
  page_view->SetID(PageInfoViewFactory::VIEW_ID_PAGE_INFO_CURRENT_VIEW);
  page_container_->SwitchToPage(std::move(page_view));
  AnnouncePageOpened(title);
  merchant_trust_coordinator_->OnBubbleOpened(referrer);
}

void PageInfoBubbleView::CloseBubble() {
  GetWidget()->CloseWithReason(
      views::Widget::ClosedReason::kCloseButtonClicked);
}

void PageInfoBubbleView::DidChangeVisibleSecurityState() {
  presenter_->UpdateSecurityState();
}

void PageInfoBubbleView::OnWidgetDestroying(views::Widget* widget) {
  PageInfoBubbleViewBase::OnWidgetDestroying(widget);

  // This method mostly shouldn't be re-entrant but there are a few cases where
  // it can be (see crbug/966308). In that case, we have already run the closing
  // callback so should not attempt to do it again. As there will always be a
  // |closing_callback_|, this is also used to ensure that the |presenter_| is
  // informed exactly once.
  if (closing_callback_) {
    bool reload_prompt;
    presenter_->OnUIClosing(&reload_prompt);

    std::move(closing_callback_).Run(widget->closed_reason(), reload_prompt);
  }
}

void PageInfoBubbleView::WebContentsDestroyed() {
  PageInfoBubbleViewBase::WebContentsDestroyed();
  weak_factory_.InvalidateWeakPtrs();
}

gfx::Size PageInfoBubbleView::CalculatePreferredSize(
    const views::SizeBounds& available_size) const {
  if (page_container_ == nullptr) {
    return views::View::CalculatePreferredSize(available_size);
  }

  int width = PageInfoViewFactory::kMinBubbleWidth;
  if (page_container_) {
    width = std::max(width, page_container_->GetPreferredSize().width());
    width = std::min(width, PageInfoViewFactory::kMaxBubbleWidth);
  }
  return gfx::Size(width,
                   GetLayoutManager()->GetPreferredHeightForWidth(this, width));
}

void PageInfoBubbleView::ChildPreferredSizeChanged(views::View* child) {
  // TODO(crbug.com/343325197) Remove this SizeToContents() once this bug is
  // fixed.
  SizeToContents();
}

void PageInfoBubbleView::AnnouncePageOpened(std::u16string announcement) {
  // Announce that the subpage was opened to inform the user about the changes
  // in the UI.
  GetViewAccessibility().AnnounceText(announcement);

  // Focus the back button by default to ensure that focus is set when new
  // content is displayed.
  auto* back_button = page_container_->GetViewByID(
      PageInfoViewFactory::VIEW_ID_PAGE_INFO_BACK_BUTTON);
  back_button->RequestFocus();
}

void ShowPageInfoDialogImpl(Browser* browser,
                            content::WebContents* web_contents,
                            const GURL& virtual_url,
                            bubble_anchor_util::Anchor anchor,
                            base::OnceClosure initialized_callback,
                            PageInfoClosingCallback closing_callback,
                            std::optional<ContentSettingsType> type) {
  AnchorConfiguration configuration =
      GetPageInfoAnchorConfiguration(browser, anchor);
  gfx::Rect anchor_rect =
      configuration.anchor_view ? gfx::Rect() : GetPageInfoAnchorRect(browser);
  gfx::NativeWindow parent_window = browser->window()->GetNativeWindow();
  DCHECK(web_contents);
  views::BubbleDialogDelegateView* bubble =
      PageInfoBubbleView::CreatePageInfoBubble(
          configuration.anchor_view, anchor_rect, parent_window, web_contents,
          virtual_url, std::move(initialized_callback),
          std::move(closing_callback), /*allow_extended_site_info=*/true, type);
  bubble->SetHighlightedButton(configuration.highlighted_button);
  bubble->SetArrow(configuration.bubble_arrow);
  bubble->GetWidget()->Show();
}

BEGIN_METADATA(PageInfoBubbleView)
END_METADATA