| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "content/browser/histogram_internals_request_job.h" | 5 #include "content/browser/histogram_internals_request_job.h" |
| 6 | 6 |
| 7 #include "base/metrics/histogram_macros.h" | 7 #include "base/metrics/histogram_macros.h" |
| 8 #include "base/metrics/statistics_recorder.h" | 8 #include "base/metrics/statistics_recorder.h" |
| 9 #include "content/browser/histogram_synchronizer.h" | 9 #include "content/browser/histogram_synchronizer.h" |
| 10 #include "content/public/browser/browser_thread.h" | 10 #include "content/public/browser/browser_thread.h" |
| 11 #include "net/base/escape.h" | 11 #include "net/base/escape.h" |
| 12 #include "net/base/net_errors.h" | 12 #include "net/base/net_errors.h" |
| 13 #include "net/url_request/url_request.h" | 13 #include "net/url_request/url_request.h" |
| 14 #include "url/gurl.h" | 14 #include "url/gurl.h" |
| 15 | 15 |
| 16 namespace content { | 16 namespace content { |
| 17 | 17 |
| 18 HistogramInternalsRequestJob::HistogramInternalsRequestJob( | 18 HistogramInternalsRequestJob::HistogramInternalsRequestJob( |
| 19 net::URLRequest* request, | 19 net::URLRequest* request, |
| 20 net::NetworkDelegate* network_delegate) | 20 net::NetworkDelegate* network_delegate) |
| 21 : net::URLRequestSimpleJob(request, network_delegate), weak_factory_(this) { | 21 : net::URLRequestSimpleJob(request, network_delegate), |
| 22 const std::string& spec = request->url().possibly_invalid_spec(); | 22 url_(request->url()), |
| 23 const url::Parsed& parsed = request->url().parsed_for_possibly_invalid_spec(); | 23 weak_factory_(this) {} |
| 24 |
| 25 HistogramInternalsRequestJob::~HistogramInternalsRequestJob() {} |
| 26 |
| 27 std::string HistogramInternalsRequestJob::GenerateHTML(const GURL& url) { |
| 28 const std::string& spec = url.possibly_invalid_spec(); |
| 29 const url::Parsed& parsed = url.parsed_for_possibly_invalid_spec(); |
| 24 // + 1 to skip the slash at the beginning of the path. | 30 // + 1 to skip the slash at the beginning of the path. |
| 25 int offset = parsed.CountCharactersBefore(url::Parsed::PATH, false) + 1; | 31 int offset = parsed.CountCharactersBefore(url::Parsed::PATH, false) + 1; |
| 26 | 32 |
| 33 std::string path; |
| 27 if (offset < static_cast<int>(spec.size())) | 34 if (offset < static_cast<int>(spec.size())) |
| 28 path_.assign(spec.substr(offset)); | 35 path = spec.substr(offset); |
| 29 } | |
| 30 | 36 |
| 31 HistogramInternalsRequestJob::~HistogramInternalsRequestJob() {} | |
| 32 | |
| 33 void AboutHistogram(std::string* data, const std::string& path) { | |
| 34 HistogramSynchronizer::FetchHistograms(); | 37 HistogramSynchronizer::FetchHistograms(); |
| 35 | 38 |
| 36 std::string unescaped_query; | 39 std::string unescaped_query; |
| 37 std::string unescaped_title("About Histograms"); | 40 std::string unescaped_title("About Histograms"); |
| 38 if (!path.empty()) { | 41 if (!path.empty()) { |
| 39 unescaped_query = net::UnescapeURLComponent(path, | 42 unescaped_query = net::UnescapeURLComponent(path, |
| 40 net::UnescapeRule::NORMAL); | 43 net::UnescapeRule::NORMAL); |
| 41 unescaped_title += " - " + unescaped_query; | 44 unescaped_title += " - " + unescaped_query; |
| 42 } | 45 } |
| 43 | 46 |
| 44 data->append("<!DOCTYPE html>\n<html>\n<head>\n"); | 47 std::string data; |
| 45 data->append( | 48 data.append("<!DOCTYPE html>\n<html>\n<head>\n"); |
| 49 data.append( |
| 46 "<meta http-equiv=\"Content-Security-Policy\" " | 50 "<meta http-equiv=\"Content-Security-Policy\" " |
| 47 "content=\"object-src 'none'; script-src 'none'\">"); | 51 "content=\"object-src 'none'; script-src 'none'\">"); |
| 48 data->append("<title>"); | 52 data.append("<title>"); |
| 49 data->append(net::EscapeForHTML(unescaped_title)); | 53 data.append(net::EscapeForHTML(unescaped_title)); |
| 50 data->append("</title>\n"); | 54 data.append("</title>\n"); |
| 51 data->append("</head><body>"); | 55 data.append("</head><body>"); |
| 52 | 56 |
| 53 // Display any stats for which we sent off requests the last time. | 57 // Display any stats for which we sent off requests the last time. |
| 54 data->append("<p>Stats accumulated from browser startup to previous "); | 58 data.append("<p>Stats accumulated from browser startup to previous "); |
| 55 data->append("page load; reload to get stats as of this page load.</p>\n"); | 59 data.append("page load; reload to get stats as of this page load.</p>\n"); |
| 56 data->append("<table width=\"100%\">\n"); | 60 data.append("<table width=\"100%\">\n"); |
| 57 | 61 |
| 58 base::StatisticsRecorder::WriteHTMLGraph(unescaped_query, data); | 62 base::StatisticsRecorder::WriteHTMLGraph(unescaped_query, &data); |
| 63 return data; |
| 59 } | 64 } |
| 60 | 65 |
| 61 void HistogramInternalsRequestJob::Start() { | 66 void HistogramInternalsRequestJob::Start() { |
| 62 // First import histograms from all providers and then start the URL fetch | 67 // First import histograms from all providers and then start the URL fetch |
| 63 // job. It's not possible to call URLRequestSimpleJob::Start through Bind, | 68 // job. It's not possible to call URLRequestSimpleJob::Start through Bind, |
| 64 // it ends up re-calling this method, so a small helper method is used. | 69 // it ends up re-calling this method, so a small helper method is used. |
| 65 content::BrowserThread::PostTaskAndReply( | 70 content::BrowserThread::PostTaskAndReply( |
| 66 content::BrowserThread::UI, FROM_HERE, | 71 content::BrowserThread::UI, FROM_HERE, |
| 67 base::Bind(&base::StatisticsRecorder::ImportProvidedHistograms), | 72 base::Bind(&base::StatisticsRecorder::ImportProvidedHistograms), |
| 68 base::Bind(&HistogramInternalsRequestJob::StartUrlRequest, | 73 base::Bind(&HistogramInternalsRequestJob::StartUrlRequest, |
| 69 weak_factory_.GetWeakPtr())); | 74 weak_factory_.GetWeakPtr())); |
| 70 } | 75 } |
| 71 | 76 |
| 72 int HistogramInternalsRequestJob::GetData( | 77 int HistogramInternalsRequestJob::GetData( |
| 73 std::string* mime_type, | 78 std::string* mime_type, |
| 74 std::string* charset, | 79 std::string* charset, |
| 75 std::string* data, | 80 std::string* data, |
| 76 const net::CompletionCallback& callback) const { | 81 const net::CompletionCallback& callback) const { |
| 77 mime_type->assign("text/html"); | 82 mime_type->assign("text/html"); |
| 78 charset->assign("UTF8"); | 83 charset->assign("UTF8"); |
| 79 | 84 |
| 80 data->clear(); | 85 *data = GenerateHTML(url_); |
| 81 AboutHistogram(data, path_); | |
| 82 return net::OK; | 86 return net::OK; |
| 83 } | 87 } |
| 84 | 88 |
| 85 void HistogramInternalsRequestJob::StartUrlRequest() { | 89 void HistogramInternalsRequestJob::StartUrlRequest() { |
| 86 URLRequestSimpleJob::Start(); | 90 URLRequestSimpleJob::Start(); |
| 87 } | 91 } |
| 88 | 92 |
| 89 } // namespace content | 93 } // namespace content |
| OLD | NEW |