Re-subimission of https://codereview.chromium.org/1041213003/
[chromium-blink-merge.git] / content / browser / histogram_message_filter.cc
blob8eb7c7231baebe20a06de219afb6bf144bff62e1
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.
5 #include "content/browser/histogram_message_filter.h"
7 #include "base/command_line.h"
8 #include "base/metrics/histogram.h"
9 #include "base/metrics/statistics_recorder.h"
10 #include "content/browser/histogram_controller.h"
11 #include "content/browser/tcmalloc_internals_request_job.h"
12 #include "content/common/child_process_messages.h"
13 #include "content/public/common/content_switches.h"
15 namespace content {
17 HistogramMessageFilter::HistogramMessageFilter()
18 : BrowserMessageFilter(ChildProcessMsgStart) {}
20 bool HistogramMessageFilter::OnMessageReceived(const IPC::Message& message) {
21 bool handled = true;
22 IPC_BEGIN_MESSAGE_MAP(HistogramMessageFilter, message)
23 IPC_MESSAGE_HANDLER(ChildProcessHostMsg_ChildHistogramData,
24 OnChildHistogramData)
25 IPC_MESSAGE_HANDLER(ChildProcessHostMsg_GetBrowserHistogram,
26 OnGetBrowserHistogram)
27 IPC_MESSAGE_UNHANDLED(handled = false)
28 IPC_END_MESSAGE_MAP()
29 return handled;
32 HistogramMessageFilter::~HistogramMessageFilter() {}
34 void HistogramMessageFilter::OnChildHistogramData(
35 int sequence_number,
36 const std::vector<std::string>& pickled_histograms) {
37 HistogramController::GetInstance()->OnHistogramDataCollected(
38 sequence_number, pickled_histograms);
41 void HistogramMessageFilter::OnGetBrowserHistogram(
42 const std::string& name,
43 std::string* histogram_json) {
44 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::IO));
45 // Security: Only allow access to browser histograms when running in the
46 // context of a test.
47 bool using_stats_collection_controller =
48 base::CommandLine::ForCurrentProcess()->HasSwitch(
49 switches::kStatsCollectionController);
50 if (!using_stats_collection_controller) {
51 LOG(ERROR) << "Attempt at reading browser histogram without specifying "
52 << "--" << switches::kStatsCollectionController << " switch.";
53 return;
55 base::HistogramBase* histogram =
56 base::StatisticsRecorder::FindHistogram(name);
57 if (!histogram) {
58 *histogram_json = "{}";
59 } else {
60 histogram->WriteJSON(histogram_json);
64 } // namespace content