Don't show supervised user as "already on this device" while they're being imported.
[chromium-blink-merge.git] / chrome / browser / ui / webui / flash_ui.cc
blobda27ac1336e06b44fa76188ec8d6ab7b7e5eefed
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/chrome_version_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 "content/public/browser/gpu_data_manager.h"
31 #include "content/public/browser/gpu_data_manager_observer.h"
32 #include "content/public/browser/plugin_service.h"
33 #include "content/public/browser/user_metrics.h"
34 #include "content/public/browser/web_contents.h"
35 #include "content/public/browser/web_ui.h"
36 #include "content/public/browser/web_ui_data_source.h"
37 #include "content/public/browser/web_ui_message_handler.h"
38 #include "content/public/common/content_constants.h"
39 #include "content/public/common/webplugininfo.h"
40 #include "gpu/config/gpu_info.h"
41 #include "grit/browser_resources.h"
42 #include "ui/base/l10n/l10n_util.h"
44 #if defined(OS_WIN)
45 #include "base/win/windows_version.h"
46 #endif
48 using base::ASCIIToUTF16;
49 using base::UserMetricsAction;
50 using content::GpuDataManager;
51 using content::PluginService;
52 using content::WebContents;
53 using content::WebUIMessageHandler;
55 namespace {
57 const char kFlashPlugin[] = "Flash plugin";
59 content::WebUIDataSource* CreateFlashUIHTMLSource() {
60 content::WebUIDataSource* source =
61 content::WebUIDataSource::Create(chrome::kChromeUIFlashHost);
63 source->AddLocalizedString("loadingMessage", IDS_FLASH_LOADING_MESSAGE);
64 source->AddLocalizedString("flashLongTitle", IDS_FLASH_TITLE_MESSAGE);
65 source->SetJsonPath("strings.js");
66 source->AddResourcePath("about_flash.js", IDR_ABOUT_FLASH_JS);
67 source->SetDefaultResource(IDR_ABOUT_FLASH_HTML);
68 return source;
71 const int kTimeout = 8 * 1000; // 8 seconds.
73 ////////////////////////////////////////////////////////////////////////////////
75 // FlashDOMHandler
77 ////////////////////////////////////////////////////////////////////////////////
79 // The handler for JavaScript messages for the about:flags page.
80 class FlashDOMHandler : public WebUIMessageHandler,
81 public CrashUploadList::Delegate,
82 public content::GpuDataManagerObserver {
83 public:
84 FlashDOMHandler();
85 ~FlashDOMHandler() override;
87 // WebUIMessageHandler implementation.
88 void RegisterMessages() override;
90 // CrashUploadList::Delegate implementation.
91 void OnUploadListAvailable() override;
93 // GpuDataManager::Observer implementation.
94 void OnGpuInfoUpdate() override;
96 // Callback for the "requestFlashInfo" message.
97 void HandleRequestFlashInfo(const base::ListValue* args);
99 // Callback for the Flash plugin information.
100 void OnGotPlugins(const std::vector<content::WebPluginInfo>& plugins);
102 private:
103 // Called when we think we might have enough information to return data back
104 // to the page.
105 void MaybeRespondToPage();
107 // In certain cases we might not get called back from the GPU process so we
108 // set an upper limit on the time we wait. This function gets called when the
109 // time has passed. This actually doesn't prevent the rest of the information
110 // to appear later, the page will just reflow when more information becomes
111 // available.
112 void OnTimeout();
114 // A timer to keep track of when the data fetching times out.
115 base::OneShotTimer<FlashDOMHandler> timeout_;
117 // Crash list.
118 scoped_refptr<CrashUploadList> upload_list_;
120 // Whether the list of all crashes is available.
121 bool crash_list_available_;
122 // Whether the page has requested data.
123 bool page_has_requested_data_;
124 // Whether the GPU data has been collected.
125 bool has_gpu_info_;
126 // Whether the plugin information is ready.
127 bool has_plugin_info_;
129 base::WeakPtrFactory<FlashDOMHandler> weak_ptr_factory_;
131 DISALLOW_COPY_AND_ASSIGN(FlashDOMHandler);
134 FlashDOMHandler::FlashDOMHandler()
135 : crash_list_available_(false),
136 page_has_requested_data_(false),
137 has_gpu_info_(false),
138 has_plugin_info_(false),
139 weak_ptr_factory_(this) {
140 // Request Crash data asynchronously.
141 upload_list_ = CrashUploadList::Create(this);
142 upload_list_->LoadUploadListAsynchronously();
144 // Watch for changes in GPUInfo.
145 GpuDataManager::GetInstance()->AddObserver(this);
147 // Tell GpuDataManager it should have full GpuInfo. If the
148 // GPU process has not run yet, this will trigger its launch.
149 GpuDataManager::GetInstance()->RequestCompleteGpuInfoIfNeeded();
151 // GPU access might not be allowed at all, which will cause us not to get a
152 // call back.
153 if (!GpuDataManager::GetInstance()->GpuAccessAllowed(NULL))
154 OnGpuInfoUpdate();
156 PluginService::GetInstance()->GetPlugins(base::Bind(
157 &FlashDOMHandler::OnGotPlugins, weak_ptr_factory_.GetWeakPtr()));
159 // And lastly, we fire off a timer to make sure we never get stuck at the
160 // "Loading..." message.
161 timeout_.Start(FROM_HERE, base::TimeDelta::FromMilliseconds(kTimeout),
162 this, &FlashDOMHandler::OnTimeout);
165 FlashDOMHandler::~FlashDOMHandler() {
166 GpuDataManager::GetInstance()->RemoveObserver(this);
167 upload_list_->ClearDelegate();
170 void FlashDOMHandler::RegisterMessages() {
171 web_ui()->RegisterMessageCallback("requestFlashInfo",
172 base::Bind(&FlashDOMHandler::HandleRequestFlashInfo,
173 base::Unretained(this)));
176 void FlashDOMHandler::OnUploadListAvailable() {
177 crash_list_available_ = true;
178 MaybeRespondToPage();
181 void AddPair(base::ListValue* list,
182 const base::string16& key,
183 const base::string16& value) {
184 base::DictionaryValue* results = new base::DictionaryValue();
185 results->SetString("key", key);
186 results->SetString("value", value);
187 list->Append(results);
190 void AddPair(base::ListValue* list,
191 const base::string16& key,
192 const std::string& value) {
193 AddPair(list, key, ASCIIToUTF16(value));
196 void FlashDOMHandler::HandleRequestFlashInfo(const base::ListValue* args) {
197 page_has_requested_data_ = true;
198 MaybeRespondToPage();
201 void FlashDOMHandler::OnGpuInfoUpdate() {
202 has_gpu_info_ = true;
203 MaybeRespondToPage();
206 void FlashDOMHandler::OnGotPlugins(
207 const std::vector<content::WebPluginInfo>& plugins) {
208 has_plugin_info_ = true;
209 MaybeRespondToPage();
212 void FlashDOMHandler::OnTimeout() {
213 // We don't set page_has_requested_data_ because that is guaranteed to appear
214 // and we shouldn't be responding to the page before then.
215 has_gpu_info_ = true;
216 crash_list_available_ = true;
217 has_plugin_info_ = true;
218 MaybeRespondToPage();
221 void FlashDOMHandler::MaybeRespondToPage() {
222 // We don't reply until everything is ready. The page is showing a 'loading'
223 // message until then. If you add criteria to this list, please update the
224 // function OnTimeout() as well.
225 if (!page_has_requested_data_ || !crash_list_available_ || !has_gpu_info_ ||
226 !has_plugin_info_) {
227 return;
230 timeout_.Stop();
232 // This is code that runs only when the user types in about:flash. We don't
233 // need to jump through hoops to offload this to the IO thread.
234 base::ThreadRestrictions::ScopedAllowIO allow_io;
236 // Obtain the Chrome version info.
237 chrome::VersionInfo version_info;
239 base::ListValue* list = new base::ListValue();
241 // Chrome version information.
242 AddPair(list,
243 l10n_util::GetStringUTF16(IDS_PRODUCT_NAME),
244 version_info.Version() + " (" +
245 chrome::VersionInfo::GetVersionStringModifier() + ")");
247 // OS version information.
248 std::string os_label = version_info.OSType();
249 #if defined(OS_WIN)
250 base::win::OSInfo* os = base::win::OSInfo::GetInstance();
251 switch (os->version()) {
252 case base::win::VERSION_XP: os_label += " XP"; break;
253 case base::win::VERSION_SERVER_2003:
254 os_label += " Server 2003 or XP Pro 64 bit";
255 break;
256 case base::win::VERSION_VISTA: os_label += " Vista or Server 2008"; break;
257 case base::win::VERSION_WIN7: os_label += " 7 or Server 2008 R2"; break;
258 case base::win::VERSION_WIN8: os_label += " 8 or Server 2012"; break;
259 default: os_label += " UNKNOWN"; break;
261 os_label += " SP" + base::IntToString(os->service_pack().major);
262 if (os->service_pack().minor > 0)
263 os_label += "." + base::IntToString(os->service_pack().minor);
264 if (os->architecture() == base::win::OSInfo::X64_ARCHITECTURE)
265 os_label += " 64 bit";
266 #endif
267 AddPair(list, l10n_util::GetStringUTF16(IDS_ABOUT_VERSION_OS), os_label);
269 // Obtain the version of the Flash plugins.
270 std::vector<content::WebPluginInfo> info_array;
271 PluginService::GetInstance()->GetPluginInfoArray(
272 GURL(), content::kFlashPluginSwfMimeType, false, &info_array, NULL);
273 if (info_array.empty()) {
274 AddPair(list, ASCIIToUTF16(kFlashPlugin), "Not installed");
275 } else {
276 PluginPrefs* plugin_prefs =
277 PluginPrefs::GetForProfile(Profile::FromWebUI(web_ui())).get();
278 bool found_enabled = false;
279 for (size_t i = 0; i < info_array.size(); ++i) {
280 base::string16 flash_version = info_array[i].version + ASCIIToUTF16(" ") +
281 info_array[i].path.LossyDisplayName();
282 if (plugin_prefs->IsPluginEnabled(info_array[i])) {
283 // If we have already found an enabled Flash version, this one
284 // is not used.
285 if (found_enabled)
286 flash_version += ASCIIToUTF16(" (not used)");
288 found_enabled = true;
289 } else {
290 flash_version += ASCIIToUTF16(" (disabled)");
292 AddPair(list, ASCIIToUTF16(kFlashPlugin), flash_version);
296 // Crash information.
297 AddPair(list, base::string16(), "--- Crash data ---");
298 bool crash_reporting_enabled =
299 ChromeMetricsServiceAccessor::IsCrashReportingEnabled();
300 if (crash_reporting_enabled) {
301 std::vector<CrashUploadList::UploadInfo> crashes;
302 upload_list_->GetUploads(10, &crashes);
304 for (std::vector<CrashUploadList::UploadInfo>::iterator i = crashes.begin();
305 i != crashes.end(); ++i) {
306 base::string16 crash_string(ASCIIToUTF16(i->id));
307 crash_string += ASCIIToUTF16(" ");
308 crash_string += base::TimeFormatFriendlyDateAndTime(i->time);
309 AddPair(list, ASCIIToUTF16("crash id"), crash_string);
311 } else {
312 AddPair(list, ASCIIToUTF16("Crash Reporting"),
313 "Enable crash reporting to see crash IDs");
314 AddPair(list, ASCIIToUTF16("For more details"),
315 chrome::kLearnMoreReportingURL);
318 // GPU information.
319 AddPair(list, base::string16(), "--- GPU information ---");
320 gpu::GPUInfo gpu_info = GpuDataManager::GetInstance()->GetGPUInfo();
322 std::string reason;
323 if (!GpuDataManager::GetInstance()->GpuAccessAllowed(&reason)) {
324 AddPair(list, ASCIIToUTF16("WARNING:"),
325 "GPU access is not allowed: " + reason);
327 #if defined(OS_WIN)
328 const gpu::DxDiagNode& node = gpu_info.dx_diagnostics;
329 for (std::map<std::string, gpu::DxDiagNode>::const_iterator it =
330 node.children.begin();
331 it != node.children.end();
332 ++it) {
333 for (std::map<std::string, std::string>::const_iterator it2 =
334 it->second.values.begin();
335 it2 != it->second.values.end();
336 ++it2) {
337 if (!it2->second.empty()) {
338 if (it2->first == "szDescription") {
339 AddPair(list, ASCIIToUTF16("Graphics card"), it2->second);
340 } else if (it2->first == "szDriverNodeStrongName") {
341 AddPair(list, ASCIIToUTF16("Driver name (strong)"), it2->second);
342 } else if (it2->first == "szDriverName") {
343 AddPair(list, ASCIIToUTF16("Driver display name"), it2->second);
348 #endif
350 AddPair(list, base::string16(), "--- GPU driver, more information ---");
351 AddPair(list,
352 ASCIIToUTF16("Vendor Id"),
353 base::StringPrintf("0x%04x", gpu_info.gpu.vendor_id));
354 AddPair(list,
355 ASCIIToUTF16("Device Id"),
356 base::StringPrintf("0x%04x", gpu_info.gpu.device_id));
357 AddPair(list, ASCIIToUTF16("Driver vendor"), gpu_info.driver_vendor);
358 AddPair(list, ASCIIToUTF16("Driver version"), gpu_info.driver_version);
359 AddPair(list, ASCIIToUTF16("Driver date"), gpu_info.driver_date);
360 AddPair(list,
361 ASCIIToUTF16("Pixel shader version"),
362 gpu_info.pixel_shader_version);
363 AddPair(list,
364 ASCIIToUTF16("Vertex shader version"),
365 gpu_info.vertex_shader_version);
366 AddPair(list, ASCIIToUTF16("GL_VENDOR"), gpu_info.gl_vendor);
367 AddPair(list, ASCIIToUTF16("GL_RENDERER"), gpu_info.gl_renderer);
368 AddPair(list, ASCIIToUTF16("GL_VERSION"), gpu_info.gl_version);
369 AddPair(list, ASCIIToUTF16("GL_EXTENSIONS"), gpu_info.gl_extensions);
371 base::DictionaryValue flashInfo;
372 flashInfo.Set("flashInfo", list);
373 web_ui()->CallJavascriptFunction("returnFlashInfo", flashInfo);
376 } // namespace
378 ///////////////////////////////////////////////////////////////////////////////
380 // FlashUI
382 ///////////////////////////////////////////////////////////////////////////////
384 FlashUI::FlashUI(content::WebUI* web_ui) : WebUIController(web_ui) {
385 content::RecordAction(
386 UserMetricsAction("ViewAboutFlash"));
388 web_ui->AddMessageHandler(new FlashDOMHandler());
390 // Set up the about:flash source.
391 Profile* profile = Profile::FromWebUI(web_ui);
392 content::WebUIDataSource::Add(profile, CreateFlashUIHTMLSource());
395 // static
396 base::RefCountedMemory* FlashUI::GetFaviconResourceBytes(
397 ui::ScaleFactor scale_factor) {
398 // Use the default icon for now.
399 return NULL;