| OLD | NEW |
| (Empty) |
| 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_observer.h" | |
| 6 | |
| 7 #include "base/memory/scoped_ptr.h" | |
| 8 #include "chrome/browser/api/infobars/confirm_infobar_delegate.h" | |
| 9 #include "chrome/browser/content_settings/host_content_settings_map.h" | |
| 10 #include "chrome/browser/infobars/infobar_tab_helper.h" | |
| 11 #include "chrome/browser/plugins/plugin_finder.h" | |
| 12 #include "chrome/browser/plugins/plugin_metadata.h" | |
| 13 #include "chrome/browser/prefs/pref_service.h" | |
| 14 #include "chrome/browser/profiles/profile.h" | |
| 15 #include "chrome/common/pref_names.h" | |
| 16 #include "content/public/browser/page_navigator.h" | |
| 17 #include "content/public/browser/plugin_service.h" | |
| 18 #include "content/public/browser/user_metrics.h" | |
| 19 #include "content/public/browser/web_contents.h" | |
| 20 #include "content/public/common/referrer.h" | |
| 21 #include "grit/generated_resources.h" | |
| 22 #include "grit/theme_resources.h" | |
| 23 #include "net/base/net_util.h" | |
| 24 #include "ui/base/l10n/l10n_util.h" | |
| 25 #include "ui/base/resource/resource_bundle.h" | |
| 26 #include "webkit/plugins/npapi/plugin_list.h" | |
| 27 #include "webkit/plugins/webplugininfo.h" | |
| 28 | |
| 29 using content::OpenURLParams; | |
| 30 using content::Referrer; | |
| 31 using content::WebContents; | |
| 32 | |
| 33 DEFINE_WEB_CONTENTS_USER_DATA_KEY(PepperBrokerObserver) | |
| 34 | |
| 35 namespace { | |
| 36 | |
| 37 // The URL for the "learn more" article about the PPAPI broker. | |
| 38 const char kPpapiBrokerLearnMoreUrl[] = | |
| 39 "https://support.google.com/chrome/?p=ib_pepper_broker"; | |
| 40 | |
| 41 class PepperBrokerInfoBarDelegate : public ConfirmInfoBarDelegate { | |
| 42 public: | |
| 43 static void Show( | |
| 44 content::WebContents* web_contents, | |
| 45 const GURL& url, | |
| 46 const FilePath& plugin_path, | |
| 47 const base::Callback<void(bool)>& callback); | |
| 48 | |
| 49 PepperBrokerInfoBarDelegate( | |
| 50 InfoBarTabHelper* helper, | |
| 51 const GURL& url, | |
| 52 const FilePath& plugin_path, | |
| 53 const std::string& languages, | |
| 54 HostContentSettingsMap* content_settings, | |
| 55 const base::Callback<void(bool)>& callback); | |
| 56 virtual ~PepperBrokerInfoBarDelegate(); | |
| 57 | |
| 58 // ConfirmInfoBarDelegate: | |
| 59 virtual string16 GetMessageText() const OVERRIDE; | |
| 60 virtual int GetButtons() const OVERRIDE; | |
| 61 virtual string16 GetButtonLabel(InfoBarButton button) const OVERRIDE; | |
| 62 virtual bool Accept() OVERRIDE; | |
| 63 virtual bool Cancel() OVERRIDE; | |
| 64 virtual string16 GetLinkText() const OVERRIDE; | |
| 65 virtual bool LinkClicked(WindowOpenDisposition disposition) OVERRIDE; | |
| 66 virtual gfx::Image* GetIcon() const OVERRIDE; | |
| 67 | |
| 68 private: | |
| 69 void DispatchCallback(bool result); | |
| 70 | |
| 71 const GURL url_; | |
| 72 const FilePath plugin_path_; | |
| 73 const std::string languages_; | |
| 74 HostContentSettingsMap* content_settings_; | |
| 75 base::Callback<void(bool)> callback_; | |
| 76 | |
| 77 DISALLOW_COPY_AND_ASSIGN(PepperBrokerInfoBarDelegate); | |
| 78 }; | |
| 79 | |
| 80 PepperBrokerInfoBarDelegate::PepperBrokerInfoBarDelegate( | |
| 81 InfoBarTabHelper* helper, | |
| 82 const GURL& url, | |
| 83 const FilePath& plugin_path, | |
| 84 const std::string& languages, | |
| 85 HostContentSettingsMap* content_settings, | |
| 86 const base::Callback<void(bool)>& callback) | |
| 87 : ConfirmInfoBarDelegate(helper), | |
| 88 url_(url), | |
| 89 plugin_path_(plugin_path), | |
| 90 languages_(languages), | |
| 91 content_settings_(content_settings), | |
| 92 callback_(callback) { | |
| 93 } | |
| 94 | |
| 95 PepperBrokerInfoBarDelegate::~PepperBrokerInfoBarDelegate() { | |
| 96 if (!callback_.is_null()) | |
| 97 callback_.Run(false); | |
| 98 } | |
| 99 | |
| 100 string16 PepperBrokerInfoBarDelegate::GetMessageText() const { | |
| 101 content::PluginService* plugin_service = | |
| 102 content::PluginService::GetInstance(); | |
| 103 webkit::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(IDS_PEPPER_BROKER_MESSAGE, | |
| 109 plugin_metadata->name(), | |
| 110 net::FormatUrl(url_.GetOrigin(), | |
| 111 languages_)); | |
| 112 } | |
| 113 | |
| 114 int PepperBrokerInfoBarDelegate::GetButtons() const { | |
| 115 return BUTTON_OK | BUTTON_CANCEL; | |
| 116 } | |
| 117 | |
| 118 string16 PepperBrokerInfoBarDelegate::GetButtonLabel( | |
| 119 InfoBarButton button) const { | |
| 120 switch (button) { | |
| 121 case BUTTON_OK: | |
| 122 return l10n_util::GetStringUTF16(IDS_PEPPER_BROKER_ALLOW_BUTTON); | |
| 123 case BUTTON_CANCEL: | |
| 124 return l10n_util::GetStringUTF16(IDS_PEPPER_BROKER_DENY_BUTTON); | |
| 125 default: | |
| 126 NOTREACHED(); | |
| 127 return string16(); | |
| 128 } | |
| 129 } | |
| 130 | |
| 131 bool PepperBrokerInfoBarDelegate::Accept() { | |
| 132 DispatchCallback(true); | |
| 133 return true; | |
| 134 } | |
| 135 | |
| 136 bool PepperBrokerInfoBarDelegate::Cancel() { | |
| 137 DispatchCallback(false); | |
| 138 return true; | |
| 139 } | |
| 140 | |
| 141 string16 PepperBrokerInfoBarDelegate::GetLinkText() const { | |
| 142 return l10n_util::GetStringUTF16(IDS_LEARN_MORE); | |
| 143 } | |
| 144 | |
| 145 bool PepperBrokerInfoBarDelegate::LinkClicked( | |
| 146 WindowOpenDisposition disposition) { | |
| 147 OpenURLParams params( | |
| 148 GURL(kPpapiBrokerLearnMoreUrl), Referrer(), | |
| 149 (disposition == CURRENT_TAB) ? NEW_FOREGROUND_TAB : disposition, | |
| 150 content::PAGE_TRANSITION_LINK, | |
| 151 false); | |
| 152 owner()->GetWebContents()->OpenURL(params); | |
| 153 return false; | |
| 154 } | |
| 155 | |
| 156 gfx::Image* PepperBrokerInfoBarDelegate::GetIcon() const { | |
| 157 return &ResourceBundle::GetSharedInstance().GetNativeImageNamed( | |
| 158 IDR_INFOBAR_PLUGIN_INSTALL); | |
| 159 } | |
| 160 | |
| 161 void PepperBrokerInfoBarDelegate::DispatchCallback(bool result) { | |
| 162 content::RecordAction(result ? | |
| 163 content::UserMetricsAction("PPAPI.BrokerInfobarClickedAllow") : | |
| 164 content::UserMetricsAction("PPAPI.BrokerInfobarClickedDeny")); | |
| 165 callback_.Run(result); | |
| 166 callback_ = base::Callback<void(bool)>(); | |
| 167 content_settings_->SetContentSetting( | |
| 168 ContentSettingsPattern::FromURLNoWildcard(url_), | |
| 169 ContentSettingsPattern::Wildcard(), | |
| 170 CONTENT_SETTINGS_TYPE_PPAPI_BROKER, | |
| 171 std::string(), result ? CONTENT_SETTING_ALLOW : CONTENT_SETTING_BLOCK); | |
| 172 } | |
| 173 | |
| 174 } // namespace | |
| 175 | |
| 176 PepperBrokerObserver::PepperBrokerObserver(WebContents* web_contents) | |
| 177 : WebContentsObserver(web_contents) {} | |
| 178 | |
| 179 PepperBrokerObserver::~PepperBrokerObserver() {} | |
| 180 | |
| 181 bool PepperBrokerObserver::RequestPpapiBrokerPermission( | |
| 182 WebContents* web_contents, | |
| 183 const GURL& url, | |
| 184 const FilePath& plugin_path, | |
| 185 const base::Callback<void(bool)>& callback) { | |
| 186 Profile* profile = | |
| 187 Profile::FromBrowserContext(web_contents->GetBrowserContext()); | |
| 188 // TODO(wad): Add ephemeral device ID support for broker in guest mode. | |
| 189 if (Profile::IsGuestSession()) { | |
| 190 callback.Run(false); | |
| 191 return true; | |
| 192 } | |
| 193 | |
| 194 HostContentSettingsMap* content_settings = | |
| 195 profile->GetHostContentSettingsMap(); | |
| 196 ContentSetting setting = | |
| 197 content_settings->GetContentSetting(url, url, | |
| 198 CONTENT_SETTINGS_TYPE_PPAPI_BROKER, | |
| 199 std::string()); | |
| 200 switch (setting) { | |
| 201 case CONTENT_SETTING_ALLOW: { | |
| 202 content::RecordAction( | |
| 203 content::UserMetricsAction("PPAPI.BrokerSettingAllow")); | |
| 204 callback.Run(true); | |
| 205 break; | |
| 206 } | |
| 207 case CONTENT_SETTING_BLOCK: { | |
| 208 content::RecordAction( | |
| 209 content::UserMetricsAction("PPAPI.BrokerSettingDeny")); | |
| 210 callback.Run(false); | |
| 211 break; | |
| 212 } | |
| 213 case CONTENT_SETTING_ASK: { | |
| 214 content::RecordAction( | |
| 215 content::UserMetricsAction("PPAPI.BrokerInfobarDisplayed")); | |
| 216 | |
| 217 InfoBarTabHelper* infobar_helper = | |
| 218 InfoBarTabHelper::FromWebContents(web_contents); | |
| 219 std::string languages = | |
| 220 profile->GetPrefs()->GetString(prefs::kAcceptLanguages); | |
| 221 infobar_helper->AddInfoBar( | |
| 222 new PepperBrokerInfoBarDelegate( | |
| 223 infobar_helper, url, plugin_path, languages, content_settings, | |
| 224 callback)); | |
| 225 break; | |
| 226 } | |
| 227 default: | |
| 228 NOTREACHED(); | |
| 229 } | |
| 230 | |
| 231 return true; | |
| 232 } | |
| OLD | NEW |