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/ui/pdf/pdf_unsupported_feature.h"
8 #include "base/memory/scoped_ptr.h"
9 #include "base/strings/utf_string_conversions.h"
10 #include "chrome/browser/lifetime/application_lifetime.h"
11 #include "chrome/browser/plugins/chrome_plugin_service_filter.h"
12 #include "chrome/browser/plugins/plugin_metadata.h"
13 #include "chrome/browser/plugins/plugin_prefs.h"
14 #include "chrome/browser/profiles/profile.h"
15 #include "chrome/browser/renderer_preferences_util.h"
16 #include "chrome/browser/tab_contents/tab_util.h"
17 #include "chrome/common/chrome_content_client.h"
18 #include "chrome/grit/generated_resources.h"
19 #include "components/pdf/browser/open_pdf_in_reader_prompt_client.h"
20 #include "components/pdf/browser/pdf_web_contents_helper.h"
21 #include "content/public/browser/interstitial_page.h"
22 #include "content/public/browser/interstitial_page_delegate.h"
23 #include "content/public/browser/navigation_details.h"
24 #include "content/public/browser/navigation_entry.h"
25 #include "content/public/browser/page_navigator.h"
26 #include "content/public/browser/render_frame_host.h"
27 #include "content/public/browser/render_process_host.h"
28 #include "content/public/browser/render_view_host.h"
29 #include "content/public/browser/user_metrics.h"
30 #include "content/public/browser/web_contents.h"
31 #include "content/public/common/renderer_preferences.h"
32 #include "grit/browser_resources.h"
33 #include "ui/base/l10n/l10n_util.h"
34 #include "ui/base/resource/resource_bundle.h"
35 #include "ui/base/webui/jstemplate_builder.h"
38 #include "base/win/metro.h"
39 #include "chrome/browser/ui/pdf/adobe_reader_info_win.h"
42 using base::UserMetricsAction
;
43 using content::InterstitialPage
;
44 using content::OpenURLParams
;
45 using content::Referrer
;
46 using content::WebContents
;
47 using content::WebPluginInfo
;
52 const char kAdobeReaderUpdateUrl
[] = "http://www.adobe.com/go/getreader_chrome";
54 // The prompt delegate used to ask the user if they want to use Adobe Reader
56 class PDFEnableAdobeReaderPromptClient
57 : public pdf::OpenPDFInReaderPromptClient
{
59 explicit PDFEnableAdobeReaderPromptClient(Profile
* profile
);
60 ~PDFEnableAdobeReaderPromptClient() override
;
62 // pdf::OpenPDFInReaderPromptClient
63 base::string16
GetMessageText() const override
;
64 base::string16
GetAcceptButtonText() const override
;
65 base::string16
GetCancelButtonText() const override
;
67 const content::LoadCommittedDetails
& details
) const override
;
68 void Accept() override
;
69 void Cancel() override
;
77 DISALLOW_IMPLICIT_CONSTRUCTORS(PDFEnableAdobeReaderPromptClient
);
80 PDFEnableAdobeReaderPromptClient::PDFEnableAdobeReaderPromptClient(
83 content::RecordAction(UserMetricsAction("PDF_EnableReaderInfoBarShown"));
86 PDFEnableAdobeReaderPromptClient::~PDFEnableAdobeReaderPromptClient() {
89 bool PDFEnableAdobeReaderPromptClient::ShouldExpire(
90 const content::LoadCommittedDetails
& details
) const {
91 ui::PageTransition transition
=
92 ui::PageTransitionStripQualifier(details
.entry
->GetTransitionType());
93 // We don't want to expire on a reload, because that is how we open the PDF in
95 return !details
.is_in_page
&& transition
!= ui::PAGE_TRANSITION_RELOAD
;
98 void PDFEnableAdobeReaderPromptClient::Accept() {
99 content::RecordAction(UserMetricsAction("PDF_EnableReaderInfoBarOK"));
100 PluginPrefs
* plugin_prefs
= PluginPrefs::GetForProfile(profile_
).get();
101 plugin_prefs
->EnablePluginGroup(
102 true, base::ASCIIToUTF16(PluginMetadata::kAdobeReaderGroupName
));
103 plugin_prefs
->EnablePluginGroup(
104 false, base::ASCIIToUTF16(ChromeContentClient::kPDFPluginName
));
107 void PDFEnableAdobeReaderPromptClient::Cancel() {
108 content::RecordAction(UserMetricsAction("PDF_EnableReaderInfoBarCancel"));
111 base::string16
PDFEnableAdobeReaderPromptClient::GetAcceptButtonText() const {
112 return l10n_util::GetStringUTF16(IDS_PDF_INFOBAR_ALWAYS_USE_READER_BUTTON
);
115 base::string16
PDFEnableAdobeReaderPromptClient::GetCancelButtonText() const {
116 return l10n_util::GetStringUTF16(IDS_DONE
);
119 base::string16
PDFEnableAdobeReaderPromptClient::GetMessageText() const {
120 return l10n_util::GetStringUTF16(IDS_PDF_INFOBAR_QUESTION_ALWAYS_USE_READER
);
123 // Launch the url to get the latest Adbobe Reader installer.
124 void OpenReaderUpdateURL(WebContents
* web_contents
) {
125 OpenURLParams
params(
126 GURL(kAdobeReaderUpdateUrl
), Referrer(), NEW_FOREGROUND_TAB
,
127 ui::PAGE_TRANSITION_LINK
, false);
128 web_contents
->OpenURL(params
);
131 // Opens the PDF using Adobe Reader.
132 void OpenUsingReader(WebContents
* web_contents
,
133 const WebPluginInfo
& reader_plugin
,
134 pdf::OpenPDFInReaderPromptClient
* client
) {
135 ChromePluginServiceFilter::GetInstance()->OverridePluginForFrame(
136 web_contents
->GetRenderProcessHost()->GetID(),
137 web_contents
->GetMainFrame()->GetRoutingID(),
138 web_contents
->GetURL(),
140 web_contents
->ReloadFocusedFrame(false);
142 pdf::PDFWebContentsHelper
* pdf_tab_helper
=
143 pdf::PDFWebContentsHelper::FromWebContents(web_contents
);
145 pdf_tab_helper
->ShowOpenInReaderPrompt(make_scoped_ptr(client
));
148 // An interstitial to be used when the user chooses to open a PDF using Adobe
149 // Reader, but it is out of date.
150 class PDFUnsupportedFeatureInterstitial
151 : public content::InterstitialPageDelegate
{
153 PDFUnsupportedFeatureInterstitial(
154 WebContents
* web_contents
,
155 const WebPluginInfo
& reader_webplugininfo
)
156 : web_contents_(web_contents
),
157 reader_webplugininfo_(reader_webplugininfo
) {
158 content::RecordAction(UserMetricsAction("PDF_ReaderInterstitialShown"));
159 interstitial_page_
= InterstitialPage::Create(
160 web_contents
, false, web_contents
->GetURL(), this);
161 interstitial_page_
->Show();
165 // InterstitialPageDelegate implementation.
166 std::string
GetHTMLContents() override
{
167 base::DictionaryValue strings
;
170 l10n_util::GetStringUTF16(IDS_READER_OUT_OF_DATE_BLOCKING_PAGE_TITLE
));
173 l10n_util::GetStringUTF16(IDS_READER_OUT_OF_DATE_BLOCKING_PAGE_BODY
));
176 l10n_util::GetStringUTF16(IDS_READER_OUT_OF_DATE_BLOCKING_PAGE_UPDATE
));
179 l10n_util::GetStringUTF16(
180 IDS_READER_OUT_OF_DATE_BLOCKING_PAGE_PROCEED
));
183 l10n_util::GetStringUTF16(IDS_READER_OUT_OF_DATE_BLOCKING_PAGE_OK
));
186 l10n_util::GetStringUTF16(IDS_READER_OUT_OF_DATE_BLOCKING_PAGE_CANCEL
));
188 base::StringPiece
html(ResourceBundle::GetSharedInstance().
189 GetRawDataResource(IDR_READER_OUT_OF_DATE_HTML
));
191 return webui::GetI18nTemplateHtml(html
, &strings
);
194 void CommandReceived(const std::string
& command
) override
{
195 if (command
== "0") {
196 content::RecordAction(
197 UserMetricsAction("PDF_ReaderInterstitialCancel"));
198 interstitial_page_
->DontProceed();
202 if (command
== "1") {
203 content::RecordAction(
204 UserMetricsAction("PDF_ReaderInterstitialUpdate"));
205 OpenReaderUpdateURL(web_contents_
);
206 } else if (command
== "2") {
207 content::RecordAction(
208 UserMetricsAction("PDF_ReaderInterstitialIgnore"));
209 // Pretend that the plugin is up-to-date so that we don't block it.
210 reader_webplugininfo_
.version
= base::ASCIIToUTF16("11.0.0.0");
211 OpenUsingReader(web_contents_
, reader_webplugininfo_
, NULL
);
215 interstitial_page_
->Proceed();
218 void OverrideRendererPrefs(content::RendererPreferences
* prefs
) override
{
220 Profile::FromBrowserContext(web_contents_
->GetBrowserContext());
221 renderer_preferences_util::UpdateFromSystemSettings(
222 prefs
, profile
, web_contents_
);
226 WebContents
* web_contents_
;
227 WebPluginInfo reader_webplugininfo_
;
228 InterstitialPage
* interstitial_page_
; // Owns us.
230 DISALLOW_COPY_AND_ASSIGN(PDFUnsupportedFeatureInterstitial
);
233 // The delegate for the bubble used to inform the user that we don't support a
234 // feature in the PDF.
235 class PDFUnsupportedFeaturePromptClient
236 : public pdf::OpenPDFInReaderPromptClient
{
238 PDFUnsupportedFeaturePromptClient(WebContents
* web_contents
,
239 const AdobeReaderPluginInfo
& reader_info
);
240 ~PDFUnsupportedFeaturePromptClient() override
;
242 // pdf::OpenPDFInReaderPromptClient:
243 base::string16
GetMessageText() const override
;
244 base::string16
GetAcceptButtonText() const override
;
245 base::string16
GetCancelButtonText() const override
;
247 const content::LoadCommittedDetails
& details
) const override
;
248 void Accept() override
;
249 void Cancel() override
;
252 WebContents
* web_contents_
;
253 const AdobeReaderPluginInfo reader_info_
;
255 DISALLOW_IMPLICIT_CONSTRUCTORS(PDFUnsupportedFeaturePromptClient
);
258 PDFUnsupportedFeaturePromptClient::PDFUnsupportedFeaturePromptClient(
259 WebContents
* web_contents
,
260 const AdobeReaderPluginInfo
& reader_info
)
261 : web_contents_(web_contents
), reader_info_(reader_info
) {
262 content::RecordAction(reader_info_
.is_installed
?
263 UserMetricsAction("PDF_UseReaderInfoBarShown") :
264 UserMetricsAction("PDF_InstallReaderInfoBarShown"));
267 PDFUnsupportedFeaturePromptClient::~PDFUnsupportedFeaturePromptClient() {
270 base::string16
PDFUnsupportedFeaturePromptClient::GetMessageText() const {
271 return l10n_util::GetStringUTF16(IDS_PDF_BUBBLE_MESSAGE
);
274 base::string16
PDFUnsupportedFeaturePromptClient::GetAcceptButtonText() const {
275 if (base::win::IsMetroProcess())
276 return l10n_util::GetStringUTF16(IDS_PDF_BUBBLE_METRO_MODE_LINK
);
278 return l10n_util::GetStringUTF16(
279 reader_info_
.is_installed
? IDS_PDF_BUBBLE_OPEN_IN_READER_LINK
280 : IDS_PDF_BUBBLE_INSTALL_READER_LINK
);
283 base::string16
PDFUnsupportedFeaturePromptClient::GetCancelButtonText() const {
284 return l10n_util::GetStringUTF16(IDS_DONE
);
287 bool PDFUnsupportedFeaturePromptClient::ShouldExpire(
288 const content::LoadCommittedDetails
& details
) const {
289 return !details
.is_in_page
;
292 void PDFUnsupportedFeaturePromptClient::Accept() {
293 if (base::win::IsMetroProcess()) {
294 chrome::AttemptRestartToDesktopMode();
298 if (!reader_info_
.is_installed
) {
299 content::RecordAction(UserMetricsAction("PDF_InstallReaderInfoBarOK"));
300 OpenReaderUpdateURL(web_contents_
);
304 content::RecordAction(UserMetricsAction("PDF_UseReaderInfoBarOK"));
306 if (!reader_info_
.is_secure
) {
307 new PDFUnsupportedFeatureInterstitial(web_contents_
,
308 reader_info_
.plugin_info
);
313 Profile::FromBrowserContext(web_contents_
->GetBrowserContext());
314 pdf::OpenPDFInReaderPromptClient
* client
=
315 new PDFEnableAdobeReaderPromptClient(profile
);
317 OpenUsingReader(web_contents_
, reader_info_
.plugin_info
, client
);
320 void PDFUnsupportedFeaturePromptClient::Cancel() {
321 content::RecordAction(reader_info_
.is_installed
?
322 UserMetricsAction("PDF_UseReaderInfoBarCancel") :
323 UserMetricsAction("PDF_InstallReaderInfoBarCancel"));
326 void MaybeShowOpenPDFInReaderPrompt(WebContents
* web_contents
,
327 const AdobeReaderPluginInfo
& reader_info
) {
328 // If the Reader plugin is disabled by policy, don't prompt them.
329 if (!reader_info
.is_installed
|| !reader_info
.is_enabled
)
332 scoped_ptr
<pdf::OpenPDFInReaderPromptClient
> prompt(
333 new PDFUnsupportedFeaturePromptClient(web_contents
, reader_info
));
334 pdf::PDFWebContentsHelper
* pdf_tab_helper
=
335 pdf::PDFWebContentsHelper::FromWebContents(web_contents
);
336 pdf_tab_helper
->ShowOpenInReaderPrompt(prompt
.Pass());
339 void GotPluginsCallback(int process_id
,
341 const AdobeReaderPluginInfo
& reader_info
) {
342 WebContents
* web_contents
=
343 tab_util::GetWebContentsByID(process_id
, routing_id
);
345 MaybeShowOpenPDFInReaderPrompt(web_contents
, reader_info
);
349 #endif // defined(OS_WIN)
351 void PDFHasUnsupportedFeature(WebContents
* web_contents
) {
353 // Only works for Windows for now. For Mac, we'll have to launch the file
354 // externally since Adobe Reader doesn't work inside Chrome.
356 Profile::FromBrowserContext(web_contents
->GetBrowserContext());
357 AdobeReaderPluginInfo reader_info
;
358 if (GetAdobeReaderPluginInfo(profile
, &reader_info
)) {
359 MaybeShowOpenPDFInReaderPrompt(web_contents
, reader_info
);
362 GetAdobeReaderPluginInfoAsync(
364 base::Bind(&GotPluginsCallback
,
365 web_contents
->GetRenderProcessHost()->GetID(),
366 web_contents
->GetRenderViewHost()->GetRoutingID()));
367 #endif // defined(OS_WIN)