Don't show supervised user as "already on this device" while they're being imported.
[chromium-blink-merge.git] / chrome / browser / ui / webui / nacl_ui.cc
blob26482c7a83c5fd4b35db212e83c05b99be30b1d0
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"
7 #include <string>
8 #include <vector>
10 #include "base/bind.h"
11 #include "base/bind_helpers.h"
12 #include "base/command_line.h"
13 #include "base/files/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 "chrome/grit/chromium_strings.h"
29 #include "chrome/grit/generated_resources.h"
30 #include "content/public/browser/browser_thread.h"
31 #include "content/public/browser/plugin_service.h"
32 #include "content/public/browser/user_metrics.h"
33 #include "content/public/browser/web_ui.h"
34 #include "content/public/browser/web_ui_data_source.h"
35 #include "content/public/browser/web_ui_message_handler.h"
36 #include "content/public/common/webplugininfo.h"
37 #include "grit/browser_resources.h"
38 #include "ui/base/l10n/l10n_util.h"
40 #if defined(OS_WIN)
41 #include "base/win/windows_version.h"
42 #endif
44 using base::ASCIIToUTF16;
45 using base::UserMetricsAction;
46 using content::BrowserThread;
47 using content::PluginService;
48 using content::WebUIMessageHandler;
50 namespace {
52 content::WebUIDataSource* CreateNaClUIHTMLSource() {
53 content::WebUIDataSource* source =
54 content::WebUIDataSource::Create(chrome::kChromeUINaClHost);
56 source->AddLocalizedString("loadingMessage", IDS_NACL_LOADING_MESSAGE);
57 source->AddLocalizedString("naclLongTitle", IDS_NACL_TITLE_MESSAGE);
58 source->SetJsonPath("strings.js");
59 source->AddResourcePath("about_nacl.css", IDR_ABOUT_NACL_CSS);
60 source->AddResourcePath("about_nacl.js", IDR_ABOUT_NACL_JS);
61 source->SetDefaultResource(IDR_ABOUT_NACL_HTML);
62 return source;
65 ////////////////////////////////////////////////////////////////////////////////
67 // NaClDomHandler
69 ////////////////////////////////////////////////////////////////////////////////
71 // The handler for JavaScript messages for the about:flags page.
72 class NaClDomHandler : public WebUIMessageHandler {
73 public:
74 NaClDomHandler();
75 ~NaClDomHandler() override;
77 // WebUIMessageHandler implementation.
78 void RegisterMessages() override;
80 private:
81 // Callback for the "requestNaClInfo" message.
82 void HandleRequestNaClInfo(const base::ListValue* args);
84 // Callback for the NaCl plugin information.
85 void OnGotPlugins(const std::vector<content::WebPluginInfo>& plugins);
87 // A helper callback that receives the result of checking if PNaCl path
88 // exists and checking the PNaCl |version|. |is_valid| is true if the PNaCl
89 // path that was returned by PathService is valid, and false otherwise.
90 void DidCheckPathAndVersion(const std::string* version, bool is_valid);
92 // Called when enough information is gathered to return data back to the page.
93 void MaybeRespondToPage();
95 // Helper for MaybeRespondToPage -- called after enough information
96 // is gathered.
97 void PopulatePageInformation(base::DictionaryValue* naclInfo);
99 // Returns whether the specified plugin is enabled.
100 bool isPluginEnabled(size_t plugin_index);
102 // Adds information regarding the operating system and chrome version to list.
103 void AddOperatingSystemInfo(base::ListValue* list);
105 // Adds the list of plugins for NaCl to list.
106 void AddPluginList(base::ListValue* list);
108 // Adds the information relevant to PNaCl (e.g., enablement, paths, version)
109 // to the list.
110 void AddPnaclInfo(base::ListValue* list);
112 // Adds the information relevant to NaCl to list.
113 void AddNaClInfo(base::ListValue* list);
115 // Whether the page has requested data.
116 bool page_has_requested_data_;
118 // Whether the plugin information is ready.
119 bool has_plugin_info_;
121 // Whether PNaCl path was validated. PathService can return a path
122 // that does not exists, so it needs to be validated.
123 bool pnacl_path_validated_;
124 bool pnacl_path_exists_;
125 std::string pnacl_version_string_;
127 // Factory for the creating refs in callbacks.
128 base::WeakPtrFactory<NaClDomHandler> weak_ptr_factory_;
130 DISALLOW_COPY_AND_ASSIGN(NaClDomHandler);
133 NaClDomHandler::NaClDomHandler()
134 : page_has_requested_data_(false),
135 has_plugin_info_(false),
136 pnacl_path_validated_(false),
137 pnacl_path_exists_(false),
138 weak_ptr_factory_(this) {
139 PluginService::GetInstance()->GetPlugins(base::Bind(
140 &NaClDomHandler::OnGotPlugins, weak_ptr_factory_.GetWeakPtr()));
143 NaClDomHandler::~NaClDomHandler() {
146 void NaClDomHandler::RegisterMessages() {
147 web_ui()->RegisterMessageCallback(
148 "requestNaClInfo",
149 base::Bind(&NaClDomHandler::HandleRequestNaClInfo,
150 base::Unretained(this)));
153 // Helper functions for collecting a list of key-value pairs that will
154 // be displayed.
155 void AddPair(base::ListValue* list,
156 const base::string16& key,
157 const base::string16& value) {
158 base::DictionaryValue* results = new base::DictionaryValue();
159 results->SetString("key", key);
160 results->SetString("value", value);
161 list->Append(results);
164 // Generate an empty data-pair which acts as a line break.
165 void AddLineBreak(base::ListValue* list) {
166 AddPair(list, ASCIIToUTF16(""), ASCIIToUTF16(""));
169 bool NaClDomHandler::isPluginEnabled(size_t plugin_index) {
170 std::vector<content::WebPluginInfo> info_array;
171 PluginService::GetInstance()->GetPluginInfoArray(
172 GURL(), "application/x-nacl", false, &info_array, NULL);
173 PluginPrefs* plugin_prefs =
174 PluginPrefs::GetForProfile(Profile::FromWebUI(web_ui())).get();
175 return (!info_array.empty() &&
176 plugin_prefs->IsPluginEnabled(info_array[plugin_index]));
179 void NaClDomHandler::AddOperatingSystemInfo(base::ListValue* list) {
180 // Obtain the Chrome version info.
181 chrome::VersionInfo version_info;
182 AddPair(list,
183 l10n_util::GetStringUTF16(IDS_PRODUCT_NAME),
184 ASCIIToUTF16(version_info.Version() + " (" +
185 chrome::VersionInfo::GetVersionStringModifier() + ")"));
187 // OS version information.
188 // TODO(jvoung): refactor this to share the extra windows labeling
189 // with about:flash, or something.
190 std::string os_label = version_info.OSType();
191 #if defined(OS_WIN)
192 base::win::OSInfo* os = base::win::OSInfo::GetInstance();
193 switch (os->version()) {
194 case base::win::VERSION_XP: os_label += " XP"; break;
195 case base::win::VERSION_SERVER_2003:
196 os_label += " Server 2003 or XP Pro 64 bit";
197 break;
198 case base::win::VERSION_VISTA: os_label += " Vista or Server 2008"; break;
199 case base::win::VERSION_WIN7: os_label += " 7 or Server 2008 R2"; break;
200 case base::win::VERSION_WIN8: os_label += " 8 or Server 2012"; break;
201 default: os_label += " UNKNOWN"; break;
203 os_label += " SP" + base::IntToString(os->service_pack().major);
204 if (os->service_pack().minor > 0)
205 os_label += "." + base::IntToString(os->service_pack().minor);
206 if (os->architecture() == base::win::OSInfo::X64_ARCHITECTURE)
207 os_label += " 64 bit";
208 #endif
209 AddPair(list,
210 l10n_util::GetStringUTF16(IDS_ABOUT_VERSION_OS),
211 ASCIIToUTF16(os_label));
212 AddLineBreak(list);
215 void NaClDomHandler::AddPluginList(base::ListValue* list) {
216 // Obtain the version of the NaCl plugin.
217 std::vector<content::WebPluginInfo> info_array;
218 PluginService::GetInstance()->GetPluginInfoArray(
219 GURL(), "application/x-nacl", false, &info_array, NULL);
220 base::string16 nacl_version;
221 base::string16 nacl_key = ASCIIToUTF16("NaCl plugin");
222 if (info_array.empty()) {
223 AddPair(list, nacl_key, ASCIIToUTF16("Disabled"));
224 } else {
225 // Only the 0th plugin is used.
226 nacl_version = info_array[0].version + ASCIIToUTF16(" ") +
227 info_array[0].path.LossyDisplayName();
228 if (!isPluginEnabled(0)) {
229 nacl_version += ASCIIToUTF16(" (Disabled in profile prefs)");
232 AddPair(list, nacl_key, nacl_version);
234 // Mark the rest as not used.
235 for (size_t i = 1; i < info_array.size(); ++i) {
236 nacl_version = info_array[i].version + ASCIIToUTF16(" ") +
237 info_array[i].path.LossyDisplayName();
238 nacl_version += ASCIIToUTF16(" (not used)");
239 if (!isPluginEnabled(i)) {
240 nacl_version += ASCIIToUTF16(" (Disabled in profile prefs)");
242 AddPair(list, nacl_key, nacl_version);
245 AddLineBreak(list);
248 void NaClDomHandler::AddPnaclInfo(base::ListValue* list) {
249 // Display whether PNaCl is enabled.
250 base::string16 pnacl_enabled_string = ASCIIToUTF16("Enabled");
251 if (!isPluginEnabled(0)) {
252 pnacl_enabled_string = ASCIIToUTF16("Disabled in profile prefs");
254 AddPair(list,
255 ASCIIToUTF16("Portable Native Client (PNaCl)"),
256 pnacl_enabled_string);
258 // Obtain the version of the PNaCl translator.
259 base::FilePath pnacl_path;
260 bool got_path = PathService::Get(chrome::DIR_PNACL_COMPONENT, &pnacl_path);
261 if (!got_path || pnacl_path.empty() || !pnacl_path_exists_) {
262 AddPair(list,
263 ASCIIToUTF16("PNaCl translator"),
264 ASCIIToUTF16("Not installed"));
265 } else {
266 AddPair(list,
267 ASCIIToUTF16("PNaCl translator path"),
268 pnacl_path.LossyDisplayName());
269 AddPair(list,
270 ASCIIToUTF16("PNaCl translator version"),
271 ASCIIToUTF16(pnacl_version_string_));
273 AddLineBreak(list);
276 void NaClDomHandler::AddNaClInfo(base::ListValue* list) {
277 base::string16 nacl_enabled_string = ASCIIToUTF16("Disabled");
278 if (isPluginEnabled(0) &&
279 base::CommandLine::ForCurrentProcess()->HasSwitch(
280 switches::kEnableNaCl)) {
281 nacl_enabled_string = ASCIIToUTF16("Enabled by flag '--enable-nacl'");
283 AddPair(list,
284 ASCIIToUTF16("Native Client (non-portable, outside web store)"),
285 nacl_enabled_string);
286 AddLineBreak(list);
289 void NaClDomHandler::HandleRequestNaClInfo(const base::ListValue* args) {
290 page_has_requested_data_ = true;
291 // Force re-validation of PNaCl's path in the next call to
292 // MaybeRespondToPage(), in case PNaCl went from not-installed
293 // to installed since the request.
294 pnacl_path_validated_ = false;
295 MaybeRespondToPage();
298 void NaClDomHandler::OnGotPlugins(
299 const std::vector<content::WebPluginInfo>& plugins) {
300 has_plugin_info_ = true;
301 MaybeRespondToPage();
304 void NaClDomHandler::PopulatePageInformation(base::DictionaryValue* naclInfo) {
305 DCHECK(pnacl_path_validated_);
306 // Store Key-Value pairs of about-information.
307 scoped_ptr<base::ListValue> list(new base::ListValue());
308 // Display the operating system and chrome version information.
309 AddOperatingSystemInfo(list.get());
310 // Display the list of plugins serving NaCl.
311 AddPluginList(list.get());
312 // Display information relevant to PNaCl.
313 AddPnaclInfo(list.get());
314 // Display information relevant to NaCl (non-portable.
315 AddNaClInfo(list.get());
316 // naclInfo will take ownership of list, and clean it up on destruction.
317 naclInfo->Set("naclInfo", list.release());
320 void NaClDomHandler::DidCheckPathAndVersion(const std::string* version,
321 bool is_valid) {
322 pnacl_path_validated_ = true;
323 pnacl_path_exists_ = is_valid;
324 pnacl_version_string_ = *version;
325 MaybeRespondToPage();
328 void CheckVersion(const base::FilePath& pnacl_path, std::string* version) {
329 base::FilePath pnacl_json_path =
330 pnacl_path.AppendASCII("pnacl_public_pnacl_json");
331 JSONFileValueDeserializer deserializer(pnacl_json_path);
332 std::string error;
333 scoped_ptr<base::Value> root(deserializer.Deserialize(NULL, &error));
334 if (!root || !root->IsType(base::Value::TYPE_DICTIONARY))
335 return;
337 // Now try to get the field. This may leave version empty if the
338 // the "get" fails (no key, or wrong type).
339 static_cast<base::DictionaryValue*>(root.get())->GetStringASCII(
340 "pnacl-version", version);
343 bool CheckPathAndVersion(std::string* version) {
344 base::FilePath pnacl_path;
345 bool got_path = PathService::Get(chrome::DIR_PNACL_COMPONENT, &pnacl_path);
346 if (got_path && !pnacl_path.empty() && base::PathExists(pnacl_path)) {
347 CheckVersion(pnacl_path, version);
348 return true;
350 return false;
353 void NaClDomHandler::MaybeRespondToPage() {
354 // Don't reply until everything is ready. The page will show a 'loading'
355 // message until then.
356 if (!page_has_requested_data_ || !has_plugin_info_)
357 return;
359 if (!pnacl_path_validated_) {
360 std::string* version_string = new std::string;
361 base::PostTaskAndReplyWithResult(
362 BrowserThread::GetBlockingPool(),
363 FROM_HERE,
364 base::Bind(&CheckPathAndVersion, version_string),
365 base::Bind(&NaClDomHandler::DidCheckPathAndVersion,
366 weak_ptr_factory_.GetWeakPtr(),
367 base::Owned(version_string)));
368 return;
371 base::DictionaryValue naclInfo;
372 PopulatePageInformation(&naclInfo);
373 web_ui()->CallJavascriptFunction("nacl.returnNaClInfo", naclInfo);
376 } // namespace
378 ///////////////////////////////////////////////////////////////////////////////
380 // NaClUI
382 ///////////////////////////////////////////////////////////////////////////////
384 NaClUI::NaClUI(content::WebUI* web_ui) : WebUIController(web_ui) {
385 content::RecordAction(UserMetricsAction("ViewAboutNaCl"));
387 web_ui->AddMessageHandler(new NaClDomHandler());
389 // Set up the about:nacl source.
390 Profile* profile = Profile::FromWebUI(web_ui);
391 content::WebUIDataSource::Add(profile, CreateNaClUIHTMLSource());