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/webui/nacl_ui.h"
10 #include "base/bind.h"
11 #include "base/bind_helpers.h"
12 #include "base/command_line.h"
13 #include "base/file_util.h"
14 #include "base/json/json_file_value_serializer.h"
15 #include "base/memory/weak_ptr.h"
16 #include "base/path_service.h"
17 #include "base/strings/string16.h"
18 #include "base/strings/string_number_conversions.h"
19 #include "base/strings/utf_string_conversions.h"
20 #include "base/threading/sequenced_worker_pool.h"
21 #include "base/values.h"
22 #include "chrome/browser/plugins/plugin_prefs.h"
23 #include "chrome/browser/profiles/profile.h"
24 #include "chrome/common/chrome_paths.h"
25 #include "chrome/common/chrome_switches.h"
26 #include "chrome/common/chrome_version_info.h"
27 #include "chrome/common/url_constants.h"
28 #include "content/public/browser/browser_thread.h"
29 #include "content/public/browser/plugin_service.h"
30 #include "content/public/browser/user_metrics.h"
31 #include "content/public/browser/web_ui.h"
32 #include "content/public/browser/web_ui_data_source.h"
33 #include "content/public/browser/web_ui_message_handler.h"
34 #include "content/public/common/webplugininfo.h"
35 #include "grit/browser_resources.h"
36 #include "grit/chromium_strings.h"
37 #include "grit/generated_resources.h"
38 #include "grit/theme_resources.h"
39 #include "ui/base/l10n/l10n_util.h"
40 #include "ui/base/resource/resource_bundle.h"
43 #include "base/win/windows_version.h"
46 using base::ASCIIToUTF16
;
47 using base::UserMetricsAction
;
48 using content::BrowserThread
;
49 using content::PluginService
;
50 using content::WebUIMessageHandler
;
54 content::WebUIDataSource
* CreateNaClUIHTMLSource() {
55 content::WebUIDataSource
* source
=
56 content::WebUIDataSource::Create(chrome::kChromeUINaClHost
);
58 source
->SetUseJsonJSFormatV2();
59 source
->AddLocalizedString("loadingMessage", IDS_NACL_LOADING_MESSAGE
);
60 source
->AddLocalizedString("naclLongTitle", IDS_NACL_TITLE_MESSAGE
);
61 source
->SetJsonPath("strings.js");
62 source
->AddResourcePath("about_nacl.css", IDR_ABOUT_NACL_CSS
);
63 source
->AddResourcePath("about_nacl.js", IDR_ABOUT_NACL_JS
);
64 source
->SetDefaultResource(IDR_ABOUT_NACL_HTML
);
68 ////////////////////////////////////////////////////////////////////////////////
72 ////////////////////////////////////////////////////////////////////////////////
74 // The handler for JavaScript messages for the about:flags page.
75 class NaClDomHandler
: public WebUIMessageHandler
{
78 virtual ~NaClDomHandler();
80 // WebUIMessageHandler implementation.
81 virtual void RegisterMessages() OVERRIDE
;
84 // Callback for the "requestNaClInfo" message.
85 void HandleRequestNaClInfo(const base::ListValue
* args
);
87 // Callback for the NaCl plugin information.
88 void OnGotPlugins(const std::vector
<content::WebPluginInfo
>& plugins
);
90 // A helper callback that receives the result of checking if PNaCl path
91 // exists and checking the PNaCl |version|. |is_valid| is true if the PNaCl
92 // path that was returned by PathService is valid, and false otherwise.
93 void DidCheckPathAndVersion(const std::string
* version
, bool is_valid
);
95 // Called when enough information is gathered to return data back to the page.
96 void MaybeRespondToPage();
98 // Helper for MaybeRespondToPage -- called after enough information
100 void PopulatePageInformation(base::DictionaryValue
* naclInfo
);
102 // Returns whether the specified plugin is enabled.
103 bool isPluginEnabled(size_t plugin_index
);
105 // Adds information regarding the operating system and chrome version to list.
106 void AddOperatingSystemInfo(base::ListValue
* list
);
108 // Adds the list of plugins for NaCl to list.
109 void AddPluginList(base::ListValue
* list
);
111 // Adds the information relevant to PNaCl (e.g., enablement, paths, version)
113 void AddPnaclInfo(base::ListValue
* list
);
115 // Adds the information relevant to NaCl to list.
116 void AddNaClInfo(base::ListValue
* list
);
118 // Whether the page has requested data.
119 bool page_has_requested_data_
;
121 // Whether the plugin information is ready.
122 bool has_plugin_info_
;
124 // Whether PNaCl path was validated. PathService can return a path
125 // that does not exists, so it needs to be validated.
126 bool pnacl_path_validated_
;
127 bool pnacl_path_exists_
;
128 std::string pnacl_version_string_
;
130 // Factory for the creating refs in callbacks.
131 base::WeakPtrFactory
<NaClDomHandler
> weak_ptr_factory_
;
133 DISALLOW_COPY_AND_ASSIGN(NaClDomHandler
);
136 NaClDomHandler::NaClDomHandler()
137 : page_has_requested_data_(false),
138 has_plugin_info_(false),
139 pnacl_path_validated_(false),
140 pnacl_path_exists_(false),
141 weak_ptr_factory_(this) {
142 PluginService::GetInstance()->GetPlugins(base::Bind(
143 &NaClDomHandler::OnGotPlugins
, weak_ptr_factory_
.GetWeakPtr()));
146 NaClDomHandler::~NaClDomHandler() {
149 void NaClDomHandler::RegisterMessages() {
150 web_ui()->RegisterMessageCallback(
152 base::Bind(&NaClDomHandler::HandleRequestNaClInfo
,
153 base::Unretained(this)));
156 // Helper functions for collecting a list of key-value pairs that will
158 void AddPair(base::ListValue
* list
,
159 const base::string16
& key
,
160 const base::string16
& value
) {
161 base::DictionaryValue
* results
= new base::DictionaryValue();
162 results
->SetString("key", key
);
163 results
->SetString("value", value
);
164 list
->Append(results
);
167 // Generate an empty data-pair which acts as a line break.
168 void AddLineBreak(base::ListValue
* list
) {
169 AddPair(list
, ASCIIToUTF16(""), ASCIIToUTF16(""));
172 bool NaClDomHandler::isPluginEnabled(size_t plugin_index
) {
173 std::vector
<content::WebPluginInfo
> info_array
;
174 PluginService::GetInstance()->GetPluginInfoArray(
175 GURL(), "application/x-nacl", false, &info_array
, NULL
);
176 PluginPrefs
* plugin_prefs
=
177 PluginPrefs::GetForProfile(Profile::FromWebUI(web_ui())).get();
178 return (!info_array
.empty() &&
179 plugin_prefs
->IsPluginEnabled(info_array
[plugin_index
]));
182 void NaClDomHandler::AddOperatingSystemInfo(base::ListValue
* list
) {
183 // Obtain the Chrome version info.
184 chrome::VersionInfo version_info
;
186 l10n_util::GetStringUTF16(IDS_PRODUCT_NAME
),
187 ASCIIToUTF16(version_info
.Version() + " (" +
188 chrome::VersionInfo::GetVersionStringModifier() + ")"));
190 // OS version information.
191 // TODO(jvoung): refactor this to share the extra windows labeling
192 // with about:flash, or something.
193 std::string os_label
= version_info
.OSType();
195 base::win::OSInfo
* os
= base::win::OSInfo::GetInstance();
196 switch (os
->version()) {
197 case base::win::VERSION_XP
: os_label
+= " XP"; break;
198 case base::win::VERSION_SERVER_2003
:
199 os_label
+= " Server 2003 or XP Pro 64 bit";
201 case base::win::VERSION_VISTA
: os_label
+= " Vista or Server 2008"; break;
202 case base::win::VERSION_WIN7
: os_label
+= " 7 or Server 2008 R2"; break;
203 case base::win::VERSION_WIN8
: os_label
+= " 8 or Server 2012"; break;
204 default: os_label
+= " UNKNOWN"; break;
206 os_label
+= " SP" + base::IntToString(os
->service_pack().major
);
207 if (os
->service_pack().minor
> 0)
208 os_label
+= "." + base::IntToString(os
->service_pack().minor
);
209 if (os
->architecture() == base::win::OSInfo::X64_ARCHITECTURE
)
210 os_label
+= " 64 bit";
213 l10n_util::GetStringUTF16(IDS_ABOUT_VERSION_OS
),
214 ASCIIToUTF16(os_label
));
218 void NaClDomHandler::AddPluginList(base::ListValue
* list
) {
219 // Obtain the version of the NaCl plugin.
220 std::vector
<content::WebPluginInfo
> info_array
;
221 PluginService::GetInstance()->GetPluginInfoArray(
222 GURL(), "application/x-nacl", false, &info_array
, NULL
);
223 base::string16 nacl_version
;
224 base::string16 nacl_key
= ASCIIToUTF16("NaCl plugin");
225 if (info_array
.empty()) {
226 AddPair(list
, nacl_key
, ASCIIToUTF16("Disabled"));
228 // Only the 0th plugin is used.
229 nacl_version
= info_array
[0].version
+ ASCIIToUTF16(" ") +
230 info_array
[0].path
.LossyDisplayName();
231 if (!isPluginEnabled(0)) {
232 nacl_version
+= ASCIIToUTF16(" (Disabled in profile prefs)");
235 AddPair(list
, nacl_key
, nacl_version
);
237 // Mark the rest as not used.
238 for (size_t i
= 1; i
< info_array
.size(); ++i
) {
239 nacl_version
= info_array
[i
].version
+ ASCIIToUTF16(" ") +
240 info_array
[i
].path
.LossyDisplayName();
241 nacl_version
+= ASCIIToUTF16(" (not used)");
242 if (!isPluginEnabled(i
)) {
243 nacl_version
+= ASCIIToUTF16(" (Disabled in profile prefs)");
245 AddPair(list
, nacl_key
, nacl_version
);
251 void NaClDomHandler::AddPnaclInfo(base::ListValue
* list
) {
252 // Display whether PNaCl is enabled.
253 base::string16 pnacl_enabled_string
= ASCIIToUTF16("Enabled");
254 if (!isPluginEnabled(0)) {
255 pnacl_enabled_string
= ASCIIToUTF16("Disabled in profile prefs");
256 } else if (CommandLine::ForCurrentProcess()->HasSwitch(
257 switches::kDisablePnacl
)) {
258 pnacl_enabled_string
= ASCIIToUTF16("Disabled by flag '--disable-pnacl'");
261 ASCIIToUTF16("Portable Native Client (PNaCl)"),
262 pnacl_enabled_string
);
264 // Obtain the version of the PNaCl translator.
265 base::FilePath pnacl_path
;
266 bool got_path
= PathService::Get(chrome::DIR_PNACL_COMPONENT
, &pnacl_path
);
267 if (!got_path
|| pnacl_path
.empty() || !pnacl_path_exists_
) {
269 ASCIIToUTF16("PNaCl translator"),
270 ASCIIToUTF16("Not installed"));
273 ASCIIToUTF16("PNaCl translator path"),
274 pnacl_path
.LossyDisplayName());
276 ASCIIToUTF16("PNaCl translator version"),
277 ASCIIToUTF16(pnacl_version_string_
));
282 void NaClDomHandler::AddNaClInfo(base::ListValue
* list
) {
283 base::string16 nacl_enabled_string
= ASCIIToUTF16("Disabled");
284 if (isPluginEnabled(0) &&
285 CommandLine::ForCurrentProcess()->HasSwitch(switches::kEnableNaCl
)) {
286 nacl_enabled_string
= ASCIIToUTF16("Enabled by flag '--enable-nacl'");
289 ASCIIToUTF16("Native Client (non-portable, outside web store)"),
290 nacl_enabled_string
);
294 void NaClDomHandler::HandleRequestNaClInfo(const base::ListValue
* args
) {
295 page_has_requested_data_
= true;
296 // Force re-validation of PNaCl's path in the next call to
297 // MaybeRespondToPage(), in case PNaCl went from not-installed
298 // to installed since the request.
299 pnacl_path_validated_
= false;
300 MaybeRespondToPage();
303 void NaClDomHandler::OnGotPlugins(
304 const std::vector
<content::WebPluginInfo
>& plugins
) {
305 has_plugin_info_
= true;
306 MaybeRespondToPage();
309 void NaClDomHandler::PopulatePageInformation(base::DictionaryValue
* naclInfo
) {
310 DCHECK(pnacl_path_validated_
);
311 // Store Key-Value pairs of about-information.
312 scoped_ptr
<base::ListValue
> list(new base::ListValue());
313 // Display the operating system and chrome version information.
314 AddOperatingSystemInfo(list
.get());
315 // Display the list of plugins serving NaCl.
316 AddPluginList(list
.get());
317 // Display information relevant to PNaCl.
318 AddPnaclInfo(list
.get());
319 // Display information relevant to NaCl (non-portable.
320 AddNaClInfo(list
.get());
321 // naclInfo will take ownership of list, and clean it up on destruction.
322 naclInfo
->Set("naclInfo", list
.release());
325 void NaClDomHandler::DidCheckPathAndVersion(const std::string
* version
,
327 pnacl_path_validated_
= true;
328 pnacl_path_exists_
= is_valid
;
329 pnacl_version_string_
= *version
;
330 MaybeRespondToPage();
333 void CheckVersion(const base::FilePath
& pnacl_path
, std::string
* version
) {
334 base::FilePath pnacl_json_path
=
335 pnacl_path
.AppendASCII("pnacl_public_pnacl_json");
336 JSONFileValueSerializer
serializer(pnacl_json_path
);
338 scoped_ptr
<base::Value
> root(serializer
.Deserialize(NULL
, &error
));
339 if (!root
|| !root
->IsType(base::Value::TYPE_DICTIONARY
))
342 // Now try to get the field. This may leave version empty if the
343 // the "get" fails (no key, or wrong type).
344 static_cast<base::DictionaryValue
*>(root
.get())->GetStringASCII(
345 "pnacl-version", version
);
348 bool CheckPathAndVersion(std::string
* version
) {
349 base::FilePath pnacl_path
;
350 bool got_path
= PathService::Get(chrome::DIR_PNACL_COMPONENT
, &pnacl_path
);
351 if (got_path
&& !pnacl_path
.empty() && base::PathExists(pnacl_path
)) {
352 CheckVersion(pnacl_path
, version
);
358 void NaClDomHandler::MaybeRespondToPage() {
359 // Don't reply until everything is ready. The page will show a 'loading'
360 // message until then.
361 if (!page_has_requested_data_
|| !has_plugin_info_
)
364 if (!pnacl_path_validated_
) {
365 std::string
* version_string
= new std::string
;
366 base::PostTaskAndReplyWithResult(
367 BrowserThread::GetBlockingPool(),
369 base::Bind(&CheckPathAndVersion
, version_string
),
370 base::Bind(&NaClDomHandler::DidCheckPathAndVersion
,
371 weak_ptr_factory_
.GetWeakPtr(),
372 base::Owned(version_string
)));
376 base::DictionaryValue naclInfo
;
377 PopulatePageInformation(&naclInfo
);
378 web_ui()->CallJavascriptFunction("nacl.returnNaClInfo", naclInfo
);
383 ///////////////////////////////////////////////////////////////////////////////
387 ///////////////////////////////////////////////////////////////////////////////
389 NaClUI::NaClUI(content::WebUI
* web_ui
) : WebUIController(web_ui
) {
390 content::RecordAction(UserMetricsAction("ViewAboutNaCl"));
392 web_ui
->AddMessageHandler(new NaClDomHandler());
394 // Set up the about:nacl source.
395 Profile
* profile
= Profile::FromWebUI(web_ui
);
396 content::WebUIDataSource::Add(profile
, CreateNaClUIHTMLSource());