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 "net/base/escape.h"
11#include "net/base/net_errors.h"
12#include "net/url_request/url_request.h"
13#include "url/gurl.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::Parsed& parsed = request->url().parsed_for_possibly_invalid_spec();
22  // + 1 to skip the slash at the beginning of the path.
23  int offset = parsed.CountCharactersBefore(url::Parsed::PATH, false) + 1;
24
25  if (offset < static_cast<int>(spec.size()))
26    path_.assign(spec.substr(offset));
27}
28
29void AboutHistogram(std::string* data, const std::string& path) {
30  HistogramSynchronizer::FetchHistograms();
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(
42      "<meta http-equiv=\"Content-Security-Policy\" "
43      "content=\"object-src 'none'; script-src 'none'\">");
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.
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");
52  data->append("<table width=\"100%\">\n");
53
54  base::StatisticsRecorder::WriteHTMLGraph(unescaped_query, data);
55}
56
57int HistogramInternalsRequestJob::GetData(
58    std::string* mime_type,
59    std::string* charset,
60    std::string* data,
61    const net::CompletionCallback& callback) const {
62  mime_type->assign("text/html");
63  charset->assign("UTF8");
64
65  data->clear();
66  AboutHistogram(data, path_);
67  return net::OK;
68}
69
70}  // namespace content
71