histogram_message_filter.cc revision a3f7b4e666c476898878fa745f637129375cd889
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_message_filter.h"
6
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"
14
15namespace content {
16
17HistogramMessageFilter::HistogramMessageFilter() {}
18
19void HistogramMessageFilter::OnChannelConnected(int32 peer_pid) {
20  BrowserMessageFilter::OnChannelConnected(peer_pid);
21}
22
23bool HistogramMessageFilter::OnMessageReceived(const IPC::Message& message,
24                                              bool* message_was_ok) {
25  bool handled = true;
26  IPC_BEGIN_MESSAGE_MAP_EX(HistogramMessageFilter, message, *message_was_ok)
27    IPC_MESSAGE_HANDLER(ChildProcessHostMsg_ChildHistogramData,
28                        OnChildHistogramData)
29    IPC_MESSAGE_HANDLER(ChildProcessHostMsg_GetBrowserHistogram,
30                        OnGetBrowserHistogram)
31    IPC_MESSAGE_UNHANDLED(handled = false)
32  IPC_END_MESSAGE_MAP_EX()
33  return handled;
34}
35
36HistogramMessageFilter::~HistogramMessageFilter() {}
37
38void HistogramMessageFilter::OnChildHistogramData(
39    int sequence_number,
40    const std::vector<std::string>& pickled_histograms) {
41  HistogramController::GetInstance()->OnHistogramDataCollected(
42      sequence_number, pickled_histograms);
43}
44
45void HistogramMessageFilter::OnGetBrowserHistogram(
46    const std::string& name,
47    std::string* histogram_json) {
48  DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::IO));
49  // Security: Only allow access to browser histograms when running in the
50  // context of a test.
51  bool using_stats_collection_controller =
52      CommandLine::ForCurrentProcess()->HasSwitch(
53          switches::kStatsCollectionController);
54  if (!using_stats_collection_controller) {
55    LOG(ERROR) << "Attempt at reading browser histogram without specifying "
56               << "--" << switches::kStatsCollectionController << " switch.";
57    return;
58  }
59  base::HistogramBase* histogram =
60      base::StatisticsRecorder::FindHistogram(name);
61  if (!histogram) {
62    *histogram_json = "{}";
63  } else {
64    histogram->WriteJSON(histogram_json);
65  }
66}
67
68}  // namespace content
69