notification_access_setup_operation.cc 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. // Copyright 2020 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. #include "ash/components/phonehub/notification_access_setup_operation.h"
  5. #include <array>
  6. #include "base/check.h"
  7. #include "base/containers/contains.h"
  8. #include "base/metrics/histogram_functions.h"
  9. namespace ash {
  10. namespace phonehub {
  11. namespace {
  12. // Status values which are considered "final" - i.e., once the status of an
  13. // operation changes to one of these values, the operation has completed. These
  14. // status values indicate either a success or a fatal error.
  15. constexpr std::array<NotificationAccessSetupOperation::Status, 4>
  16. kOperationFinishedStatus{
  17. NotificationAccessSetupOperation::Status::kTimedOutConnecting,
  18. NotificationAccessSetupOperation::Status::kConnectionDisconnected,
  19. NotificationAccessSetupOperation::Status::kCompletedSuccessfully,
  20. NotificationAccessSetupOperation::Status::
  21. kProhibitedFromProvidingAccess,
  22. };
  23. // Used for metrics; do not change.
  24. constexpr size_t kNumSetupDurationHistogramBuckets = 50;
  25. constexpr base::TimeDelta kSetupDurationHistogramMinTime = base::Seconds(1);
  26. constexpr base::TimeDelta kSetupDurationHistogramMaxTime = base::Minutes(10);
  27. } // namespace
  28. // static
  29. bool NotificationAccessSetupOperation::IsFinalStatus(Status status) {
  30. return base::Contains(kOperationFinishedStatus, status);
  31. }
  32. NotificationAccessSetupOperation::NotificationAccessSetupOperation(
  33. Delegate* delegate,
  34. base::OnceClosure destructor_callback)
  35. : delegate_(delegate),
  36. destructor_callback_(std::move(destructor_callback)) {
  37. DCHECK(delegate_);
  38. DCHECK(destructor_callback_);
  39. }
  40. NotificationAccessSetupOperation::~NotificationAccessSetupOperation() {
  41. if (current_status_) {
  42. base::UmaHistogramEnumeration("PhoneHub.NotificationAccessSetup.LastStatus",
  43. *current_status_);
  44. }
  45. std::move(destructor_callback_).Run();
  46. }
  47. void NotificationAccessSetupOperation::NotifyNotificationStatusChanged(
  48. Status new_status) {
  49. base::UmaHistogramEnumeration("PhoneHub.NotificationAccessSetup.AllStatuses",
  50. new_status);
  51. if (new_status == Status::kCompletedSuccessfully) {
  52. base::UmaHistogramCustomTimes(
  53. "PhoneHub.NotificationAccessSetup.SuccessfulSetupDuration",
  54. base::TimeTicks::Now() - start_timestamp_,
  55. kSetupDurationHistogramMinTime, kSetupDurationHistogramMaxTime,
  56. kNumSetupDurationHistogramBuckets);
  57. }
  58. current_status_ = new_status;
  59. delegate_->OnNotificationStatusChange(new_status);
  60. }
  61. std::ostream& operator<<(std::ostream& stream,
  62. NotificationAccessSetupOperation::Status status) {
  63. switch (status) {
  64. case NotificationAccessSetupOperation::Status::kConnecting:
  65. stream << "[Connecting]";
  66. break;
  67. case NotificationAccessSetupOperation::Status::kTimedOutConnecting:
  68. stream << "[Timed out connecting]";
  69. break;
  70. case NotificationAccessSetupOperation::Status::kConnectionDisconnected:
  71. stream << "[Connection disconnected]";
  72. break;
  73. case NotificationAccessSetupOperation::Status::
  74. kSentMessageToPhoneAndWaitingForResponse:
  75. stream << "[Sent message to phone; waiting for response]";
  76. break;
  77. case NotificationAccessSetupOperation::Status::kCompletedSuccessfully:
  78. stream << "[Completed successfully]";
  79. break;
  80. case NotificationAccessSetupOperation::Status::
  81. kProhibitedFromProvidingAccess:
  82. stream << "[Prohibited from providing access]";
  83. break;
  84. }
  85. return stream;
  86. }
  87. } // namespace phonehub
  88. } // namespace ash