| OLD | NEW |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "chromecast/base/metrics/cast_metrics_helper.h" | 5 #include "chromecast/base/metrics/cast_metrics_helper.h" |
| 6 | 6 |
| 7 #include <memory> |
| 7 #include <string> | 8 #include <string> |
| 8 #include <vector> | 9 #include <vector> |
| 9 | 10 |
| 10 #include "base/bind.h" | 11 #include "base/bind.h" |
| 11 #include "base/bind_helpers.h" | 12 #include "base/bind_helpers.h" |
| 12 #include "base/json/json_string_value_serializer.h" | 13 #include "base/json/json_string_value_serializer.h" |
| 13 #include "base/location.h" | 14 #include "base/location.h" |
| 14 #include "base/memory/scoped_ptr.h" | |
| 15 #include "base/metrics/histogram.h" | 15 #include "base/metrics/histogram.h" |
| 16 #include "base/metrics/user_metrics.h" | 16 #include "base/metrics/user_metrics.h" |
| 17 #include "base/single_thread_task_runner.h" | 17 #include "base/single_thread_task_runner.h" |
| 18 #include "base/strings/string_split.h" | 18 #include "base/strings/string_split.h" |
| 19 #include "base/strings/string_util.h" | 19 #include "base/strings/string_util.h" |
| 20 #include "base/time/time.h" | 20 #include "base/time/time.h" |
| 21 #include "base/values.h" | 21 #include "base/values.h" |
| 22 #include "chromecast/base/metrics/cast_histograms.h" | 22 #include "chromecast/base/metrics/cast_histograms.h" |
| 23 #include "chromecast/base/metrics/grouped_histogram.h" | 23 #include "chromecast/base/metrics/grouped_histogram.h" |
| 24 | 24 |
| 25 namespace chromecast { | 25 namespace chromecast { |
| 26 namespace metrics { | 26 namespace metrics { |
| 27 | 27 |
| 28 // A useful macro to make sure current member function runs on the valid thread. | 28 // A useful macro to make sure current member function runs on the valid thread. |
| 29 #define MAKE_SURE_THREAD(callback, ...) \ | 29 #define MAKE_SURE_THREAD(callback, ...) \ |
| 30 if (!task_runner_->BelongsToCurrentThread()) { \ | 30 if (!task_runner_->BelongsToCurrentThread()) { \ |
| 31 task_runner_->PostTask(FROM_HERE, \ | 31 task_runner_->PostTask(FROM_HERE, \ |
| 32 base::Bind(&CastMetricsHelper::callback, \ | 32 base::Bind(&CastMetricsHelper::callback, \ |
| 33 base::Unretained(this), ##__VA_ARGS__)); \ | 33 base::Unretained(this), ##__VA_ARGS__)); \ |
| 34 return; \ | 34 return; \ |
| 35 } | 35 } |
| 36 | 36 |
| 37 namespace { | 37 namespace { |
| 38 | 38 |
| 39 CastMetricsHelper* g_instance = NULL; | 39 CastMetricsHelper* g_instance = NULL; |
| 40 | 40 |
| 41 const char kMetricsNameAppInfoDelimiter = '#'; | 41 const char kMetricsNameAppInfoDelimiter = '#'; |
| 42 | 42 |
| 43 scoped_ptr<std::string> SerializeToJson(const base::Value& value) { | 43 std::unique_ptr<std::string> SerializeToJson(const base::Value& value) { |
| 44 scoped_ptr<std::string> json_str(new std::string()); | 44 std::unique_ptr<std::string> json_str(new std::string()); |
| 45 JSONStringValueSerializer serializer(json_str.get()); | 45 JSONStringValueSerializer serializer(json_str.get()); |
| 46 if (!serializer.Serialize(value)) | 46 if (!serializer.Serialize(value)) |
| 47 json_str.reset(nullptr); | 47 json_str.reset(nullptr); |
| 48 return json_str; | 48 return json_str; |
| 49 } | 49 } |
| 50 | 50 |
| 51 } // namespace | 51 } // namespace |
| 52 | 52 |
| 53 // static | 53 // static |
| 54 | 54 |
| (...skipping 230 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 285 const std::string& name, | 285 const std::string& name, |
| 286 const base::TimeDelta& value) { | 286 const base::TimeDelta& value) { |
| 287 // Follow UMA_HISTOGRAM_MEDIUM_TIMES definition. | 287 // Follow UMA_HISTOGRAM_MEDIUM_TIMES definition. |
| 288 LogTimeHistogramEvent(name, value, | 288 LogTimeHistogramEvent(name, value, |
| 289 base::TimeDelta::FromMilliseconds(10), | 289 base::TimeDelta::FromMilliseconds(10), |
| 290 base::TimeDelta::FromMinutes(3), | 290 base::TimeDelta::FromMinutes(3), |
| 291 50); | 291 50); |
| 292 } | 292 } |
| 293 | 293 |
| 294 void CastMetricsHelper::RecordApplicationEvent(const std::string& event) { | 294 void CastMetricsHelper::RecordApplicationEvent(const std::string& event) { |
| 295 scoped_ptr<base::DictionaryValue> cast_event(new base::DictionaryValue()); | 295 std::unique_ptr<base::DictionaryValue> cast_event( |
| 296 new base::DictionaryValue()); |
| 296 cast_event->SetString("name", event); | 297 cast_event->SetString("name", event); |
| 297 base::TimeTicks now = base::TimeTicks::Now(); | 298 base::TimeTicks now = base::TimeTicks::Now(); |
| 298 cast_event->SetDouble("time", now.ToInternalValue()); | 299 cast_event->SetDouble("time", now.ToInternalValue()); |
| 299 cast_event->SetString("app_id", app_id_); | 300 cast_event->SetString("app_id", app_id_); |
| 300 cast_event->SetString("session_id", session_id_); | 301 cast_event->SetString("session_id", session_id_); |
| 301 cast_event->SetString("sdk_version", sdk_version_); | 302 cast_event->SetString("sdk_version", sdk_version_); |
| 302 const std::string message = *SerializeToJson(*cast_event.get()).get(); | 303 const std::string message = *SerializeToJson(*cast_event.get()).get(); |
| 303 RecordSimpleAction(message); | 304 RecordSimpleAction(message); |
| 304 } | 305 } |
| 305 | 306 |
| 306 void CastMetricsHelper::RecordApplicationEventWithValue( | 307 void CastMetricsHelper::RecordApplicationEventWithValue( |
| 307 const std::string& event, | 308 const std::string& event, |
| 308 int value) { | 309 int value) { |
| 309 scoped_ptr<base::DictionaryValue> cast_event(new base::DictionaryValue()); | 310 std::unique_ptr<base::DictionaryValue> cast_event( |
| 311 new base::DictionaryValue()); |
| 310 cast_event->SetString("name", event); | 312 cast_event->SetString("name", event); |
| 311 base::TimeTicks now = base::TimeTicks::Now(); | 313 base::TimeTicks now = base::TimeTicks::Now(); |
| 312 cast_event->SetDouble("time", now.ToInternalValue()); | 314 cast_event->SetDouble("time", now.ToInternalValue()); |
| 313 cast_event->SetString("app_id", app_id_); | 315 cast_event->SetString("app_id", app_id_); |
| 314 cast_event->SetString("session_id", session_id_); | 316 cast_event->SetString("session_id", session_id_); |
| 315 cast_event->SetString("sdk_version", sdk_version_); | 317 cast_event->SetString("sdk_version", sdk_version_); |
| 316 cast_event->SetInteger("value", value); | 318 cast_event->SetInteger("value", value); |
| 317 const std::string message = *SerializeToJson(*cast_event.get()).get(); | 319 const std::string message = *SerializeToJson(*cast_event.get()).get(); |
| 318 RecordSimpleAction(message); | 320 RecordSimpleAction(message); |
| 319 } | 321 } |
| 320 | 322 |
| 321 } // namespace metrics | 323 } // namespace metrics |
| 322 } // namespace chromecast | 324 } // namespace chromecast |
| OLD | NEW |