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/flash_ui.h"
11 #include "base/bind.h"
12 #include "base/bind_helpers.h"
13 #include "base/i18n/time_formatting.h"
14 #include "base/memory/weak_ptr.h"
15 #include "base/strings/string16.h"
16 #include "base/strings/string_number_conversions.h"
17 #include "base/strings/stringprintf.h"
18 #include "base/strings/utf_string_conversions.h"
19 #include "base/threading/thread_restrictions.h"
20 #include "base/timer/timer.h"
21 #include "base/values.h"
22 #include "chrome/browser/crash_upload_list.h"
23 #include "chrome/browser/plugins/plugin_prefs.h"
24 #include "chrome/browser/profiles/profile.h"
25 #include "chrome/browser/ui/webui/crashes_ui.h"
26 #include "chrome/common/chrome_version_info.h"
27 #include "chrome/common/url_constants.h"
28 #include "content/public/browser/gpu_data_manager.h"
29 #include "content/public/browser/gpu_data_manager_observer.h"
30 #include "content/public/browser/plugin_service.h"
31 #include "content/public/browser/user_metrics.h"
32 #include "content/public/browser/web_contents.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/content_constants.h"
37 #include "content/public/common/webplugininfo.h"
38 #include "gpu/config/gpu_info.h"
39 #include "grit/browser_resources.h"
40 #include "grit/chromium_strings.h"
41 #include "grit/generated_resources.h"
42 #include "grit/theme_resources.h"
43 #include "ui/base/l10n/l10n_util.h"
44 #include "ui/base/resource/resource_bundle.h"
47 #include "base/win/windows_version.h"
50 using content::GpuDataManager;
51 using content::PluginService;
52 using content::UserMetricsAction;
53 using content::WebContents;
54 using content::WebUIMessageHandler;
58 const char kFlashPlugin[] = "Flash plugin";
60 content::WebUIDataSource* CreateFlashUIHTMLSource() {
61 content::WebUIDataSource* source =
62 content::WebUIDataSource::Create(chrome::kChromeUIFlashHost);
64 source->SetUseJsonJSFormatV2();
65 source->AddLocalizedString("loadingMessage", IDS_FLASH_LOADING_MESSAGE);
66 source->AddLocalizedString("flashLongTitle", IDS_FLASH_TITLE_MESSAGE);
67 source->SetJsonPath("strings.js");
68 source->AddResourcePath("about_flash.js", IDR_ABOUT_FLASH_JS);
69 source->SetDefaultResource(IDR_ABOUT_FLASH_HTML);
73 const int kTimeout = 8 * 1000; // 8 seconds.
75 ////////////////////////////////////////////////////////////////////////////////
79 ////////////////////////////////////////////////////////////////////////////////
81 // The handler for JavaScript messages for the about:flags page.
82 class FlashDOMHandler : public WebUIMessageHandler,
83 public CrashUploadList::Delegate,
84 public content::GpuDataManagerObserver {
87 virtual ~FlashDOMHandler();
89 // WebUIMessageHandler implementation.
90 virtual void RegisterMessages() OVERRIDE;
92 // CrashUploadList::Delegate implementation.
93 virtual void OnUploadListAvailable() OVERRIDE;
95 // GpuDataManager::Observer implementation.
96 virtual void OnGpuInfoUpdate() OVERRIDE;
98 // Callback for the "requestFlashInfo" message.
99 void HandleRequestFlashInfo(const ListValue* args);
101 // Callback for the Flash plugin information.
102 void OnGotPlugins(const std::vector<content::WebPluginInfo>& plugins);
105 // Called when we think we might have enough information to return data back
107 void MaybeRespondToPage();
109 // In certain cases we might not get called back from the GPU process so we
110 // set an upper limit on the time we wait. This function gets called when the
111 // time has passed. This actually doesn't prevent the rest of the information
112 // to appear later, the page will just reflow when more information becomes
116 // A timer to keep track of when the data fetching times out.
117 base::OneShotTimer<FlashDOMHandler> timeout_;
120 scoped_refptr<CrashUploadList> upload_list_;
122 // Whether the list of all crashes is available.
123 bool crash_list_available_;
124 // Whether the page has requested data.
125 bool page_has_requested_data_;
126 // Whether the GPU data has been collected.
128 // Whether the plugin information is ready.
129 bool has_plugin_info_;
131 base::WeakPtrFactory<FlashDOMHandler> weak_ptr_factory_;
133 DISALLOW_COPY_AND_ASSIGN(FlashDOMHandler);
136 FlashDOMHandler::FlashDOMHandler()
137 : crash_list_available_(false),
138 page_has_requested_data_(false),
139 has_gpu_info_(false),
140 has_plugin_info_(false),
141 weak_ptr_factory_(this) {
142 // Request Crash data asynchronously.
143 upload_list_ = CrashUploadList::Create(this);
144 upload_list_->LoadUploadListAsynchronously();
146 // Watch for changes in GPUInfo.
147 GpuDataManager::GetInstance()->AddObserver(this);
149 // Tell GpuDataManager it should have full GpuInfo. If the
150 // GPU process has not run yet, this will trigger its launch.
151 GpuDataManager::GetInstance()->RequestCompleteGpuInfoIfNeeded();
153 // GPU access might not be allowed at all, which will cause us not to get a
155 if (!GpuDataManager::GetInstance()->GpuAccessAllowed(NULL))
158 PluginService::GetInstance()->GetPlugins(base::Bind(
159 &FlashDOMHandler::OnGotPlugins, weak_ptr_factory_.GetWeakPtr()));
161 // And lastly, we fire off a timer to make sure we never get stuck at the
162 // "Loading..." message.
163 timeout_.Start(FROM_HERE, base::TimeDelta::FromMilliseconds(kTimeout),
164 this, &FlashDOMHandler::OnTimeout);
167 FlashDOMHandler::~FlashDOMHandler() {
168 GpuDataManager::GetInstance()->RemoveObserver(this);
169 upload_list_->ClearDelegate();
172 void FlashDOMHandler::RegisterMessages() {
173 web_ui()->RegisterMessageCallback("requestFlashInfo",
174 base::Bind(&FlashDOMHandler::HandleRequestFlashInfo,
175 base::Unretained(this)));
178 void FlashDOMHandler::OnUploadListAvailable() {
179 crash_list_available_ = true;
180 MaybeRespondToPage();
183 void AddPair(ListValue* list, const string16& key, const string16& value) {
184 DictionaryValue* results = new DictionaryValue();
185 results->SetString("key", key);
186 results->SetString("value", value);
187 list->Append(results);
190 void AddPair(ListValue* list, const string16& key, const std::string& value) {
191 AddPair(list, key, ASCIIToUTF16(value));
194 void FlashDOMHandler::HandleRequestFlashInfo(const ListValue* args) {
195 page_has_requested_data_ = true;
196 MaybeRespondToPage();
199 void FlashDOMHandler::OnGpuInfoUpdate() {
200 has_gpu_info_ = true;
201 MaybeRespondToPage();
204 void FlashDOMHandler::OnGotPlugins(
205 const std::vector<content::WebPluginInfo>& plugins) {
206 has_plugin_info_ = true;
207 MaybeRespondToPage();
210 void FlashDOMHandler::OnTimeout() {
211 // We don't set page_has_requested_data_ because that is guaranteed to appear
212 // and we shouldn't be responding to the page before then.
213 has_gpu_info_ = true;
214 crash_list_available_ = true;
215 has_plugin_info_ = true;
216 MaybeRespondToPage();
219 void FlashDOMHandler::MaybeRespondToPage() {
220 // We don't reply until everything is ready. The page is showing a 'loading'
221 // message until then. If you add criteria to this list, please update the
222 // function OnTimeout() as well.
223 if (!page_has_requested_data_ || !crash_list_available_ || !has_gpu_info_ ||
230 // This is code that runs only when the user types in about:flash. We don't
231 // need to jump through hoops to offload this to the IO thread.
232 base::ThreadRestrictions::ScopedAllowIO allow_io;
234 // Obtain the Chrome version info.
235 chrome::VersionInfo version_info;
237 ListValue* list = new ListValue();
239 // Chrome version information.
241 l10n_util::GetStringUTF16(IDS_PRODUCT_NAME),
242 version_info.Version() + " (" +
243 chrome::VersionInfo::GetVersionStringModifier() + ")");
245 // OS version information.
246 std::string os_label = version_info.OSType();
248 base::win::OSInfo* os = base::win::OSInfo::GetInstance();
249 switch (os->version()) {
250 case base::win::VERSION_XP: os_label += " XP"; break;
251 case base::win::VERSION_SERVER_2003:
252 os_label += " Server 2003 or XP Pro 64 bit";
254 case base::win::VERSION_VISTA: os_label += " Vista or Server 2008"; break;
255 case base::win::VERSION_WIN7: os_label += " 7 or Server 2008 R2"; break;
256 case base::win::VERSION_WIN8: os_label += " 8 or Server 2012"; break;
257 default: os_label += " UNKNOWN"; break;
259 os_label += " SP" + base::IntToString(os->service_pack().major);
260 if (os->service_pack().minor > 0)
261 os_label += "." + base::IntToString(os->service_pack().minor);
262 if (os->architecture() == base::win::OSInfo::X64_ARCHITECTURE)
263 os_label += " 64 bit";
265 AddPair(list, l10n_util::GetStringUTF16(IDS_ABOUT_VERSION_OS), os_label);
267 // Obtain the version of the Flash plugins.
268 std::vector<content::WebPluginInfo> info_array;
269 PluginService::GetInstance()->GetPluginInfoArray(
270 GURL(), content::kFlashPluginSwfMimeType, false, &info_array, NULL);
271 if (info_array.empty()) {
272 AddPair(list, ASCIIToUTF16(kFlashPlugin), "Not installed");
274 PluginPrefs* plugin_prefs =
275 PluginPrefs::GetForProfile(Profile::FromWebUI(web_ui())).get();
276 bool found_enabled = false;
277 for (size_t i = 0; i < info_array.size(); ++i) {
278 string16 flash_version = info_array[i].version + ASCIIToUTF16(" ") +
279 info_array[i].path.LossyDisplayName();
280 if (plugin_prefs->IsPluginEnabled(info_array[i])) {
281 // If we have already found an enabled Flash version, this one
284 flash_version += ASCIIToUTF16(" (not used)");
286 found_enabled = true;
288 flash_version += ASCIIToUTF16(" (disabled)");
290 AddPair(list, ASCIIToUTF16(kFlashPlugin), flash_version);
294 // Crash information.
295 AddPair(list, string16(), "--- Crash data ---");
296 bool crash_reporting_enabled = CrashesUI::CrashReportingUIEnabled();
297 if (crash_reporting_enabled) {
298 std::vector<CrashUploadList::UploadInfo> crashes;
299 upload_list_->GetUploads(10, &crashes);
301 for (std::vector<CrashUploadList::UploadInfo>::iterator i = crashes.begin();
302 i != crashes.end(); ++i) {
303 string16 crash_string(ASCIIToUTF16(i->id));
304 crash_string += ASCIIToUTF16(" ");
305 crash_string += base::TimeFormatFriendlyDateAndTime(i->time);
306 AddPair(list, ASCIIToUTF16("crash id"), crash_string);
309 AddPair(list, ASCIIToUTF16("Crash Reporting"),
310 "Enable crash reporting to see crash IDs");
311 AddPair(list, ASCIIToUTF16("For more details"),
312 chrome::kLearnMoreReportingURL);
316 AddPair(list, string16(), "--- GPU information ---");
317 gpu::GPUInfo gpu_info = GpuDataManager::GetInstance()->GetGPUInfo();
320 if (!GpuDataManager::GetInstance()->GpuAccessAllowed(&reason)) {
321 AddPair(list, ASCIIToUTF16("WARNING:"),
322 "GPU access is not allowed: " + reason);
325 const gpu::DxDiagNode& node = gpu_info.dx_diagnostics;
326 for (std::map<std::string, gpu::DxDiagNode>::const_iterator it =
327 node.children.begin();
328 it != node.children.end();
330 for (std::map<std::string, std::string>::const_iterator it2 =
331 it->second.values.begin();
332 it2 != it->second.values.end();
334 if (!it2->second.empty()) {
335 if (it2->first == "szDescription") {
336 AddPair(list, ASCIIToUTF16("Graphics card"), it2->second);
337 } else if (it2->first == "szDriverNodeStrongName") {
338 AddPair(list, ASCIIToUTF16("Driver name (strong)"), it2->second);
339 } else if (it2->first == "szDriverName") {
340 AddPair(list, ASCIIToUTF16("Driver display name"), it2->second);
347 AddPair(list, string16(), "--- GPU driver, more information ---");
349 ASCIIToUTF16("Vendor Id"),
350 base::StringPrintf("0x%04x", gpu_info.gpu.vendor_id));
352 ASCIIToUTF16("Device Id"),
353 base::StringPrintf("0x%04x", gpu_info.gpu.device_id));
354 AddPair(list, ASCIIToUTF16("Driver vendor"), gpu_info.driver_vendor);
355 AddPair(list, ASCIIToUTF16("Driver version"), gpu_info.driver_version);
356 AddPair(list, ASCIIToUTF16("Driver date"), gpu_info.driver_date);
358 ASCIIToUTF16("Pixel shader version"),
359 gpu_info.pixel_shader_version);
361 ASCIIToUTF16("Vertex shader version"),
362 gpu_info.vertex_shader_version);
363 AddPair(list, ASCIIToUTF16("GL version"), gpu_info.gl_version);
364 AddPair(list, ASCIIToUTF16("GL_VENDOR"), gpu_info.gl_vendor);
365 AddPair(list, ASCIIToUTF16("GL_RENDERER"), gpu_info.gl_renderer);
366 AddPair(list, ASCIIToUTF16("GL_VERSION"), gpu_info.gl_version_string);
367 AddPair(list, ASCIIToUTF16("GL_EXTENSIONS"), gpu_info.gl_extensions);
369 DictionaryValue flashInfo;
370 flashInfo.Set("flashInfo", list);
371 web_ui()->CallJavascriptFunction("returnFlashInfo", flashInfo);
376 ///////////////////////////////////////////////////////////////////////////////
380 ///////////////////////////////////////////////////////////////////////////////
382 FlashUI::FlashUI(content::WebUI* web_ui) : WebUIController(web_ui) {
383 content::RecordAction(
384 UserMetricsAction("ViewAboutFlash"));
386 web_ui->AddMessageHandler(new FlashDOMHandler());
388 // Set up the about:flash source.
389 Profile* profile = Profile::FromWebUI(web_ui);
390 content::WebUIDataSource::Add(profile, CreateFlashUIHTMLSource());
394 base::RefCountedMemory* FlashUI::GetFaviconResourceBytes(
395 ui::ScaleFactor scale_factor) {
396 // Use the default icon for now.