[email protected] | 83ab4a28 | 2012-07-12 18:19:45 | [diff] [blame] | 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 "content/browser/histogram_internals_request_job.h" |
| 6 | |
| 7 | #include "base/metrics/histogram.h" |
[email protected] | 567d30e | 2012-07-13 21:48:29 | [diff] [blame] | 8 | #include "base/metrics/statistics_recorder.h" |
[email protected] | 83ab4a28 | 2012-07-12 18:19:45 | [diff] [blame] | 9 | #include "content/browser/histogram_synchronizer.h" |
[email protected] | 83ab4a28 | 2012-07-12 18:19:45 | [diff] [blame] | 10 | #include "net/base/escape.h" |
[email protected] | 36ffc734 | 2012-07-23 17:15:10 | [diff] [blame] | 11 | #include "net/base/net_errors.h" |
[email protected] | 83ab4a28 | 2012-07-12 18:19:45 | [diff] [blame] | 12 | #include "net/url_request/url_request.h" |
[email protected] | 707e1c4 | 2013-07-09 21:18:58 | [diff] [blame] | 13 | #include "url/gurl.h" |
[email protected] | 83ab4a28 | 2012-07-12 18:19:45 | [diff] [blame] | 14 | |
| 15 | namespace content { |
| 16 | |
| 17 | HistogramInternalsRequestJob::HistogramInternalsRequestJob( |
[email protected] | 9f17046 | 2012-08-24 01:06:58 | [diff] [blame] | 18 | net::URLRequest* request, net::NetworkDelegate* network_delegate) |
| 19 | : net::URLRequestSimpleJob(request, network_delegate) { |
[email protected] | 83ab4a28 | 2012-07-12 18:19:45 | [diff] [blame] | 20 | const std::string& spec = request->url().possibly_invalid_spec(); |
[email protected] | ea0c7a8 | 2014-05-01 06:12:27 | [diff] [blame] | 21 | const url::Parsed& parsed = request->url().parsed_for_possibly_invalid_spec(); |
[email protected] | 83ab4a28 | 2012-07-12 18:19:45 | [diff] [blame] | 22 | // + 1 to skip the slash at the beginning of the path. |
[email protected] | ea0c7a8 | 2014-05-01 06:12:27 | [diff] [blame] | 23 | int offset = parsed.CountCharactersBefore(url::Parsed::PATH, false) + 1; |
[email protected] | 83ab4a28 | 2012-07-12 18:19:45 | [diff] [blame] | 24 | |
| 25 | if (offset < static_cast<int>(spec.size())) |
| 26 | path_.assign(spec.substr(offset)); |
| 27 | } |
| 28 | |
| 29 | void AboutHistogram(std::string* data, const std::string& path) { |
[email protected] | 4648832 | 2012-10-30 03:22:20 | [diff] [blame] | 30 | HistogramSynchronizer::FetchHistograms(); |
[email protected] | 83ab4a28 | 2012-07-12 18:19:45 | [diff] [blame] | 31 | |
| 32 | std::string unescaped_query; |
| 33 | std::string unescaped_title("About Histograms"); |
| 34 | if (!path.empty()) { |
| 35 | unescaped_query = net::UnescapeURLComponent(path, |
| 36 | net::UnescapeRule::NORMAL); |
| 37 | unescaped_title += " - " + unescaped_query; |
| 38 | } |
| 39 | |
| 40 | data->append("<!DOCTYPE html>\n<html>\n<head>\n"); |
| 41 | data->append( |
[email protected] | 4db7287 | 2012-11-07 15:34:50 | [diff] [blame] | 42 | "<meta http-equiv=\"Content-Security-Policy\" " |
[email protected] | 6ed944a | 2013-05-31 22:41:58 | [diff] [blame] | 43 | "content=\"object-src 'none'; script-src 'none'\">"); |
[email protected] | 83ab4a28 | 2012-07-12 18:19:45 | [diff] [blame] | 44 | data->append("<title>"); |
| 45 | data->append(net::EscapeForHTML(unescaped_title)); |
| 46 | data->append("</title>\n"); |
| 47 | data->append("</head><body>"); |
| 48 | |
| 49 | // Display any stats for which we sent off requests the last time. |
[email protected] | f2175b4 | 2014-07-30 07:49:35 | [diff] [blame] | 50 | data->append("<p>Stats accumulated from browser startup to previous "); |
| 51 | data->append("page load; reload to get stats as of this page load.</p>\n"); |
[email protected] | 83ab4a28 | 2012-07-12 18:19:45 | [diff] [blame] | 52 | data->append("<table width=\"100%\">\n"); |
| 53 | |
| 54 | base::StatisticsRecorder::WriteHTMLGraph(unescaped_query, data); |
| 55 | } |
| 56 | |
[email protected] | 36ffc734 | 2012-07-23 17:15:10 | [diff] [blame] | 57 | int HistogramInternalsRequestJob::GetData( |
| 58 | std::string* mime_type, |
| 59 | std::string* charset, |
| 60 | std::string* data, |
| 61 | const net::CompletionCallback& callback) const { |
[email protected] | 83ab4a28 | 2012-07-12 18:19:45 | [diff] [blame] | 62 | mime_type->assign("text/html"); |
| 63 | charset->assign("UTF8"); |
| 64 | |
| 65 | data->clear(); |
| 66 | AboutHistogram(data, path_); |
[email protected] | 36ffc734 | 2012-07-23 17:15:10 | [diff] [blame] | 67 | return net::OK; |
[email protected] | 83ab4a28 | 2012-07-12 18:19:45 | [diff] [blame] | 68 | } |
| 69 | |
| 70 | } // namespace content |