File: arc_vm_data_migration_notifier.cc

package info (click to toggle)
chromium 138.0.7204.183-1~deb12u1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm-proposed-updates
  • size: 6,080,960 kB
  • sloc: cpp: 34,937,079; ansic: 7,176,967; javascript: 4,110,704; python: 1,419,954; asm: 946,768; xml: 739,971; pascal: 187,324; sh: 89,623; perl: 88,663; objc: 79,944; sql: 50,304; cs: 41,786; fortran: 24,137; makefile: 21,811; php: 13,980; tcl: 13,166; yacc: 8,925; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (199 lines) | stat: -rw-r--r-- 7,729 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
// Copyright 2022 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "chrome/browser/ash/arc/notification/arc_vm_data_migration_notifier.h"

#include "ash/constants/notifier_catalogs.h"
#include "ash/public/cpp/notification_utils.h"
#include "ash/resources/vector_icons/vector_icons.h"
#include "ash/strings/grit/ash_strings.h"
#include "base/metrics/histogram_functions.h"
#include "base/time/time.h"
#include "chrome/browser/ash/arc/arc_util.h"
#include "chrome/browser/ash/arc/policy/arc_policy_util.h"
#include "chrome/browser/lifetime/application_lifetime.h"
#include "chrome/browser/notifications/notification_display_service.h"
#include "chrome/browser/notifications/notification_display_service_factory.h"
#include "chrome/browser/ui/ash/arc/arc_vm_data_migration_confirmation_dialog.h"
#include "chromeos/ash/experiences/arc/arc_features.h"
#include "chromeos/ash/experiences/arc/arc_prefs.h"
#include "chromeos/ash/experiences/arc/arc_util.h"
#include "chromeos/ash/experiences/arc/session/arc_vm_data_migration_status.h"
#include "components/prefs/pref_service.h"
#include "components/strings/grit/components_strings.h"
#include "ui/base/l10n/l10n_util.h"
#include "ui/message_center/public/cpp/notification.h"
#include "ui/message_center/public/cpp/notification_delegate.h"
#include "ui/message_center/public/cpp/notification_types.h"
#include "ui/message_center/public/cpp/notifier_id.h"

