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.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/theme_resources.h"
24 #include "net/base/net_util.h"
25 #include "ui/base/l10n/l10n_util.h"
29 void PepperBrokerInfoBarDelegate::Create(
30 content::WebContents
* web_contents
,
32 const base::FilePath
& plugin_path
,
33 const base::Callback
<void(bool)>& callback
) {
35 Profile::FromBrowserContext(web_contents
->GetBrowserContext());
36 // TODO(wad): Add ephemeral device ID support for broker in guest mode.
37 if (profile
->IsGuestSession()) {
42 TabSpecificContentSettings
* tab_content_settings
=
43 TabSpecificContentSettings::FromWebContents(web_contents
);
45 HostContentSettingsMap
* content_settings
=
46 profile
->GetHostContentSettingsMap();
47 ContentSetting setting
=
48 content_settings
->GetContentSetting(url
, url
,
49 CONTENT_SETTINGS_TYPE_PPAPI_BROKER
,
52 if (setting
== CONTENT_SETTING_ASK
) {
53 content::RecordAction(
54 base::UserMetricsAction("PPAPI.BrokerInfobarDisplayed"));
55 InfoBarService
* infobar_service
=
56 InfoBarService::FromWebContents(web_contents
);
57 infobar_service
->AddInfoBar(ConfirmInfoBarDelegate::CreateInfoBar(
58 scoped_ptr
<ConfirmInfoBarDelegate
>(new PepperBrokerInfoBarDelegate(
60 profile
->GetPrefs()->GetString(prefs::kAcceptLanguages
),
61 content_settings
, tab_content_settings
, callback
))));
65 bool allowed
= (setting
== CONTENT_SETTING_ALLOW
);
66 content::RecordAction(allowed
?
67 base::UserMetricsAction("PPAPI.BrokerSettingAllow") :
68 base::UserMetricsAction("PPAPI.BrokerSettingDeny"));
69 tab_content_settings
->SetPepperBrokerAllowed(allowed
);
70 callback
.Run(allowed
);
73 PepperBrokerInfoBarDelegate::PepperBrokerInfoBarDelegate(
75 const base::FilePath
& plugin_path
,
76 const std::string
& languages
,
77 HostContentSettingsMap
* content_settings
,
78 TabSpecificContentSettings
* tab_content_settings
,
79 const base::Callback
<void(bool)>& callback
)
80 : ConfirmInfoBarDelegate(),
82 plugin_path_(plugin_path
),
83 languages_(languages
),
84 content_settings_(content_settings
),
85 tab_content_settings_(tab_content_settings
),
89 PepperBrokerInfoBarDelegate::~PepperBrokerInfoBarDelegate() {
90 if (!callback_
.is_null())
94 int PepperBrokerInfoBarDelegate::GetIconID() const {
95 return IDR_INFOBAR_PLUGIN_INSTALL
;
98 base::string16
PepperBrokerInfoBarDelegate::GetMessageText() const {
99 content::PluginService
* plugin_service
=
100 content::PluginService::GetInstance();
101 content::WebPluginInfo plugin
;
102 bool success
= plugin_service
->GetPluginInfoByPath(plugin_path_
, &plugin
);
104 scoped_ptr
<PluginMetadata
> plugin_metadata(
105 PluginFinder::GetInstance()->GetPluginMetadata(plugin
));
106 return l10n_util::GetStringFUTF16(IDS_PEPPER_BROKER_MESSAGE
,
107 plugin_metadata
->name(),
108 net::FormatUrl(url_
.GetOrigin(),
112 base::string16
PepperBrokerInfoBarDelegate::GetButtonLabel(
113 InfoBarButton button
) const {
114 return l10n_util::GetStringUTF16((button
== BUTTON_OK
) ?
115 IDS_PEPPER_BROKER_ALLOW_BUTTON
: IDS_PEPPER_BROKER_DENY_BUTTON
);
118 bool PepperBrokerInfoBarDelegate::Accept() {
119 DispatchCallback(true);
123 bool PepperBrokerInfoBarDelegate::Cancel() {
124 DispatchCallback(false);
128 base::string16
PepperBrokerInfoBarDelegate::GetLinkText() const {
129 return l10n_util::GetStringUTF16(IDS_LEARN_MORE
);
132 bool PepperBrokerInfoBarDelegate::LinkClicked(
133 WindowOpenDisposition disposition
) {
134 GURL
learn_more_url("https://support.google.com/chrome/?p=ib_pepper_broker");
135 InfoBarService::WebContentsFromInfoBar(infobar())->OpenURL(
136 content::OpenURLParams(
137 learn_more_url
, content::Referrer(),
138 (disposition
== CURRENT_TAB
) ? NEW_FOREGROUND_TAB
: disposition
,
139 content::PAGE_TRANSITION_LINK
, false));
143 void PepperBrokerInfoBarDelegate::DispatchCallback(bool result
) {
144 content::RecordAction(result
?
145 base::UserMetricsAction("PPAPI.BrokerInfobarClickedAllow") :
146 base::UserMetricsAction("PPAPI.BrokerInfobarClickedDeny"));
147 callback_
.Run(result
);
148 callback_
= base::Callback
<void(bool)>();
149 content_settings_
->SetContentSetting(
150 ContentSettingsPattern::FromURLNoWildcard(url_
),
151 ContentSettingsPattern::Wildcard(),
152 CONTENT_SETTINGS_TYPE_PPAPI_BROKER
,
153 std::string(), result
? CONTENT_SETTING_ALLOW
: CONTENT_SETTING_BLOCK
);
154 tab_content_settings_
->SetPepperBrokerAllowed(result
);