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

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: make list alloc more apparent 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
« no previous file with comments | « chrome/browser/ui/webui/nacl_ui.h ('k') | chrome/chrome_browser.gypi » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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
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",
116 base::Bind(&NaClDOMHandler::HandleRequestNaClInfo,
117 base::Unretained(this)));
118 }
119
120 void AddPair(ListValue* list, const string16& key, const string16& value) {
121 DictionaryValue* results = new DictionaryValue();
122 results->SetString("key", key);
123 results->SetString("value", value);
124 list->Append(results);
125 }
126
127 void AddPair(ListValue* list, const string16& key, const std::string& value) {
robertm 2012/08/09 14:59:38 what about adding another variant where the first
jvoung (off chromium) 2012/08/09 17:00:22 Done.
128 AddPair(list, key, ASCIIToUTF16(value));
129 }
130
131 void AddLineBreak(ListValue* list) {
132 AddPair(list, ASCIIToUTF16(""), ASCIIToUTF16(""));
133 }
134
135 void ListFlagStatus(ListValue* list, const std::string& flag_label,
136 const std::string& flag_name) {
137 string16 flag_label_as_key = ASCIIToUTF16(flag_label);
138 if (CommandLine::ForCurrentProcess()->HasSwitch(flag_name)) {
139 AddPair(list, flag_label_as_key, "On");
140 } else {
141 AddPair(list, flag_label_as_key, "Off");
142 }
143 }
144
145 void NaClDOMHandler::HandleRequestNaClInfo(const ListValue* args) {
146 page_has_requested_data_ = true;
147 MaybeRespondToPage();
148 }
149
150 void NaClDOMHandler::OnGotPlugins(
151 const std::vector<webkit::WebPluginInfo>& plugins) {
152 has_plugin_info_ = true;
153 MaybeRespondToPage();
154 }
155
156 void NaClDOMHandler::PopulatePageInformation(DictionaryValue* naclInfo) {
157 // Store Key-Value pairs of about-information.
158 scoped_ptr<ListValue> list(new ListValue());
robertm 2012/08/09 14:59:38 the scoped_ptr seems a little overkill'ish
jvoung (off chromium) 2012/08/09 17:00:22 Well, just wanted to make it clear how its memory
159
160 // Obtain the Chrome version info.
161 chrome::VersionInfo version_info;
162 AddPair(list.get(),
163 l10n_util::GetStringUTF16(IDS_PRODUCT_NAME),
164 version_info.Version() + " (" +
165 chrome::VersionInfo::GetVersionStringModifier() + ")");
166
167 // OS version information.
168 // TODO(jvoung): refactor this to share the extra windows labeling
169 // with about:flash, or something.
170 std::string os_label = version_info.OSType();
171 #if defined(OS_WIN)
172 base::win::OSInfo* os = base::win::OSInfo::GetInstance();
173 switch (os->version()) {
174 case base::win::VERSION_XP: os_label += " XP"; break;
175 case base::win::VERSION_SERVER_2003:
176 os_label += " Server 2003 or XP Pro 64 bit";
177 break;
178 case base::win::VERSION_VISTA: os_label += " Vista or Server 2008"; break;
179 case base::win::VERSION_WIN7: os_label += " 7 or Server 2008 R2"; break;
180 case base::win::VERSION_WIN8: os_label += " 8 or Server 2012"; break;
181 default: os_label += " UNKNOWN"; break;
182 }
183 os_label += " SP" + base::IntToString(os->service_pack().major);
184 if (os->service_pack().minor > 0)
185 os_label += "." + base::IntToString(os->service_pack().minor);
186 if (os->architecture() == base::win::OSInfo::X64_ARCHITECTURE)
187 os_label += " 64 bit";
188 #endif
189 AddPair(list.get(), l10n_util::GetStringUTF16(IDS_ABOUT_VERSION_OS), os_label) ;
190
191 AddLineBreak(list.get());
192
193 // Obtain the version of the NaCl plugin.
194 std::vector<webkit::WebPluginInfo> info_array;
195 PluginService::GetInstance()->GetPluginInfoArray(
196 GURL(), "application/x-nacl", false, &info_array, NULL);
197 string16 nacl_version;
198 if (info_array.empty()) {
199 AddPair(list.get(), ASCIIToUTF16("NaCl plugin"), "Disabled");
200 } else {
201 PluginPrefs* plugin_prefs =
202 PluginPrefs::GetForProfile(Profile::FromWebUI(web_ui()));
203
204 // Only the 0th plugin is used.
205 nacl_version = info_array[0].version + ASCIIToUTF16(" ") +
206 info_array[0].path.LossyDisplayName();
207 if (!plugin_prefs->IsPluginEnabled(info_array[0])) {
208 nacl_version += ASCIIToUTF16(" (Disabled in profile prefs)");
209 AddPair(list.get(), ASCIIToUTF16("NaCl plugin"), nacl_version);
210 }
211
212 AddPair(list.get(), ASCIIToUTF16("NaCl plugin"), nacl_version);
213
214 // Mark the rest as not used.
215 for (size_t i = 1; i < info_array.size(); ++i) {
216 nacl_version = info_array[i].version + ASCIIToUTF16(" ") +
217 info_array[i].path.LossyDisplayName();
218 nacl_version += ASCIIToUTF16(" (not used)");
219 if (!plugin_prefs->IsPluginEnabled(info_array[i])) {
220 nacl_version += ASCIIToUTF16(" (Disabled in profile prefs)");
221 }
222 AddPair(list.get(), ASCIIToUTF16("NaCl plugin"), nacl_version);
223 }
224 }
225
226 // Check that commandline flags are enabled.
227 ListFlagStatus(list.get(), "Flag '--enable-nacl'", switches::kEnableNaCl);
228
229 AddLineBreak(list.get());
230
231 // Obtain the version of the PNaCl translator.
232 FilePath pnacl_path;
233 bool got_path = PathService::Get(chrome::DIR_PNACL_COMPONENT, &pnacl_path);
234 if (!got_path || pnacl_path.empty()) {
235 AddPair(list.get(), ASCIIToUTF16("PNaCl translator"), "Not installed");
236 } else {
237 AddPair(list.get(), ASCIIToUTF16("PNaCl translator path: "),
238 pnacl_path.LossyDisplayName());
239 AddPair(list.get(), ASCIIToUTF16("PNaCl translator version: "),
240 pnacl_path.BaseName().LossyDisplayName());
241 }
242
243 ListFlagStatus(list.get(), "Flag '--enable-pnacl'", switches::kEnablePnacl);
244 // naclInfo will take ownership of list, and clean it up on destruction.
245 naclInfo->Set("naclInfo", list.release());
246 }
247
248 void NaClDOMHandler::MaybeRespondToPage() {
249 // We don't reply until everything is ready. The page is showing a 'loading'
250 // message until then.
251 if (!page_has_requested_data_ || !has_plugin_info_) {
252 return;
253 }
254
255 DictionaryValue naclInfo;
256 PopulatePageInformation(&naclInfo);
257 web_ui()->CallJavascriptFunction("returnNaClInfo", naclInfo);
258 }
259
260 } // namespace
261
262 ///////////////////////////////////////////////////////////////////////////////
263 //
264 // NaClUI
265 //
266 ///////////////////////////////////////////////////////////////////////////////
267
268 NaClUI::NaClUI(content::WebUI* web_ui) : WebUIController(web_ui) {
269 content::RecordAction(UserMetricsAction("ViewAboutNaCl"));
270
271 web_ui->AddMessageHandler(new NaClDOMHandler());
272
273 // Set up the about:nacl source.
274 Profile* profile = Profile::FromWebUI(web_ui);
275 ChromeURLDataManager::AddDataSource(profile, CreateNaClUIHTMLSource());
276 }
OLDNEW
« no previous file with comments | « chrome/browser/ui/webui/nacl_ui.h ('k') | chrome/chrome_browser.gypi » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698