Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / chrome / browser / ui / webui / flash_ui.cc
blobdf176c90db6d7a4df844728f6012dd35f942c39b
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"
7 #include <map>
8 #include <string>
9 #include <vector>
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/metrics/chrome_metrics_service_accessor.h"
24 #include "chrome/browser/plugins/plugin_prefs.h"
25 #include "chrome/browser/profiles/profile.h"
26 #include "chrome/common/channel_info.h"
27 #include "chrome/common/url_constants.h"
28 #include "chrome/grit/chromium_strings.h"
29 #include "chrome/grit/generated_resources.h"
30 #include "components/version_info/version_info.h"
31 #include "content/public/browser/gpu_data_manager.h"
32 #include "content/public/browser/gpu_data_manager_observer.h"
33 #include "content/public/browser/plugin_service.h"
34 #include "content/public/browser/user_metrics.h"
35 #include "content/public/browser/web_contents.h"
36 #include "content/public/browser/web_ui.h"
37 #include "content/public/browser/web_ui_data_source.h"
38 #include "content/public/browser/web_ui_message_handler.h"
39 #include "content/public/common/content_constants.h"
40 #include "content/public/common/webplugininfo.h"
41 #include "gpu/config/gpu_info.h"
42 #include "grit/browser_resources.h"
43 #include "ui/base/l10n/l10n_util.h"
45 #if defined(OS_WIN)
46 #include "base/win/windows_version.h"
47 #endif
49 using base::ASCIIToUTF16;
50 using base::UserMetricsAction;
51 using content::GpuDataManager;
52 using content::PluginService;
53 using content::WebContents;
54 using content::WebUIMessageHandler;
56 namespace {
58 const char kFlashPlugin[] = "Flash plugin";
60 content::WebUIDataSource* CreateFlashUIHTMLSource() {
61 content::WebUIDataSource* source =
62 content::WebUIDataSource::Create(chrome::kChromeUIFlashHost);
64 source->AddLocalizedString("loadingMessage", IDS_FLASH_LOADING_MESSAGE);
65 source->AddLocalizedString("flashLongTitle", IDS_FLASH_TITLE_MESSAGE);
66 source->SetJsonPath("strings.js");
67 source->AddResourcePath("about_flash.js", IDR_ABOUT_FLASH_JS);
68 source->SetDefaultResource(IDR_ABOUT_FLASH_HTML);
69 return source;
72 const int kTimeout = 8 * 1000; // 8 seconds.
74 ////////////////////////////////////////////////////////////////////////////////
76 // FlashDOMHandler
78 ////////////////////////////////////////////////////////////////////////////////
80 // The handler for JavaScript messages for the about:flags page.
81 class FlashDOMHandler : public WebUIMessageHandler,
82 public CrashUploadList::Delegate,
83 public content::GpuDataManagerObserver {
84 public:
85 FlashDOMHandler();
86 ~FlashDOMHandler() override;
88 // WebUIMessageHandler implementation.
89 void RegisterMessages() override;
91 // CrashUploadList::Delegate implementation.
92 void OnUploadListAvailable() override;
94 // GpuDataManager::Observer implementation.
95 void OnGpuInfoUpdate() override;
97 // Callback for the "requestFlashInfo" message.
98 void HandleRequestFlashInfo(const base::ListValue* args);
100 // Callback for the Flash plugin information.
101 void OnGotPlugins(const std::vector<content::WebPluginInfo>& plugins);
103 private:
104 // Called when we think we might have enough information to return data back
105 // to the page.
106 void MaybeRespondToPage();
108 // In certain cases we might not get called back from the GPU process so we
109 // set an upper limit on the time we wait. This function gets called when the
110 // time has passed. This actually doesn't prevent the rest of the information
111 // to appear later, the page will just reflow when more information becomes
112 // available.
113 void OnTimeout();
115 // A timer to keep track of when the data fetching times out.
116 base::OneShotTimer<FlashDOMHandler> timeout_;
118 // Crash list.
119 scoped_refptr<CrashUploadList> upload_list_;
121 // Whether the list of all crashes is available.
122 bool crash_list_available_;
123 // Whether the page has requested data.
124 bool page_has_requested_data_;
125 // Whether the GPU data has been collected.
126 bool has_gpu_info_;
127 // Whether the plugin information is ready.
128 bool has_plugin_info_;
130 base::WeakPtrFactory<FlashDOMHandler> weak_ptr_factory_;
132 DISALLOW_COPY_AND_ASSIGN(FlashDOMHandler);
135 FlashDOMHandler::FlashDOMHandler()
136 : crash_list_available_(false),
137 page_has_requested_data_(false),
138 has_gpu_info_(false),
139 has_plugin_info_(false),
140 weak_ptr_factory_(this) {
141 // Request Crash data asynchronously.
142 upload_list_ = CreateCrashUploadList(this);
143 upload_list_->LoadUploadListAsynchronously();
145 // Watch for changes in GPUInfo.
146 GpuDataManager::GetInstance()->AddObserver(this);
148 // Tell GpuDataManager it should have full GpuInfo. If the
149 // GPU process has not run yet, this will trigger its launch.
150 GpuDataManager::GetInstance()->RequestCompleteGpuInfoIfNeeded();
152 // GPU access might not be allowed at all, which will cause us not to get a
153 // call back.
154 if (!GpuDataManager::GetInstance()->GpuAccessAllowed(NULL))
155 OnGpuInfoUpdate();
157 PluginService::GetInstance()->GetPlugins(base::Bind(
158 &FlashDOMHandler::OnGotPlugins, weak_ptr_factory_.GetWeakPtr()));
160 // And lastly, we fire off a timer to make sure we never get stuck at the
161 // "Loading..." message.
162 timeout_.Start(FROM_HERE, base::TimeDelta::FromMilliseconds(kTimeout),
163 this, &FlashDOMHandler::OnTimeout);
166 FlashDOMHandler::~FlashDOMHandler() {
167 GpuDataManager::GetInstance()->RemoveObserver(this);
168 upload_list_->ClearDelegate();
171 void FlashDOMHandler::RegisterMessages() {
172 web_ui()->RegisterMessageCallback("requestFlashInfo",
173 base::Bind(&FlashDOMHandler::HandleRequestFlashInfo,
174 base::Unretained(this)));
177 void FlashDOMHandler::OnUploadListAvailable() {
178 crash_list_available_ = true;
179 MaybeRespondToPage();
182 void AddPair(base::ListValue* list,
183 const base::string16& key,
184 const base::string16& value) {
185 base::DictionaryValue* results = new base::DictionaryValue();
186 results->SetString("key", key);
187 results->SetString("value", value);
188 list->Append(results);
191 void AddPair(base::ListValue* list,
192 const base::string16& key,
193 const std::string& value) {
194 AddPair(list, key, ASCIIToUTF16(value));
197 void FlashDOMHandler::HandleRequestFlashInfo(const base::ListValue* args) {
198 page_has_requested_data_ = true;
199 MaybeRespondToPage();
202 void FlashDOMHandler::OnGpuInfoUpdate() {
203 has_gpu_info_ = true;
204 MaybeRespondToPage();
207 void FlashDOMHandler::OnGotPlugins(
208 const std::vector<content::WebPluginInfo>& plugins) {
209 has_plugin_info_ = true;
210 MaybeRespondToPage();
213 void FlashDOMHandler::OnTimeout() {
214 // We don't set page_has_requested_data_ because that is guaranteed to appear
215 // and we shouldn't be responding to the page before then.
216 has_gpu_info_ = true;
217 crash_list_available_ = true;
218 has_plugin_info_ = true;
219 MaybeRespondToPage();
222 void FlashDOMHandler::MaybeRespondToPage() {
223 // We don't reply until everything is ready. The page is showing a 'loading'
224 // message until then. If you add criteria to this list, please update the
225 // function OnTimeout() as well.
226 if (!page_has_requested_data_ || !crash_list_available_ || !has_gpu_info_ ||
227 !has_plugin_info_) {
228 return;
231 timeout_.Stop();
233 // This is code that runs only when the user types in about:flash. We don't
234 // need to jump through hoops to offload this to the IO thread.
235 base::ThreadRestrictions::ScopedAllowIO allow_io;
237 base::ListValue* list = new base::ListValue();
239 // Chrome version information.
240 AddPair(list,
241 l10n_util::GetStringUTF16(IDS_PRODUCT_NAME),
242 version_info::GetVersionNumber() + " (" +
243 chrome::GetChannelString() + ")");
245 // OS version information.
246 std::string os_label = version_info::GetOSType();
247 #if defined(OS_WIN)
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";
253 break;
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";
264 #endif
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");
273 } else {
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 base::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
282 // is not used.
283 if (found_enabled)
284 flash_version += ASCIIToUTF16(" (not used)");
286 found_enabled = true;
287 } else {
288 flash_version += ASCIIToUTF16(" (disabled)");
290 AddPair(list, ASCIIToUTF16(kFlashPlugin), flash_version);
294 // Crash information.
295 AddPair(list, base::string16(), "--- Crash data ---");
296 bool crash_reporting_enabled =
297 ChromeMetricsServiceAccessor::IsMetricsAndCrashReportingEnabled();
298 if (crash_reporting_enabled) {
299 std::vector<CrashUploadList::UploadInfo> crashes;
300 upload_list_->GetUploads(10, &crashes);
302 for (std::vector<CrashUploadList::UploadInfo>::iterator i = crashes.begin();
303 i != crashes.end(); ++i) {
304 base::string16 crash_string(ASCIIToUTF16(i->id));
305 crash_string += ASCIIToUTF16(" ");
306 crash_string += base::TimeFormatFriendlyDateAndTime(i->time);
307 AddPair(list, ASCIIToUTF16("crash id"), crash_string);
309 } else {
310 AddPair(list, ASCIIToUTF16("Crash Reporting"),
311 "Enable crash reporting to see crash IDs");
312 AddPair(list, ASCIIToUTF16("For more details"),
313 chrome::kLearnMoreReportingURL);
316 // GPU information.
317 AddPair(list, base::string16(), "--- GPU information ---");
318 gpu::GPUInfo gpu_info = GpuDataManager::GetInstance()->GetGPUInfo();
320 std::string reason;
321 if (!GpuDataManager::GetInstance()->GpuAccessAllowed(&reason)) {
322 AddPair(list, ASCIIToUTF16("WARNING:"),
323 "GPU access is not allowed: " + reason);
325 #if defined(OS_WIN)
326 const gpu::DxDiagNode& node = gpu_info.dx_diagnostics;
327 for (std::map<std::string, gpu::DxDiagNode>::const_iterator it =
328 node.children.begin();
329 it != node.children.end();
330 ++it) {
331 for (std::map<std::string, std::string>::const_iterator it2 =
332 it->second.values.begin();
333 it2 != it->second.values.end();
334 ++it2) {
335 if (!it2->second.empty()) {
336 if (it2->first == "szDescription") {
337 AddPair(list, ASCIIToUTF16("Graphics card"), it2->second);
338 } else if (it2->first == "szDriverNodeStrongName") {
339 AddPair(list, ASCIIToUTF16("Driver name (strong)"), it2->second);
340 } else if (it2->first == "szDriverName") {
341 AddPair(list, ASCIIToUTF16("Driver display name"), it2->second);
346 #endif
348 AddPair(list, base::string16(), "--- GPU driver, more information ---");
349 AddPair(list,
350 ASCIIToUTF16("Vendor Id"),
351 base::StringPrintf("0x%04x", gpu_info.gpu.vendor_id));
352 AddPair(list,
353 ASCIIToUTF16("Device Id"),
354 base::StringPrintf("0x%04x", gpu_info.gpu.device_id));
355 AddPair(list, ASCIIToUTF16("Driver vendor"), gpu_info.driver_vendor);
356 AddPair(list, ASCIIToUTF16("Driver version"), gpu_info.driver_version);
357 AddPair(list, ASCIIToUTF16("Driver date"), gpu_info.driver_date);
358 AddPair(list,
359 ASCIIToUTF16("Pixel shader version"),
360 gpu_info.pixel_shader_version);
361 AddPair(list,
362 ASCIIToUTF16("Vertex shader version"),
363 gpu_info.vertex_shader_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);
367 AddPair(list, ASCIIToUTF16("GL_EXTENSIONS"), gpu_info.gl_extensions);
369 base::DictionaryValue flashInfo;
370 flashInfo.Set("flashInfo", list);
371 web_ui()->CallJavascriptFunction("returnFlashInfo", flashInfo);
374 } // namespace
376 ///////////////////////////////////////////////////////////////////////////////
378 // FlashUI
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());
393 // static
394 base::RefCountedMemory* FlashUI::GetFaviconResourceBytes(
395 ui::ScaleFactor scale_factor) {
396 // Use the default icon for now.
397 return NULL;