Disable view source for Developer Tools.
[chromium-blink-merge.git] / chrome / browser / ui / android / content_settings / popup_blocked_infobar_delegate.cc
blob7283eadebb42ea96a7969bbdac1a65faf1a97974
1 // Copyright 2013 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "chrome/browser/ui/android/content_settings/popup_blocked_infobar_delegate.h"
7 #include "base/prefs/pref_service.h"
8 #include "chrome/browser/content_settings/host_content_settings_map.h"
9 #include "chrome/browser/infobars/infobar.h"
10 #include "chrome/browser/infobars/infobar_service.h"
11 #include "chrome/browser/profiles/profile.h"
12 #include "chrome/browser/ui/blocked_content/popup_blocker_tab_helper.h"
13 #include "chrome/common/content_settings_types.h"
14 #include "grit/generated_resources.h"
15 #include "grit/theme_resources.h"
16 #include "ui/base/l10n/l10n_util.h"
19 // static
20 void PopupBlockedInfoBarDelegate::Create(InfoBarService* infobar_service,
21 int num_popups) {
22 scoped_ptr<InfoBar> infobar(ConfirmInfoBarDelegate::CreateInfoBar(
23 scoped_ptr<ConfirmInfoBarDelegate>(
24 new PopupBlockedInfoBarDelegate(num_popups))));
26 // See if there is an existing popup infobar already.
27 // TODO(dfalcantara) When triggering more than one popup the infobar
28 // will be shown once, then hide then be shown again.
29 // This will be fixed once we have an in place replace infobar mechanism.
30 for (size_t i = 0; i < infobar_service->infobar_count(); ++i) {
31 InfoBar* existing_infobar = infobar_service->infobar_at(i);
32 if (existing_infobar->delegate()->AsPopupBlockedInfoBarDelegate()) {
33 infobar_service->ReplaceInfoBar(existing_infobar, infobar.Pass());
34 return;
38 infobar_service->AddInfoBar(infobar.Pass());
41 PopupBlockedInfoBarDelegate::~PopupBlockedInfoBarDelegate() {
44 int PopupBlockedInfoBarDelegate::GetIconID() const {
45 return IDR_BLOCKED_POPUPS;
48 PopupBlockedInfoBarDelegate*
49 PopupBlockedInfoBarDelegate::AsPopupBlockedInfoBarDelegate() {
50 return this;
53 PopupBlockedInfoBarDelegate::PopupBlockedInfoBarDelegate(int num_popups)
54 : ConfirmInfoBarDelegate(),
55 num_popups_(num_popups) {
58 base::string16 PopupBlockedInfoBarDelegate::GetMessageText() const {
59 return l10n_util::GetStringFUTF16Int(IDS_POPUPS_BLOCKED_INFOBAR_TEXT,
60 num_popups_);
63 int PopupBlockedInfoBarDelegate::GetButtons() const {
64 return BUTTON_OK;
67 base::string16 PopupBlockedInfoBarDelegate::GetButtonLabel(
68 InfoBarButton button) const {
69 return l10n_util::GetStringUTF16(IDS_POPUPS_BLOCKED_INFOBAR_BUTTON_SHOW);
72 bool PopupBlockedInfoBarDelegate::Accept() {
73 // Create exceptions.
74 const GURL& url = web_contents()->GetURL();
75 Profile* profile = Profile::FromBrowserContext(
76 web_contents()->GetBrowserContext());
77 profile->GetHostContentSettingsMap()->AddExceptionForURL(
78 url, url, CONTENT_SETTINGS_TYPE_POPUPS, CONTENT_SETTING_ALLOW);
80 // Launch popups.
81 PopupBlockerTabHelper* popup_blocker_helper =
82 PopupBlockerTabHelper::FromWebContents(web_contents());
83 DCHECK(popup_blocker_helper);
84 PopupBlockerTabHelper::PopupIdMap blocked_popups =
85 popup_blocker_helper->GetBlockedPopupRequests();
86 for (PopupBlockerTabHelper::PopupIdMap::iterator it = blocked_popups.begin();
87 it != blocked_popups.end(); ++it)
88 popup_blocker_helper->ShowBlockedPopup(it->first);
90 return true;