OLD | NEW |
---|---|
(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> | |
James Hawkins
2012/08/11 19:21:07
Where is this used?
jvoung (off chromium)
2012/08/13 22:15:43
Oops not used anymore. Removed.
| |
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.css", IDR_ABOUT_NACL_CSS); | |
58 source->add_resource_path("about_nacl.js", IDR_ABOUT_NACL_JS); | |
59 source->set_default_resource(IDR_ABOUT_NACL_HTML); | |
60 return source; | |
61 } | |
62 | |
63 //////////////////////////////////////////////////////////////////////////////// | |
64 // | |
65 // NaClDOMHandler | |
66 // | |
67 //////////////////////////////////////////////////////////////////////////////// | |
68 | |
69 // The handler for JavaScript messages for the about:flags page. | |
70 class NaClDOMHandler : public WebUIMessageHandler { | |
71 public: | |
72 NaClDOMHandler(); | |
73 virtual ~NaClDOMHandler(); | |
74 | |
75 // WebUIMessageHandler implementation. | |
76 virtual void RegisterMessages() OVERRIDE; | |
77 | |
78 // Callback for the "requestNaClInfo" message. | |
79 void HandleRequestNaClInfo(const ListValue* args); | |
80 | |
81 // Callback for the NaCl plugin information. | |
82 void OnGotPlugins(const std::vector<webkit::WebPluginInfo>& plugins); | |
83 | |
84 private: | |
85 // Called when enough information is gathered to return data back to the page. | |
86 void MaybeRespondToPage(); | |
87 | |
88 // Helper for MaybeRespondToPage -- called after enough information | |
89 // is gathered. | |
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. | |
James Hawkins
2012/08/11 19:21:07
nit: Add a blank line above this.
jvoung (off chromium)
2012/08/13 22:15:43
Done.
| |
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( | |
116 "requestNaClInfo", | |
117 base::Bind(&NaClDOMHandler::HandleRequestNaClInfo, | |
118 base::Unretained(this))); | |
119 } | |
120 | |
121 // Helper functions for collecting a list of key-value pairs that will | |
122 // be displayed. | |
123 void AddPair(ListValue* list, const string16& key, const string16& value) { | |
124 DictionaryValue* results = new DictionaryValue(); | |
125 results->SetString("key", key); | |
126 results->SetString("value", value); | |
127 list->Append(results); | |
128 } | |
129 | |
130 void AddPair(ListValue* list, const string16& key, const std::string& value) { | |
131 AddPair(list, key, ASCIIToUTF16(value)); | |
132 } | |
133 | |
134 void AddPair(ListValue* list, const std::string& key, const string16& value) { | |
135 AddPair(list, ASCIIToUTF16(key), value); | |
136 } | |
137 | |
138 void AddPair(ListValue* list, | |
139 const std::string& key, const std::string& value) { | |
140 AddPair(list, ASCIIToUTF16(key), ASCIIToUTF16(value)); | |
141 } | |
142 | |
143 // Generate an empty data-pair which acts as a line break. | |
144 void AddLineBreak(ListValue* list) { | |
145 AddPair(list, "", ""); | |
146 } | |
147 | |
148 // Check whether a commandline switch is turned on or off. | |
149 void ListFlagStatus(ListValue* list, const std::string& flag_label, | |
150 const std::string& flag_name) { | |
151 if (CommandLine::ForCurrentProcess()->HasSwitch(flag_name)) { | |
152 AddPair(list, flag_label, "On"); | |
James Hawkins
2012/08/11 19:21:07
Optional nit: Remove braces to save a line.
jvoung (off chromium)
2012/08/13 22:15:43
Done.
| |
153 } else { | |
154 AddPair(list, flag_label, "Off"); | |
155 } | |
156 } | |
157 | |
158 void NaClDOMHandler::HandleRequestNaClInfo(const ListValue* args) { | |
159 page_has_requested_data_ = true; | |
160 MaybeRespondToPage(); | |
161 } | |
162 | |
163 void NaClDOMHandler::OnGotPlugins( | |
164 const std::vector<webkit::WebPluginInfo>& plugins) { | |
165 has_plugin_info_ = true; | |
166 MaybeRespondToPage(); | |
167 } | |
168 | |
169 void NaClDOMHandler::PopulatePageInformation(DictionaryValue* naclInfo) { | |
170 // Store Key-Value pairs of about-information. | |
171 scoped_ptr<ListValue> list(new ListValue()); | |
172 | |
173 // Obtain the Chrome version info. | |
174 chrome::VersionInfo version_info; | |
175 AddPair(list.get(), | |
176 l10n_util::GetStringUTF16(IDS_PRODUCT_NAME), | |
177 version_info.Version() + " (" + | |
178 chrome::VersionInfo::GetVersionStringModifier() + ")"); | |
179 | |
180 // OS version information. | |
181 // TODO(jvoung): refactor this to share the extra windows labeling | |
182 // with about:flash, or something. | |
183 std::string os_label = version_info.OSType(); | |
184 #if defined(OS_WIN) | |
185 base::win::OSInfo* os = base::win::OSInfo::GetInstance(); | |
186 switch (os->version()) { | |
187 case base::win::VERSION_XP: os_label += " XP"; break; | |
188 case base::win::VERSION_SERVER_2003: | |
189 os_label += " Server 2003 or XP Pro 64 bit"; | |
190 break; | |
191 case base::win::VERSION_VISTA: os_label += " Vista or Server 2008"; break; | |
192 case base::win::VERSION_WIN7: os_label += " 7 or Server 2008 R2"; break; | |
193 case base::win::VERSION_WIN8: os_label += " 8 or Server 2012"; break; | |
194 default: os_label += " UNKNOWN"; break; | |
195 } | |
196 os_label += " SP" + base::IntToString(os->service_pack().major); | |
197 if (os->service_pack().minor > 0) | |
198 os_label += "." + base::IntToString(os->service_pack().minor); | |
199 if (os->architecture() == base::win::OSInfo::X64_ARCHITECTURE) | |
200 os_label += " 64 bit"; | |
201 #endif | |
202 AddPair(list.get(), | |
203 l10n_util::GetStringUTF16(IDS_ABOUT_VERSION_OS), os_label); | |
204 | |
205 AddLineBreak(list.get()); | |
206 | |
207 // Obtain the version of the NaCl plugin. | |
208 std::vector<webkit::WebPluginInfo> info_array; | |
209 PluginService::GetInstance()->GetPluginInfoArray( | |
210 GURL(), "application/x-nacl", false, &info_array, NULL); | |
211 string16 nacl_version; | |
212 string16 nacl_key = ASCIIToUTF16("NaCl plugin"); | |
213 if (info_array.empty()) { | |
214 AddPair(list.get(), nacl_key, "Disabled"); | |
215 } else { | |
216 PluginPrefs* plugin_prefs = | |
217 PluginPrefs::GetForProfile(Profile::FromWebUI(web_ui())); | |
218 | |
219 // Only the 0th plugin is used. | |
220 nacl_version = info_array[0].version + ASCIIToUTF16(" ") + | |
221 info_array[0].path.LossyDisplayName(); | |
222 if (!plugin_prefs->IsPluginEnabled(info_array[0])) { | |
223 nacl_version += ASCIIToUTF16(" (Disabled in profile prefs)"); | |
224 AddPair(list.get(), nacl_key, nacl_version); | |
225 } | |
226 | |
227 AddPair(list.get(), nacl_key, nacl_version); | |
228 | |
229 // Mark the rest as not used. | |
230 for (size_t i = 1; i < info_array.size(); ++i) { | |
231 nacl_version = info_array[i].version + ASCIIToUTF16(" ") + | |
232 info_array[i].path.LossyDisplayName(); | |
233 nacl_version += ASCIIToUTF16(" (not used)"); | |
234 if (!plugin_prefs->IsPluginEnabled(info_array[i])) { | |
235 nacl_version += ASCIIToUTF16(" (Disabled in profile prefs)"); | |
James Hawkins
2012/08/11 19:21:07
Optional nit: Remove braces to save a line.
jvoung (off chromium)
2012/08/13 22:15:43
Done.
| |
236 } | |
237 AddPair(list.get(), nacl_key, nacl_version); | |
238 } | |
239 } | |
240 | |
241 // Check that commandline flags are enabled. | |
242 ListFlagStatus(list.get(), "Flag '--enable-nacl'", switches::kEnableNaCl); | |
243 | |
244 AddLineBreak(list.get()); | |
245 | |
246 // Obtain the version of the PNaCl translator. | |
247 FilePath pnacl_path; | |
248 bool got_path = PathService::Get(chrome::DIR_PNACL_COMPONENT, &pnacl_path); | |
249 if (!got_path || pnacl_path.empty()) { | |
250 AddPair(list.get(), "PNaCl translator", "Not installed"); | |
251 } else { | |
252 AddPair(list.get(), "PNaCl translator path: ", | |
253 pnacl_path.LossyDisplayName()); | |
254 AddPair(list.get(), "PNaCl translator version: ", | |
255 pnacl_path.BaseName().LossyDisplayName()); | |
256 } | |
257 | |
258 ListFlagStatus(list.get(), "Flag '--enable-pnacl'", switches::kEnablePnacl); | |
259 // naclInfo will take ownership of list, and clean it up on destruction. | |
260 naclInfo->Set("naclInfo", list.release()); | |
261 } | |
262 | |
263 void NaClDOMHandler::MaybeRespondToPage() { | |
264 // Don't reply until everything is ready. The page will show a 'loading' | |
265 // message until then. | |
266 if (!page_has_requested_data_ || !has_plugin_info_) | |
267 return; | |
268 | |
269 DictionaryValue naclInfo; | |
270 PopulatePageInformation(&naclInfo); | |
271 web_ui()->CallJavascriptFunction("nacl.returnNaClInfo", naclInfo); | |
272 } | |
273 | |
274 } // namespace | |
275 | |
276 /////////////////////////////////////////////////////////////////////////////// | |
277 // | |
278 // NaClUI | |
279 // | |
280 /////////////////////////////////////////////////////////////////////////////// | |
281 | |
282 NaClUI::NaClUI(content::WebUI* web_ui) : WebUIController(web_ui) { | |
283 content::RecordAction(UserMetricsAction("ViewAboutNaCl")); | |
284 | |
285 web_ui->AddMessageHandler(new NaClDOMHandler()); | |
286 | |
287 // Set up the about:nacl source. | |
288 Profile* profile = Profile::FromWebUI(web_ui); | |
289 ChromeURLDataManager::AddDataSource(profile, CreateNaClUIHTMLSource()); | |
290 } | |
OLD | NEW |