Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(8)

Side by Side Diff: chrome/browser/dom_ui/gpu_internals_ui.cc

Issue 5228004: Switch the about:gpu implementation from an about handler to dom_ui.... (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src/
Patch Set: '' Created 10 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « chrome/browser/dom_ui/gpu_internals_ui.h ('k') | chrome/browser/dom_ui/net_internals_ui.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Property Changes:
Added: svn:eol-style
+ LF
OLDNEW
(Empty)
1 // Copyright (c) 2010 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/dom_ui/gpu_internals_ui.h"
6
7 #include <algorithm>
8 #include <string>
9 #include <utility>
10 #include <vector>
11
12 #include "app/l10n_util.h"
13 #include "app/resource_bundle.h"
14 #include "base/command_line.h"
15 #include "base/file_util.h"
16 #include "base/message_loop.h"
17 #include "base/path_service.h"
18 #include "base/singleton.h"
19 #include "base/string_number_conversions.h"
20 #include "base/string_piece.h"
21 #include "base/utf_string_conversions.h"
22 #include "base/values.h"
23 #include "chrome/browser/browser_process.h"
24 #include "chrome/browser/browser_thread.h"
25 #include "chrome/browser/dom_ui/chrome_url_data_manager.h"
26 #include "chrome/browser/gpu_process_host.h"
27 #include "chrome/browser/gpu_process_host_ui_shim.h"
28 #include "chrome/browser/io_thread.h"
29 #include "chrome/browser/net/chrome_net_log.h"
30 #include "chrome/browser/net/connection_tester.h"
31 #include "chrome/browser/net/passive_log_collector.h"
32 #include "chrome/browser/net/url_fixer_upper.h"
33 #include "chrome/browser/platform_util.h"
34 #include "chrome/common/chrome_paths.h"
35 #include "chrome/common/chrome_version_info.h"
36 #include "chrome/common/jstemplate_builder.h"
37 #include "chrome/common/net/url_request_context_getter.h"
38 #include "chrome/common/url_constants.h"
39 #include "grit/browser_resources.h"
40 #include "grit/generated_resources.h"
41 #include "net/base/escape.h"
42
43
44 namespace {
45
46 class GpuHTMLSource : public ChromeURLDataManager::DataSource {
47 public:
48 GpuHTMLSource();
49
50 // Called when the network layer has requested a resource underneath
51 // the path we registered.
52 virtual void StartDataRequest(const std::string& path,
53 bool is_off_the_record,
54 int request_id);
55 virtual std::string GetMimeType(const std::string&) const;
56
57 private:
58 ~GpuHTMLSource() {}
59 DISALLOW_COPY_AND_ASSIGN(GpuHTMLSource);
60 };
61
62 // This class receives javascript messages from the renderer.
63 // Note that the DOMUI infrastructure runs on the UI thread, therefore all of
64 // this class's methods are expected to run on the UI thread.
65 class GpuMessageHandler
66 : public DOMMessageHandler,
67 public base::SupportsWeakPtr<GpuMessageHandler> {
68 public:
69 GpuMessageHandler();
70 virtual ~GpuMessageHandler();
71
72 // DOMMessageHandler implementation.
73 virtual DOMMessageHandler* Attach(DOMUI* dom_ui);
74 virtual void RegisterMessages();
75
76 // Mesages
77 void OnCallAsync(const ListValue* list);
78
79 // Submessages dispatched from OnCallAsync
80 Value* OnRequestGpuInfo(const ListValue* list);
81 Value* OnRequestClientInfo(const ListValue* list);
82
83 // Executes the javascript function |function_name| in the renderer, passing
84 // it the argument |value|.
85 void CallJavascriptFunction(const std::wstring& function_name,
86 const Value* value);
87
88 private:
89 DISALLOW_COPY_AND_ASSIGN(GpuMessageHandler);
90 };
91
92 ////////////////////////////////////////////////////////////////////////////////
93 //
94 // GpuHTMLSource
95 //
96 ////////////////////////////////////////////////////////////////////////////////
97
98 GpuHTMLSource::GpuHTMLSource()
99 : DataSource(chrome::kChromeUIGpuInternalsHost, MessageLoop::current()) {
100 }
101
102 void GpuHTMLSource::StartDataRequest(const std::string& path,
103 bool is_off_the_record,
104 int request_id) {
105 DictionaryValue localized_strings;
106 SetFontAndTextDirection(&localized_strings);
107
108 base::StringPiece gpu_html(
109 ResourceBundle::GetSharedInstance().GetRawDataResource(
110 IDR_GPU_INTERNALS_HTML));
111 std::string full_html(gpu_html.data(), gpu_html.size());
112 jstemplate_builder::AppendJsonHtml(&localized_strings, &full_html);
113 jstemplate_builder::AppendI18nTemplateSourceHtml(&full_html);
114 jstemplate_builder::AppendI18nTemplateProcessHtml(&full_html);
115 jstemplate_builder::AppendJsTemplateSourceHtml(&full_html);
116
117
118 scoped_refptr<RefCountedBytes> html_bytes(new RefCountedBytes);
119 html_bytes->data.resize(full_html.size());
120 std::copy(full_html.begin(), full_html.end(), html_bytes->data.begin());
121
122 SendResponse(request_id, html_bytes);
123 }
124
125 std::string GpuHTMLSource::GetMimeType(const std::string&) const {
126 return "text/html";
127 }
128
129 ////////////////////////////////////////////////////////////////////////////////
130 //
131 // GpuMessageHandler
132 //
133 ////////////////////////////////////////////////////////////////////////////////
134
135 GpuMessageHandler::GpuMessageHandler() {
136 }
137
138 GpuMessageHandler::~GpuMessageHandler() {}
139
140 DOMMessageHandler* GpuMessageHandler::Attach(DOMUI* dom_ui) {
141 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
142 DOMMessageHandler* result = DOMMessageHandler::Attach(dom_ui);
143 return result;
144 }
145
146 /* BrowserBridge.callAsync prepends a requestID to these messages. */
147 void GpuMessageHandler::RegisterMessages() {
148 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
149
150 dom_ui_->RegisterMessageCallback(
151 "callAsync",
152 NewCallback(this, &GpuMessageHandler::OnCallAsync));
153 }
154
155 void GpuMessageHandler::OnCallAsync(const ListValue* args) {
156 DCHECK_GE(args->GetSize(), static_cast<size_t>(2));
157 // unpack args into requestId, submessage and submessageArgs
158 bool ok;
159 Value* requestId;
160 ok = args->Get(0, &requestId);
161 DCHECK(ok);
162
163 std::string submessage;
164 ok = args->GetString(1, &submessage);
165 DCHECK(ok);
166
167 ListValue* submessageArgs = new ListValue();
168 for (size_t i = 2; i < args->GetSize(); ++i) {
169 Value* arg;
170 ok = args->Get(i, &arg);
171 DCHECK(ok);
172
173 Value* argCopy = arg->DeepCopy();
174 submessageArgs->Append(argCopy);
175 }
176
177 // call the submessage handler
178 Value* ret = NULL;
179 if (submessage == "requestGpuInfo") {
180 ret = OnRequestGpuInfo(submessageArgs);
181 } else if (submessage == "requestClientInfo") {
182 ret = OnRequestClientInfo(submessageArgs);
183 } else { // unrecognized submessage
184 NOTREACHED();
185 delete submessageArgs;
186 return;
187 }
188 delete submessageArgs;
189
190 // call BrowserBridge.onCallAsyncReply with result
191 if (ret) {
192 dom_ui_->CallJavascriptFunction(L"browserBridge.onCallAsyncReply",
193 *requestId,
194 *ret);
195 delete ret;
196 } else {
197 dom_ui_->CallJavascriptFunction(L"browserBridge.onCallAsyncReply",
198 *requestId);
199 }
200 }
201
202 Value* GpuMessageHandler::OnRequestClientInfo(const ListValue* list) {
203 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
204
205 DictionaryValue* dict = new DictionaryValue();
206
207 chrome::VersionInfo version_info;
208
209 if (!version_info.is_valid()) {
210 DLOG(ERROR) << "Unable to create chrome::VersionInfo";
211 } else {
212 // We have everything we need to send the right values.
213 dict->SetString("version", version_info.Version());
214 dict->SetString("cl", version_info.LastChange());
215 dict->SetString("version_mod",
216 platform_util::GetVersionStringModifier());
217 dict->SetString("official",
218 l10n_util::GetStringUTF16(
219 version_info.IsOfficialBuild() ?
220 IDS_ABOUT_VERSION_OFFICIAL
221 : IDS_ABOUT_VERSION_UNOFFICIAL));
222
223 dict->SetString("command_line",
224 CommandLine::ForCurrentProcess()->command_line_string());
225 }
226
227 return dict;
228 }
229
230 DictionaryValue* NewDescriptionValuePair(const std::string& desc,
231 const std::string& value) {
232 DictionaryValue* dict = new DictionaryValue();
233 dict->SetString("description", desc);
234 dict->SetString("value", value);
235 return dict;
236 }
237
238 DictionaryValue* NewDescriptionValuePair(const std::string& desc,
239 Value* value) {
240 DictionaryValue* dict = new DictionaryValue();
241 dict->SetString("description", desc);
242 dict->Set("value", value);
243 return dict;
244 }
245
246 #if defined(OS_WIN)
247 // Output DxDiagNode tree as nested array of {description,value} pairs
248 ListValue* DxDiagNodeToList(const DxDiagNode& node) {
249 ListValue* list = new ListValue();
250 for (std::map<std::string, std::string>::const_iterator it =
251 node.values.begin();
252 it != node.values.end();
253 ++it) {
254 list->Append(NewDescriptionValuePair(it->first, it->second));
255 }
256
257 for (std::map<std::string, DxDiagNode>::const_iterator it =
258 node.children.begin();
259 it != node.children.end();
260 ++it) {
261 ListValue* sublist = DxDiagNodeToList(it->second);
262 list->Append(NewDescriptionValuePair(it->first, sublist));
263 }
264 return list;
265 }
266
267 #endif // OS_WIN
268
269 std::string VersionNumberToString(uint32 value) {
270 int hi = (value >> 8) & 0xff;
271 int low = value & 0xff;
272 return base::IntToString(hi) + "." + base::IntToString(low);
273 }
274
275 DictionaryValue* GpuInfoToDict(const GPUInfo& gpu_info) {
276 ListValue* basic_info = new ListValue();
277 basic_info->Append(NewDescriptionValuePair("Initialization time",
278 base::Int64ToString(gpu_info.initialization_time().InMilliseconds())));
279 basic_info->Append(NewDescriptionValuePair("Vendor Id",
280 base::StringPrintf("0x%04x", gpu_info.vendor_id())));
281 basic_info->Append(NewDescriptionValuePair("Device Id",
282 base::StringPrintf("0x%04x", gpu_info.device_id())));
283 basic_info->Append(NewDescriptionValuePair("Driver version",
284 WideToASCII(gpu_info.driver_version()).c_str()));
285 basic_info->Append(NewDescriptionValuePair("Pixel shader version",
286 VersionNumberToString(gpu_info.pixel_shader_version())));
287 basic_info->Append(NewDescriptionValuePair("Vertex shader version",
288 VersionNumberToString(gpu_info.vertex_shader_version())));
289 basic_info->Append(NewDescriptionValuePair("GL version",
290 VersionNumberToString(gpu_info.gl_version())));
291
292 DictionaryValue* info = new DictionaryValue();
293 info->Set("basic_info", basic_info);
294
295 if (gpu_info.progress() == GPUInfo::kPartial) {
296 info->SetString("progress", "partial");
297 } else {
298 info->SetString("progress", "complete");
299 }
300 #if defined(OS_WIN)
301 if (gpu_info.progress() == GPUInfo::kComplete) {
302 ListValue* dx_info = DxDiagNodeToList(gpu_info.dx_diagnostics());
303 info->Set("diagnostics", dx_info);
304 }
305 #endif
306
307 return info;
308 }
309
310 Value* GpuMessageHandler::OnRequestGpuInfo(const ListValue* list) {
311 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
312
313 // Get GPU Info.
314 GPUInfo gpu_info = GpuProcessHostUIShim::Get()->gpu_info();
315
316 std::string html;
317 if (gpu_info.progress() != GPUInfo::kComplete) {
318 GpuProcessHostUIShim::Get()->CollectGraphicsInfoAsynchronously();
319 }
320
321 if (gpu_info.progress() != GPUInfo::kUninitialized) {
322 return GpuInfoToDict(gpu_info);
323 } else {
324 return NULL;
325 }
326 }
327
328 } // namespace
329
330
331 ////////////////////////////////////////////////////////////////////////////////
332 //
333 // GpuUI
334 //
335 ////////////////////////////////////////////////////////////////////////////////
336
337 GpuUI::GpuUI(TabContents* contents) : DOMUI(contents) {
338 AddMessageHandler((new GpuMessageHandler())->Attach(this));
339
340 GpuHTMLSource* html_source = new GpuHTMLSource();
341
342 // Set up the chrome://gpu/ source.
343 BrowserThread::PostTask(
344 BrowserThread::IO, FROM_HERE,
345 NewRunnableMethod(
346 Singleton<ChromeURLDataManager>::get(),
347 &ChromeURLDataManager::AddDataSource,
348 make_scoped_refptr(html_source)));
349 }
350
OLDNEW
« no previous file with comments | « chrome/browser/dom_ui/gpu_internals_ui.h ('k') | chrome/browser/dom_ui/net_internals_ui.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698