Upstream version 5.34.104.0
[platform/framework/web/crosswalk.git] / src / chrome / browser / ui / android / content_settings / popup_blocked_infobar_delegate.cc
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.
4
5 #include "chrome/browser/ui/android/content_settings/popup_blocked_infobar_delegate.h"
6
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.h"
14 #include "chrome/common/content_settings_types.h"
15 #include "grit/generated_resources.h"
16 #include "grit/theme_resources.h"
17 #include "ui/base/l10n/l10n_util.h"
18
19
20 // static
21 void PopupBlockedInfoBarDelegate::Create(content::WebContents* web_contents,
22                                          int num_popups) {
23   const GURL& url = web_contents->GetURL();
24   Profile* profile =
25       Profile::FromBrowserContext(web_contents->GetBrowserContext());
26   scoped_ptr<InfoBar> infobar(ConfirmInfoBarDelegate::CreateInfoBar(
27       scoped_ptr<ConfirmInfoBarDelegate>(
28           new PopupBlockedInfoBarDelegate(
29                 num_popups, url, profile->GetHostContentSettingsMap()))));
30
31   InfoBarService* infobar_service =
32       InfoBarService::FromWebContents(web_contents);
33   // See if there is an existing popup infobar already.
34   // TODO(dfalcantara) When triggering more than one popup the infobar
35   // will be shown once, then hide then be shown again.
36   // This will be fixed once we have an in place replace infobar mechanism.
37   for (size_t i = 0; i < infobar_service->infobar_count(); ++i) {
38     InfoBar* existing_infobar = infobar_service->infobar_at(i);
39     if (existing_infobar->delegate()->AsPopupBlockedInfoBarDelegate()) {
40       infobar_service->ReplaceInfoBar(existing_infobar, infobar.Pass());
41       return;
42     }
43   }
44
45   infobar_service->AddInfoBar(infobar.Pass());
46 }
47
48 PopupBlockedInfoBarDelegate::~PopupBlockedInfoBarDelegate() {
49 }
50
51 int PopupBlockedInfoBarDelegate::GetIconID() const {
52   return IDR_BLOCKED_POPUPS;
53 }
54
55 PopupBlockedInfoBarDelegate*
56     PopupBlockedInfoBarDelegate::AsPopupBlockedInfoBarDelegate() {
57   return this;
58 }
59
60 PopupBlockedInfoBarDelegate::PopupBlockedInfoBarDelegate(
61     int num_popups,
62     const GURL& url,
63     HostContentSettingsMap* map)
64     : ConfirmInfoBarDelegate(), num_popups_(num_popups), url_(url), map_(map) {
65   content_settings::SettingInfo setting_info;
66   scoped_ptr<base::Value> setting(
67       map->GetWebsiteSetting(
68           url,
69           url,
70           CONTENT_SETTINGS_TYPE_POPUPS,
71           std::string(),
72           &setting_info));
73   can_show_popups_ =
74       setting_info.source != content_settings::SETTING_SOURCE_POLICY;
75 }
76
77 base::string16 PopupBlockedInfoBarDelegate::GetMessageText() const {
78   return l10n_util::GetStringFUTF16Int(IDS_POPUPS_BLOCKED_INFOBAR_TEXT,
79                                        num_popups_);
80 }
81
82 int PopupBlockedInfoBarDelegate::GetButtons() const {
83   if (!can_show_popups_)
84     return 0;
85
86   return BUTTON_OK;
87 }
88
89 base::string16 PopupBlockedInfoBarDelegate::GetButtonLabel(
90     InfoBarButton button) const {
91   return l10n_util::GetStringUTF16(IDS_POPUPS_BLOCKED_INFOBAR_BUTTON_SHOW);
92 }
93
94 bool PopupBlockedInfoBarDelegate::Accept() {
95   DCHECK(can_show_popups_);
96
97   // Create exceptions.
98   map_->AddExceptionForURL(
99       url_, url_, CONTENT_SETTINGS_TYPE_POPUPS, CONTENT_SETTING_ALLOW);
100
101   // Launch popups.
102   PopupBlockerTabHelper* popup_blocker_helper =
103       PopupBlockerTabHelper::FromWebContents(web_contents());
104   DCHECK(popup_blocker_helper);
105   PopupBlockerTabHelper::PopupIdMap blocked_popups =
106       popup_blocker_helper->GetBlockedPopupRequests();
107   for (PopupBlockerTabHelper::PopupIdMap::iterator it = blocked_popups.begin();
108       it != blocked_popups.end(); ++it)
109     popup_blocker_helper->ShowBlockedPopup(it->first);
110
111   return true;
112 }
113