Home | History | Annotate | Download | only in browser
      1 // Copyright (c) 2012 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/pepper_broker_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/content_settings/tab_specific_content_settings.h"
     10 #include "chrome/browser/infobars/infobar_service.h"
     11 #include "chrome/browser/plugins/plugin_finder.h"
     12 #include "chrome/browser/plugins/plugin_metadata.h"
     13 #include "chrome/browser/profiles/profile.h"
     14 #include "chrome/common/pref_names.h"
     15 #include "chrome/grit/generated_resources.h"
     16 #include "components/infobars/core/infobar.h"
     17 #include "content/public/browser/page_navigator.h"
     18 #include "content/public/browser/plugin_service.h"
     19 #include "content/public/browser/user_metrics.h"
     20 #include "content/public/browser/web_contents.h"
     21 #include "content/public/common/referrer.h"
     22 #include "content/public/common/webplugininfo.h"
     23 #include "grit/components_strings.h"
     24 #include "grit/theme_resources.h"
     25 #include "net/base/net_util.h"
     26 #include "ui/base/l10n/l10n_util.h"
     27 
     28 
     29 // static
     30 void PepperBrokerInfoBarDelegate::Create(
     31     content::WebContents* web_contents,
     32     const GURL& url,
     33     const base::FilePath& plugin_path,
     34     const base::Callback<void(bool)>& callback) {
     35   Profile* profile =
     36       Profile::FromBrowserContext(web_contents->GetBrowserContext());
     37   // TODO(wad): Add ephemeral device ID support for broker in guest mode.
     38   if (profile->IsGuestSession()) {
     39     callback.Run(false);
     40     return;
     41   }
     42 
     43   TabSpecificContentSettings* tab_content_settings =
     44       TabSpecificContentSettings::FromWebContents(web_contents);
     45 
     46   HostContentSettingsMap* content_settings =
     47       profile->GetHostContentSettingsMap();
     48   ContentSetting setting =
     49       content_settings->GetContentSetting(url, url,
     50                                           CONTENT_SETTINGS_TYPE_PPAPI_BROKER,
     51                                           std::string());
     52 
     53   if (setting == CONTENT_SETTING_ASK) {
     54     content::RecordAction(
     55         base::UserMetricsAction("PPAPI.BrokerInfobarDisplayed"));
     56     InfoBarService* infobar_service =
     57         InfoBarService::FromWebContents(web_contents);
     58     infobar_service->AddInfoBar(ConfirmInfoBarDelegate::CreateInfoBar(
     59         scoped_ptr<ConfirmInfoBarDelegate>(new PepperBrokerInfoBarDelegate(
     60             url, plugin_path,
     61             profile->GetPrefs()->GetString(prefs::kAcceptLanguages),
     62             content_settings, tab_content_settings, callback))));
     63     return;
     64   }
     65 
     66   bool allowed = (setting == CONTENT_SETTING_ALLOW);
     67   content::RecordAction(allowed ?
     68       base::UserMetricsAction("PPAPI.BrokerSettingAllow") :
     69       base::UserMetricsAction("PPAPI.BrokerSettingDeny"));
     70   tab_content_settings->SetPepperBrokerAllowed(allowed);
     71   callback.Run(allowed);
     72 }
     73 
     74 PepperBrokerInfoBarDelegate::PepperBrokerInfoBarDelegate(
     75     const GURL& url,
     76     const base::FilePath& plugin_path,
     77     const std::string& languages,
     78     HostContentSettingsMap* content_settings,
     79     TabSpecificContentSettings* tab_content_settings,
     80     const base::Callback<void(bool)>& callback)
     81     : ConfirmInfoBarDelegate(),
     82       url_(url),
     83       plugin_path_(plugin_path),
     84       languages_(languages),
     85       content_settings_(content_settings),
     86       tab_content_settings_(tab_content_settings),
     87       callback_(callback) {
     88 }
     89 
     90 PepperBrokerInfoBarDelegate::~PepperBrokerInfoBarDelegate() {
     91   if (!callback_.is_null())
     92     callback_.Run(false);
     93 }
     94 
     95 int PepperBrokerInfoBarDelegate::GetIconID() const {
     96   return IDR_INFOBAR_PLUGIN_INSTALL;
     97 }
     98 
     99 base::string16 PepperBrokerInfoBarDelegate::GetMessageText() const {
    100   content::PluginService* plugin_service =
    101       content::PluginService::GetInstance();
    102   content::WebPluginInfo plugin;
    103   bool success = plugin_service->GetPluginInfoByPath(plugin_path_, &plugin);
    104   DCHECK(success);
    105   scoped_ptr<PluginMetadata> plugin_metadata(
    106       PluginFinder::GetInstance()->GetPluginMetadata(plugin));
    107   return l10n_util::GetStringFUTF16(IDS_PEPPER_BROKER_MESSAGE,
    108                                     plugin_metadata->name(),
    109                                     net::FormatUrl(url_.GetOrigin(),
    110                                                    languages_));
    111 }
    112 
    113 base::string16 PepperBrokerInfoBarDelegate::GetButtonLabel(
    114     InfoBarButton button) const {
    115   return l10n_util::GetStringUTF16((button == BUTTON_OK) ?
    116       IDS_PEPPER_BROKER_ALLOW_BUTTON : IDS_PEPPER_BROKER_DENY_BUTTON);
    117 }
    118 
    119 bool PepperBrokerInfoBarDelegate::Accept() {
    120   DispatchCallback(true);
    121   return true;
    122 }
    123 
    124 bool PepperBrokerInfoBarDelegate::Cancel() {
    125   DispatchCallback(false);
    126   return true;
    127 }
    128 
    129 base::string16 PepperBrokerInfoBarDelegate::GetLinkText() const {
    130   return l10n_util::GetStringUTF16(IDS_LEARN_MORE);
    131 }
    132 
    133 bool PepperBrokerInfoBarDelegate::LinkClicked(
    134     WindowOpenDisposition disposition) {
    135   GURL learn_more_url("https://support.google.com/chrome/?p=ib_pepper_broker");
    136   InfoBarService::WebContentsFromInfoBar(infobar())->OpenURL(
    137       content::OpenURLParams(
    138           learn_more_url, content::Referrer(),
    139           (disposition == CURRENT_TAB) ? NEW_FOREGROUND_TAB : disposition,
    140           ui::PAGE_TRANSITION_LINK, false));
    141   return false;
    142 }
    143 
    144 void PepperBrokerInfoBarDelegate::DispatchCallback(bool result) {
    145   content::RecordAction(result ?
    146       base::UserMetricsAction("PPAPI.BrokerInfobarClickedAllow") :
    147       base::UserMetricsAction("PPAPI.BrokerInfobarClickedDeny"));
    148   callback_.Run(result);
    149   callback_ = base::Callback<void(bool)>();
    150   content_settings_->SetContentSetting(
    151       ContentSettingsPattern::FromURLNoWildcard(url_),
    152       ContentSettingsPattern::Wildcard(),
    153       CONTENT_SETTINGS_TYPE_PPAPI_BROKER,
    154       std::string(), result ? CONTENT_SETTING_ALLOW : CONTENT_SETTING_BLOCK);
    155   tab_content_settings_->SetPepperBrokerAllowed(result);
    156 }
    157