| 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 "components/metrics/metrics_log.h" | 5 #include "components/metrics/metrics_log.h" |
| 6 | 6 |
| 7 #include <stddef.h> | 7 #include <stddef.h> |
| 8 #include <stdint.h> | 8 #include <stdint.h> |
| 9 | 9 |
| 10 #include <string> | 10 #include <string> |
| 11 | 11 |
| 12 #include "base/base64.h" | 12 #include "base/base64.h" |
| 13 #include "base/macros.h" | 13 #include "base/macros.h" |
| 14 #include "base/memory/scoped_vector.h" | 14 #include "base/memory/ptr_util.h" |
| 15 #include "base/metrics/bucket_ranges.h" | 15 #include "base/metrics/bucket_ranges.h" |
| 16 #include "base/metrics/sample_vector.h" | 16 #include "base/metrics/sample_vector.h" |
| 17 #include "base/strings/string_number_conversions.h" | 17 #include "base/strings/string_number_conversions.h" |
| 18 #include "base/sys_info.h" | 18 #include "base/sys_info.h" |
| 19 #include "base/time/time.h" | 19 #include "base/time/time.h" |
| 20 #include "components/metrics/metrics_pref_names.h" | 20 #include "components/metrics/metrics_pref_names.h" |
| 21 #include "components/metrics/metrics_state_manager.h" | 21 #include "components/metrics/metrics_state_manager.h" |
| 22 #include "components/metrics/proto/chrome_user_metrics_extension.pb.h" | 22 #include "components/metrics/proto/chrome_user_metrics_extension.pb.h" |
| 23 #include "components/metrics/test_metrics_provider.h" | 23 #include "components/metrics/test_metrics_provider.h" |
| 24 #include "components/metrics/test_metrics_service_client.h" | 24 #include "components/metrics/test_metrics_service_client.h" |
| (...skipping 257 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 282 TEST_F(MetricsLogTest, RecordEnvironment) { | 282 TEST_F(MetricsLogTest, RecordEnvironment) { |
| 283 TestMetricsServiceClient client; | 283 TestMetricsServiceClient client; |
| 284 TestMetricsLog log( | 284 TestMetricsLog log( |
| 285 kClientId, kSessionId, MetricsLog::ONGOING_LOG, &client, &prefs_); | 285 kClientId, kSessionId, MetricsLog::ONGOING_LOG, &client, &prefs_); |
| 286 | 286 |
| 287 std::vector<variations::ActiveGroupId> synthetic_trials; | 287 std::vector<variations::ActiveGroupId> synthetic_trials; |
| 288 // Add two synthetic trials. | 288 // Add two synthetic trials. |
| 289 synthetic_trials.push_back(kSyntheticTrials[0]); | 289 synthetic_trials.push_back(kSyntheticTrials[0]); |
| 290 synthetic_trials.push_back(kSyntheticTrials[1]); | 290 synthetic_trials.push_back(kSyntheticTrials[1]); |
| 291 | 291 |
| 292 log.RecordEnvironment(std::vector<MetricsProvider*>(), synthetic_trials, | 292 log.RecordEnvironment(std::vector<std::unique_ptr<MetricsProvider>>(), |
| 293 kInstallDate, kEnabledDate); | 293 synthetic_trials, kInstallDate, kEnabledDate); |
| 294 // Check that the system profile on the log has the correct values set. | 294 // Check that the system profile on the log has the correct values set. |
| 295 CheckSystemProfile(log.system_profile()); | 295 CheckSystemProfile(log.system_profile()); |
| 296 | 296 |
| 297 // Check that the system profile has also been written to prefs. | 297 // Check that the system profile has also been written to prefs. |
| 298 const std::string base64_system_profile = | 298 const std::string base64_system_profile = |
| 299 prefs_.GetString(prefs::kStabilitySavedSystemProfile); | 299 prefs_.GetString(prefs::kStabilitySavedSystemProfile); |
| 300 EXPECT_FALSE(base64_system_profile.empty()); | 300 EXPECT_FALSE(base64_system_profile.empty()); |
| 301 std::string serialied_system_profile; | 301 std::string serialied_system_profile; |
| 302 EXPECT_TRUE(base::Base64Decode(base64_system_profile, | 302 EXPECT_TRUE(base::Base64Decode(base64_system_profile, |
| 303 &serialied_system_profile)); | 303 &serialied_system_profile)); |
| (...skipping 16 matching lines...) Expand all Loading... |
| 320 kClientId, kSessionId, MetricsLog::ONGOING_LOG, &client, &prefs_); | 320 kClientId, kSessionId, MetricsLog::ONGOING_LOG, &client, &prefs_); |
| 321 std::string app_version; | 321 std::string app_version; |
| 322 EXPECT_FALSE(log.LoadSavedEnvironmentFromPrefs(&app_version)); | 322 EXPECT_FALSE(log.LoadSavedEnvironmentFromPrefs(&app_version)); |
| 323 EXPECT_TRUE(app_version.empty()); | 323 EXPECT_TRUE(app_version.empty()); |
| 324 } | 324 } |
| 325 | 325 |
| 326 // Do a RecordEnvironment() call and check whether the pref is recorded. | 326 // Do a RecordEnvironment() call and check whether the pref is recorded. |
| 327 { | 327 { |
| 328 TestMetricsLog log( | 328 TestMetricsLog log( |
| 329 kClientId, kSessionId, MetricsLog::ONGOING_LOG, &client, &prefs_); | 329 kClientId, kSessionId, MetricsLog::ONGOING_LOG, &client, &prefs_); |
| 330 log.RecordEnvironment(std::vector<MetricsProvider*>(), | 330 log.RecordEnvironment(std::vector<std::unique_ptr<MetricsProvider>>(), |
| 331 std::vector<variations::ActiveGroupId>(), | 331 std::vector<variations::ActiveGroupId>(), |
| 332 kInstallDate, kEnabledDate); | 332 kInstallDate, kEnabledDate); |
| 333 EXPECT_FALSE(prefs_.GetString(kSystemProfilePref).empty()); | 333 EXPECT_FALSE(prefs_.GetString(kSystemProfilePref).empty()); |
| 334 EXPECT_FALSE(prefs_.GetString(kSystemProfileHashPref).empty()); | 334 EXPECT_FALSE(prefs_.GetString(kSystemProfileHashPref).empty()); |
| 335 } | 335 } |
| 336 | 336 |
| 337 { | 337 { |
| 338 TestMetricsLog log( | 338 TestMetricsLog log( |
| 339 kClientId, kSessionId, MetricsLog::ONGOING_LOG, &client, &prefs_); | 339 kClientId, kSessionId, MetricsLog::ONGOING_LOG, &client, &prefs_); |
| 340 std::string app_version; | 340 std::string app_version; |
| 341 EXPECT_TRUE(log.LoadSavedEnvironmentFromPrefs(&app_version)); | 341 EXPECT_TRUE(log.LoadSavedEnvironmentFromPrefs(&app_version)); |
| 342 EXPECT_EQ("bogus version", app_version); | 342 EXPECT_EQ("bogus version", app_version); |
| 343 // Check some values in the system profile. | 343 // Check some values in the system profile. |
| 344 EXPECT_EQ(kInstallDateExpected, log.system_profile().install_date()); | 344 EXPECT_EQ(kInstallDateExpected, log.system_profile().install_date()); |
| 345 EXPECT_EQ(kEnabledDateExpected, log.system_profile().uma_enabled_date()); | 345 EXPECT_EQ(kEnabledDateExpected, log.system_profile().uma_enabled_date()); |
| 346 // Ensure that the call did not clear the prefs. | 346 // Ensure that the call did not clear the prefs. |
| 347 EXPECT_FALSE(prefs_.GetString(kSystemProfilePref).empty()); | 347 EXPECT_FALSE(prefs_.GetString(kSystemProfilePref).empty()); |
| 348 EXPECT_FALSE(prefs_.GetString(kSystemProfileHashPref).empty()); | 348 EXPECT_FALSE(prefs_.GetString(kSystemProfileHashPref).empty()); |
| 349 } | 349 } |
| 350 | 350 |
| 351 // Ensure that a non-matching hash results in the pref being invalid. | 351 // Ensure that a non-matching hash results in the pref being invalid. |
| 352 { | 352 { |
| 353 TestMetricsLog log( | 353 TestMetricsLog log( |
| 354 kClientId, kSessionId, MetricsLog::ONGOING_LOG, &client, &prefs_); | 354 kClientId, kSessionId, MetricsLog::ONGOING_LOG, &client, &prefs_); |
| 355 // Call RecordEnvironment() to record the pref again. | 355 // Call RecordEnvironment() to record the pref again. |
| 356 log.RecordEnvironment(std::vector<MetricsProvider*>(), | 356 log.RecordEnvironment(std::vector<std::unique_ptr<MetricsProvider>>(), |
| 357 std::vector<variations::ActiveGroupId>(), | 357 std::vector<variations::ActiveGroupId>(), |
| 358 kInstallDate, kEnabledDate); | 358 kInstallDate, kEnabledDate); |
| 359 } | 359 } |
| 360 | 360 |
| 361 { | 361 { |
| 362 // Set the hash to a bad value. | 362 // Set the hash to a bad value. |
| 363 prefs_.SetString(kSystemProfileHashPref, "deadbeef"); | 363 prefs_.SetString(kSystemProfileHashPref, "deadbeef"); |
| 364 TestMetricsLog log( | 364 TestMetricsLog log( |
| 365 kClientId, kSessionId, MetricsLog::ONGOING_LOG, &client, &prefs_); | 365 kClientId, kSessionId, MetricsLog::ONGOING_LOG, &client, &prefs_); |
| 366 std::string app_version; | 366 std::string app_version; |
| 367 EXPECT_FALSE(log.LoadSavedEnvironmentFromPrefs(&app_version)); | 367 EXPECT_FALSE(log.LoadSavedEnvironmentFromPrefs(&app_version)); |
| 368 EXPECT_TRUE(app_version.empty()); | 368 EXPECT_TRUE(app_version.empty()); |
| 369 // Ensure that the prefs are not cleared, even if the call failed. | 369 // Ensure that the prefs are not cleared, even if the call failed. |
| 370 EXPECT_FALSE(prefs_.GetString(kSystemProfilePref).empty()); | 370 EXPECT_FALSE(prefs_.GetString(kSystemProfilePref).empty()); |
| 371 EXPECT_FALSE(prefs_.GetString(kSystemProfileHashPref).empty()); | 371 EXPECT_FALSE(prefs_.GetString(kSystemProfileHashPref).empty()); |
| 372 } | 372 } |
| 373 } | 373 } |
| 374 | 374 |
| 375 TEST_F(MetricsLogTest, RecordEnvironmentEnableDefault) { | 375 TEST_F(MetricsLogTest, RecordEnvironmentEnableDefault) { |
| 376 TestMetricsServiceClient client; | 376 TestMetricsServiceClient client; |
| 377 TestMetricsLog log_unknown(kClientId, kSessionId, MetricsLog::ONGOING_LOG, | 377 TestMetricsLog log_unknown(kClientId, kSessionId, MetricsLog::ONGOING_LOG, |
| 378 &client, &prefs_); | 378 &client, &prefs_); |
| 379 | 379 |
| 380 std::vector<variations::ActiveGroupId> synthetic_trials; | 380 std::vector<variations::ActiveGroupId> synthetic_trials; |
| 381 | 381 |
| 382 log_unknown.RecordEnvironment(std::vector<MetricsProvider*>(), | 382 log_unknown.RecordEnvironment(std::vector<std::unique_ptr<MetricsProvider>>(), |
| 383 synthetic_trials, kInstallDate, kEnabledDate); | 383 synthetic_trials, kInstallDate, kEnabledDate); |
| 384 EXPECT_FALSE(log_unknown.system_profile().has_uma_default_state()); | 384 EXPECT_FALSE(log_unknown.system_profile().has_uma_default_state()); |
| 385 | 385 |
| 386 client.set_enable_default(EnableMetricsDefault::OPT_IN); | 386 client.set_enable_default(EnableMetricsDefault::OPT_IN); |
| 387 TestMetricsLog log_opt_in(kClientId, kSessionId, MetricsLog::ONGOING_LOG, | 387 TestMetricsLog log_opt_in(kClientId, kSessionId, MetricsLog::ONGOING_LOG, |
| 388 &client, &prefs_); | 388 &client, &prefs_); |
| 389 log_opt_in.RecordEnvironment(std::vector<MetricsProvider*>(), | 389 log_opt_in.RecordEnvironment(std::vector<std::unique_ptr<MetricsProvider>>(), |
| 390 synthetic_trials, kInstallDate, kEnabledDate); | 390 synthetic_trials, kInstallDate, kEnabledDate); |
| 391 EXPECT_TRUE(log_opt_in.system_profile().has_uma_default_state()); | 391 EXPECT_TRUE(log_opt_in.system_profile().has_uma_default_state()); |
| 392 EXPECT_EQ(SystemProfileProto_UmaDefaultState_OPT_IN, | 392 EXPECT_EQ(SystemProfileProto_UmaDefaultState_OPT_IN, |
| 393 log_opt_in.system_profile().uma_default_state()); | 393 log_opt_in.system_profile().uma_default_state()); |
| 394 | 394 |
| 395 client.set_enable_default(EnableMetricsDefault::OPT_OUT); | 395 client.set_enable_default(EnableMetricsDefault::OPT_OUT); |
| 396 TestMetricsLog log_opt_out(kClientId, kSessionId, MetricsLog::ONGOING_LOG, | 396 TestMetricsLog log_opt_out(kClientId, kSessionId, MetricsLog::ONGOING_LOG, |
| 397 &client, &prefs_); | 397 &client, &prefs_); |
| 398 log_opt_out.RecordEnvironment(std::vector<MetricsProvider*>(), | 398 log_opt_out.RecordEnvironment(std::vector<std::unique_ptr<MetricsProvider>>(), |
| 399 synthetic_trials, kInstallDate, kEnabledDate); | 399 synthetic_trials, kInstallDate, kEnabledDate); |
| 400 EXPECT_TRUE(log_opt_out.system_profile().has_uma_default_state()); | 400 EXPECT_TRUE(log_opt_out.system_profile().has_uma_default_state()); |
| 401 EXPECT_EQ(SystemProfileProto_UmaDefaultState_OPT_OUT, | 401 EXPECT_EQ(SystemProfileProto_UmaDefaultState_OPT_OUT, |
| 402 log_opt_out.system_profile().uma_default_state()); | 402 log_opt_out.system_profile().uma_default_state()); |
| 403 | 403 |
| 404 client.set_reporting_is_managed(true); | 404 client.set_reporting_is_managed(true); |
| 405 TestMetricsLog log_managed(kClientId, kSessionId, MetricsLog::ONGOING_LOG, | 405 TestMetricsLog log_managed(kClientId, kSessionId, MetricsLog::ONGOING_LOG, |
| 406 &client, &prefs_); | 406 &client, &prefs_); |
| 407 log_managed.RecordEnvironment(std::vector<MetricsProvider*>(), | 407 log_managed.RecordEnvironment(std::vector<std::unique_ptr<MetricsProvider>>(), |
| 408 synthetic_trials, kInstallDate, kEnabledDate); | 408 synthetic_trials, kInstallDate, kEnabledDate); |
| 409 EXPECT_TRUE(log_managed.system_profile().has_uma_default_state()); | 409 EXPECT_TRUE(log_managed.system_profile().has_uma_default_state()); |
| 410 EXPECT_EQ(SystemProfileProto_UmaDefaultState_POLICY_FORCED_ENABLED, | 410 EXPECT_EQ(SystemProfileProto_UmaDefaultState_POLICY_FORCED_ENABLED, |
| 411 log_managed.system_profile().uma_default_state()); | 411 log_managed.system_profile().uma_default_state()); |
| 412 } | 412 } |
| 413 | 413 |
| 414 TEST_F(MetricsLogTest, InitialLogStabilityMetrics) { | 414 TEST_F(MetricsLogTest, InitialLogStabilityMetrics) { |
| 415 TestMetricsServiceClient client; | 415 TestMetricsServiceClient client; |
| 416 TestMetricsLog log(kClientId, | 416 TestMetricsLog log(kClientId, |
| 417 kSessionId, | 417 kSessionId, |
| 418 MetricsLog::INITIAL_STABILITY_LOG, | 418 MetricsLog::INITIAL_STABILITY_LOG, |
| 419 &client, | 419 &client, |
| 420 &prefs_); | 420 &prefs_); |
| 421 TestMetricsProvider* test_provider = new TestMetricsProvider(); | 421 TestMetricsProvider* test_provider = new TestMetricsProvider(); |
| 422 ScopedVector<MetricsProvider> metrics_providers; | 422 std::vector<std::unique_ptr<MetricsProvider>> metrics_providers; |
| 423 metrics_providers.push_back(test_provider); | 423 metrics_providers.push_back(base::WrapUnique<MetricsProvider>(test_provider)); |
| 424 log.RecordEnvironment(metrics_providers.get(), | 424 log.RecordEnvironment(metrics_providers, |
| 425 std::vector<variations::ActiveGroupId>(), kInstallDate, | 425 std::vector<variations::ActiveGroupId>(), kInstallDate, |
| 426 kEnabledDate); | 426 kEnabledDate); |
| 427 log.RecordStabilityMetrics(metrics_providers.get(), base::TimeDelta(), | 427 log.RecordStabilityMetrics(metrics_providers, base::TimeDelta(), |
| 428 base::TimeDelta()); | 428 base::TimeDelta()); |
| 429 const SystemProfileProto_Stability& stability = | 429 const SystemProfileProto_Stability& stability = |
| 430 log.system_profile().stability(); | 430 log.system_profile().stability(); |
| 431 // Required metrics: | 431 // Required metrics: |
| 432 EXPECT_TRUE(stability.has_launch_count()); | 432 EXPECT_TRUE(stability.has_launch_count()); |
| 433 EXPECT_TRUE(stability.has_crash_count()); | 433 EXPECT_TRUE(stability.has_crash_count()); |
| 434 // Initial log metrics: only expected if non-zero. | 434 // Initial log metrics: only expected if non-zero. |
| 435 EXPECT_FALSE(stability.has_incomplete_shutdown_count()); | 435 EXPECT_FALSE(stability.has_incomplete_shutdown_count()); |
| 436 EXPECT_FALSE(stability.has_breakpad_registration_success_count()); | 436 EXPECT_FALSE(stability.has_breakpad_registration_success_count()); |
| 437 EXPECT_FALSE(stability.has_breakpad_registration_failure_count()); | 437 EXPECT_FALSE(stability.has_breakpad_registration_failure_count()); |
| 438 EXPECT_FALSE(stability.has_debugger_present_count()); | 438 EXPECT_FALSE(stability.has_debugger_present_count()); |
| 439 EXPECT_FALSE(stability.has_debugger_not_present_count()); | 439 EXPECT_FALSE(stability.has_debugger_not_present_count()); |
| 440 | 440 |
| 441 // The test provider should have been called upon to provide initial | 441 // The test provider should have been called upon to provide initial |
| 442 // stability and regular stability metrics. | 442 // stability and regular stability metrics. |
| 443 EXPECT_TRUE(test_provider->provide_initial_stability_metrics_called()); | 443 EXPECT_TRUE(test_provider->provide_initial_stability_metrics_called()); |
| 444 EXPECT_TRUE(test_provider->provide_stability_metrics_called()); | 444 EXPECT_TRUE(test_provider->provide_stability_metrics_called()); |
| 445 } | 445 } |
| 446 | 446 |
| 447 TEST_F(MetricsLogTest, OngoingLogStabilityMetrics) { | 447 TEST_F(MetricsLogTest, OngoingLogStabilityMetrics) { |
| 448 TestMetricsServiceClient client; | 448 TestMetricsServiceClient client; |
| 449 TestMetricsLog log( | 449 TestMetricsLog log( |
| 450 kClientId, kSessionId, MetricsLog::ONGOING_LOG, &client, &prefs_); | 450 kClientId, kSessionId, MetricsLog::ONGOING_LOG, &client, &prefs_); |
| 451 TestMetricsProvider* test_provider = new TestMetricsProvider(); | 451 TestMetricsProvider* test_provider = new TestMetricsProvider(); |
| 452 ScopedVector<MetricsProvider> metrics_providers; | 452 std::vector<std::unique_ptr<MetricsProvider>> metrics_providers; |
| 453 metrics_providers.push_back(test_provider); | 453 metrics_providers.push_back(base::WrapUnique<MetricsProvider>(test_provider)); |
| 454 log.RecordEnvironment(metrics_providers.get(), | 454 log.RecordEnvironment(metrics_providers, |
| 455 std::vector<variations::ActiveGroupId>(), kInstallDate, | 455 std::vector<variations::ActiveGroupId>(), kInstallDate, |
| 456 kEnabledDate); | 456 kEnabledDate); |
| 457 log.RecordStabilityMetrics(metrics_providers.get(), base::TimeDelta(), | 457 log.RecordStabilityMetrics(metrics_providers, base::TimeDelta(), |
| 458 base::TimeDelta()); | 458 base::TimeDelta()); |
| 459 const SystemProfileProto_Stability& stability = | 459 const SystemProfileProto_Stability& stability = |
| 460 log.system_profile().stability(); | 460 log.system_profile().stability(); |
| 461 // Required metrics: | 461 // Required metrics: |
| 462 EXPECT_TRUE(stability.has_launch_count()); | 462 EXPECT_TRUE(stability.has_launch_count()); |
| 463 EXPECT_TRUE(stability.has_crash_count()); | 463 EXPECT_TRUE(stability.has_crash_count()); |
| 464 // Initial log metrics: only expected if non-zero. | 464 // Initial log metrics: only expected if non-zero. |
| 465 EXPECT_FALSE(stability.has_incomplete_shutdown_count()); | 465 EXPECT_FALSE(stability.has_incomplete_shutdown_count()); |
| 466 EXPECT_FALSE(stability.has_breakpad_registration_success_count()); | 466 EXPECT_FALSE(stability.has_breakpad_registration_success_count()); |
| 467 EXPECT_FALSE(stability.has_breakpad_registration_failure_count()); | 467 EXPECT_FALSE(stability.has_breakpad_registration_failure_count()); |
| (...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 499 TestMetricsServiceClient client; | 499 TestMetricsServiceClient client; |
| 500 client.set_product(kTestProduct); | 500 client.set_product(kTestProduct); |
| 501 TestMetricsLog log( | 501 TestMetricsLog log( |
| 502 kClientId, kSessionId, MetricsLog::ONGOING_LOG, &client, &prefs_); | 502 kClientId, kSessionId, MetricsLog::ONGOING_LOG, &client, &prefs_); |
| 503 // Check that the product is set to |kTestProduct|. | 503 // Check that the product is set to |kTestProduct|. |
| 504 EXPECT_TRUE(log.uma_proto().has_product()); | 504 EXPECT_TRUE(log.uma_proto().has_product()); |
| 505 EXPECT_EQ(kTestProduct, log.uma_proto().product()); | 505 EXPECT_EQ(kTestProduct, log.uma_proto().product()); |
| 506 } | 506 } |
| 507 | 507 |
| 508 } // namespace metrics | 508 } // namespace metrics |
| OLD | NEW |