Update To 11.40.268.0
[platform/framework/web/crosswalk.git] / src / content / browser / histogram_internals_request_job.cc
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 "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"
14 #include "url/gurl.h"
15
16 namespace content {
17
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;
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 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");
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   // 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"));
67
68   mime_type->assign("text/html");
69   charset->assign("UTF8");
70
71   data->clear();
72   AboutHistogram(data, path_);
73   return net::OK;
74 }
75
76 }  // namespace content