15821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Copyright (c) 2012 The Chromium Authors. All rights reserved.
25821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Use of this source code is governed by a BSD-style license that can be
35821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// found in the LICENSE file.
45821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
55821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "content/browser/histogram_internals_request_job.h"
65821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
75821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "base/metrics/histogram.h"
85821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "base/metrics/statistics_recorder.h"
95821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "content/browser/histogram_synchronizer.h"
105821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "net/base/escape.h"
115821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "net/base/net_errors.h"
125821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "net/url_request/url_request.h"
137dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch#include "url/gurl.h"
145821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
155821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)namespace content {
165821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
175821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)HistogramInternalsRequestJob::HistogramInternalsRequestJob(
185821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    net::URLRequest* request, net::NetworkDelegate* network_delegate)
195821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    : net::URLRequestSimpleJob(request, network_delegate) {
205821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  const std::string& spec = request->url().possibly_invalid_spec();
215c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu  const url::Parsed& parsed = request->url().parsed_for_possibly_invalid_spec();
225821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // + 1 to skip the slash at the beginning of the path.
235c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu  int offset = parsed.CountCharactersBefore(url::Parsed::PATH, false) + 1;
245821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
255821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  if (offset < static_cast<int>(spec.size()))
265821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    path_.assign(spec.substr(offset));
275821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
285821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
295821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)void AboutHistogram(std::string* data, const std::string& path) {
305821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  HistogramSynchronizer::FetchHistograms();
315821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
325821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  std::string unescaped_query;
335821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  std::string unescaped_title("About Histograms");
345821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  if (!path.empty()) {
355821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    unescaped_query = net::UnescapeURLComponent(path,
365821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                                                net::UnescapeRule::NORMAL);
375821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    unescaped_title += " - " + unescaped_query;
385821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
395821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
405821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  data->append("<!DOCTYPE html>\n<html>\n<head>\n");
415821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  data->append(
425821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      "<meta http-equiv=\"Content-Security-Policy\" "
43868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)      "content=\"object-src 'none'; script-src 'none'\">");
445821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  data->append("<title>");
455821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  data->append(net::EscapeForHTML(unescaped_title));
465821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  data->append("</title>\n");
475821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  data->append("</head><body>");
485821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
495821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Display any stats for which we sent off requests the last time.
505f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)  data->append("<p>Stats accumulated from browser startup to previous ");
515f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)  data->append("page load; reload to get stats as of this page load.</p>\n");
525821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  data->append("<table width=\"100%\">\n");
535821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
545821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  base::StatisticsRecorder::WriteHTMLGraph(unescaped_query, data);
555821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
565821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
575821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)int HistogramInternalsRequestJob::GetData(
585821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    std::string* mime_type,
595821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    std::string* charset,
605821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    std::string* data,
615821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    const net::CompletionCallback& callback) const {
625821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  mime_type->assign("text/html");
635821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  charset->assign("UTF8");
645821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
655821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  data->clear();
665821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  AboutHistogram(data, path_);
675821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  return net::OK;
685821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
695821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
705821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}  // namespace content
71