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/plugins/plugin_infobar_delegates.h"
8 #include "base/path_service.h"
9 #include "base/strings/utf_string_conversions.h"
10 #include "chrome/browser/content_settings/host_content_settings_map.h"
11 #include "chrome/browser/google/google_util.h"
12 #include "chrome/browser/infobars/infobar.h"
13 #include "chrome/browser/infobars/infobar_service.h"
14 #include "chrome/browser/lifetime/application_lifetime.h"
15 #include "chrome/browser/plugins/chrome_plugin_service_filter.h"
16 #include "chrome/browser/plugins/plugin_metadata.h"
17 #include "chrome/browser/profiles/profile.h"
18 #include "chrome/browser/shell_integration.h"
19 #include "chrome/browser/ui/browser_commands.h"
20 #include "chrome/common/url_constants.h"
21 #include "content/public/browser/browser_thread.h"
22 #include "content/public/browser/render_process_host.h"
23 #include "content/public/browser/render_view_host.h"
24 #include "content/public/browser/user_metrics.h"
25 #include "content/public/browser/web_contents.h"
26 #include "grit/generated_resources.h"
27 #include "grit/locale_settings.h"
28 #include "grit/theme_resources.h"
29 #include "ui/base/l10n/l10n_util.h"
31 #if defined(ENABLE_PLUGIN_INSTALLATION)
33 #include "base/win/metro.h"
35 #include "chrome/browser/plugins/plugin_installer.h"
40 #include "ui/base/win/shell.h"
43 #include "ui/aura/remote_root_window_host_win.h"
47 using base::UserMetricsAction
;
50 // PluginInfoBarDelegate ------------------------------------------------------
52 PluginInfoBarDelegate::PluginInfoBarDelegate(const std::string
& identifier
)
53 : ConfirmInfoBarDelegate(),
54 identifier_(identifier
) {
57 PluginInfoBarDelegate::~PluginInfoBarDelegate() {
60 bool PluginInfoBarDelegate::LinkClicked(WindowOpenDisposition disposition
) {
61 web_contents()->OpenURL(content::OpenURLParams(
62 GURL(GetLearnMoreURL()), content::Referrer(),
63 (disposition
== CURRENT_TAB
) ? NEW_FOREGROUND_TAB
: disposition
,
64 content::PAGE_TRANSITION_LINK
, false));
68 void PluginInfoBarDelegate::LoadBlockedPlugins() {
69 ChromePluginServiceFilter::GetInstance()->AuthorizeAllPlugins(
70 web_contents(), true, identifier_
);
73 int PluginInfoBarDelegate::GetIconID() const {
74 return IDR_INFOBAR_PLUGIN_INSTALL
;
77 base::string16
PluginInfoBarDelegate::GetLinkText() const {
78 return l10n_util::GetStringUTF16(IDS_LEARN_MORE
);
82 // UnauthorizedPluginInfoBarDelegate ------------------------------------------
85 void UnauthorizedPluginInfoBarDelegate::Create(
86 InfoBarService
* infobar_service
,
87 HostContentSettingsMap
* content_settings
,
88 const base::string16
& name
,
89 const std::string
& identifier
) {
90 infobar_service
->AddInfoBar(ConfirmInfoBarDelegate::CreateInfoBar(
91 scoped_ptr
<ConfirmInfoBarDelegate
>(new UnauthorizedPluginInfoBarDelegate(
92 content_settings
, name
, identifier
))));
94 content::RecordAction(UserMetricsAction("BlockedPluginInfobar.Shown"));
95 std::string
utf8_name(base::UTF16ToUTF8(name
));
96 if (utf8_name
== PluginMetadata::kJavaGroupName
) {
97 content::RecordAction(UserMetricsAction("BlockedPluginInfobar.Shown.Java"));
98 } else if (utf8_name
== PluginMetadata::kQuickTimeGroupName
) {
99 content::RecordAction(
100 UserMetricsAction("BlockedPluginInfobar.Shown.QuickTime"));
101 } else if (utf8_name
== PluginMetadata::kShockwaveGroupName
) {
102 content::RecordAction(
103 UserMetricsAction("BlockedPluginInfobar.Shown.Shockwave"));
104 } else if (utf8_name
== PluginMetadata::kRealPlayerGroupName
) {
105 content::RecordAction(
106 UserMetricsAction("BlockedPluginInfobar.Shown.RealPlayer"));
107 } else if (utf8_name
== PluginMetadata::kWindowsMediaPlayerGroupName
) {
108 content::RecordAction(
109 UserMetricsAction("BlockedPluginInfobar.Shown.WindowsMediaPlayer"));
113 UnauthorizedPluginInfoBarDelegate::UnauthorizedPluginInfoBarDelegate(
114 HostContentSettingsMap
* content_settings
,
115 const base::string16
& name
,
116 const std::string
& identifier
)
117 : PluginInfoBarDelegate(identifier
),
118 content_settings_(content_settings
),
122 UnauthorizedPluginInfoBarDelegate::~UnauthorizedPluginInfoBarDelegate() {
123 content::RecordAction(UserMetricsAction("BlockedPluginInfobar.Closed"));
126 std::string
UnauthorizedPluginInfoBarDelegate::GetLearnMoreURL() const {
127 return chrome::kBlockedPluginLearnMoreURL
;
130 base::string16
UnauthorizedPluginInfoBarDelegate::GetMessageText() const {
131 return l10n_util::GetStringFUTF16(IDS_PLUGIN_NOT_AUTHORIZED
, name_
);
134 base::string16
UnauthorizedPluginInfoBarDelegate::GetButtonLabel(
135 InfoBarButton button
) const {
136 return l10n_util::GetStringUTF16((button
== BUTTON_OK
) ?
137 IDS_PLUGIN_ENABLE_TEMPORARILY
: IDS_PLUGIN_ENABLE_ALWAYS
);
140 bool UnauthorizedPluginInfoBarDelegate::Accept() {
141 content::RecordAction(
142 UserMetricsAction("BlockedPluginInfobar.AllowThisTime"));
143 LoadBlockedPlugins();
147 bool UnauthorizedPluginInfoBarDelegate::Cancel() {
148 content::RecordAction(UserMetricsAction("BlockedPluginInfobar.AlwaysAllow"));
149 const GURL
& url
= web_contents()->GetURL();
150 content_settings_
->AddExceptionForURL(url
, url
, CONTENT_SETTINGS_TYPE_PLUGINS
,
151 CONTENT_SETTING_ALLOW
);
152 LoadBlockedPlugins();
156 void UnauthorizedPluginInfoBarDelegate::InfoBarDismissed() {
157 content::RecordAction(UserMetricsAction("BlockedPluginInfobar.Dismissed"));
160 bool UnauthorizedPluginInfoBarDelegate::LinkClicked(
161 WindowOpenDisposition disposition
) {
162 content::RecordAction(UserMetricsAction("BlockedPluginInfobar.LearnMore"));
163 return PluginInfoBarDelegate::LinkClicked(disposition
);
167 #if defined(ENABLE_PLUGIN_INSTALLATION)
169 // OutdatedPluginInfoBarDelegate ----------------------------------------------
171 void OutdatedPluginInfoBarDelegate::Create(
172 InfoBarService
* infobar_service
,
173 PluginInstaller
* installer
,
174 scoped_ptr
<PluginMetadata
> plugin_metadata
) {
175 // Copy the name out of |plugin_metadata| now, since the Pass() call below
176 // will make it impossible to get at.
177 base::string16
name(plugin_metadata
->name());
178 infobar_service
->AddInfoBar(ConfirmInfoBarDelegate::CreateInfoBar(
179 scoped_ptr
<ConfirmInfoBarDelegate
>(new OutdatedPluginInfoBarDelegate(
180 installer
, plugin_metadata
.Pass(), l10n_util::GetStringFUTF16(
181 (installer
->state() == PluginInstaller::INSTALLER_STATE_IDLE
) ?
182 IDS_PLUGIN_OUTDATED_PROMPT
: IDS_PLUGIN_DOWNLOADING
,
186 OutdatedPluginInfoBarDelegate::OutdatedPluginInfoBarDelegate(
187 PluginInstaller
* installer
,
188 scoped_ptr
<PluginMetadata
> plugin_metadata
,
189 const base::string16
& message
)
190 : PluginInfoBarDelegate(plugin_metadata
->identifier()),
191 WeakPluginInstallerObserver(installer
),
192 plugin_metadata_(plugin_metadata
.Pass()),
194 content::RecordAction(UserMetricsAction("OutdatedPluginInfobar.Shown"));
195 std::string name
= base::UTF16ToUTF8(plugin_metadata_
->name());
196 if (name
== PluginMetadata::kJavaGroupName
) {
197 content::RecordAction(
198 UserMetricsAction("OutdatedPluginInfobar.Shown.Java"));
199 } else if (name
== PluginMetadata::kQuickTimeGroupName
) {
200 content::RecordAction(
201 UserMetricsAction("OutdatedPluginInfobar.Shown.QuickTime"));
202 } else if (name
== PluginMetadata::kShockwaveGroupName
) {
203 content::RecordAction(
204 UserMetricsAction("OutdatedPluginInfobar.Shown.Shockwave"));
205 } else if (name
== PluginMetadata::kRealPlayerGroupName
) {
206 content::RecordAction(
207 UserMetricsAction("OutdatedPluginInfobar.Shown.RealPlayer"));
208 } else if (name
== PluginMetadata::kSilverlightGroupName
) {
209 content::RecordAction(
210 UserMetricsAction("OutdatedPluginInfobar.Shown.Silverlight"));
211 } else if (name
== PluginMetadata::kAdobeReaderGroupName
) {
212 content::RecordAction(
213 UserMetricsAction("OutdatedPluginInfobar.Shown.Reader"));
217 OutdatedPluginInfoBarDelegate::~OutdatedPluginInfoBarDelegate() {
218 content::RecordAction(UserMetricsAction("OutdatedPluginInfobar.Closed"));
221 std::string
OutdatedPluginInfoBarDelegate::GetLearnMoreURL() const {
222 return chrome::kOutdatedPluginLearnMoreURL
;
225 base::string16
OutdatedPluginInfoBarDelegate::GetMessageText() const {
229 base::string16
OutdatedPluginInfoBarDelegate::GetButtonLabel(
230 InfoBarButton button
) const {
231 return l10n_util::GetStringUTF16((button
== BUTTON_OK
) ?
232 IDS_PLUGIN_UPDATE
: IDS_PLUGIN_ENABLE_TEMPORARILY
);
235 bool OutdatedPluginInfoBarDelegate::Accept() {
236 DCHECK_EQ(PluginInstaller::INSTALLER_STATE_IDLE
, installer()->state());
237 content::RecordAction(UserMetricsAction("OutdatedPluginInfobar.Update"));
238 // A call to any of |OpenDownloadURL()| or |StartInstalling()| will
239 // result in deleting ourselves. Accordingly, we make sure to
240 // not pass a reference to an object that can go away.
241 GURL
plugin_url(plugin_metadata_
->plugin_url());
242 if (plugin_metadata_
->url_for_display())
243 installer()->OpenDownloadURL(plugin_url
, web_contents());
245 installer()->StartInstalling(plugin_url
, web_contents());
249 bool OutdatedPluginInfoBarDelegate::Cancel() {
250 content::RecordAction(
251 UserMetricsAction("OutdatedPluginInfobar.AllowThisTime"));
252 LoadBlockedPlugins();
256 void OutdatedPluginInfoBarDelegate::InfoBarDismissed() {
257 content::RecordAction(UserMetricsAction("OutdatedPluginInfobar.Dismissed"));
260 bool OutdatedPluginInfoBarDelegate::LinkClicked(
261 WindowOpenDisposition disposition
) {
262 content::RecordAction(UserMetricsAction("OutdatedPluginInfobar.LearnMore"));
263 return PluginInfoBarDelegate::LinkClicked(disposition
);
266 void OutdatedPluginInfoBarDelegate::DownloadStarted() {
267 ReplaceWithInfoBar(l10n_util::GetStringFUTF16(IDS_PLUGIN_DOWNLOADING
,
268 plugin_metadata_
->name()));
271 void OutdatedPluginInfoBarDelegate::DownloadError(const std::string
& message
) {
272 ReplaceWithInfoBar(l10n_util::GetStringFUTF16(IDS_PLUGIN_DOWNLOAD_ERROR_SHORT
,
273 plugin_metadata_
->name()));
276 void OutdatedPluginInfoBarDelegate::DownloadCancelled() {
277 ReplaceWithInfoBar(l10n_util::GetStringFUTF16(IDS_PLUGIN_DOWNLOAD_CANCELLED
,
278 plugin_metadata_
->name()));
281 void OutdatedPluginInfoBarDelegate::DownloadFinished() {
282 ReplaceWithInfoBar(l10n_util::GetStringFUTF16(IDS_PLUGIN_UPDATING
,
283 plugin_metadata_
->name()));
286 void OutdatedPluginInfoBarDelegate::OnlyWeakObserversLeft() {
287 infobar()->RemoveSelf();
290 void OutdatedPluginInfoBarDelegate::ReplaceWithInfoBar(
291 const base::string16
& message
) {
292 // Return early if the message doesn't change. This is important in case the
293 // PluginInstaller is still iterating over its observers (otherwise we would
294 // keep replacing infobar delegates infinitely).
295 if ((message_
== message
) || !infobar()->owner())
297 PluginInstallerInfoBarDelegate::Replace(
298 infobar(), installer(), plugin_metadata_
->Clone(), false, message
);
302 // PluginInstallerInfoBarDelegate ---------------------------------------------
304 void PluginInstallerInfoBarDelegate::Create(
305 InfoBarService
* infobar_service
,
306 PluginInstaller
* installer
,
307 scoped_ptr
<PluginMetadata
> plugin_metadata
,
308 const InstallCallback
& callback
) {
309 base::string16
name(plugin_metadata
->name());
311 if (base::win::IsMetroProcess()) {
312 PluginMetroModeInfoBarDelegate::Create(
313 infobar_service
, PluginMetroModeInfoBarDelegate::MISSING_PLUGIN
, name
);
317 infobar_service
->AddInfoBar(ConfirmInfoBarDelegate::CreateInfoBar(
318 scoped_ptr
<ConfirmInfoBarDelegate
>(new PluginInstallerInfoBarDelegate(
319 installer
, plugin_metadata
.Pass(), callback
, true,
320 l10n_util::GetStringFUTF16(
321 (installer
->state() == PluginInstaller::INSTALLER_STATE_IDLE
) ?
322 IDS_PLUGININSTALLER_INSTALLPLUGIN_PROMPT
:
323 IDS_PLUGIN_DOWNLOADING
,
328 void PluginInstallerInfoBarDelegate::Replace(
330 PluginInstaller
* installer
,
331 scoped_ptr
<PluginMetadata
> plugin_metadata
,
333 const base::string16
& message
) {
334 DCHECK(infobar
->owner());
335 infobar
->owner()->ReplaceInfoBar(infobar
,
336 ConfirmInfoBarDelegate::CreateInfoBar(scoped_ptr
<ConfirmInfoBarDelegate
>(
337 new PluginInstallerInfoBarDelegate(
338 installer
, plugin_metadata
.Pass(),
339 PluginInstallerInfoBarDelegate::InstallCallback(), new_install
,
343 PluginInstallerInfoBarDelegate::PluginInstallerInfoBarDelegate(
344 PluginInstaller
* installer
,
345 scoped_ptr
<PluginMetadata
> plugin_metadata
,
346 const InstallCallback
& callback
,
348 const base::string16
& message
)
349 : ConfirmInfoBarDelegate(),
350 WeakPluginInstallerObserver(installer
),
351 plugin_metadata_(plugin_metadata
.Pass()),
353 new_install_(new_install
),
357 PluginInstallerInfoBarDelegate::~PluginInstallerInfoBarDelegate() {
360 int PluginInstallerInfoBarDelegate::GetIconID() const {
361 return IDR_INFOBAR_PLUGIN_INSTALL
;
364 base::string16
PluginInstallerInfoBarDelegate::GetMessageText() const {
368 int PluginInstallerInfoBarDelegate::GetButtons() const {
369 return callback_
.is_null() ? BUTTON_NONE
: BUTTON_OK
;
372 base::string16
PluginInstallerInfoBarDelegate::GetButtonLabel(
373 InfoBarButton button
) const {
374 DCHECK_EQ(BUTTON_OK
, button
);
375 return l10n_util::GetStringUTF16(IDS_PLUGININSTALLER_INSTALLPLUGIN_BUTTON
);
378 bool PluginInstallerInfoBarDelegate::Accept() {
379 callback_
.Run(plugin_metadata_
.get());
383 base::string16
PluginInstallerInfoBarDelegate::GetLinkText() const {
384 return l10n_util::GetStringUTF16(new_install_
?
385 IDS_PLUGININSTALLER_PROBLEMSINSTALLING
:
386 IDS_PLUGININSTALLER_PROBLEMSUPDATING
);
389 bool PluginInstallerInfoBarDelegate::LinkClicked(
390 WindowOpenDisposition disposition
) {
391 GURL
url(plugin_metadata_
->help_url());
392 if (url
.is_empty()) {
393 url
= google_util::AppendGoogleLocaleParam(GURL(
394 "https://www.google.com/support/chrome/bin/answer.py?answer=142064"));
396 web_contents()->OpenURL(content::OpenURLParams(
397 url
, content::Referrer(),
398 (disposition
== CURRENT_TAB
) ? NEW_FOREGROUND_TAB
: disposition
,
399 content::PAGE_TRANSITION_LINK
, false));
403 void PluginInstallerInfoBarDelegate::DownloadStarted() {
404 ReplaceWithInfoBar(l10n_util::GetStringFUTF16(IDS_PLUGIN_DOWNLOADING
,
405 plugin_metadata_
->name()));
408 void PluginInstallerInfoBarDelegate::DownloadCancelled() {
409 ReplaceWithInfoBar(l10n_util::GetStringFUTF16(IDS_PLUGIN_DOWNLOAD_CANCELLED
,
410 plugin_metadata_
->name()));
413 void PluginInstallerInfoBarDelegate::DownloadError(const std::string
& message
) {
414 ReplaceWithInfoBar(l10n_util::GetStringFUTF16(IDS_PLUGIN_DOWNLOAD_ERROR_SHORT
,
415 plugin_metadata_
->name()));
418 void PluginInstallerInfoBarDelegate::DownloadFinished() {
420 l10n_util::GetStringFUTF16(
421 new_install_
? IDS_PLUGIN_INSTALLING
: IDS_PLUGIN_UPDATING
,
422 plugin_metadata_
->name()));
425 void PluginInstallerInfoBarDelegate::OnlyWeakObserversLeft() {
426 infobar()->RemoveSelf();
429 void PluginInstallerInfoBarDelegate::ReplaceWithInfoBar(
430 const base::string16
& message
) {
431 // Return early if the message doesn't change. This is important in case the
432 // PluginInstaller is still iterating over its observers (otherwise we would
433 // keep replacing infobar delegates infinitely).
434 if ((message_
== message
) || !infobar()->owner())
436 Replace(infobar(), installer(), plugin_metadata_
->Clone(), new_install_
,
443 // PluginMetroModeInfoBarDelegate ---------------------------------------------
446 void PluginMetroModeInfoBarDelegate::Create(
447 InfoBarService
* infobar_service
,
448 PluginMetroModeInfoBarDelegate::Mode mode
,
449 const base::string16
& name
) {
450 infobar_service
->AddInfoBar(ConfirmInfoBarDelegate::CreateInfoBar(
451 scoped_ptr
<ConfirmInfoBarDelegate
>(
452 new PluginMetroModeInfoBarDelegate(mode
, name
))));
455 PluginMetroModeInfoBarDelegate::PluginMetroModeInfoBarDelegate(
456 PluginMetroModeInfoBarDelegate::Mode mode
,
457 const base::string16
& name
)
458 : ConfirmInfoBarDelegate(),
463 PluginMetroModeInfoBarDelegate::~PluginMetroModeInfoBarDelegate() {
466 int PluginMetroModeInfoBarDelegate::GetIconID() const {
467 return IDR_INFOBAR_PLUGIN_INSTALL
;
470 base::string16
PluginMetroModeInfoBarDelegate::GetMessageText() const {
471 return l10n_util::GetStringFUTF16((mode_
== MISSING_PLUGIN
) ?
472 IDS_METRO_MISSING_PLUGIN_PROMPT
: IDS_METRO_NPAPI_PLUGIN_PROMPT
, name_
);
475 int PluginMetroModeInfoBarDelegate::GetButtons() const {
479 base::string16
PluginMetroModeInfoBarDelegate::GetButtonLabel(
480 InfoBarButton button
) const {
481 #if defined(USE_AURA) && defined(USE_ASH)
482 return l10n_util::GetStringUTF16(IDS_WIN8_DESKTOP_RESTART
);
484 return l10n_util::GetStringUTF16((mode_
== MISSING_PLUGIN
) ?
485 IDS_WIN8_DESKTOP_RESTART
: IDS_WIN8_DESKTOP_OPEN
);
489 #if defined(USE_AURA) && defined(USE_ASH)
490 void LaunchDesktopInstanceHelper(const base::string16
& url
) {
491 base::FilePath exe_path
;
492 if (!PathService::Get(base::FILE_EXE
, &exe_path
))
494 base::FilePath
shortcut_path(
495 ShellIntegration::GetStartMenuShortcut(exe_path
));
497 // Actually launching the process needs to happen in the metro viewer,
498 // otherwise it won't automatically transition to desktop. So we have
499 // to send an IPC to the viewer to do the ShellExecute.
500 aura::RemoteWindowTreeHostWin::Instance()->HandleOpenURLOnDesktop(
505 bool PluginMetroModeInfoBarDelegate::Accept() {
506 chrome::AttemptRestartToDesktopMode();
510 base::string16
PluginMetroModeInfoBarDelegate::GetLinkText() const {
511 return l10n_util::GetStringUTF16(IDS_LEARN_MORE
);
514 bool PluginMetroModeInfoBarDelegate::LinkClicked(
515 WindowOpenDisposition disposition
) {
516 // TODO(shrikant): We may need to change language a little at following
517 // support URLs. With new approach we will just restart for both missing
518 // and not missing mode.
519 web_contents()->OpenURL(content::OpenURLParams(
520 GURL((mode_
== MISSING_PLUGIN
) ?
521 "https://support.google.com/chrome/?p=ib_display_in_desktop" :
522 "https://support.google.com/chrome/?p=ib_redirect_to_desktop"),
524 (disposition
== CURRENT_TAB
) ? NEW_FOREGROUND_TAB
: disposition
,
525 content::PAGE_TRANSITION_LINK
, false));
529 #endif // defined(OS_WIN)
531 #endif // defined(ENABLE_PLUGIN_INSTALLATION)