Home | History | Annotate | Download | only in browser
      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 
     15 namespace content {
     16 
     17 HistogramInternalsRequestJob::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 
     30 void AboutHistogram(std::string* data, const std::string& path) {
     31   HistogramSynchronizer::FetchHistograms();
     32 
     33   std::string unescaped_query;
     34   std::string unescaped_title("About Histograms");
     35   if (!path.empty()) {
     36     unescaped_query = net::UnescapeURLComponent(path,
     37                                                 net::UnescapeRule::NORMAL);
     38     unescaped_title += " - " + unescaped_query;
     39   }
     40 
     41   data->append("<!DOCTYPE html>\n<html>\n<head>\n");
     42   data->append(
     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>");
     49 
     50   // Display any stats for which we sent off requests the last time.
     51   data->append("<p>Stats as of last page load;");
     52   data->append("reload to get stats as of this page load.</p>\n");
     53   data->append("<table width=\"100%\">\n");
     54 
     55   base::StatisticsRecorder::WriteHTMLGraph(unescaped_query, data);
     56 }
     57 
     58 int HistogramInternalsRequestJob::GetData(
     59     std::string* mime_type,
     60     std::string* charset,
     61     std::string* data,
     62     const net::CompletionCallback& callback) const {
     63   mime_type->assign("text/html");
     64   charset->assign("UTF8");
     65 
     66   data->clear();
     67   AboutHistogram(data, path_);
     68   return net::OK;
     69 }
     70 
     71 }  // namespace content
     72