1f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)// Copyright 2013 The Chromium Authors. All rights reserved.
2f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)// Use of this source code is governed by a BSD-style license that can be
3f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)// found in the LICENSE file.
4f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)
5f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)#include "chrome/browser/ui/android/content_settings/popup_blocked_infobar_delegate.h"
6f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)
7f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)#include "base/prefs/pref_service.h"
8f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)#include "chrome/browser/content_settings/host_content_settings_map.h"
9f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)#include "chrome/browser/infobars/infobar_service.h"
10f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)#include "chrome/browser/profiles/profile.h"
11f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)#include "chrome/browser/ui/blocked_content/popup_blocker_tab_helper.h"
121320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci#include "chrome/grit/generated_resources.h"
131320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci#include "components/content_settings/core/common/content_settings.h"
145f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)#include "components/content_settings/core/common/content_settings_types.h"
150529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch#include "components/infobars/core/infobar.h"
16f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)#include "grit/theme_resources.h"
17f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)#include "ui/base/l10n/l10n_util.h"
18f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)
19f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)
20f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)// static
215d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)void PopupBlockedInfoBarDelegate::Create(content::WebContents* web_contents,
22f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)                                         int num_popups) {
235d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  const GURL& url = web_contents->GetURL();
245d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  Profile* profile =
255d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      Profile::FromBrowserContext(web_contents->GetBrowserContext());
260529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch  scoped_ptr<infobars::InfoBar> infobar(ConfirmInfoBarDelegate::CreateInfoBar(
270529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch      scoped_ptr<ConfirmInfoBarDelegate>(new PopupBlockedInfoBarDelegate(
280529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch          num_popups, url, profile->GetHostContentSettingsMap()))));
29f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)
30a02191e04bc25c4935f804f2c080ae28663d096dBen Murdoch  InfoBarService* infobar_service =
31a02191e04bc25c4935f804f2c080ae28663d096dBen Murdoch      InfoBarService::FromWebContents(web_contents);
32f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  // See if there is an existing popup infobar already.
33f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  // TODO(dfalcantara) When triggering more than one popup the infobar
34f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  // will be shown once, then hide then be shown again.
35f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  // This will be fixed once we have an in place replace infobar mechanism.
36a02191e04bc25c4935f804f2c080ae28663d096dBen Murdoch  for (size_t i = 0; i < infobar_service->infobar_count(); ++i) {
370529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch    infobars::InfoBar* existing_infobar = infobar_service->infobar_at(i);
38a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)    if (existing_infobar->delegate()->AsPopupBlockedInfoBarDelegate()) {
39a02191e04bc25c4935f804f2c080ae28663d096dBen Murdoch      infobar_service->ReplaceInfoBar(existing_infobar, infobar.Pass());
40f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)      return;
41f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)    }
42f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  }
43f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)
44a02191e04bc25c4935f804f2c080ae28663d096dBen Murdoch  infobar_service->AddInfoBar(infobar.Pass());
45f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)}
46f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)
47f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)PopupBlockedInfoBarDelegate::~PopupBlockedInfoBarDelegate() {
48f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)}
49f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)
50f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)int PopupBlockedInfoBarDelegate::GetIconID() const {
51f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  return IDR_BLOCKED_POPUPS;
52f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)}
53f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)
54f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)PopupBlockedInfoBarDelegate*
55f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)    PopupBlockedInfoBarDelegate::AsPopupBlockedInfoBarDelegate() {
56f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  return this;
57f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)}
58f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)
595d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)PopupBlockedInfoBarDelegate::PopupBlockedInfoBarDelegate(
605d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    int num_popups,
615d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    const GURL& url,
625d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    HostContentSettingsMap* map)
635d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    : ConfirmInfoBarDelegate(), num_popups_(num_popups), url_(url), map_(map) {
645d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  content_settings::SettingInfo setting_info;
651320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  scoped_ptr<base::Value> setting = map->GetWebsiteSetting(
661320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci      url, url, CONTENT_SETTINGS_TYPE_POPUPS, std::string(), &setting_info);
675d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  can_show_popups_ =
685d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      setting_info.source != content_settings::SETTING_SOURCE_POLICY;
69f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)}
70f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)
715d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)base::string16 PopupBlockedInfoBarDelegate::GetMessageText() const {
72f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  return l10n_util::GetStringFUTF16Int(IDS_POPUPS_BLOCKED_INFOBAR_TEXT,
73f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)                                       num_popups_);
74f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)}
75f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)
76f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)int PopupBlockedInfoBarDelegate::GetButtons() const {
775d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  if (!can_show_popups_)
785d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    return 0;
795d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
80f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  return BUTTON_OK;
81f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)}
82f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)
835d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)base::string16 PopupBlockedInfoBarDelegate::GetButtonLabel(
84f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)    InfoBarButton button) const {
85f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  return l10n_util::GetStringUTF16(IDS_POPUPS_BLOCKED_INFOBAR_BUTTON_SHOW);
86f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)}
87f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)
88f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)bool PopupBlockedInfoBarDelegate::Accept() {
895d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  DCHECK(can_show_popups_);
905d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
91f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  // Create exceptions.
925d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  map_->AddExceptionForURL(
935d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      url_, url_, CONTENT_SETTINGS_TYPE_POPUPS, CONTENT_SETTING_ALLOW);
94f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)
95f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  // Launch popups.
96a02191e04bc25c4935f804f2c080ae28663d096dBen Murdoch  content::WebContents* web_contents =
97a02191e04bc25c4935f804f2c080ae28663d096dBen Murdoch      InfoBarService::WebContentsFromInfoBar(infobar());
98f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  PopupBlockerTabHelper* popup_blocker_helper =
99a02191e04bc25c4935f804f2c080ae28663d096dBen Murdoch      PopupBlockerTabHelper::FromWebContents(web_contents);
100f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  DCHECK(popup_blocker_helper);
101f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  PopupBlockerTabHelper::PopupIdMap blocked_popups =
102f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)      popup_blocker_helper->GetBlockedPopupRequests();
103f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  for (PopupBlockerTabHelper::PopupIdMap::iterator it = blocked_popups.begin();
104f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)      it != blocked_popups.end(); ++it)
105f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)    popup_blocker_helper->ShowBlockedPopup(it->first);
106f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)
107f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  return true;
108f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)}
109