| OLD | NEW |
| (Empty) |
| 1 // Copyright 2016 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 "ios/chrome/browser/ui/webui/history/metrics_handler.h" | |
| 6 | |
| 7 #include "base/bind.h" | |
| 8 #include "base/bind_helpers.h" | |
| 9 #include "base/logging.h" | |
| 10 #include "base/metrics/histogram.h" | |
| 11 #include "base/metrics/user_metrics.h" | |
| 12 #include "base/strings/utf_string_conversions.h" | |
| 13 #include "base/values.h" | |
| 14 #include "ios/web/public/webui/web_ui_ios.h" | |
| 15 | |
| 16 using base::ListValue; | |
| 17 using base::UserMetricsAction; | |
| 18 | |
| 19 MetricsHandler::MetricsHandler() {} | |
| 20 MetricsHandler::~MetricsHandler() {} | |
| 21 | |
| 22 void MetricsHandler::RegisterMessages() { | |
| 23 web_ui()->RegisterMessageCallback( | |
| 24 "metricsHandler:recordAction", | |
| 25 base::Bind(&MetricsHandler::HandleRecordAction, base::Unretained(this))); | |
| 26 web_ui()->RegisterMessageCallback( | |
| 27 "metricsHandler:recordInHistogram", | |
| 28 base::Bind(&MetricsHandler::HandleRecordInHistogram, | |
| 29 base::Unretained(this))); | |
| 30 } | |
| 31 | |
| 32 void MetricsHandler::HandleRecordAction(const base::ListValue* args) { | |
| 33 std::string string_action = base::UTF16ToUTF8(ExtractStringValue(args)); | |
| 34 base::RecordComputedAction(string_action); | |
| 35 } | |
| 36 | |
| 37 void MetricsHandler::HandleRecordInHistogram(const base::ListValue* args) { | |
| 38 std::string histogram_name; | |
| 39 double value; | |
| 40 double boundary_value; | |
| 41 if (!args->GetString(0, &histogram_name) || !args->GetDouble(1, &value) || | |
| 42 !args->GetDouble(2, &boundary_value)) { | |
| 43 NOTREACHED(); | |
| 44 return; | |
| 45 } | |
| 46 | |
| 47 int int_value = static_cast<int>(value); | |
| 48 int int_boundary_value = static_cast<int>(boundary_value); | |
| 49 if (int_boundary_value >= 4000 || int_value > int_boundary_value || | |
| 50 int_value < 0) { | |
| 51 NOTREACHED(); | |
| 52 return; | |
| 53 } | |
| 54 | |
| 55 int bucket_count = int_boundary_value; | |
| 56 while (bucket_count >= 100) { | |
| 57 bucket_count /= 10; | |
| 58 } | |
| 59 | |
| 60 // As |histogram_name| may change between calls, the UMA_HISTOGRAM_ENUMERATION | |
| 61 // macro cannot be used here. | |
| 62 base::HistogramBase* counter = base::LinearHistogram::FactoryGet( | |
| 63 histogram_name, 1, int_boundary_value, bucket_count + 1, | |
| 64 base::HistogramBase::kUmaTargetedHistogramFlag); | |
| 65 counter->Add(int_value); | |
| 66 } | |
| OLD | NEW |