Check USB device path access when prompting users to select a device.
[chromium-blink-merge.git] / chrome / browser / plugins / plugin_infobar_delegates.cc
blob198777e4e53f96eca5a7aa9aa06a3113a7ff1cc1
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"
7 #include "base/bind.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)
31 #if defined(OS_WIN)
32 #include "base/win/metro.h"
33 #endif
34 #include "chrome/browser/plugins/plugin_installer.h"
35 #endif
37 #if defined(OS_WIN)
38 #include <shellapi.h>
39 #include "ui/base/win/shell.h"
41 #if defined(USE_AURA)
42 #include "ui/aura/remote_window_tree_host_win.h"
43 #endif
44 #endif
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));
65 return 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,
100 name)))));
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()),
110 message_(message) {
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 {
143 return message_;
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);
163 else
164 installer()->StartInstalling(plugin_url, web_contents);
165 return false;
168 bool OutdatedPluginInfoBarDelegate::Cancel() {
169 content::RecordAction(
170 UserMetricsAction("OutdatedPluginInfobar.AllowThisTime"));
171 LoadBlockedPlugins();
172 return true;
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())
215 return;
216 Replace(infobar(), installer(), plugin_metadata_->Clone(), message);
219 // static
220 void OutdatedPluginInfoBarDelegate::Replace(
221 infobars::InfoBar* infobar,
222 PluginInstaller* installer,
223 scoped_ptr<PluginMetadata> plugin_metadata,
224 const base::string16& message) {
225 DCHECK(infobar->owner());
226 infobar->owner()->ReplaceInfoBar(
227 infobar,
228 infobar->owner()->CreateConfirmInfoBar(
229 scoped_ptr<ConfirmInfoBarDelegate>(new OutdatedPluginInfoBarDelegate(
230 installer, plugin_metadata.Pass(), message))));
233 #if defined(OS_WIN)
235 // PluginMetroModeInfoBarDelegate ---------------------------------------------
237 // static
238 void PluginMetroModeInfoBarDelegate::Create(
239 InfoBarService* infobar_service,
240 PluginMetroModeInfoBarDelegate::Mode mode,
241 const base::string16& name) {
242 infobar_service->AddInfoBar(
243 infobar_service->CreateConfirmInfoBar(scoped_ptr<ConfirmInfoBarDelegate>(
244 new PluginMetroModeInfoBarDelegate(mode, name))));
247 PluginMetroModeInfoBarDelegate::PluginMetroModeInfoBarDelegate(
248 PluginMetroModeInfoBarDelegate::Mode mode,
249 const base::string16& name)
250 : ConfirmInfoBarDelegate(),
251 mode_(mode),
252 name_(name) {
255 PluginMetroModeInfoBarDelegate::~PluginMetroModeInfoBarDelegate() {
258 int PluginMetroModeInfoBarDelegate::GetIconID() const {
259 return IDR_INFOBAR_PLUGIN_INSTALL;
262 base::string16 PluginMetroModeInfoBarDelegate::GetMessageText() const {
263 return l10n_util::GetStringFUTF16((mode_ == MISSING_PLUGIN) ?
264 IDS_METRO_MISSING_PLUGIN_PROMPT : IDS_METRO_NPAPI_PLUGIN_PROMPT, name_);
267 int PluginMetroModeInfoBarDelegate::GetButtons() const {
268 return BUTTON_OK;
271 base::string16 PluginMetroModeInfoBarDelegate::GetButtonLabel(
272 InfoBarButton button) const {
273 return l10n_util::GetStringUTF16(IDS_WIN_DESKTOP_RESTART);
276 void LaunchDesktopInstanceHelper(const base::string16& url) {
277 base::FilePath exe_path;
278 if (!PathService::Get(base::FILE_EXE, &exe_path))
279 return;
280 base::FilePath shortcut_path(
281 ShellIntegration::GetStartMenuShortcut(exe_path));
283 // Actually launching the process needs to happen in the metro viewer,
284 // otherwise it won't automatically transition to desktop. So we have
285 // to send an IPC to the viewer to do the ShellExecute.
286 aura::RemoteWindowTreeHostWin::Instance()->HandleOpenURLOnDesktop(
287 shortcut_path, url);
290 bool PluginMetroModeInfoBarDelegate::Accept() {
291 chrome::AttemptRestartToDesktopMode();
292 return true;
295 base::string16 PluginMetroModeInfoBarDelegate::GetLinkText() const {
296 return l10n_util::GetStringUTF16(IDS_LEARN_MORE);
299 bool PluginMetroModeInfoBarDelegate::LinkClicked(
300 WindowOpenDisposition disposition) {
301 // TODO(shrikant): We may need to change language a little at following
302 // support URLs. With new approach we will just restart for both missing
303 // and not missing mode.
304 InfoBarService::WebContentsFromInfoBar(infobar())->OpenURL(
305 content::OpenURLParams(
306 GURL((mode_ == MISSING_PLUGIN) ?
307 "https://support.google.com/chrome/?p=ib_display_in_desktop" :
308 "https://support.google.com/chrome/?p=ib_redirect_to_desktop"),
309 content::Referrer(),
310 (disposition == CURRENT_TAB) ? NEW_FOREGROUND_TAB : disposition,
311 ui::PAGE_TRANSITION_LINK, false));
312 return false;
315 #endif // defined(OS_WIN)
317 #endif // defined(ENABLE_PLUGIN_INSTALLATION)