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