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 "chrome/browser/api/infobars/confirm_infobar_delegate.h" |
| 8 #include "chrome/browser/content_settings/host_content_settings_map.h" |
| 9 #include "chrome/browser/infobars/infobar_tab_helper.h" |
| 10 #include "chrome/browser/prefs/pref_service.h" |
| 11 #include "chrome/browser/profiles/profile.h" |
| 12 #include "chrome/browser/ui/tab_contents/tab_contents.h" |
| 13 #include "chrome/common/pref_names.h" |
| 14 #include "content/public/browser/page_navigator.h" |
| 15 #include "content/public/browser/plugin_service.h" |
| 16 #include "content/public/browser/web_contents.h" |
| 17 #include "content/public/common/referrer.h" |
| 18 #include "grit/generated_resources.h" |
| 19 #include "grit/theme_resources.h" |
| 20 #include "net/base/net_util.h" |
| 21 #include "ui/base/l10n/l10n_util.h" |
| 22 #include "ui/base/resource/resource_bundle.h" |
| 23 #include "webkit/plugins/npapi/plugin_group.h" |
| 24 #include "webkit/plugins/npapi/plugin_list.h" |
| 25 #include "webkit/plugins/webplugininfo.h" |
| 26 |
| 27 using content::OpenURLParams; |
| 28 using content::Referrer; |
| 29 using content::WebContents; |
| 30 |
| 31 namespace { |
| 32 |
| 33 // The URL for the "learn more" article about the PPAPI broker. |
| 34 const char kPpapiBrokerLearnMoreUrl[] = |
| 35 "https://support.google.com/chrome/?p=ib_pepper_broker"; |
| 36 |
| 37 class PepperBrokerInfoBarDelegate : public ConfirmInfoBarDelegate { |
| 38 public: |
| 39 static void Show( |
| 40 content::WebContents* web_contents, |
| 41 const GURL& url, |
| 42 const FilePath& plugin_path, |
| 43 const base::Callback<void(bool)>& callback); |
| 44 |
| 45 PepperBrokerInfoBarDelegate( |
| 46 InfoBarTabHelper* helper, |
| 47 const GURL& url, |
| 48 const FilePath& plugin_path, |
| 49 const std::string& languages, |
| 50 HostContentSettingsMap* content_settings, |
| 51 const base::Callback<void(bool)>& callback); |
| 52 virtual ~PepperBrokerInfoBarDelegate(); |
| 53 |
| 54 // ConfirmInfoBarDelegate: |
| 55 virtual string16 GetMessageText() const OVERRIDE; |
| 56 virtual int GetButtons() const OVERRIDE; |
| 57 virtual string16 GetButtonLabel(InfoBarButton button) const OVERRIDE; |
| 58 virtual bool Accept() OVERRIDE; |
| 59 virtual bool Cancel() OVERRIDE; |
| 60 virtual string16 GetLinkText() const OVERRIDE; |
| 61 virtual bool LinkClicked(WindowOpenDisposition disposition) OVERRIDE; |
| 62 virtual gfx::Image* GetIcon() const OVERRIDE; |
| 63 |
| 64 private: |
| 65 const GURL url_; |
| 66 const FilePath plugin_path_; |
| 67 const std::string languages_; |
| 68 HostContentSettingsMap* content_settings_; |
| 69 base::Callback<void(bool)> callback_; |
| 70 |
| 71 DISALLOW_COPY_AND_ASSIGN(PepperBrokerInfoBarDelegate); |
| 72 }; |
| 73 |
| 74 PepperBrokerInfoBarDelegate::PepperBrokerInfoBarDelegate( |
| 75 InfoBarTabHelper* helper, |
| 76 const GURL& url, |
| 77 const FilePath& plugin_path, |
| 78 const std::string& languages, |
| 79 HostContentSettingsMap* content_settings, |
| 80 const base::Callback<void(bool)>& callback) |
| 81 : ConfirmInfoBarDelegate(helper), |
| 82 url_(url), |
| 83 plugin_path_(plugin_path), |
| 84 languages_(languages), |
| 85 content_settings_(content_settings), |
| 86 callback_(callback) { |
| 87 } |
| 88 |
| 89 PepperBrokerInfoBarDelegate::~PepperBrokerInfoBarDelegate() { |
| 90 if (!callback_.is_null()) |
| 91 callback_.Run(false); |
| 92 } |
| 93 |
| 94 string16 PepperBrokerInfoBarDelegate::GetMessageText() const { |
| 95 content::PluginService* plugin_service = |
| 96 content::PluginService::GetInstance(); |
| 97 webkit::WebPluginInfo plugin; |
| 98 bool success = plugin_service->GetPluginInfoByPath(plugin_path_, &plugin); |
| 99 DCHECK(success); |
| 100 scoped_ptr<webkit::npapi::PluginGroup> plugin_group( |
| 101 plugin_service->GetPluginList()->GetPluginGroup(plugin)); |
| 102 return l10n_util::GetStringFUTF16(IDS_PPAPI_BROKER_INFOBAR_QUESTION, |
| 103 plugin_group->GetGroupName(), |
| 104 net::FormatUrl(url_, languages_)); |
| 105 } |
| 106 |
| 107 int PepperBrokerInfoBarDelegate::GetButtons() const { |
| 108 return BUTTON_OK | BUTTON_CANCEL; |
| 109 } |
| 110 |
| 111 string16 PepperBrokerInfoBarDelegate::GetButtonLabel( |
| 112 InfoBarButton button) const { |
| 113 switch (button) { |
| 114 case BUTTON_OK: |
| 115 return l10n_util::GetStringUTF16(IDS_PPAPI_BROKER_ALLOW_BUTTON); |
| 116 case BUTTON_CANCEL: |
| 117 return l10n_util::GetStringUTF16(IDS_PPAPI_BROKER_DENY_BUTTON); |
| 118 default: |
| 119 NOTREACHED(); |
| 120 return string16(); |
| 121 } |
| 122 } |
| 123 |
| 124 bool PepperBrokerInfoBarDelegate::Accept() { |
| 125 callback_.Run(true); |
| 126 callback_ = base::Callback<void(bool)>(); |
| 127 content_settings_->SetContentSetting( |
| 128 ContentSettingsPattern::FromURLNoWildcard(url_), |
| 129 ContentSettingsPattern::Wildcard(), |
| 130 CONTENT_SETTINGS_TYPE_PPAPI_BROKER, |
| 131 std::string(), CONTENT_SETTING_ALLOW); |
| 132 return true; |
| 133 } |
| 134 |
| 135 bool PepperBrokerInfoBarDelegate::Cancel() { |
| 136 callback_.Run(false); |
| 137 callback_ = base::Callback<void(bool)>(); |
| 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 } // namespace |
| 162 |
| 163 PepperBrokerObserver::PepperBrokerObserver(WebContents* web_contents) |
| 164 : WebContentsObserver(web_contents) {} |
| 165 |
| 166 PepperBrokerObserver::~PepperBrokerObserver() {} |
| 167 |
| 168 bool PepperBrokerObserver::RequestPpapiBrokerPermission( |
| 169 WebContents* web_contents, |
| 170 const GURL& url, |
| 171 const FilePath& plugin_path, |
| 172 const base::Callback<void(bool)>& callback) { |
| 173 TabContents* tab = TabContents::FromWebContents(web_contents); |
| 174 if (!tab) { |
| 175 callback.Run(false); |
| 176 return true; |
| 177 } |
| 178 |
| 179 Profile* profile = tab->profile(); |
| 180 // Disallow broker access in incognito mode. |
| 181 if (profile->IsOffTheRecord() || Profile::IsGuestSession()) { |
| 182 callback.Run(false); |
| 183 return true; |
| 184 } |
| 185 |
| 186 HostContentSettingsMap* content_settings = |
| 187 profile->GetHostContentSettingsMap(); |
| 188 ContentSetting setting = |
| 189 content_settings->GetContentSetting(url, url, |
| 190 CONTENT_SETTINGS_TYPE_PPAPI_BROKER, |
| 191 std::string()); |
| 192 switch (setting) { |
| 193 case CONTENT_SETTING_ALLOW: { |
| 194 callback.Run(true); |
| 195 break; |
| 196 } |
| 197 case CONTENT_SETTING_BLOCK: { |
| 198 callback.Run(false); |
| 199 break; |
| 200 } |
| 201 case CONTENT_SETTING_ASK: { |
| 202 InfoBarTabHelper* infobar_helper = tab->infobar_tab_helper(); |
| 203 std::string languages = |
| 204 profile->GetPrefs()->GetString(prefs::kAcceptLanguages); |
| 205 infobar_helper->AddInfoBar( |
| 206 new PepperBrokerInfoBarDelegate( |
| 207 infobar_helper, url, plugin_path, languages, content_settings, |
| 208 callback)); |
| 209 break; |
| 210 } |
| 211 default: |
| 212 NOTREACHED(); |
| 213 } |
| 214 |
| 215 return true; |
| 216 } |
OLD | NEW |