File: popup_blocked_infobar_delegate.cc

package info (click to toggle)
chromium 139.0.7258.127-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 6,122,156 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 (124 lines) | stat: -rw-r--r-- 4,108 bytes parent folder | download | duplicates (6)
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
// Copyright 2013 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/blocked_content/android/popup_blocked_infobar_delegate.h"

#include <stddef.h>
#include <utility>

#include "components/blocked_content/android/popup_blocked_helper.h"
#include "components/content_settings/core/browser/host_content_settings_map.h"
#include "components/content_settings/core/common/content_settings.h"
#include "components/content_settings/core/common/content_settings_types.h"
#include "components/infobars/android/confirm_infobar.h"
#include "components/infobars/content/content_infobar_manager.h"
#include "components/infobars/core/infobar.h"
#include "components/prefs/pref_service.h"
#include "components/resources/android/theme_resources.h"
#include "components/strings/grit/components_strings.h"
#include "ui/base/l10n/l10n_util.h"

namespace blocked_content {

// static
bool PopupBlockedInfoBarDelegate::Create(
    infobars::ContentInfoBarManager* infobar_manager,
    int num_popups,
    HostContentSettingsMap* settings_map,
    base::OnceClosure on_accept_callback) {
  const GURL& url = infobar_manager->web_contents()->GetURL();
  auto infobar = std::make_unique<infobars::ConfirmInfoBar>(
      base::WrapUnique<PopupBlockedInfoBarDelegate>(
          new PopupBlockedInfoBarDelegate(num_popups, url, settings_map,
                                          std::move(on_accept_callback))));

  // See if there is an existing popup infobar already.
  // TODO(dfalcantara) When triggering more than one popup the infobar
  // will be shown once, then hide then be shown again.
  // This will be fixed once we have an in place replace infobar mechanism.
  for (infobars::InfoBar* existing_infobar : infobar_manager->infobars()) {
    if (existing_infobar->delegate()->AsPopupBlockedInfoBarDelegate()) {
      infobar_manager->ReplaceInfoBar(existing_infobar, std::move(infobar));
      return false;
    }
  }

  infobar_manager->AddInfoBar(std::move(infobar));

  return true;
}

PopupBlockedInfoBarDelegate::~PopupBlockedInfoBarDelegate() = default;

infobars::InfoBarDelegate::InfoBarIdentifier
PopupBlockedInfoBarDelegate::GetIdentifier() const {
  return POPUP_BLOCKED_INFOBAR_DELEGATE_MOBILE;
}

int PopupBlockedInfoBarDelegate::GetIconId() const {
  return IDR_ANDROID_INFOBAR_BLOCKED_POPUPS;
}

PopupBlockedInfoBarDelegate*
PopupBlockedInfoBarDelegate::AsPopupBlockedInfoBarDelegate() {
  return this;
}

PopupBlockedInfoBarDelegate::PopupBlockedInfoBarDelegate(
    int num_popups,
    const GURL& url,
    HostContentSettingsMap* map,
    base::OnceClosure on_accept_callback)
    : ConfirmInfoBarDelegate(),
      num_popups_(num_popups),
      url_(url),
      map_(map),
      on_accept_callback_(std::move(on_accept_callback)) {
  can_show_popups_ = !PopupSettingManagedByPolicy(map, url);
}

std::u16string PopupBlockedInfoBarDelegate::GetMessageText() const {
  return l10n_util::GetPluralStringFUTF16(IDS_POPUPS_BLOCKED_INFOBAR_TEXT,
                                          num_popups_);
}

int PopupBlockedInfoBarDelegate::GetButtons() const {
  if (!can_show_popups_)
    return 0;

  int buttons = BUTTON_OK;

  return buttons;
}

std::u16string PopupBlockedInfoBarDelegate::GetButtonLabel(
    InfoBarButton button) const {
  switch (button) {
    case BUTTON_OK:
      return l10n_util::GetStringUTF16(IDS_POPUPS_BLOCKED_INFOBAR_BUTTON_SHOW);
    case BUTTON_CANCEL:
      return l10n_util::GetStringUTF16(IDS_PERMISSION_DENY);
    default:
      NOTREACHED();
  }
}

bool PopupBlockedInfoBarDelegate::Accept() {
  DCHECK(can_show_popups_);

  // Create exceptions.
  map_->SetNarrowestContentSetting(url_, url_, ContentSettingsType::POPUPS,
                                   CONTENT_SETTING_ALLOW);

  // Launch popups.
  content::WebContents* web_contents =
      infobars::ContentInfoBarManager::WebContentsFromInfoBar(infobar());
  ShowBlockedPopups(web_contents);

  if (on_accept_callback_)
    std::move(on_accept_callback_).Run();
  return true;
}

}  // namespace blocked_content