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> | |
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 // Factory for the creating refs in callbacks. | |
89 base::WeakPtrFactory<NaClDOMHandler> weak_ptr_factory_; | |
90 | |
91 // Whether the page has requested data. | |
92 bool page_has_requested_data_; | |
93 // Whether the plugin information is ready. | |
94 bool has_plugin_info_; | |
95 | |
96 DISALLOW_COPY_AND_ASSIGN(NaClDOMHandler); | |
97 }; | |
98 | |
99 NaClDOMHandler::NaClDOMHandler() | |
100 : weak_ptr_factory_(ALLOW_THIS_IN_INITIALIZER_LIST(this)), | |
101 page_has_requested_data_(false), | |
102 has_plugin_info_(false) { | |
103 PluginService::GetInstance()->GetPlugins(base::Bind( | |
104 &NaClDOMHandler::OnGotPlugins, weak_ptr_factory_.GetWeakPtr())); | |
105 } | |
106 | |
107 NaClDOMHandler::~NaClDOMHandler() { | |
108 } | |
109 | |
110 void NaClDOMHandler::RegisterMessages() { | |
111 web_ui()->RegisterMessageCallback("requestNaClInfo", | |
112 base::Bind(&NaClDOMHandler::HandleRequestNaClInfo, | |
113 base::Unretained(this))); | |
114 } | |
115 | |
116 void AddPair(ListValue* list, const string16& key, const string16& value) { | |
117 DictionaryValue* results = new DictionaryValue(); | |
118 results->SetString("key", key); | |
119 results->SetString("value", value); | |
120 list->Append(results); | |
121 } | |
122 | |
123 void AddPair(ListValue* list, const string16& key, const std::string& value) { | |
124 AddPair(list, key, ASCIIToUTF16(value)); | |
125 } | |
126 | |
127 void AddLineBreak(ListValue* list) { | |
128 AddPair(list, ASCIIToUTF16(""), ASCIIToUTF16("")); | |
129 } | |
130 | |
131 void ListFlagStatus(ListValue* list, const std::string& flag_label, | |
132 const std::string& flag_name) { | |
133 string16 flag_label_as_key = ASCIIToUTF16(flag_label); | |
134 if (CommandLine::ForCurrentProcess()->HasSwitch(flag_name)) { | |
135 AddPair(list, flag_label_as_key, "On"); | |
136 } else { | |
137 AddPair(list, flag_label_as_key, "Off"); | |
138 } | |
139 } | |
140 | |
141 void NaClDOMHandler::HandleRequestNaClInfo(const ListValue* args) { | |
142 page_has_requested_data_ = true; | |
143 MaybeRespondToPage(); | |
144 } | |
145 | |
146 void NaClDOMHandler::OnGotPlugins( | |
147 const std::vector<webkit::WebPluginInfo>& plugins) { | |
148 has_plugin_info_ = true; | |
149 MaybeRespondToPage(); | |
150 } | |
151 | |
152 void NaClDOMHandler::MaybeRespondToPage() { | |
153 // We don't reply until everything is ready. The page is showing a 'loading' | |
154 // message until then. | |
155 if (!page_has_requested_data_ || !has_plugin_info_) { | |
156 return; | |
157 } | |
158 | |
159 // Store Key-Value pairs of about-information. | |
Robert Muth (chromium)
2012/08/08 17:57:58
I think the code would be clearer if everything
wh
jvoung - send to chromium...
2012/08/08 19:03:11
Done -- made that separate function allocate list
| |
160 ListValue* list = new ListValue(); | |
Robert Muth (chromium)
2012/08/08 17:57:58
indicate whose responsibility it is to free "list"
jvoung - send to chromium...
2012/08/08 19:03:11
Done.
| |
161 | |
162 // Obtain the Chrome version info. | |
163 chrome::VersionInfo version_info; | |
164 AddPair(list, | |
165 l10n_util::GetStringUTF16(IDS_PRODUCT_NAME), | |
166 version_info.Version() + " (" + | |
167 chrome::VersionInfo::GetVersionStringModifier() + ")"); | |
168 | |
169 // OS version information. | |
170 // TODO(jvoung): refactor this to share the extra windows labeling | |
171 // with about:flash, or something. | |
172 std::string os_label = version_info.OSType(); | |
173 #if defined(OS_WIN) | |
174 base::win::OSInfo* os = base::win::OSInfo::GetInstance(); | |
175 switch (os->version()) { | |
176 case base::win::VERSION_XP: os_label += " XP"; break; | |
177 case base::win::VERSION_SERVER_2003: | |
178 os_label += " Server 2003 or XP Pro 64 bit"; | |
179 break; | |
180 case base::win::VERSION_VISTA: os_label += " Vista or Server 2008"; break; | |
181 case base::win::VERSION_WIN7: os_label += " 7 or Server 2008 R2"; break; | |
182 case base::win::VERSION_WIN8: os_label += " 8 or Server 2012"; break; | |
183 default: os_label += " UNKNOWN"; break; | |
184 } | |
185 os_label += " SP" + base::IntToString(os->service_pack().major); | |
186 if (os->service_pack().minor > 0) | |
187 os_label += "." + base::IntToString(os->service_pack().minor); | |
188 if (os->architecture() == base::win::OSInfo::X64_ARCHITECTURE) | |
189 os_label += " 64 bit"; | |
190 #endif | |
191 AddPair(list, l10n_util::GetStringUTF16(IDS_ABOUT_VERSION_OS), os_label); | |
192 | |
193 AddLineBreak(list); | |
194 | |
195 // Obtain the version of the NaCl plugin. | |
196 std::vector<webkit::WebPluginInfo> info_array; | |
197 PluginService::GetInstance()->GetPluginInfoArray( | |
198 GURL(), "application/x-nacl", false, &info_array, NULL); | |
199 string16 nacl_version; | |
200 if (info_array.empty()) { | |
201 AddPair(list, ASCIIToUTF16("NaCl plugin"), "Disabled"); | |
202 } else { | |
203 PluginPrefs* plugin_prefs = | |
204 PluginPrefs::GetForProfile(Profile::FromWebUI(web_ui())); | |
205 | |
206 // Only the 0th plugin is used. | |
207 nacl_version = info_array[0].version + ASCIIToUTF16(" ") + | |
208 info_array[0].path.LossyDisplayName(); | |
209 if (!plugin_prefs->IsPluginEnabled(info_array[0])) { | |
210 nacl_version += ASCIIToUTF16(" (Disabled in profile prefs)"); | |
211 AddPair(list, ASCIIToUTF16("NaCl plugin"), nacl_version); | |
212 } | |
213 | |
214 AddPair(list, ASCIIToUTF16("NaCl plugin"), nacl_version); | |
215 | |
216 // Mark the rest as not used. | |
217 for (size_t i = 1; i < info_array.size(); ++i) { | |
218 nacl_version = info_array[i].version + ASCIIToUTF16(" ") + | |
219 info_array[i].path.LossyDisplayName(); | |
220 nacl_version += ASCIIToUTF16(" (not used)"); | |
221 if (!plugin_prefs->IsPluginEnabled(info_array[i])) { | |
222 nacl_version += ASCIIToUTF16(" (Disabled in profile prefs)"); | |
223 } | |
224 AddPair(list, ASCIIToUTF16("NaCl plugin"), nacl_version); | |
225 } | |
226 } | |
227 | |
228 // Check that commandline flags are enabled. | |
229 ListFlagStatus(list, "Flag '--enable-nacl'", switches::kEnableNaCl); | |
230 | |
231 AddLineBreak(list); | |
232 | |
233 // Obtain the version of the PNaCl translator. | |
234 FilePath pnacl_path; | |
235 bool got_path = PathService::Get(chrome::DIR_PNACL_COMPONENT, &pnacl_path); | |
236 if (!got_path || pnacl_path.empty()) { | |
237 AddPair(list, ASCIIToUTF16("PNaCl translator"), "Not installed"); | |
238 } else { | |
239 AddPair(list, ASCIIToUTF16("PNaCl translator path: "), | |
240 pnacl_path.LossyDisplayName()); | |
241 AddPair(list, ASCIIToUTF16("PNaCl translator version: "), | |
242 pnacl_path.BaseName().LossyDisplayName()); | |
243 } | |
244 | |
245 ListFlagStatus(list, "Flag '--enable-pnacl'", switches::kEnablePnacl); | |
246 | |
247 DictionaryValue naclInfo; | |
248 naclInfo.Set("naclInfo", list); | |
249 web_ui()->CallJavascriptFunction("returnNaClInfo", naclInfo); | |
250 } | |
251 | |
252 } // namespace | |
253 | |
254 /////////////////////////////////////////////////////////////////////////////// | |
255 // | |
256 // NaClUI | |
257 // | |
258 /////////////////////////////////////////////////////////////////////////////// | |
259 | |
260 NaClUI::NaClUI(content::WebUI* web_ui) : WebUIController(web_ui) { | |
261 content::RecordAction(UserMetricsAction("ViewAboutNaCl")); | |
262 | |
263 web_ui->AddMessageHandler(new NaClDOMHandler()); | |
264 | |
265 // Set up the about:nacl source. | |
266 Profile* profile = Profile::FromWebUI(web_ui); | |
267 ChromeURLDataManager::AddDataSource(profile, CreateNaClUIHTMLSource()); | |
268 } | |
OLD | NEW |