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 "content/browser/histogram_internals_request_job.h"
7 #include "base/metrics/histogram.h"
8 #include "base/metrics/statistics_recorder.h"
9 #include "base/profiler/scoped_tracker.h"
10 #include "content/browser/histogram_synchronizer.h"
11 #include "net/base/escape.h"
12 #include "net/base/net_errors.h"
13 #include "net/url_request/url_request.h"
18 HistogramInternalsRequestJob::HistogramInternalsRequestJob(
19 net::URLRequest* request, net::NetworkDelegate* network_delegate)
20 : net::URLRequestSimpleJob(request, network_delegate) {
21 const std::string& spec = request->url().possibly_invalid_spec();
22 const url::Parsed& parsed = request->url().parsed_for_possibly_invalid_spec();
23 // + 1 to skip the slash at the beginning of the path.
24 int offset = parsed.CountCharactersBefore(url::Parsed::PATH, false) + 1;
26 if (offset < static_cast<int>(spec.size()))
27 path_.assign(spec.substr(offset));
30 void AboutHistogram(std::string* data, const std::string& path) {
31 HistogramSynchronizer::FetchHistograms();
33 std::string unescaped_query;
34 std::string unescaped_title("About Histograms");
36 unescaped_query = net::UnescapeURLComponent(path,
37 net::UnescapeRule::NORMAL);
38 unescaped_title += " - " + unescaped_query;
41 data->append("<!DOCTYPE html>\n<html>\n<head>\n");
43 "<meta http-equiv=\"Content-Security-Policy\" "
44 "content=\"object-src 'none'; script-src 'none'\">");
45 data->append("<title>");
46 data->append(net::EscapeForHTML(unescaped_title));
47 data->append("</title>\n");
48 data->append("</head><body>");
50 // Display any stats for which we sent off requests the last time.
51 data->append("<p>Stats accumulated from browser startup to previous ");
52 data->append("page load; reload to get stats as of this page load.</p>\n");
53 data->append("<table width=\"100%\">\n");
55 base::StatisticsRecorder::WriteHTMLGraph(unescaped_query, data);
58 int HistogramInternalsRequestJob::GetData(
59 std::string* mime_type,
62 const net::CompletionCallback& callback) const {
63 // TODO(vadimt): Remove ScopedTracker below once crbug.com/422489 is fixed.
64 tracked_objects::ScopedTracker tracking_profile(
65 FROM_HERE_WITH_EXPLICIT_FUNCTION(
66 "422489 HistogramInternalsRequestJob::GetData"));
68 mime_type->assign("text/html");
69 charset->assign("UTF8");
72 AboutHistogram(data, path_);
76 } // namespace content