MD Downloads: UI review feedback
[chromium-blink-merge.git] / chrome / browser / pepper_broker_infobar_delegate.cc
blob65f866f07e6562515e5841eb06494023d2b6d791
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.
5 #include "chrome/browser/pepper_broker_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/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/content_settings/core/browser/host_content_settings_map.h"
17 #include "components/infobars/core/infobar.h"
18 #include "components/url_formatter/url_formatter.h"
19 #include "content/public/browser/page_navigator.h"
20 #include "content/public/browser/plugin_service.h"
21 #include "content/public/browser/user_metrics.h"
22 #include "content/public/browser/web_contents.h"
23 #include "content/public/common/referrer.h"
24 #include "content/public/common/webplugininfo.h"
25 #include "grit/components_strings.h"
26 #include "grit/theme_resources.h"
27 #include "ui/base/l10n/l10n_util.h"
30 // static
31 void PepperBrokerInfoBarDelegate::Create(
32 content::WebContents* web_contents,
33 const GURL& url,
34 const base::FilePath& plugin_path,
35 const base::Callback<void(bool)>& callback) {
36 Profile* profile =
37 Profile::FromBrowserContext(web_contents->GetBrowserContext());
38 // TODO(wad): Add ephemeral device ID support for broker in guest mode.
39 if (profile->IsGuestSession()) {
40 callback.Run(false);
41 return;
44 TabSpecificContentSettings* tab_content_settings =
45 TabSpecificContentSettings::FromWebContents(web_contents);
47 HostContentSettingsMap* content_settings =
48 HostContentSettingsMapFactory::GetForProfile(profile);
49 ContentSetting setting =
50 content_settings->GetContentSetting(url, url,
51 CONTENT_SETTINGS_TYPE_PPAPI_BROKER,
52 std::string());
54 if (setting == CONTENT_SETTING_ASK) {
55 content::RecordAction(
56 base::UserMetricsAction("PPAPI.BrokerInfobarDisplayed"));
57 InfoBarService* infobar_service =
58 InfoBarService::FromWebContents(web_contents);
59 infobar_service->AddInfoBar(infobar_service->CreateConfirmInfoBar(
60 scoped_ptr<ConfirmInfoBarDelegate>(new PepperBrokerInfoBarDelegate(
61 url, plugin_path,
62 profile->GetPrefs()->GetString(prefs::kAcceptLanguages),
63 content_settings, tab_content_settings, callback))));
64 return;
67 bool allowed = (setting == CONTENT_SETTING_ALLOW);
68 content::RecordAction(allowed ?
69 base::UserMetricsAction("PPAPI.BrokerSettingAllow") :
70 base::UserMetricsAction("PPAPI.BrokerSettingDeny"));
71 tab_content_settings->SetPepperBrokerAllowed(allowed);
72 callback.Run(allowed);
75 PepperBrokerInfoBarDelegate::PepperBrokerInfoBarDelegate(
76 const GURL& url,
77 const base::FilePath& plugin_path,
78 const std::string& languages,
79 HostContentSettingsMap* content_settings,
80 TabSpecificContentSettings* tab_content_settings,
81 const base::Callback<void(bool)>& callback)
82 : ConfirmInfoBarDelegate(),
83 url_(url),
84 plugin_path_(plugin_path),
85 languages_(languages),
86 content_settings_(content_settings),
87 tab_content_settings_(tab_content_settings),
88 callback_(callback) {
91 PepperBrokerInfoBarDelegate::~PepperBrokerInfoBarDelegate() {
92 if (!callback_.is_null())
93 callback_.Run(false);
96 int PepperBrokerInfoBarDelegate::GetIconId() const {
97 return IDR_INFOBAR_PLUGIN_INSTALL;
100 base::string16 PepperBrokerInfoBarDelegate::GetMessageText() const {
101 content::PluginService* plugin_service =
102 content::PluginService::GetInstance();
103 content::WebPluginInfo plugin;
104 bool success = plugin_service->GetPluginInfoByPath(plugin_path_, &plugin);
105 DCHECK(success);
106 scoped_ptr<PluginMetadata> plugin_metadata(
107 PluginFinder::GetInstance()->GetPluginMetadata(plugin));
108 return l10n_util::GetStringFUTF16(
109 IDS_PEPPER_BROKER_MESSAGE, plugin_metadata->name(),
110 url_formatter::FormatUrl(url_.GetOrigin(), languages_));
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);
119 bool PepperBrokerInfoBarDelegate::Accept() {
120 DispatchCallback(true);
121 return true;
124 bool PepperBrokerInfoBarDelegate::Cancel() {
125 DispatchCallback(false);
126 return true;
129 base::string16 PepperBrokerInfoBarDelegate::GetLinkText() const {
130 return l10n_util::GetStringUTF16(IDS_LEARN_MORE);
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;
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);