namespace arc {

namespace {

constexpr char kNotifierId[] = "arc_vm_data_migration_notifier";
constexpr char kNotificationId[] = "arc_vm_data_migration_notification";

bool ShouldShowNotification(Profile* profile) {
  switch (GetArcVmDataMigrationStatus(profile->GetPrefs())) {
    case ArcVmDataMigrationStatus::kUnnotified:
    case ArcVmDataMigrationStatus::kNotified:
    case ArcVmDataMigrationStatus::kConfirmed:
      return !policy_util::IsAccountManaged(profile) ||
             GetArcVmDataMigrationStrategy(profile->GetPrefs()) ==
                 ArcVmDataMigrationStrategy::kPrompt;
    case ArcVmDataMigrationStatus::kStarted:
    case ArcVmDataMigrationStatus::kFinished:
      return false;
  }
}

void ReportNotificationShownForTheFirstTime() {
  base::UmaHistogramBoolean(
      "Arc.VmDataMigration.NotificationShownForTheFirstTime", true);
}

void ReportNotificationShown(int days_until_deadline) {
  base::UmaHistogramExactLinear(
      "Arc.VmDataMigration.RemainingDays.NotificationShown",
      days_until_deadline, kArcVmDataMigrationNumberOfDismissibleDays);
}

}  // namespace

ArcVmDataMigrationNotifier::ArcVmDataMigrationNotifier(Profile* profile)
    : profile_(profile) {
  DCHECK(ArcSessionManager::Get());
  arc_session_observation_.Observe(ArcSessionManager::Get());
}

ArcVmDataMigrationNotifier::~ArcVmDataMigrationNotifier() = default;

void ArcVmDataMigrationNotifier::OnArcStarted() {
  // Show a notification only when the migration is enabled.
  if (!base::FeatureList::IsEnabled(kEnableArcVmDataMigration))
    return;

  // Report the migration status at the beginning of each ARC session.
  // The status might have been changed to kFinished by ArcSessionManager in its
  // initialization step when there is no Android /data to migrate.
  // The status might be changed to kNotified within this function call when the
  // notification is shown.
  base::UmaHistogramEnumeration(
      GetHistogramNameByUserType(
          kArcVmDataMigrationStatusOnArcStartedHistogramName, profile_),
      GetArcVmDataMigrationStatus(profile_->GetPrefs()));

  // Do not show a notification if virtio-blk /data is forcibly enabled, in
  // which case the migration is not needed.
  if (base::FeatureList::IsEnabled(kEnableVirtioBlkForData))
    return;

  if (!ShouldShowNotification(profile_)) {
    return;
  }

  if (GetArcVmDataMigrationStatus(profile_->GetPrefs()) ==
      ArcVmDataMigrationStatus::kUnnotified) {
    ReportNotificationShownForTheFirstTime();
    profile_->GetPrefs()->SetTime(
        prefs::kArcVmDataMigrationNotificationFirstShownTime,
        base::Time::Now());
  }
  SetArcVmDataMigrationStatus(profile_->GetPrefs(),
                              ArcVmDataMigrationStatus::kNotified);
  ShowNotification();
}

void ArcVmDataMigrationNotifier::OnArcSessionStopped(ArcStopReason reason) {
  CloseNotification();
}

void ArcVmDataMigrationNotifier::OnArcSessionBlockedByArcVmDataMigration(
    bool auto_resume_enabled) {
  if (auto_resume_enabled) {
    // No need to show a notification.
    return;
  }
  LOG(WARNING) << "Showing a non-dismissible notification for ARCVM /data "
                  "migration, because auto-resume is disabled";
  // TODO(b/258278176): Implement appropriate UI.
  base::UmaHistogramBoolean("Arc.VmDataMigration.ResumeNotificationShown",
                            true);
  ShowNotification();
}

void ArcVmDataMigrationNotifier::ShowNotification() {
  const int days_until_deadline =
      GetDaysUntilArcVmDataMigrationDeadline(profile_->GetPrefs());
  ReportNotificationShown(days_until_deadline);

  message_center::Notification notification = ash::CreateSystemNotification(
      message_center::NOTIFICATION_TYPE_SIMPLE, kNotificationId,
      l10n_util::GetStringUTF16(IDS_ARC_VM_DATA_MIGRATION_NOTIFICATION_TITLE),
      l10n_util::GetPluralStringFUTF16(
          IDS_ARC_VM_DATA_MIGRATION_NOTIFICATION_DAYS_UNTIL_DEADLINE,
          days_until_deadline),
      l10n_util::GetStringUTF16(IDS_ASH_MESSAGE_CENTER_SYSTEM_APP_NAME),
      GURL() /* origin_url */,
      message_center::NotifierId(
          message_center::NotifierType::SYSTEM_COMPONENT, kNotifierId,
          ash::NotificationCatalogName::kArcVmDataMigration),
      message_center::RichNotificationData() /* optional_fields */,
      base::MakeRefCounted<message_center::HandleNotificationClickDelegate>(
          base::BindRepeating(
              &ArcVmDataMigrationNotifier::OnNotificationClicked,
              weak_ptr_factory_.GetWeakPtr())),
      ash::kSystemMenuUpdateIcon,
      message_center::SystemNotificationWarningLevel::NORMAL);
  notification.set_buttons(
      {message_center::ButtonInfo(l10n_util::GetStringUTF16(
          IDS_ARC_VM_DATA_MIGRATION_NOTIFICATION_ACCEPT_BUTTON_LABEL))});

  // Set the highest (system) priority.
  notification.SetSystemPriority();
  // Set no timeout so that the notification never disappears spontaneously.
  notification.set_never_timeout(true);

  NotificationDisplayServiceFactory::GetForProfile(profile_)->Display(
      NotificationHandler::Type::TRANSIENT, notification,
      nullptr /* metadata */);
}

void ArcVmDataMigrationNotifier::CloseNotification() {
  auto* notification_display_service =
      NotificationDisplayServiceFactory::GetForProfile(profile_);
  if (notification_display_service) {
    notification_display_service->Close(NotificationHandler::Type::TRANSIENT,
                                        kNotificationId);
  }
}

void ArcVmDataMigrationNotifier::OnNotificationClicked(
    std::optional<int> button_index) {
  if (!button_index) {
    // Notification message body is clicked.
    return;
  }

  CloseNotification();

  ShowArcVmDataMigrationConfirmationDialog(
      profile_->GetPrefs(),
      base::BindOnce(&ArcVmDataMigrationNotifier::OnRestartAccepted,
                     weak_ptr_factory_.GetWeakPtr()));
}

void ArcVmDataMigrationNotifier::OnRestartAccepted(bool accepted) {
  if (accepted) {
    auto* prefs = profile_->GetPrefs();
    if (GetArcVmDataMigrationStatus(prefs) !=
        ArcVmDataMigrationStatus::kStarted) {
      SetArcVmDataMigrationStatus(prefs, ArcVmDataMigrationStatus::kConfirmed);
    }
    chrome::AttemptRestart();
  }
}

}  // namespace arc