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/infobars/infobar_service.h"
11 #include "chrome/browser/lifetime/application_lifetime.h"
12 #include "chrome/browser/plugins/chrome_plugin_service_filter.h"
13 #include "chrome/browser/plugins/plugin_metadata.h"
14 #include "chrome/browser/profiles/profile.h"
15 #include "chrome/browser/shell_integration.h"
16 #include "chrome/browser/ui/browser_commands.h"
17 #include "chrome/common/url_constants.h"
18 #include "chrome/grit/generated_resources.h"
19 #include "chrome/grit/locale_settings.h"
20 #include "components/google/core/browser/google_util.h"
21 #include "components/infobars/core/infobar.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/components_strings.h"
27 #include "grit/theme_resources.h"
28 #include "ui/base/l10n/l10n_util.h"
30 #if defined(ENABLE_PLUGIN_INSTALLATION)
32 #include "base/win/metro.h"
34 #include "chrome/browser/plugins/plugin_installer.h"
39 #include "ui/base/win/shell.h"
42 #include "ui/aura/remote_window_tree_host_win.h"
46 using base::UserMetricsAction
;
49 // PluginInfoBarDelegate ------------------------------------------------------
51 PluginInfoBarDelegate::PluginInfoBarDelegate(const std::string
& identifier
)
52 : ConfirmInfoBarDelegate(),
53 identifier_(identifier
) {
56 PluginInfoBarDelegate::~PluginInfoBarDelegate() {
59 bool PluginInfoBarDelegate::LinkClicked(WindowOpenDisposition disposition
) {
60 InfoBarService::WebContentsFromInfoBar(infobar())->OpenURL(
61 content::OpenURLParams(
62 GURL(GetLearnMoreURL()), content::Referrer(),
63 (disposition
== CURRENT_TAB
) ? NEW_FOREGROUND_TAB
: disposition
,
64 ui::PAGE_TRANSITION_LINK
, false));
68 void PluginInfoBarDelegate::LoadBlockedPlugins() {
69 content::WebContents
* web_contents
=
70 InfoBarService::WebContentsFromInfoBar(infobar());
71 ChromePluginServiceFilter::GetInstance()->AuthorizeAllPlugins(
72 web_contents
, true, identifier_
);
75 int PluginInfoBarDelegate::GetIconID() const {
76 return IDR_INFOBAR_PLUGIN_INSTALL
;
79 base::string16
PluginInfoBarDelegate::GetLinkText() const {
80 return l10n_util::GetStringUTF16(IDS_LEARN_MORE
);
83 #if defined(ENABLE_PLUGIN_INSTALLATION)
85 // OutdatedPluginInfoBarDelegate ----------------------------------------------
87 void OutdatedPluginInfoBarDelegate::Create(
88 InfoBarService
* infobar_service
,
89 PluginInstaller
* installer
,
90 scoped_ptr
<PluginMetadata
> plugin_metadata
) {
91 // Copy the name out of |plugin_metadata| now, since the Pass() call below
92 // will make it impossible to get at.
93 base::string16
name(plugin_metadata
->name());
94 infobar_service
->AddInfoBar(infobar_service
->CreateConfirmInfoBar(
95 scoped_ptr
<ConfirmInfoBarDelegate
>(new OutdatedPluginInfoBarDelegate(
96 installer
, plugin_metadata
.Pass(),
97 l10n_util::GetStringFUTF16(
98 (installer
->state() == PluginInstaller::INSTALLER_STATE_IDLE
) ?
99 IDS_PLUGIN_OUTDATED_PROMPT
: IDS_PLUGIN_DOWNLOADING
,
103 OutdatedPluginInfoBarDelegate::OutdatedPluginInfoBarDelegate(
104 PluginInstaller
* installer
,
105 scoped_ptr
<PluginMetadata
> plugin_metadata
,
106 const base::string16
& message
)
107 : PluginInfoBarDelegate(plugin_metadata
->identifier()),
108 WeakPluginInstallerObserver(installer
),
109 plugin_metadata_(plugin_metadata
.Pass()),
111 content::RecordAction(UserMetricsAction("OutdatedPluginInfobar.Shown"));
112 std::string name
= base::UTF16ToUTF8(plugin_metadata_
->name());
113 if (name
== PluginMetadata::kJavaGroupName
) {
114 content::RecordAction(
115 UserMetricsAction("OutdatedPluginInfobar.Shown.Java"));
116 } else if (name
== PluginMetadata::kQuickTimeGroupName
) {
117 content::RecordAction(
118 UserMetricsAction("OutdatedPluginInfobar.Shown.QuickTime"));
119 } else if (name
== PluginMetadata::kShockwaveGroupName
) {
120 content::RecordAction(
121 UserMetricsAction("OutdatedPluginInfobar.Shown.Shockwave"));
122 } else if (name
== PluginMetadata::kRealPlayerGroupName
) {
123 content::RecordAction(
124 UserMetricsAction("OutdatedPluginInfobar.Shown.RealPlayer"));
125 } else if (name
== PluginMetadata::kSilverlightGroupName
) {
126 content::RecordAction(
127 UserMetricsAction("OutdatedPluginInfobar.Shown.Silverlight"));
128 } else if (name
== PluginMetadata::kAdobeReaderGroupName
) {
129 content::RecordAction(
130 UserMetricsAction("OutdatedPluginInfobar.Shown.Reader"));
134 OutdatedPluginInfoBarDelegate::~OutdatedPluginInfoBarDelegate() {
135 content::RecordAction(UserMetricsAction("OutdatedPluginInfobar.Closed"));
138 void OutdatedPluginInfoBarDelegate::InfoBarDismissed() {
139 content::RecordAction(UserMetricsAction("OutdatedPluginInfobar.Dismissed"));
142 base::string16
OutdatedPluginInfoBarDelegate::GetMessageText() const {
146 base::string16
OutdatedPluginInfoBarDelegate::GetButtonLabel(
147 InfoBarButton button
) const {
148 return l10n_util::GetStringUTF16((button
== BUTTON_OK
) ?
149 IDS_PLUGIN_UPDATE
: IDS_PLUGIN_ENABLE_TEMPORARILY
);
152 bool OutdatedPluginInfoBarDelegate::Accept() {
153 DCHECK_EQ(PluginInstaller::INSTALLER_STATE_IDLE
, installer()->state());
154 content::RecordAction(UserMetricsAction("OutdatedPluginInfobar.Update"));
155 // A call to any of |OpenDownloadURL()| or |StartInstalling()| will
156 // result in deleting ourselves. Accordingly, we make sure to
157 // not pass a reference to an object that can go away.
158 GURL
plugin_url(plugin_metadata_
->plugin_url());
159 content::WebContents
* web_contents
=
160 InfoBarService::WebContentsFromInfoBar(infobar());
161 if (plugin_metadata_
->url_for_display())
162 installer()->OpenDownloadURL(plugin_url
, web_contents
);
164 installer()->StartInstalling(plugin_url
, web_contents
);
168 bool OutdatedPluginInfoBarDelegate::Cancel() {
169 content::RecordAction(
170 UserMetricsAction("OutdatedPluginInfobar.AllowThisTime"));
171 LoadBlockedPlugins();
175 bool OutdatedPluginInfoBarDelegate::LinkClicked(
176 WindowOpenDisposition disposition
) {
177 content::RecordAction(UserMetricsAction("OutdatedPluginInfobar.LearnMore"));
178 return PluginInfoBarDelegate::LinkClicked(disposition
);
181 std::string
OutdatedPluginInfoBarDelegate::GetLearnMoreURL() const {
182 return chrome::kOutdatedPluginLearnMoreURL
;
185 void OutdatedPluginInfoBarDelegate::DownloadStarted() {
186 ReplaceWithInfoBar(l10n_util::GetStringFUTF16(IDS_PLUGIN_DOWNLOADING
,
187 plugin_metadata_
->name()));
190 void OutdatedPluginInfoBarDelegate::DownloadError(const std::string
& message
) {
191 ReplaceWithInfoBar(l10n_util::GetStringFUTF16(IDS_PLUGIN_DOWNLOAD_ERROR_SHORT
,
192 plugin_metadata_
->name()));
195 void OutdatedPluginInfoBarDelegate::DownloadCancelled() {
196 ReplaceWithInfoBar(l10n_util::GetStringFUTF16(IDS_PLUGIN_DOWNLOAD_CANCELLED
,
197 plugin_metadata_
->name()));
200 void OutdatedPluginInfoBarDelegate::DownloadFinished() {
201 ReplaceWithInfoBar(l10n_util::GetStringFUTF16(IDS_PLUGIN_UPDATING
,
202 plugin_metadata_
->name()));
205 void OutdatedPluginInfoBarDelegate::OnlyWeakObserversLeft() {
206 infobar()->RemoveSelf();
209 void OutdatedPluginInfoBarDelegate::ReplaceWithInfoBar(
210 const base::string16
& message
) {
211 // Return early if the message doesn't change. This is important in case the
212 // PluginInstaller is still iterating over its observers (otherwise we would
213 // keep replacing infobar delegates infinitely).
214 if ((message_
== message
) || !infobar()->owner())
216 PluginInstallerInfoBarDelegate::Replace(
217 infobar(), installer(), plugin_metadata_
->Clone(), false, message
);
221 // PluginInstallerInfoBarDelegate ---------------------------------------------
223 void PluginInstallerInfoBarDelegate::Create(
224 InfoBarService
* infobar_service
,
225 PluginInstaller
* installer
,
226 scoped_ptr
<PluginMetadata
> plugin_metadata
,
227 const InstallCallback
& callback
) {
228 base::string16
name(plugin_metadata
->name());
230 if (base::win::IsMetroProcess()) {
231 PluginMetroModeInfoBarDelegate::Create(
232 infobar_service
, PluginMetroModeInfoBarDelegate::MISSING_PLUGIN
, name
);
236 infobar_service
->AddInfoBar(infobar_service
->CreateConfirmInfoBar(
237 scoped_ptr
<ConfirmInfoBarDelegate
>(new PluginInstallerInfoBarDelegate(
238 installer
, plugin_metadata
.Pass(), callback
, true,
239 l10n_util::GetStringFUTF16(
240 (installer
->state() == PluginInstaller::INSTALLER_STATE_IDLE
)
241 ? IDS_PLUGININSTALLER_INSTALLPLUGIN_PROMPT
242 : IDS_PLUGIN_DOWNLOADING
,
246 void PluginInstallerInfoBarDelegate::Replace(
247 infobars::InfoBar
* infobar
,
248 PluginInstaller
* installer
,
249 scoped_ptr
<PluginMetadata
> plugin_metadata
,
251 const base::string16
& message
) {
252 DCHECK(infobar
->owner());
253 infobar
->owner()->ReplaceInfoBar(
255 infobar
->owner()->CreateConfirmInfoBar(
256 scoped_ptr
<ConfirmInfoBarDelegate
>(new PluginInstallerInfoBarDelegate(
257 installer
, plugin_metadata
.Pass(),
258 PluginInstallerInfoBarDelegate::InstallCallback(), new_install
,
262 PluginInstallerInfoBarDelegate::PluginInstallerInfoBarDelegate(
263 PluginInstaller
* installer
,
264 scoped_ptr
<PluginMetadata
> plugin_metadata
,
265 const InstallCallback
& callback
,
267 const base::string16
& message
)
268 : ConfirmInfoBarDelegate(),
269 WeakPluginInstallerObserver(installer
),
270 plugin_metadata_(plugin_metadata
.Pass()),
272 new_install_(new_install
),
276 PluginInstallerInfoBarDelegate::~PluginInstallerInfoBarDelegate() {
279 int PluginInstallerInfoBarDelegate::GetIconID() const {
280 return IDR_INFOBAR_PLUGIN_INSTALL
;
283 base::string16
PluginInstallerInfoBarDelegate::GetMessageText() const {
287 int PluginInstallerInfoBarDelegate::GetButtons() const {
288 return callback_
.is_null() ? BUTTON_NONE
: BUTTON_OK
;
291 base::string16
PluginInstallerInfoBarDelegate::GetButtonLabel(
292 InfoBarButton button
) const {
293 DCHECK_EQ(BUTTON_OK
, button
);
294 return l10n_util::GetStringUTF16(IDS_PLUGININSTALLER_INSTALLPLUGIN_BUTTON
);
297 bool PluginInstallerInfoBarDelegate::Accept() {
298 callback_
.Run(plugin_metadata_
.get());
302 base::string16
PluginInstallerInfoBarDelegate::GetLinkText() const {
303 return l10n_util::GetStringUTF16(new_install_
?
304 IDS_PLUGININSTALLER_PROBLEMSINSTALLING
:
305 IDS_PLUGININSTALLER_PROBLEMSUPDATING
);
308 bool PluginInstallerInfoBarDelegate::LinkClicked(
309 WindowOpenDisposition disposition
) {
310 GURL
url(plugin_metadata_
->help_url());
311 if (url
.is_empty()) {
313 "https://www.google.com/support/chrome/bin/answer.py?answer=142064");
315 InfoBarService::WebContentsFromInfoBar(infobar())->OpenURL(
316 content::OpenURLParams(
317 url
, content::Referrer(),
318 (disposition
== CURRENT_TAB
) ? NEW_FOREGROUND_TAB
: disposition
,
319 ui::PAGE_TRANSITION_LINK
, false));
323 void PluginInstallerInfoBarDelegate::DownloadStarted() {
324 ReplaceWithInfoBar(l10n_util::GetStringFUTF16(IDS_PLUGIN_DOWNLOADING
,
325 plugin_metadata_
->name()));
328 void PluginInstallerInfoBarDelegate::DownloadCancelled() {
329 ReplaceWithInfoBar(l10n_util::GetStringFUTF16(IDS_PLUGIN_DOWNLOAD_CANCELLED
,
330 plugin_metadata_
->name()));
333 void PluginInstallerInfoBarDelegate::DownloadError(const std::string
& message
) {
334 ReplaceWithInfoBar(l10n_util::GetStringFUTF16(IDS_PLUGIN_DOWNLOAD_ERROR_SHORT
,
335 plugin_metadata_
->name()));
338 void PluginInstallerInfoBarDelegate::DownloadFinished() {
340 l10n_util::GetStringFUTF16(
341 new_install_
? IDS_PLUGIN_INSTALLING
: IDS_PLUGIN_UPDATING
,
342 plugin_metadata_
->name()));
345 void PluginInstallerInfoBarDelegate::OnlyWeakObserversLeft() {
346 infobar()->RemoveSelf();
349 void PluginInstallerInfoBarDelegate::ReplaceWithInfoBar(
350 const base::string16
& message
) {
351 // Return early if the message doesn't change. This is important in case the
352 // PluginInstaller is still iterating over its observers (otherwise we would
353 // keep replacing infobar delegates infinitely).
354 if ((message_
== message
) || !infobar()->owner())
356 Replace(infobar(), installer(), plugin_metadata_
->Clone(), new_install_
,
363 // PluginMetroModeInfoBarDelegate ---------------------------------------------
366 void PluginMetroModeInfoBarDelegate::Create(
367 InfoBarService
* infobar_service
,
368 PluginMetroModeInfoBarDelegate::Mode mode
,
369 const base::string16
& name
) {
370 infobar_service
->AddInfoBar(
371 infobar_service
->CreateConfirmInfoBar(scoped_ptr
<ConfirmInfoBarDelegate
>(
372 new PluginMetroModeInfoBarDelegate(mode
, name
))));
375 PluginMetroModeInfoBarDelegate::PluginMetroModeInfoBarDelegate(
376 PluginMetroModeInfoBarDelegate::Mode mode
,
377 const base::string16
& name
)
378 : ConfirmInfoBarDelegate(),
383 PluginMetroModeInfoBarDelegate::~PluginMetroModeInfoBarDelegate() {
386 int PluginMetroModeInfoBarDelegate::GetIconID() const {
387 return IDR_INFOBAR_PLUGIN_INSTALL
;
390 base::string16
PluginMetroModeInfoBarDelegate::GetMessageText() const {
391 return l10n_util::GetStringFUTF16((mode_
== MISSING_PLUGIN
) ?
392 IDS_METRO_MISSING_PLUGIN_PROMPT
: IDS_METRO_NPAPI_PLUGIN_PROMPT
, name_
);
395 int PluginMetroModeInfoBarDelegate::GetButtons() const {
399 base::string16
PluginMetroModeInfoBarDelegate::GetButtonLabel(
400 InfoBarButton button
) const {
401 return l10n_util::GetStringUTF16(IDS_WIN_DESKTOP_RESTART
);
404 void LaunchDesktopInstanceHelper(const base::string16
& url
) {
405 base::FilePath exe_path
;
406 if (!PathService::Get(base::FILE_EXE
, &exe_path
))
408 base::FilePath
shortcut_path(
409 ShellIntegration::GetStartMenuShortcut(exe_path
));
411 // Actually launching the process needs to happen in the metro viewer,
412 // otherwise it won't automatically transition to desktop. So we have
413 // to send an IPC to the viewer to do the ShellExecute.
414 aura::RemoteWindowTreeHostWin::Instance()->HandleOpenURLOnDesktop(
418 bool PluginMetroModeInfoBarDelegate::Accept() {
419 chrome::AttemptRestartToDesktopMode();
423 base::string16
PluginMetroModeInfoBarDelegate::GetLinkText() const {
424 return l10n_util::GetStringUTF16(IDS_LEARN_MORE
);
427 bool PluginMetroModeInfoBarDelegate::LinkClicked(
428 WindowOpenDisposition disposition
) {
429 // TODO(shrikant): We may need to change language a little at following
430 // support URLs. With new approach we will just restart for both missing
431 // and not missing mode.
432 InfoBarService::WebContentsFromInfoBar(infobar())->OpenURL(
433 content::OpenURLParams(
434 GURL((mode_
== MISSING_PLUGIN
) ?
435 "https://support.google.com/chrome/?p=ib_display_in_desktop" :
436 "https://support.google.com/chrome/?p=ib_redirect_to_desktop"),
438 (disposition
== CURRENT_TAB
) ? NEW_FOREGROUND_TAB
: disposition
,
439 ui::PAGE_TRANSITION_LINK
, false));
443 #endif // defined(OS_WIN)
445 #endif // defined(ENABLE_PLUGIN_INSTALLATION)