- add sources.
[platform/framework/web/crosswalk.git] / src / chrome / browser / ui / webui / flash_ui.cc
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/flash_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/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"
45
46 #if defined(OS_WIN)
47 #include "base/win/windows_version.h"
48 #endif
49
50 using content::GpuDataManager;
51 using content::PluginService;
52 using content::UserMetricsAction;
53 using content::WebContents;
54 using content::WebUIMessageHandler;
55
56 namespace {
57
58 const char kFlashPlugin[] = "Flash plugin";
59
60 content::WebUIDataSource* CreateFlashUIHTMLSource() {
61   content::WebUIDataSource* source =
62       content::WebUIDataSource::Create(chrome::kChromeUIFlashHost);
63
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);
70   return source;
71 }
72
73 const int kTimeout = 8 * 1000;  // 8 seconds.
74
75 ////////////////////////////////////////////////////////////////////////////////
76 //
77 // FlashDOMHandler
78 //
79 ////////////////////////////////////////////////////////////////////////////////
80
81 // The handler for JavaScript messages for the about:flags page.
82 class FlashDOMHandler : public WebUIMessageHandler,
83                         public CrashUploadList::Delegate,
84                         public content::GpuDataManagerObserver {
85  public:
86   FlashDOMHandler();
87   virtual ~FlashDOMHandler();
88
89   // WebUIMessageHandler implementation.
90   virtual void RegisterMessages() OVERRIDE;
91
92   // CrashUploadList::Delegate implementation.
93   virtual void OnUploadListAvailable() OVERRIDE;
94
95   // GpuDataManager::Observer implementation.
96   virtual void OnGpuInfoUpdate() OVERRIDE;
97
98   // Callback for the "requestFlashInfo" message.
99   void HandleRequestFlashInfo(const ListValue* args);
100
101   // Callback for the Flash plugin information.
102   void OnGotPlugins(const std::vector<content::WebPluginInfo>& plugins);
103
104  private:
105   // Called when we think we might have enough information to return data back
106   // to the page.
107   void MaybeRespondToPage();
108
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
113   // available.
114   void OnTimeout();
115
116   // A timer to keep track of when the data fetching times out.
117   base::OneShotTimer<FlashDOMHandler> timeout_;
118
119   // Crash list.
120   scoped_refptr<CrashUploadList> upload_list_;
121
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.
127   bool has_gpu_info_;
128   // Whether the plugin information is ready.
129   bool has_plugin_info_;
130
131   base::WeakPtrFactory<FlashDOMHandler> weak_ptr_factory_;
132
133   DISALLOW_COPY_AND_ASSIGN(FlashDOMHandler);
134 };
135
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();
145
146   // Watch for changes in GPUInfo.
147   GpuDataManager::GetInstance()->AddObserver(this);
148
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();
152
153   // GPU access might not be allowed at all, which will cause us not to get a
154   // call back.
155   if (!GpuDataManager::GetInstance()->GpuAccessAllowed(NULL))
156     OnGpuInfoUpdate();
157
158   PluginService::GetInstance()->GetPlugins(base::Bind(
159       &FlashDOMHandler::OnGotPlugins, weak_ptr_factory_.GetWeakPtr()));
160
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);
165 }
166
167 FlashDOMHandler::~FlashDOMHandler() {
168   GpuDataManager::GetInstance()->RemoveObserver(this);
169   upload_list_->ClearDelegate();
170 }
171
172 void FlashDOMHandler::RegisterMessages() {
173   web_ui()->RegisterMessageCallback("requestFlashInfo",
174       base::Bind(&FlashDOMHandler::HandleRequestFlashInfo,
175                  base::Unretained(this)));
176 }
177
178 void FlashDOMHandler::OnUploadListAvailable() {
179   crash_list_available_ = true;
180   MaybeRespondToPage();
181 }
182
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);
188 }
189
190 void AddPair(ListValue* list, const string16& key, const std::string& value) {
191   AddPair(list, key, ASCIIToUTF16(value));
192 }
193
194 void FlashDOMHandler::HandleRequestFlashInfo(const ListValue* args) {
195   page_has_requested_data_ = true;
196   MaybeRespondToPage();
197 }
198
199 void FlashDOMHandler::OnGpuInfoUpdate() {
200   has_gpu_info_ = true;
201   MaybeRespondToPage();
202 }
203
204 void FlashDOMHandler::OnGotPlugins(
205     const std::vector<content::WebPluginInfo>& plugins) {
206   has_plugin_info_ = true;
207   MaybeRespondToPage();
208 }
209
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();
217 }
218
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_ ||
224       !has_plugin_info_) {
225     return;
226   }
227
228   timeout_.Stop();
229
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;
233
234   // Obtain the Chrome version info.
235   chrome::VersionInfo version_info;
236
237   ListValue* list = new ListValue();
238
239   // Chrome version information.
240   AddPair(list,
241           l10n_util::GetStringUTF16(IDS_PRODUCT_NAME),
242           version_info.Version() + " (" +
243           chrome::VersionInfo::GetVersionStringModifier() + ")");
244
245   // OS version information.
246   std::string os_label = version_info.OSType();
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;
258   }
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);
266
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       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)");
285
286         found_enabled = true;
287       } else {
288         flash_version += ASCIIToUTF16(" (disabled)");
289       }
290       AddPair(list, ASCIIToUTF16(kFlashPlugin), flash_version);
291     }
292   }
293
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);
300
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);
307     }
308   } else {
309     AddPair(list, ASCIIToUTF16("Crash Reporting"),
310                   "Enable crash reporting to see crash IDs");
311     AddPair(list, ASCIIToUTF16("For more details"),
312                   chrome::kLearnMoreReportingURL);
313   }
314
315   // GPU information.
316   AddPair(list, string16(), "--- GPU information ---");
317   gpu::GPUInfo gpu_info = GpuDataManager::GetInstance()->GetGPUInfo();
318
319   std::string reason;
320   if (!GpuDataManager::GetInstance()->GpuAccessAllowed(&reason)) {
321     AddPair(list, ASCIIToUTF16("WARNING:"),
322             "GPU access is not allowed: " + reason);
323   }
324 #if defined(OS_WIN)
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();
329        ++it) {
330     for (std::map<std::string, std::string>::const_iterator it2 =
331              it->second.values.begin();
332          it2 != it->second.values.end();
333          ++it2) {
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);
341         }
342       }
343     }
344   }
345 #endif
346
347   AddPair(list, string16(), "--- GPU driver, more information ---");
348   AddPair(list,
349           ASCIIToUTF16("Vendor Id"),
350           base::StringPrintf("0x%04x", gpu_info.gpu.vendor_id));
351   AddPair(list,
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);
357   AddPair(list,
358           ASCIIToUTF16("Pixel shader version"),
359           gpu_info.pixel_shader_version);
360   AddPair(list,
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);
368
369   DictionaryValue flashInfo;
370   flashInfo.Set("flashInfo", list);
371   web_ui()->CallJavascriptFunction("returnFlashInfo", flashInfo);
372 }
373
374 }  // namespace
375
376 ///////////////////////////////////////////////////////////////////////////////
377 //
378 // FlashUI
379 //
380 ///////////////////////////////////////////////////////////////////////////////
381
382 FlashUI::FlashUI(content::WebUI* web_ui) : WebUIController(web_ui) {
383   content::RecordAction(
384       UserMetricsAction("ViewAboutFlash"));
385
386   web_ui->AddMessageHandler(new FlashDOMHandler());
387
388   // Set up the about:flash source.
389   Profile* profile = Profile::FromWebUI(web_ui);
390   content::WebUIDataSource::Add(profile, CreateFlashUIHTMLSource());
391 }
392
393 // static
394 base::RefCountedMemory* FlashUI::GetFaviconResourceBytes(
395       ui::ScaleFactor scale_factor) {
396   // Use the default icon for now.
397   return NULL;
398 }