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/chromium_strings.h"
19 #include "chrome/grit/generated_resources.h"
20 #include "chrome/grit/locale_settings.h"
21 #include "components/google/core/browser/google_util.h"
22 #include "components/infobars/core/infobar.h"
23 #include "content/public/browser/render_process_host.h"
24 #include "content/public/browser/render_view_host.h"
25 #include "content/public/browser/user_metrics.h"
26 #include "content/public/browser/web_contents.h"
27 #include "grit/components_strings.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_window_tree_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 InfoBarService::WebContentsFromInfoBar(infobar())->OpenURL(
62 content::OpenURLParams(
63 GURL(GetLearnMoreURL()), content::Referrer(),
64 (disposition
== CURRENT_TAB
) ? NEW_FOREGROUND_TAB
: disposition
,
65 ui::PAGE_TRANSITION_LINK
, false));
69 void PluginInfoBarDelegate::LoadBlockedPlugins() {
70 content::WebContents
* web_contents
=
71 InfoBarService::WebContentsFromInfoBar(infobar());
72 ChromePluginServiceFilter::GetInstance()->AuthorizeAllPlugins(
73 web_contents
, true, identifier_
);
76 int PluginInfoBarDelegate::GetIconId() const {
77 return IDR_INFOBAR_PLUGIN_INSTALL
;
80 base::string16
PluginInfoBarDelegate::GetLinkText() const {
81 return l10n_util::GetStringUTF16(IDS_LEARN_MORE
);
84 #if defined(ENABLE_PLUGIN_INSTALLATION)
86 // OutdatedPluginInfoBarDelegate ----------------------------------------------
88 void OutdatedPluginInfoBarDelegate::Create(
89 InfoBarService
* infobar_service
,
90 PluginInstaller
* installer
,
91 scoped_ptr
<PluginMetadata
> plugin_metadata
) {
92 // Copy the name out of |plugin_metadata| now, since the Pass() call below
93 // will make it impossible to get at.
94 base::string16
name(plugin_metadata
->name());
95 infobar_service
->AddInfoBar(infobar_service
->CreateConfirmInfoBar(
96 scoped_ptr
<ConfirmInfoBarDelegate
>(new OutdatedPluginInfoBarDelegate(
97 installer
, plugin_metadata
.Pass(),
98 l10n_util::GetStringFUTF16(
99 (installer
->state() == PluginInstaller::INSTALLER_STATE_IDLE
) ?
100 IDS_PLUGIN_OUTDATED_PROMPT
: IDS_PLUGIN_DOWNLOADING
,
104 OutdatedPluginInfoBarDelegate::OutdatedPluginInfoBarDelegate(
105 PluginInstaller
* installer
,
106 scoped_ptr
<PluginMetadata
> plugin_metadata
,
107 const base::string16
& message
)
108 : PluginInfoBarDelegate(plugin_metadata
->identifier()),
109 WeakPluginInstallerObserver(installer
),
110 plugin_metadata_(plugin_metadata
.Pass()),
112 content::RecordAction(UserMetricsAction("OutdatedPluginInfobar.Shown"));
113 std::string name
= base::UTF16ToUTF8(plugin_metadata_
->name());
114 if (name
== PluginMetadata::kJavaGroupName
) {
115 content::RecordAction(
116 UserMetricsAction("OutdatedPluginInfobar.Shown.Java"));
117 } else if (name
== PluginMetadata::kQuickTimeGroupName
) {
118 content::RecordAction(
119 UserMetricsAction("OutdatedPluginInfobar.Shown.QuickTime"));
120 } else if (name
== PluginMetadata::kShockwaveGroupName
) {
121 content::RecordAction(
122 UserMetricsAction("OutdatedPluginInfobar.Shown.Shockwave"));
123 } else if (name
== PluginMetadata::kRealPlayerGroupName
) {
124 content::RecordAction(
125 UserMetricsAction("OutdatedPluginInfobar.Shown.RealPlayer"));
126 } else if (name
== PluginMetadata::kSilverlightGroupName
) {
127 content::RecordAction(
128 UserMetricsAction("OutdatedPluginInfobar.Shown.Silverlight"));
129 } else if (name
== PluginMetadata::kAdobeReaderGroupName
) {
130 content::RecordAction(
131 UserMetricsAction("OutdatedPluginInfobar.Shown.Reader"));
135 OutdatedPluginInfoBarDelegate::~OutdatedPluginInfoBarDelegate() {
136 content::RecordAction(UserMetricsAction("OutdatedPluginInfobar.Closed"));
139 void OutdatedPluginInfoBarDelegate::InfoBarDismissed() {
140 content::RecordAction(UserMetricsAction("OutdatedPluginInfobar.Dismissed"));
143 base::string16
OutdatedPluginInfoBarDelegate::GetMessageText() const {
147 base::string16
OutdatedPluginInfoBarDelegate::GetButtonLabel(
148 InfoBarButton button
) const {
149 return l10n_util::GetStringUTF16((button
== BUTTON_OK
) ?
150 IDS_PLUGIN_UPDATE
: IDS_PLUGIN_ENABLE_TEMPORARILY
);
153 bool OutdatedPluginInfoBarDelegate::Accept() {
154 DCHECK_EQ(PluginInstaller::INSTALLER_STATE_IDLE
, installer()->state());
155 content::RecordAction(UserMetricsAction("OutdatedPluginInfobar.Update"));
156 // A call to any of |OpenDownloadURL()| or |StartInstalling()| will
157 // result in deleting ourselves. Accordingly, we make sure to
158 // not pass a reference to an object that can go away.
159 GURL
plugin_url(plugin_metadata_
->plugin_url());
160 content::WebContents
* web_contents
=
161 InfoBarService::WebContentsFromInfoBar(infobar());
162 if (plugin_metadata_
->url_for_display())
163 installer()->OpenDownloadURL(plugin_url
, web_contents
);
165 installer()->StartInstalling(plugin_url
, web_contents
);
169 bool OutdatedPluginInfoBarDelegate::Cancel() {
170 content::RecordAction(
171 UserMetricsAction("OutdatedPluginInfobar.AllowThisTime"));
172 LoadBlockedPlugins();
176 bool OutdatedPluginInfoBarDelegate::LinkClicked(
177 WindowOpenDisposition disposition
) {
178 content::RecordAction(UserMetricsAction("OutdatedPluginInfobar.LearnMore"));
179 return PluginInfoBarDelegate::LinkClicked(disposition
);
182 std::string
OutdatedPluginInfoBarDelegate::GetLearnMoreURL() const {
183 return chrome::kOutdatedPluginLearnMoreURL
;
186 void OutdatedPluginInfoBarDelegate::DownloadStarted() {
187 ReplaceWithInfoBar(l10n_util::GetStringFUTF16(IDS_PLUGIN_DOWNLOADING
,
188 plugin_metadata_
->name()));
191 void OutdatedPluginInfoBarDelegate::DownloadError(const std::string
& message
) {
192 ReplaceWithInfoBar(l10n_util::GetStringFUTF16(IDS_PLUGIN_DOWNLOAD_ERROR_SHORT
,
193 plugin_metadata_
->name()));
196 void OutdatedPluginInfoBarDelegate::DownloadCancelled() {
197 ReplaceWithInfoBar(l10n_util::GetStringFUTF16(IDS_PLUGIN_DOWNLOAD_CANCELLED
,
198 plugin_metadata_
->name()));
201 void OutdatedPluginInfoBarDelegate::DownloadFinished() {
202 ReplaceWithInfoBar(l10n_util::GetStringFUTF16(IDS_PLUGIN_UPDATING
,
203 plugin_metadata_
->name()));
206 void OutdatedPluginInfoBarDelegate::OnlyWeakObserversLeft() {
207 infobar()->RemoveSelf();
210 void OutdatedPluginInfoBarDelegate::ReplaceWithInfoBar(
211 const base::string16
& message
) {
212 // Return early if the message doesn't change. This is important in case the
213 // PluginInstaller is still iterating over its observers (otherwise we would
214 // keep replacing infobar delegates infinitely).
215 if ((message_
== message
) || !infobar()->owner())
217 Replace(infobar(), installer(), plugin_metadata_
->Clone(), message
);
221 void OutdatedPluginInfoBarDelegate::Replace(
222 infobars::InfoBar
* infobar
,
223 PluginInstaller
* installer
,
224 scoped_ptr
<PluginMetadata
> plugin_metadata
,
225 const base::string16
& message
) {
226 DCHECK(infobar
->owner());
227 infobar
->owner()->ReplaceInfoBar(
229 infobar
->owner()->CreateConfirmInfoBar(
230 scoped_ptr
<ConfirmInfoBarDelegate
>(new OutdatedPluginInfoBarDelegate(
231 installer
, plugin_metadata
.Pass(), message
))));
236 // PluginMetroModeInfoBarDelegate ---------------------------------------------
239 void PluginMetroModeInfoBarDelegate::Create(
240 InfoBarService
* infobar_service
,
241 PluginMetroModeInfoBarDelegate::Mode mode
,
242 const base::string16
& name
) {
243 infobar_service
->AddInfoBar(
244 infobar_service
->CreateConfirmInfoBar(scoped_ptr
<ConfirmInfoBarDelegate
>(
245 new PluginMetroModeInfoBarDelegate(mode
, name
))));
248 PluginMetroModeInfoBarDelegate::PluginMetroModeInfoBarDelegate(
249 PluginMetroModeInfoBarDelegate::Mode mode
,
250 const base::string16
& name
)
251 : ConfirmInfoBarDelegate(),
256 PluginMetroModeInfoBarDelegate::~PluginMetroModeInfoBarDelegate() {
259 int PluginMetroModeInfoBarDelegate::GetIconId() const {
260 return IDR_INFOBAR_PLUGIN_INSTALL
;
263 base::string16
PluginMetroModeInfoBarDelegate::GetMessageText() const {
264 return l10n_util::GetStringFUTF16((mode_
== MISSING_PLUGIN
) ?
265 IDS_METRO_MISSING_PLUGIN_PROMPT
: IDS_METRO_NPAPI_PLUGIN_PROMPT
, name_
);
268 int PluginMetroModeInfoBarDelegate::GetButtons() const {
272 base::string16
PluginMetroModeInfoBarDelegate::GetButtonLabel(
273 InfoBarButton button
) const {
274 return l10n_util::GetStringUTF16(IDS_WIN_DESKTOP_RESTART
);
277 void LaunchDesktopInstanceHelper(const base::string16
& url
) {
278 base::FilePath exe_path
;
279 if (!PathService::Get(base::FILE_EXE
, &exe_path
))
281 base::FilePath
shortcut_path(
282 ShellIntegration::GetStartMenuShortcut(exe_path
));
284 // Actually launching the process needs to happen in the metro viewer,
285 // otherwise it won't automatically transition to desktop. So we have
286 // to send an IPC to the viewer to do the ShellExecute.
287 aura::RemoteWindowTreeHostWin::Instance()->HandleOpenURLOnDesktop(
291 bool PluginMetroModeInfoBarDelegate::Accept() {
292 chrome::AttemptRestartToDesktopMode();
296 base::string16
PluginMetroModeInfoBarDelegate::GetLinkText() const {
297 return l10n_util::GetStringUTF16(IDS_LEARN_MORE
);
300 bool PluginMetroModeInfoBarDelegate::LinkClicked(
301 WindowOpenDisposition disposition
) {
302 // TODO(shrikant): We may need to change language a little at following
303 // support URLs. With new approach we will just restart for both missing
304 // and not missing mode.
305 InfoBarService::WebContentsFromInfoBar(infobar())->OpenURL(
306 content::OpenURLParams(
307 GURL((mode_
== MISSING_PLUGIN
) ?
308 "https://support.google.com/chrome/?p=ib_display_in_desktop" :
309 "https://support.google.com/chrome/?p=ib_redirect_to_desktop"),
311 (disposition
== CURRENT_TAB
) ? NEW_FOREGROUND_TAB
: disposition
,
312 ui::PAGE_TRANSITION_LINK
, false));
316 #endif // defined(OS_WIN)
318 #endif // defined(ENABLE_PLUGIN_INSTALLATION)