histogram_internals_request_job.cc revision 5821806d5e7f356e8fa4b058a389a808ea183019
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"
8#include "base/metrics/statistics_recorder.h"
9#include "content/browser/histogram_synchronizer.h"
10#include "googleurl/src/gurl.h"
11#include "net/base/escape.h"
12#include "net/base/net_errors.h"
13#include "net/url_request/url_request.h"
14
15namespace content {
16
17HistogramInternalsRequestJob::HistogramInternalsRequestJob(
18    net::URLRequest* request, net::NetworkDelegate* network_delegate)
19    : net::URLRequestSimpleJob(request, network_delegate) {
20  const std::string& spec = request->url().possibly_invalid_spec();
21  const url_parse::Parsed& parsed =
22      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_parse::Parsed::PATH, false) + 1;
25
26  if (offset < static_cast<int>(spec.size()))
27    path_.assign(spec.substr(offset));
28}
29
30void AboutHistogram(std::string* data, const std::string& path) {
31#ifndef NDEBUG
32  // We only rush the acquisition of Histogram meta-data (meta-histograms) in
33  // Debug mode, so that developers don't damage our data that we upload to UMA
34  // (in official builds).
35  base::StatisticsRecorder::CollectHistogramStats("Browser");
36#endif
37  HistogramSynchronizer::FetchHistograms();
38
39  std::string unescaped_query;
40  std::string unescaped_title("About Histograms");
41  if (!path.empty()) {
42    unescaped_query = net::UnescapeURLComponent(path,
43                                                net::UnescapeRule::NORMAL);
44    unescaped_title += " - " + unescaped_query;
45  }
46
47  data->append("<!DOCTYPE html>\n<html>\n<head>\n");
48  data->append(
49      "<meta http-equiv=\"Content-Security-Policy\" "
50      "content=\"object-src 'none'; script-src 'none' 'unsafe-eval'\">");
51  data->append("<title>");
52  data->append(net::EscapeForHTML(unescaped_title));
53  data->append("</title>\n");
54  data->append("</head><body>");
55
56  // Display any stats for which we sent off requests the last time.
57  data->append("<p>Stats as of last page load;");
58  data->append("reload to get stats as of this page load.</p>\n");
59  data->append("<table width=\"100%\">\n");
60
61  base::StatisticsRecorder::WriteHTMLGraph(unescaped_query, data);
62}
63
64int HistogramInternalsRequestJob::GetData(
65    std::string* mime_type,
66    std::string* charset,
67    std::string* data,
68    const net::CompletionCallback& callback) const {
69  mime_type->assign("text/html");
70  charset->assign("UTF8");
71
72  data->clear();
73  AboutHistogram(data, path_);
74  return net::OK;
75}
76
77}  // namespace content
78