|
OLD | NEW |
---|---|
(Empty) | |
1 // Copyright (c) 2011 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. | |
4 | |
5 #include "chrome/browser/pdf_unsupported_feature.h" | |
6 | |
7 #include "app/l10n_util.h" | |
8 #include "app/resource_bundle.h" | |
9 #include "base/utf_string_conversions.h" | |
10 #include "base/values.h" | |
11 #include "base/version.h" | |
12 #include "chrome/browser/plugin_service.h" | |
13 #include "chrome/browser/renderer_host/render_process_host.h" | |
14 #include "chrome/browser/renderer_host/render_view_host.h" | |
15 #include "chrome/browser/tab_contents/infobar_delegate.h" | |
16 #include "chrome/browser/tab_contents/interstitial_page.h" | |
17 #include "chrome/browser/tab_contents/tab_contents.h" | |
18 #include "chrome/common/jstemplate_builder.h" | |
19 #include "grit/browser_resources.h" | |
20 #include "grit/generated_resources.h" | |
21 #include "webkit/plugins/npapi/plugin_group.h" | |
22 #include "webkit/plugins/npapi/plugin_list.h" | |
23 #include "webkit/plugins/npapi/webplugininfo.h" | |
24 | |
25 using webkit::npapi::PluginGroup; | |
26 using webkit::npapi::PluginList; | |
27 using webkit::npapi::WebPluginInfo; | |
28 | |
29 // Only launch Adobe Reader X or later. | |
30 #define kMinReaderVersionToUse 10 | |
brettw
2011/01/21 21:42:30
Can this be a static const int?
| |
31 | |
32 namespace { | |
33 | |
34 // Launch the url to get the latest Adbobe Reader installer. | |
35 void OpenReaderUpdateURL(TabContents* tab) { | |
36 tab->OpenURL(GURL(PluginGroup::kAdobeReaderUpdateURL), GURL(), CURRENT_TAB, | |
37 PageTransition::LINK); | |
38 } | |
39 | |
40 // Opens the PDF using Adobe Reader. | |
41 void OpenUsingReader(TabContents* tab, const WebPluginInfo& reader_plugin) { | |
42 PluginService::OverriddenPlugin plugin; | |
43 plugin.render_process_id = tab->GetRenderProcessHost()->id(); | |
44 plugin.render_view_id = tab->render_view_host()->routing_id(); | |
45 plugin.url = tab->GetURL(); | |
46 plugin.plugin = reader_plugin; | |
47 | |
48 PluginService::GetInstance()->OverridePluginForTab(plugin); | |
49 tab->render_view_host()->ReloadFrame(); | |
50 } | |
51 | |
52 // An interstitial to be used when the user chooses to open a PDF using Adobe | |
53 // Reader, but it is out of date. | |
54 class PDFUnsupportedFeatureInterstitial : public InterstitialPage { | |
55 public: | |
56 PDFUnsupportedFeatureInterstitial( | |
57 TabContents* tab, | |
brettw
2011/01/21 21:42:30
Style: indent these 4 lines 2 more spaces.
| |
58 const WebPluginInfo& reader_webplugininfo) | |
59 : InterstitialPage(tab, false, tab->GetURL()), | |
60 reader_webplugininfo_(reader_webplugininfo) { | |
61 } | |
62 | |
63 protected: | |
64 // InterstitialPage implementation. | |
65 virtual std::string GetHTMLContents() { | |
66 DictionaryValue strings; | |
67 strings.SetString( | |
68 "title", | |
69 l10n_util::GetStringUTF16(IDS_READER_OUT_OF_DATE_BLOCKING_PAGE_TITLE)); | |
70 strings.SetString( | |
71 "headLine", | |
72 l10n_util::GetStringUTF16(IDS_READER_OUT_OF_DATE_BLOCKING_PAGE_BODY)); | |
73 strings.SetString( | |
74 "update", | |
75 l10n_util::GetStringUTF16(IDS_READER_OUT_OF_DATE_BLOCKING_PAGE_UPDATE)); | |
76 strings.SetString( | |
77 "open_with_reader", | |
78 l10n_util::GetStringUTF16( | |
79 IDS_READER_OUT_OF_DATE_BLOCKING_PAGE_PROCEED)); | |
80 strings.SetString( | |
81 "ok", | |
82 l10n_util::GetStringUTF16(IDS_READER_OUT_OF_DATE_BLOCKING_PAGE_OK)); | |
83 strings.SetString( | |
84 "cancel", | |
85 l10n_util::GetStringUTF16(IDS_READER_OUT_OF_DATE_BLOCKING_PAGE_CANCEL)); | |
86 | |
87 base::StringPiece html(ResourceBundle::GetSharedInstance(). | |
88 GetRawDataResource(IDR_READER_OUT_OF_DATE_HTML)); | |
89 | |
90 return jstemplate_builder::GetI18nTemplateHtml(html, &strings); | |
91 } | |
92 | |
93 virtual void CommandReceived(const std::string& command) { | |
94 if (command == "0") { | |
95 DontProceed(); | |
96 return; | |
97 } | |
98 | |
99 if (command == "1") { | |
100 OpenReaderUpdateURL(tab()); | |
101 } else if (command == "2") { | |
102 OpenUsingReader(tab(), reader_webplugininfo_); | |
103 } else { | |
104 NOTREACHED(); | |
105 } | |
106 Proceed(); | |
107 } | |
108 | |
109 private: | |
110 WebPluginInfo reader_webplugininfo_; | |
111 | |
112 DISALLOW_COPY_AND_ASSIGN(PDFUnsupportedFeatureInterstitial); | |
113 }; | |
114 | |
115 // The info bar delegate used to inform the user that we don't support a feature | |
116 // in the PDF. | |
117 class PDFUnsupportedFeatureConfirmInfoBarDelegate | |
118 : public ConfirmInfoBarDelegate { | |
119 public: | |
120 PDFUnsupportedFeatureConfirmInfoBarDelegate( | |
brettw
2011/01/21 21:42:30
Can you add a comment here that reader_group may b
| |
121 TabContents* tab_contents, | |
122 PluginGroup* reader_group) | |
123 : ConfirmInfoBarDelegate(tab_contents), | |
124 tab_contents_(tab_contents), | |
125 reader_installed_(!!reader_group), | |
126 reader_vulnerable_(false) { | |
127 if (reader_installed_) { | |
128 std::vector<WebPluginInfo> plugins = reader_group->web_plugin_infos(); | |
129 DCHECK_EQ(plugins.size(), 1u); | |
130 reader_webplugininfo_ = plugins[0]; | |
131 | |
132 reader_vulnerable_ = reader_group->IsVulnerable(); | |
133 if (!reader_vulnerable_) { | |
134 scoped_ptr<Version> version(PluginGroup::CreateVersionFromString( | |
135 reader_webplugininfo_.version)); | |
136 if (version.get()) { | |
137 if (version->components()[0] < kMinReaderVersionToUse) | |
138 reader_vulnerable_ = true; | |
139 } | |
140 } | |
141 } | |
142 } | |
143 | |
144 // ConfirmInfoBarDelegate | |
145 virtual void InfoBarClosed() { | |
146 delete this; | |
147 } | |
148 virtual Type GetInfoBarType() { | |
149 return PAGE_ACTION_TYPE; | |
150 } | |
151 virtual bool Accept() { | |
152 LaunchReader(); | |
153 return true; | |
154 } | |
155 virtual int GetButtons() const { | |
156 return BUTTON_OK | BUTTON_CANCEL; | |
157 } | |
158 virtual string16 GetButtonLabel(InfoBarButton button) const { | |
159 switch (button) { | |
160 case BUTTON_OK: | |
161 return l10n_util::GetStringUTF16( | |
162 IDS_CONFIRM_MESSAGEBOX_YES_BUTTON_LABEL); | |
163 case BUTTON_CANCEL: | |
164 return l10n_util::GetStringUTF16( | |
165 IDS_CONFIRM_MESSAGEBOX_NO_BUTTON_LABEL); | |
166 default: | |
167 // All buttons are labeled above. | |
168 NOTREACHED() << "Bad button id " << button; | |
169 return string16(); | |
170 } | |
171 } | |
172 virtual string16 GetMessageText() const { | |
173 return l10n_util::GetStringUTF16(reader_installed_ ? | |
174 IDS_PDF_INFOBAR_QUESTION_READER_INSTALLED : | |
175 IDS_PDF_INFOBAR_QUESTION_READER_NOT_INSTALLED); | |
176 } | |
177 | |
178 private: | |
179 void LaunchReader() { | |
180 if (!reader_installed_) { | |
181 OpenReaderUpdateURL(tab_contents_); | |
182 return; | |
183 } | |
184 | |
185 if (reader_vulnerable_) { | |
186 PDFUnsupportedFeatureInterstitial* interstitial = new | |
187 PDFUnsupportedFeatureInterstitial( | |
188 tab_contents_, reader_webplugininfo_); | |
189 interstitial->Show(); | |
190 return; | |
191 } | |
192 | |
193 OpenUsingReader(tab_contents_, reader_webplugininfo_); | |
194 } | |
195 | |
196 TabContents* tab_contents_; | |
197 bool reader_installed_; | |
198 bool reader_vulnerable_; | |
199 WebPluginInfo reader_webplugininfo_; | |
200 | |
201 DISALLOW_IMPLICIT_CONSTRUCTORS(PDFUnsupportedFeatureConfirmInfoBarDelegate); | |
202 }; | |
203 | |
204 } // namespace | |
205 | |
206 void PDFHasUnsupportedFeature(TabContents* tab) { | |
207 #if !defined(OS_WIN) | |
brettw
2011/01/21 21:42:30
I would have expected this entire function content
| |
208 // Only works for Windows for now. For Mac, we'll have to launch the file | |
209 // externall since Adobe Reader doesn't work inside Chrome. | |
brettw
2011/01/21 21:42:30
spelling: externally
| |
210 return; | |
211 #endif | |
212 | |
213 PluginGroup* reader_group = NULL; | |
214 std::vector<PluginGroup> plugin_groups; | |
215 PluginList::Singleton()->GetPluginGroups( | |
216 false, &plugin_groups); | |
217 string16 reader_group_name(UTF8ToUTF16(PluginGroup::kAdobeReaderGroupName)); | |
218 for (size_t i = 0; i < plugin_groups.size(); ++i) { | |
219 if (plugin_groups[i].GetGroupName() == reader_group_name) { | |
220 reader_group = &plugin_groups[i]; | |
221 break; | |
222 } | |
223 } | |
224 | |
225 // If the plugin is disabled by policy or by the user, don't prompt them. | |
226 if (reader_group && !reader_group->Enabled()) | |
227 return; | |
228 | |
229 tab->AddInfoBar(new PDFUnsupportedFeatureConfirmInfoBarDelegate( | |
230 tab, reader_group)); | |
brettw
2011/01/21 21:42:30
Indent 2 more spaces.
| |
231 } | |
OLD | NEW |