Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / chrome / browser / ui / android / content_settings / popup_blocked_infobar_delegate.cc
blobb502d3a72a6bcd541c340448f075c6df2b0d6d0d
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_factory.h"
9 #include "chrome/browser/infobars/infobar_service.h"
10 #include "chrome/browser/profiles/profile.h"
11 #include "chrome/browser/ui/blocked_content/popup_blocker_tab_helper.h"
12 #include "chrome/grit/generated_resources.h"
13 #include "components/content_settings/core/browser/host_content_settings_map.h"
14 #include "components/content_settings/core/common/content_settings.h"
15 #include "components/content_settings/core/common/content_settings_types.h"
16 #include "components/infobars/core/infobar.h"
17 #include "grit/theme_resources.h"
18 #include "ui/base/l10n/l10n_util.h"
21 // static
22 void PopupBlockedInfoBarDelegate::Create(content::WebContents* web_contents,
23 int num_popups) {
24 const GURL& url = web_contents->GetURL();
25 Profile* profile =
26 Profile::FromBrowserContext(web_contents->GetBrowserContext());
27 InfoBarService* infobar_service =
28 InfoBarService::FromWebContents(web_contents);
29 scoped_ptr<infobars::InfoBar> infobar(infobar_service->CreateConfirmInfoBar(
30 scoped_ptr<ConfirmInfoBarDelegate>(new PopupBlockedInfoBarDelegate(
31 num_popups,
32 url,
33 HostContentSettingsMapFactory::GetForProfile(profile)))));
35 // See if there is an existing popup infobar already.
36 // TODO(dfalcantara) When triggering more than one popup the infobar
37 // will be shown once, then hide then be shown again.
38 // This will be fixed once we have an in place replace infobar mechanism.
39 for (size_t i = 0; i < infobar_service->infobar_count(); ++i) {
40 infobars::InfoBar* existing_infobar = infobar_service->infobar_at(i);
41 if (existing_infobar->delegate()->AsPopupBlockedInfoBarDelegate()) {
42 infobar_service->ReplaceInfoBar(existing_infobar, infobar.Pass());
43 return;
47 infobar_service->AddInfoBar(infobar.Pass());
50 PopupBlockedInfoBarDelegate::~PopupBlockedInfoBarDelegate() {
53 int PopupBlockedInfoBarDelegate::GetIconId() const {
54 return IDR_BLOCKED_POPUPS;
57 PopupBlockedInfoBarDelegate*
58 PopupBlockedInfoBarDelegate::AsPopupBlockedInfoBarDelegate() {
59 return this;
62 PopupBlockedInfoBarDelegate::PopupBlockedInfoBarDelegate(
63 int num_popups,
64 const GURL& url,
65 HostContentSettingsMap* map)
66 : ConfirmInfoBarDelegate(), num_popups_(num_popups), url_(url), map_(map) {
67 content_settings::SettingInfo setting_info;
68 scoped_ptr<base::Value> setting = map->GetWebsiteSetting(
69 url, url, CONTENT_SETTINGS_TYPE_POPUPS, std::string(), &setting_info);
70 can_show_popups_ =
71 setting_info.source != content_settings::SETTING_SOURCE_POLICY;
74 base::string16 PopupBlockedInfoBarDelegate::GetMessageText() const {
75 return l10n_util::GetStringFUTF16Int(IDS_POPUPS_BLOCKED_INFOBAR_TEXT,
76 num_popups_);
79 int PopupBlockedInfoBarDelegate::GetButtons() const {
80 if (!can_show_popups_)
81 return 0;
83 return BUTTON_OK;
86 base::string16 PopupBlockedInfoBarDelegate::GetButtonLabel(
87 InfoBarButton button) const {
88 return l10n_util::GetStringUTF16(IDS_POPUPS_BLOCKED_INFOBAR_BUTTON_SHOW);
91 bool PopupBlockedInfoBarDelegate::Accept() {
92 DCHECK(can_show_popups_);
94 // Create exceptions.
95 map_->AddExceptionForURL(
96 url_, url_, CONTENT_SETTINGS_TYPE_POPUPS, CONTENT_SETTING_ALLOW);
98 // Launch popups.
99 content::WebContents* web_contents =
100 InfoBarService::WebContentsFromInfoBar(infobar());
101 PopupBlockerTabHelper* popup_blocker_helper =
102 PopupBlockerTabHelper::FromWebContents(web_contents);
103 DCHECK(popup_blocker_helper);
104 PopupBlockerTabHelper::PopupIdMap blocked_popups =
105 popup_blocker_helper->GetBlockedPopupRequests();
106 for (PopupBlockerTabHelper::PopupIdMap::iterator it = blocked_popups.begin();
107 it != blocked_popups.end(); ++it)
108 popup_blocker_helper->ShowBlockedPopup(it->first);
110 return true;