Switch global error menu icon to vectorized MD asset
[chromium-blink-merge.git] / chrome / browser / pepper_broker_infobar_delegate.cc
blobd7ec3d3001fa8afe5461327bb3eb8e643a062363
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/tab_specific_content_settings.h"
9 #include "chrome/browser/infobars/infobar_service.h"
10 #include "chrome/browser/plugins/plugin_finder.h"
11 #include "chrome/browser/plugins/plugin_metadata.h"
12 #include "chrome/browser/profiles/profile.h"
13 #include "chrome/common/pref_names.h"
14 #include "chrome/grit/generated_resources.h"
15 #include "components/content_settings/core/browser/host_content_settings_map.h"
16 #include "components/infobars/core/infobar.h"
17 #include "components/url_formatter/url_formatter.h"
18 #include "content/public/browser/page_navigator.h"
19 #include "content/public/browser/plugin_service.h"
20 #include "content/public/browser/user_metrics.h"
21 #include "content/public/browser/web_contents.h"
22 #include "content/public/common/referrer.h"
23 #include "content/public/common/webplugininfo.h"
24 #include "grit/components_strings.h"
25 #include "grit/theme_resources.h"
26 #include "ui/base/l10n/l10n_util.h"
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;
43 TabSpecificContentSettings* tab_content_settings =
44 TabSpecificContentSettings::FromWebContents(web_contents);
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());
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(infobar_service->CreateConfirmInfoBar(
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;
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);
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) {
90 PepperBrokerInfoBarDelegate::~PepperBrokerInfoBarDelegate() {
91 if (!callback_.is_null())
92 callback_.Run(false);
95 int PepperBrokerInfoBarDelegate::GetIconId() const {
96 return IDR_INFOBAR_PLUGIN_INSTALL;
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(
108 IDS_PEPPER_BROKER_MESSAGE, plugin_metadata->name(),
109 url_formatter::FormatUrl(url_.GetOrigin(), languages_));
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);
120 return true;
123 bool PepperBrokerInfoBarDelegate::Cancel() {
124 DispatchCallback(false);
125 return true;
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 ui::PAGE_TRANSITION_LINK, false));
140 return 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);