Re-land https://src.chromium.org/viewvc/chrome?view=rev&revision=145774
[chromium-blink-merge.git] / chrome / browser / plugin_infobar_delegates.cc
blob577a4fbebabf848afe1d9112c23c58df5afb6d55
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/plugin_infobar_delegates.h"
7 #include "base/utf_string_conversions.h"
8 #include "chrome/browser/content_settings/host_content_settings_map.h"
9 #include "chrome/browser/google/google_util.h"
10 #include "chrome/browser/infobars/infobar_tab_helper.h"
11 #include "chrome/browser/plugin_observer.h"
12 #include "chrome/browser/ui/tab_contents/tab_contents.h"
13 #include "chrome/common/render_messages.h"
14 #include "chrome/common/url_constants.h"
15 #include "content/public/browser/render_view_host.h"
16 #include "content/public/browser/user_metrics.h"
17 #include "content/public/browser/web_contents.h"
18 #include "grit/generated_resources.h"
19 #include "grit/locale_settings.h"
20 #include "grit/theme_resources_standard.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"
25 #if defined(OS_WIN)
26 #include <shellapi.h>
27 #include "ui/base/win/shell.h"
28 #endif
30 #if defined(ENABLE_PLUGIN_INSTALLATION)
31 #include "chrome/browser/plugin_installer.h"
32 #endif // defined(ENABLE_PLUGIN_INSTALLATION)
34 using content::OpenURLParams;
35 using content::Referrer;
36 using content::UserMetricsAction;
38 PluginInfoBarDelegate::PluginInfoBarDelegate(InfoBarTabHelper* infobar_helper,
39 const string16& name,
40 const std::string& identifier)
41 : ConfirmInfoBarDelegate(infobar_helper),
42 name_(name),
43 identifier_(identifier) {
46 PluginInfoBarDelegate::~PluginInfoBarDelegate() {
49 bool PluginInfoBarDelegate::LinkClicked(WindowOpenDisposition disposition) {
50 OpenURLParams params(
51 GURL(GetLearnMoreURL()), Referrer(),
52 (disposition == CURRENT_TAB) ? NEW_FOREGROUND_TAB : disposition,
53 content::PAGE_TRANSITION_LINK,
54 false);
55 owner()->web_contents()->OpenURL(params);
56 return false;
59 void PluginInfoBarDelegate::LoadBlockedPlugins() {
60 owner()->Send(
61 new ChromeViewMsg_LoadBlockedPlugins(owner()->routing_id(), identifier_));
64 gfx::Image* PluginInfoBarDelegate::GetIcon() const {
65 return &ResourceBundle::GetSharedInstance().GetNativeImageNamed(
66 IDR_INFOBAR_PLUGIN_INSTALL);
69 string16 PluginInfoBarDelegate::GetLinkText() const {
70 return l10n_util::GetStringUTF16(IDS_LEARN_MORE);
73 // UnauthorizedPluginInfoBarDelegate ------------------------------------------
75 UnauthorizedPluginInfoBarDelegate::UnauthorizedPluginInfoBarDelegate(
76 InfoBarTabHelper* infobar_helper,
77 HostContentSettingsMap* content_settings,
78 const string16& utf16_name,
79 const std::string& identifier)
80 : PluginInfoBarDelegate(infobar_helper, utf16_name, identifier),
81 content_settings_(content_settings) {
82 content::RecordAction(UserMetricsAction("BlockedPluginInfobar.Shown"));
83 std::string name = UTF16ToUTF8(utf16_name);
84 if (name == webkit::npapi::PluginGroup::kJavaGroupName)
85 content::RecordAction(
86 UserMetricsAction("BlockedPluginInfobar.Shown.Java"));
87 else if (name == webkit::npapi::PluginGroup::kQuickTimeGroupName)
88 content::RecordAction(
89 UserMetricsAction("BlockedPluginInfobar.Shown.QuickTime"));
90 else if (name == webkit::npapi::PluginGroup::kShockwaveGroupName)
91 content::RecordAction(
92 UserMetricsAction("BlockedPluginInfobar.Shown.Shockwave"));
93 else if (name == webkit::npapi::PluginGroup::kRealPlayerGroupName)
94 content::RecordAction(
95 UserMetricsAction("BlockedPluginInfobar.Shown.RealPlayer"));
96 else if (name == webkit::npapi::PluginGroup::kWindowsMediaPlayerGroupName)
97 content::RecordAction(
98 UserMetricsAction("BlockedPluginInfobar.Shown.WindowsMediaPlayer"));
101 UnauthorizedPluginInfoBarDelegate::~UnauthorizedPluginInfoBarDelegate() {
102 content::RecordAction(UserMetricsAction("BlockedPluginInfobar.Closed"));
105 std::string UnauthorizedPluginInfoBarDelegate::GetLearnMoreURL() const {
106 return chrome::kBlockedPluginLearnMoreURL;
109 string16 UnauthorizedPluginInfoBarDelegate::GetMessageText() const {
110 return l10n_util::GetStringFUTF16(IDS_PLUGIN_NOT_AUTHORIZED, name_);
113 string16 UnauthorizedPluginInfoBarDelegate::GetButtonLabel(
114 InfoBarButton button) const {
115 return l10n_util::GetStringUTF16((button == BUTTON_OK) ?
116 IDS_PLUGIN_ENABLE_TEMPORARILY : IDS_PLUGIN_ENABLE_ALWAYS);
119 bool UnauthorizedPluginInfoBarDelegate::Accept() {
120 content::RecordAction(
121 UserMetricsAction("BlockedPluginInfobar.AllowThisTime"));
122 LoadBlockedPlugins();
123 return true;
126 bool UnauthorizedPluginInfoBarDelegate::Cancel() {
127 content::RecordAction(
128 UserMetricsAction("BlockedPluginInfobar.AlwaysAllow"));
129 content_settings_->AddExceptionForURL(owner()->web_contents()->GetURL(),
130 owner()->web_contents()->GetURL(),
131 CONTENT_SETTINGS_TYPE_PLUGINS,
132 std::string(),
133 CONTENT_SETTING_ALLOW);
134 LoadBlockedPlugins();
135 return true;
138 void UnauthorizedPluginInfoBarDelegate::InfoBarDismissed() {
139 content::RecordAction(
140 UserMetricsAction("BlockedPluginInfobar.Dismissed"));
143 bool UnauthorizedPluginInfoBarDelegate::LinkClicked(
144 WindowOpenDisposition disposition) {
145 content::RecordAction(
146 UserMetricsAction("BlockedPluginInfobar.LearnMore"));
147 return PluginInfoBarDelegate::LinkClicked(disposition);
150 #if defined(ENABLE_PLUGIN_INSTALLATION)
151 // OutdatedPluginInfoBarDelegate ----------------------------------------------
153 InfoBarDelegate* OutdatedPluginInfoBarDelegate::Create(
154 PluginObserver* observer,
155 PluginInstaller* installer) {
156 string16 message;
157 switch (installer->state()) {
158 case PluginInstaller::INSTALLER_STATE_IDLE:
159 message = l10n_util::GetStringFUTF16(IDS_PLUGIN_OUTDATED_PROMPT,
160 installer->name());
161 break;
162 case PluginInstaller::INSTALLER_STATE_DOWNLOADING:
163 message = l10n_util::GetStringFUTF16(IDS_PLUGIN_DOWNLOADING,
164 installer->name());
165 break;
167 return new OutdatedPluginInfoBarDelegate(
168 observer, installer, message);
171 OutdatedPluginInfoBarDelegate::OutdatedPluginInfoBarDelegate(
172 PluginObserver* observer,
173 PluginInstaller* installer,
174 const string16& message)
175 : PluginInfoBarDelegate(
176 observer->tab_contents()->infobar_tab_helper(),
177 installer->name(),
178 installer->identifier()),
179 WeakPluginInstallerObserver(installer),
180 observer_(observer),
181 message_(message) {
182 content::RecordAction(UserMetricsAction("OutdatedPluginInfobar.Shown"));
183 std::string name = UTF16ToUTF8(installer->name());
184 if (name == webkit::npapi::PluginGroup::kJavaGroupName)
185 content::RecordAction(
186 UserMetricsAction("OutdatedPluginInfobar.Shown.Java"));
187 else if (name == webkit::npapi::PluginGroup::kQuickTimeGroupName)
188 content::RecordAction(
189 UserMetricsAction("OutdatedPluginInfobar.Shown.QuickTime"));
190 else if (name == webkit::npapi::PluginGroup::kShockwaveGroupName)
191 content::RecordAction(
192 UserMetricsAction("OutdatedPluginInfobar.Shown.Shockwave"));
193 else if (name == webkit::npapi::PluginGroup::kRealPlayerGroupName)
194 content::RecordAction(
195 UserMetricsAction("OutdatedPluginInfobar.Shown.RealPlayer"));
196 else if (name == webkit::npapi::PluginGroup::kSilverlightGroupName)
197 content::RecordAction(
198 UserMetricsAction("OutdatedPluginInfobar.Shown.Silverlight"));
199 else if (name == webkit::npapi::PluginGroup::kAdobeReaderGroupName)
200 content::RecordAction(
201 UserMetricsAction("OutdatedPluginInfobar.Shown.Reader"));
204 OutdatedPluginInfoBarDelegate::~OutdatedPluginInfoBarDelegate() {
205 content::RecordAction(UserMetricsAction("OutdatedPluginInfobar.Closed"));
208 std::string OutdatedPluginInfoBarDelegate::GetLearnMoreURL() const {
209 return chrome::kOutdatedPluginLearnMoreURL;
212 string16 OutdatedPluginInfoBarDelegate::GetMessageText() const {
213 return message_;
216 string16 OutdatedPluginInfoBarDelegate::GetButtonLabel(
217 InfoBarButton button) const {
218 return l10n_util::GetStringUTF16((button == BUTTON_OK) ?
219 IDS_PLUGIN_UPDATE : IDS_PLUGIN_ENABLE_TEMPORARILY);
222 bool OutdatedPluginInfoBarDelegate::Accept() {
223 content::RecordAction(UserMetricsAction("OutdatedPluginInfobar.Update"));
224 if (installer()->state() != PluginInstaller::INSTALLER_STATE_IDLE) {
225 NOTREACHED();
226 return false;
229 content::WebContents* web_contents = owner()->web_contents();
230 if (installer()->url_for_display()) {
231 installer()->OpenDownloadURL(web_contents);
232 } else {
233 installer()->StartInstalling(observer_->tab_contents());
235 return false;
238 bool OutdatedPluginInfoBarDelegate::Cancel() {
239 content::RecordAction(
240 UserMetricsAction("OutdatedPluginInfobar.AllowThisTime"));
241 LoadBlockedPlugins();
242 return true;
245 void OutdatedPluginInfoBarDelegate::InfoBarDismissed() {
246 content::RecordAction(
247 UserMetricsAction("OutdatedPluginInfobar.Dismissed"));
250 bool OutdatedPluginInfoBarDelegate::LinkClicked(
251 WindowOpenDisposition disposition) {
252 content::RecordAction(
253 UserMetricsAction("OutdatedPluginInfobar.LearnMore"));
254 return PluginInfoBarDelegate::LinkClicked(disposition);
257 void OutdatedPluginInfoBarDelegate::DownloadStarted() {
258 ReplaceWithInfoBar(l10n_util::GetStringFUTF16(IDS_PLUGIN_DOWNLOADING,
259 installer()->name()));
262 void OutdatedPluginInfoBarDelegate::DownloadError(const std::string& message) {
263 ReplaceWithInfoBar(
264 l10n_util::GetStringFUTF16(IDS_PLUGIN_DOWNLOAD_ERROR_SHORT,
265 installer()->name()));
268 void OutdatedPluginInfoBarDelegate::DownloadCancelled() {
269 ReplaceWithInfoBar(l10n_util::GetStringFUTF16(IDS_PLUGIN_DOWNLOAD_CANCELLED,
270 installer()->name()));
273 void OutdatedPluginInfoBarDelegate::DownloadFinished() {
274 ReplaceWithInfoBar(l10n_util::GetStringFUTF16(IDS_PLUGIN_UPDATING,
275 installer()->name()));
278 void OutdatedPluginInfoBarDelegate::OnlyWeakObserversLeft() {
279 if (owner())
280 owner()->RemoveInfoBar(this);
283 void OutdatedPluginInfoBarDelegate::ReplaceWithInfoBar(
284 const string16& message) {
285 // Return early if the message doesn't change. This is important in case the
286 // PluginInstaller is still iterating over its observers (otherwise we would
287 // keep replacing infobar delegates infinitely).
288 if (message_ == message)
289 return;
290 if (!owner())
291 return;
292 InfoBarDelegate* delegate = new PluginInstallerInfoBarDelegate(
293 owner(), installer(), base::Closure(), false, message);
294 owner()->ReplaceInfoBar(this, delegate);
297 // PluginInstallerInfoBarDelegate ---------------------------------------------
299 PluginInstallerInfoBarDelegate::PluginInstallerInfoBarDelegate(
300 InfoBarTabHelper* infobar_helper,
301 PluginInstaller* installer,
302 const base::Closure& callback,
303 bool new_install,
304 const string16& message)
305 : ConfirmInfoBarDelegate(infobar_helper),
306 WeakPluginInstallerObserver(installer),
307 callback_(callback),
308 new_install_(new_install),
309 message_(message) {
312 PluginInstallerInfoBarDelegate::~PluginInstallerInfoBarDelegate() {
315 InfoBarDelegate* PluginInstallerInfoBarDelegate::Create(
316 InfoBarTabHelper* infobar_helper,
317 PluginInstaller* installer,
318 const base::Closure& callback) {
319 string16 message;
320 const string16& plugin_name = installer->name();
321 switch (installer->state()) {
322 case PluginInstaller::INSTALLER_STATE_IDLE:
323 message = l10n_util::GetStringFUTF16(
324 IDS_PLUGININSTALLER_INSTALLPLUGIN_PROMPT, plugin_name);
325 break;
326 case PluginInstaller::INSTALLER_STATE_DOWNLOADING:
327 message = l10n_util::GetStringFUTF16(IDS_PLUGIN_DOWNLOADING, plugin_name);
328 break;
330 return new PluginInstallerInfoBarDelegate(
331 infobar_helper, installer, callback, true, message);
334 gfx::Image* PluginInstallerInfoBarDelegate::GetIcon() const {
335 return &ResourceBundle::GetSharedInstance().GetNativeImageNamed(
336 IDR_INFOBAR_PLUGIN_INSTALL);
339 string16 PluginInstallerInfoBarDelegate::GetMessageText() const {
340 return message_;
343 int PluginInstallerInfoBarDelegate::GetButtons() const {
344 return callback_.is_null() ? BUTTON_NONE : BUTTON_OK;
347 string16 PluginInstallerInfoBarDelegate::GetButtonLabel(
348 InfoBarButton button) const {
349 DCHECK_EQ(BUTTON_OK, button);
350 return l10n_util::GetStringUTF16(IDS_PLUGININSTALLER_INSTALLPLUGIN_BUTTON);
353 bool PluginInstallerInfoBarDelegate::Accept() {
354 callback_.Run();
355 return false;
358 string16 PluginInstallerInfoBarDelegate::GetLinkText() const {
359 return l10n_util::GetStringUTF16(
360 new_install_ ? IDS_PLUGININSTALLER_PROBLEMSINSTALLING
361 : IDS_PLUGININSTALLER_PROBLEMSUPDATING);
364 bool PluginInstallerInfoBarDelegate::LinkClicked(
365 WindowOpenDisposition disposition) {
366 GURL url(installer()->help_url());
367 if (url.is_empty()) {
368 url = google_util::AppendGoogleLocaleParam(GURL(
369 "https://www.google.com/support/chrome/bin/answer.py?answer=142064"));
372 OpenURLParams params(
373 url, Referrer(),
374 (disposition == CURRENT_TAB) ? NEW_FOREGROUND_TAB : disposition,
375 content::PAGE_TRANSITION_LINK, false);
376 owner()->web_contents()->OpenURL(params);
377 return false;
380 void PluginInstallerInfoBarDelegate::DownloadStarted() {
381 ReplaceWithInfoBar(l10n_util::GetStringFUTF16(IDS_PLUGIN_DOWNLOADING,
382 installer()->name()));
385 void PluginInstallerInfoBarDelegate::DownloadCancelled() {
386 ReplaceWithInfoBar(l10n_util::GetStringFUTF16(IDS_PLUGIN_DOWNLOAD_CANCELLED,
387 installer()->name()));
390 void PluginInstallerInfoBarDelegate::DownloadError(const std::string& message) {
391 ReplaceWithInfoBar(
392 l10n_util::GetStringFUTF16(IDS_PLUGIN_DOWNLOAD_ERROR_SHORT,
393 installer()->name()));
396 void PluginInstallerInfoBarDelegate::DownloadFinished() {
397 ReplaceWithInfoBar(l10n_util::GetStringFUTF16(
398 new_install_ ? IDS_PLUGIN_INSTALLING : IDS_PLUGIN_UPDATING,
399 installer()->name()));
402 void PluginInstallerInfoBarDelegate::OnlyWeakObserversLeft() {
403 if (owner())
404 owner()->RemoveInfoBar(this);
407 void PluginInstallerInfoBarDelegate::ReplaceWithInfoBar(
408 const string16& message) {
409 // Return early if the message doesn't change. This is important in case the
410 // PluginInstaller is still iterating over its observers (otherwise we would
411 // keep replacing infobar delegates infinitely).
412 if (message_ == message)
413 return;
414 if (!owner())
415 return;
416 InfoBarDelegate* delegate = new PluginInstallerInfoBarDelegate(
417 owner(), installer(), base::Closure(), new_install_, message);
418 owner()->ReplaceInfoBar(this, delegate);
421 // PluginMetroModeInfoBarDelegate ---------------------------------------------
422 #if defined(OS_WIN)
423 InfoBarDelegate* PluginMetroModeInfoBarDelegate::Create(
424 InfoBarTabHelper* infobar_helper, const string16& plugin_name) {
425 string16 message = l10n_util::GetStringFUTF16(
426 IDS_METRO_MISSING_PLUGIN_PROMPT, plugin_name);
427 return new PluginMetroModeInfoBarDelegate(
428 infobar_helper, message);
431 PluginMetroModeInfoBarDelegate::PluginMetroModeInfoBarDelegate(
432 InfoBarTabHelper* infobar_helper, const string16& message)
433 : ConfirmInfoBarDelegate(infobar_helper),
434 message_(message) {
437 PluginMetroModeInfoBarDelegate::~PluginMetroModeInfoBarDelegate() {
440 gfx::Image* PluginMetroModeInfoBarDelegate::GetIcon() const {
441 return &ResourceBundle::GetSharedInstance().GetNativeImageNamed(
442 IDR_INFOBAR_PLUGIN_INSTALL);
445 string16 PluginMetroModeInfoBarDelegate::GetMessageText() const {
446 return message_;
449 int PluginMetroModeInfoBarDelegate::GetButtons() const {
450 return BUTTON_OK;
453 string16 PluginMetroModeInfoBarDelegate::GetButtonLabel(
454 InfoBarButton button) const {
455 DCHECK_EQ(BUTTON_OK, button);
456 return l10n_util::GetStringUTF16(IDS_METRO_SWITCH_TO_DESKTOP_BUTTON);
459 bool PluginMetroModeInfoBarDelegate::Accept() {
460 content::WebContents* web_contents = owner()->web_contents();
461 if (!web_contents)
462 return false;
463 // Note that empty urls are not valid.
464 if (!web_contents->GetURL().is_valid())
465 return false;
466 std::string url(web_contents->GetURL().spec());
467 // This obscure use of the 'log usage' mask for windows 8 is documented
468 // here http://goo.gl/HBOe9.
469 ui::win::OpenAnyViaShell(UTF8ToUTF16(url),
470 string16(),
471 SEE_MASK_FLAG_LOG_USAGE);
472 return true;
475 string16 PluginMetroModeInfoBarDelegate::GetLinkText() const {
476 return l10n_util::GetStringUTF16(IDS_METRO_SWITCH_WHY_LINK);
479 bool PluginMetroModeInfoBarDelegate::LinkClicked(
480 WindowOpenDisposition disposition) {
481 // TODO(cpu): replace with the final url.
482 GURL url = google_util::AppendGoogleLocaleParam(GURL(
483 "https://support.google.com/chrome/?ib_display_in_desktop"));
484 OpenURLParams params(
485 url, Referrer(),
486 (disposition == CURRENT_TAB) ? NEW_FOREGROUND_TAB : disposition,
487 content::PAGE_TRANSITION_LINK, false);
488 owner()->web_contents()->OpenURL(params);
489 return false;
491 #endif // defined(OS_WIN)
492 #endif // defined(ENABLE_PLUGIN_INSTALLATION)