Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(108)

Side by Side Diff: chrome/browser/ui/webui/nacl_ui.cc

Issue 10823214: Add a chrome://nacl page to show if NaCl is enabled and if PNaCl is installed. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: handle disable_nacl=1 Created 8 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
(Empty)
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.
4
5 #include "chrome/browser/ui/webui/nacl_ui.h"
6
7 #include <map>
8 #include <string>
9 #include <vector>
10
11 #include "base/bind.h"
12 #include "base/bind_helpers.h"
13 #include "base/command_line.h"
14 #include "base/memory/weak_ptr.h"
15 #include "base/path_service.h"
16 #include "base/string16.h"
17 #include "base/string_number_conversions.h"
18 #include "base/utf_string_conversions.h"
19 #include "base/values.h"
20 #include "chrome/browser/plugin_prefs.h"
21 #include "chrome/browser/profiles/profile.h"
22 #include "chrome/browser/ui/webui/chrome_url_data_manager.h"
23 #include "chrome/browser/ui/webui/chrome_web_ui_data_source.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/plugin_service.h"
29 #include "content/public/browser/user_metrics.h"
30 #include "content/public/browser/web_ui.h"
31 #include "content/public/browser/web_ui_message_handler.h"
32 #include "grit/browser_resources.h"
33 #include "grit/chromium_strings.h"
34 #include "grit/generated_resources.h"
35 #include "grit/theme_resources.h"
36 #include "ui/base/l10n/l10n_util.h"
37 #include "ui/base/resource/resource_bundle.h"
38 #include "webkit/plugins/webplugininfo.h"
39
40 #if defined(OS_WIN)
41 #include "base/win/windows_version.h"
42 #endif
43
44 using content::PluginService;
45 using content::UserMetricsAction;
46 using content::WebUIMessageHandler;
47
48 namespace {
49
50 ChromeWebUIDataSource* CreateNaClUIHTMLSource() {
51 ChromeWebUIDataSource* source =
52 new ChromeWebUIDataSource(chrome::kChromeUINaClHost);
53
54 source->AddLocalizedString("loadingMessage", IDS_NACL_LOADING_MESSAGE);
55 source->AddLocalizedString("naclLongTitle", IDS_NACL_TITLE_MESSAGE);
56 source->set_json_path("strings.js");
57 source->add_resource_path("about_nacl.js", IDR_ABOUT_NACL_JS);
58 source->set_default_resource(IDR_ABOUT_NACL_HTML);
59 return source;
60 }
61
62 ////////////////////////////////////////////////////////////////////////////////
63 //
64 // NaClDOMHandler
65 //
66 ////////////////////////////////////////////////////////////////////////////////
67
68 // The handler for JavaScript messages for the about:flags page.
69 class NaClDOMHandler : public WebUIMessageHandler {
70 public:
71 NaClDOMHandler();
72 virtual ~NaClDOMHandler();
73
74 // WebUIMessageHandler implementation.
75 virtual void RegisterMessages() OVERRIDE;
76
77 // Callback for the "requestNaClInfo" message.
78 void HandleRequestNaClInfo(const ListValue* args);
79
80 // Callback for the NaCl plugin information.
81 void OnGotPlugins(const std::vector<webkit::WebPluginInfo>& plugins);
82
83 private:
84 // Called when we think we might have enough information to return data back
James Hawkins 2012/08/09 17:16:55 nit: Don't use pronouns (we) in comments; pronouns
jvoung (off chromium) 2012/08/09 20:03:35 Done.
85 // to the page.
86 void MaybeRespondToPage();
87
88 // Helper for MaybeRespondToPage -- should be called after we have enough
89 // information gathered (asynchronously).
90 void PopulatePageInformation(DictionaryValue* naclInfo);
91
92 // Factory for the creating refs in callbacks.
93 base::WeakPtrFactory<NaClDOMHandler> weak_ptr_factory_;
94
95 // Whether the page has requested data.
96 bool page_has_requested_data_;
97 // Whether the plugin information is ready.
98 bool has_plugin_info_;
99
100 DISALLOW_COPY_AND_ASSIGN(NaClDOMHandler);
101 };
102
103 NaClDOMHandler::NaClDOMHandler()
104 : weak_ptr_factory_(ALLOW_THIS_IN_INITIALIZER_LIST(this)),
105 page_has_requested_data_(false),
106 has_plugin_info_(false) {
107 PluginService::GetInstance()->GetPlugins(base::Bind(
108 &NaClDOMHandler::OnGotPlugins, weak_ptr_factory_.GetWeakPtr()));
109 }
110
111 NaClDOMHandler::~NaClDOMHandler() {
112 }
113
114 void NaClDOMHandler::RegisterMessages() {
115 web_ui()->RegisterMessageCallback("requestNaClInfo",
James Hawkins 2012/08/09 17:16:55 nit: Start of parameter rows must align on the sam
jvoung (off chromium) 2012/08/09 20:03:35 Done.
116 base::Bind(&NaClDOMHandler::HandleRequestNaClInfo,
117 base::Unretained(this)));
118 }
119
120 void AddPair(ListValue* list, const string16& key, const string16& value) {
James Hawkins 2012/08/09 17:16:55 nit: These methods need to be documented.
jvoung (off chromium) 2012/08/09 20:03:35 Done.
121 DictionaryValue* results = new DictionaryValue();
122 results->SetString("key", key);
James Hawkins 2012/08/09 17:16:55 results->SetString(key, value)?
jvoung (off chromium) 2012/08/09 20:03:35 Wouldn't that mess up the templating?
James Hawkins 2012/08/11 19:24:28 I don't know.
jvoung (off chromium) 2012/08/13 22:15:43 Yeah that simplification would have messed up the
123 results->SetString("value", value);
124 list->Append(results);
125 }
126
127 void AddPair(ListValue* list, const string16& key, const std::string& value) {
James Hawkins 2012/08/09 17:16:55 Why do we need all of these permutations of AddPai
jvoung (off chromium) 2012/08/09 20:03:35 Hmm, some of the "values" are string16, coming fro
James Hawkins 2012/08/11 19:24:28 Yes.
128 AddPair(list, key, ASCIIToUTF16(value));
129 }
130
131 void AddPair(ListValue* list, const std::string& key, const string16& value) {
132 AddPair(list, ASCIIToUTF16(key), value);
133 }
134
135 void AddPair(ListValue* list, const std::string& key,
136 const std::string& value) {
137 AddPair(list, ASCIIToUTF16(key), ASCIIToUTF16(value));
138 }
139
140 void AddLineBreak(ListValue* list) {
141 AddPair(list, "", "");
142 }
143
144 void ListFlagStatus(ListValue* list, const std::string& flag_label,
145 const std::string& flag_name) {
146 if (CommandLine::ForCurrentProcess()->HasSwitch(flag_name)) {
147 AddPair(list, flag_label, "On");
148 } else {
149 AddPair(list, flag_label, "Off");
150 }
151 }
152
153 void NaClDOMHandler::HandleRequestNaClInfo(const ListValue* args) {
154 page_has_requested_data_ = true;
155 MaybeRespondToPage();
156 }
157
158 void NaClDOMHandler::OnGotPlugins(
159 const std::vector<webkit::WebPluginInfo>& plugins) {
160 has_plugin_info_ = true;
161 MaybeRespondToPage();
162 }
163
164 void NaClDOMHandler::PopulatePageInformation(DictionaryValue* naclInfo) {
165 // Store Key-Value pairs of about-information.
166 scoped_ptr<ListValue> list(new ListValue());
167
168 // Obtain the Chrome version info.
169 chrome::VersionInfo version_info;
170 AddPair(list.get(),
171 l10n_util::GetStringUTF16(IDS_PRODUCT_NAME),
172 version_info.Version() + " (" +
173 chrome::VersionInfo::GetVersionStringModifier() + ")");
174
175 // OS version information.
176 // TODO(jvoung): refactor this to share the extra windows labeling
177 // with about:flash, or something.
178 std::string os_label = version_info.OSType();
179 #if defined(OS_WIN)
180 base::win::OSInfo* os = base::win::OSInfo::GetInstance();
181 switch (os->version()) {
182 case base::win::VERSION_XP: os_label += " XP"; break;
183 case base::win::VERSION_SERVER_2003:
184 os_label += " Server 2003 or XP Pro 64 bit";
185 break;
186 case base::win::VERSION_VISTA: os_label += " Vista or Server 2008"; break;
187 case base::win::VERSION_WIN7: os_label += " 7 or Server 2008 R2"; break;
188 case base::win::VERSION_WIN8: os_label += " 8 or Server 2012"; break;
189 default: os_label += " UNKNOWN"; break;
190 }
191 os_label += " SP" + base::IntToString(os->service_pack().major);
192 if (os->service_pack().minor > 0)
193 os_label += "." + base::IntToString(os->service_pack().minor);
194 if (os->architecture() == base::win::OSInfo::X64_ARCHITECTURE)
195 os_label += " 64 bit";
196 #endif
197 AddPair(list.get(),
198 l10n_util::GetStringUTF16(IDS_ABOUT_VERSION_OS), os_label);
199
200 AddLineBreak(list.get());
201
202 // Obtain the version of the NaCl plugin.
203 std::vector<webkit::WebPluginInfo> info_array;
204 PluginService::GetInstance()->GetPluginInfoArray(
205 GURL(), "application/x-nacl", false, &info_array, NULL);
206 string16 nacl_version;
207 if (info_array.empty()) {
208 AddPair(list.get(), "NaCl plugin", "Disabled");
209 } else {
210 PluginPrefs* plugin_prefs =
211 PluginPrefs::GetForProfile(Profile::FromWebUI(web_ui()));
212
213 // Only the 0th plugin is used.
214 nacl_version = info_array[0].version + ASCIIToUTF16(" ") +
215 info_array[0].path.LossyDisplayName();
216 if (!plugin_prefs->IsPluginEnabled(info_array[0])) {
217 nacl_version += ASCIIToUTF16(" (Disabled in profile prefs)");
218 AddPair(list.get(), "NaCl plugin", nacl_version);
219 }
220
221 AddPair(list.get(), "NaCl plugin", nacl_version);
222
223 // Mark the rest as not used.
224 for (size_t i = 1; i < info_array.size(); ++i) {
225 nacl_version = info_array[i].version + ASCIIToUTF16(" ") +
226 info_array[i].path.LossyDisplayName();
227 nacl_version += ASCIIToUTF16(" (not used)");
228 if (!plugin_prefs->IsPluginEnabled(info_array[i])) {
229 nacl_version += ASCIIToUTF16(" (Disabled in profile prefs)");
230 }
231 AddPair(list.get(), "NaCl plugin", nacl_version);
232 }
233 }
234
235 // Check that commandline flags are enabled.
236 ListFlagStatus(list.get(), "Flag '--enable-nacl'", switches::kEnableNaCl);
237
238 AddLineBreak(list.get());
239
240 // Obtain the version of the PNaCl translator.
241 FilePath pnacl_path;
242 bool got_path = PathService::Get(chrome::DIR_PNACL_COMPONENT, &pnacl_path);
243 if (!got_path || pnacl_path.empty()) {
244 AddPair(list.get(), "PNaCl translator", "Not installed");
245 } else {
246 AddPair(list.get(), "PNaCl translator path: ",
247 pnacl_path.LossyDisplayName());
248 AddPair(list.get(), "PNaCl translator version: ",
249 pnacl_path.BaseName().LossyDisplayName());
250 }
251
252 ListFlagStatus(list.get(), "Flag '--enable-pnacl'", switches::kEnablePnacl);
253 // naclInfo will take ownership of list, and clean it up on destruction.
254 naclInfo->Set("naclInfo", list.release());
255 }
256
257 void NaClDOMHandler::MaybeRespondToPage() {
258 // We don't reply until everything is ready. The page is showing a 'loading'
259 // message until then.
260 if (!page_has_requested_data_ || !has_plugin_info_) {
James Hawkins 2012/08/09 17:16:55 Optional nit: Save a line by removing braces for s
jvoung (off chromium) 2012/08/09 20:03:35 Done.
261 return;
262 }
263
264 DictionaryValue naclInfo;
265 PopulatePageInformation(&naclInfo);
266 web_ui()->CallJavascriptFunction("returnNaClInfo", naclInfo);
267 }
268
269 } // namespace
270
271 ///////////////////////////////////////////////////////////////////////////////
272 //
273 // NaClUI
274 //
275 ///////////////////////////////////////////////////////////////////////////////
276
277 NaClUI::NaClUI(content::WebUI* web_ui) : WebUIController(web_ui) {
278 content::RecordAction(UserMetricsAction("ViewAboutNaCl"));
279
280 web_ui->AddMessageHandler(new NaClDOMHandler());
281
282 // Set up the about:nacl source.
283 Profile* profile = Profile::FromWebUI(web_ui);
284 ChromeURLDataManager::AddDataSource(profile, CreateNaClUIHTMLSource());
285 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698