OLD | NEW |
(Empty) | |
| 1 // Copyright 2017 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 "chrome/browser/chromeos/arc/notification/arc_provision_notification_se
rvice.h" |
| 6 |
| 7 #include <utility> |
| 8 |
| 9 #include "ash/system/devicetype_utils.h" |
| 10 #include "base/memory/ptr_util.h" |
| 11 #include "base/strings/utf_string_conversions.h" |
| 12 #include "chrome/browser/chromeos/arc/arc_util.h" |
| 13 #include "chrome/grit/component_extension_resources.h" |
| 14 #include "chrome/grit/generated_resources.h" |
| 15 #include "chrome/grit/theme_resources.h" |
| 16 #include "components/signin/core/account_id/account_id.h" |
| 17 #include "components/user_manager/user.h" |
| 18 #include "components/user_manager/user_manager.h" |
| 19 #include "ui/base/l10n/l10n_util.h" |
| 20 #include "ui/base/resource/resource_bundle.h" |
| 21 #include "ui/gfx/image/image.h" |
| 22 #include "ui/message_center/message_center.h" |
| 23 #include "ui/message_center/notification.h" |
| 24 #include "ui/message_center/notification_types.h" |
| 25 #include "ui/message_center/notifier_settings.h" |
| 26 #include "url/gurl.h" |
| 27 |
| 28 namespace arc { |
| 29 |
| 30 namespace { |
| 31 |
| 32 constexpr char kManagedProvisionNotificationId[] = "arc_managed_provision"; |
| 33 constexpr char kManagedProvisionNotifierId[] = "arc_managed_provision"; |
| 34 constexpr char kManagedProvisionDisplaySource[] = "arc_managed_provision"; |
| 35 |
| 36 class DelegateImpl : public ArcProvisionNotificationService::Delegate { |
| 37 public: |
| 38 void ShowManagedProvisionNotification() override; |
| 39 void RemoveManagedProvisionNotification() override; |
| 40 }; |
| 41 |
| 42 void DelegateImpl::ShowManagedProvisionNotification() { |
| 43 message_center::NotifierId notifier_id( |
| 44 message_center::NotifierId::SYSTEM_COMPONENT, |
| 45 kManagedProvisionNotifierId); |
| 46 const AccountId& account_id = |
| 47 user_manager::UserManager::Get()->GetPrimaryUser()->GetAccountId(); |
| 48 notifier_id.profile_id = account_id.GetUserEmail(); |
| 49 message_center::RichNotificationData optional_fields; |
| 50 optional_fields.clickable = false; |
| 51 optional_fields.never_timeout = true; |
| 52 |
| 53 message_center::MessageCenter::Get()->AddNotification( |
| 54 base::MakeUnique<message_center::Notification>( |
| 55 message_center::NOTIFICATION_TYPE_SIMPLE, |
| 56 kManagedProvisionNotificationId, |
| 57 l10n_util::GetStringUTF16( |
| 58 IDS_ARC_MANAGED_PROVISION_NOTIFICATION_TITLE), |
| 59 l10n_util::GetStringFUTF16( |
| 60 IDS_ARC_MANAGED_PROVISION_NOTIFICATION_MESSAGE, |
| 61 ash::GetChromeOSDeviceName()), |
| 62 gfx::Image(ui::ResourceBundle::GetSharedInstance().GetImageNamed( |
| 63 IDR_ARC_PLAY_STORE_OPTIN_IN_PROGRESS_NOTIFICATION)), |
| 64 base::UTF8ToUTF16(kManagedProvisionDisplaySource), GURL(), |
| 65 notifier_id, optional_fields, nullptr)); |
| 66 } |
| 67 |
| 68 void DelegateImpl::RemoveManagedProvisionNotification() { |
| 69 message_center::MessageCenter::Get()->RemoveNotification( |
| 70 kManagedProvisionNotificationId, false); |
| 71 } |
| 72 |
| 73 } // namespace |
| 74 |
| 75 ArcProvisionNotificationService::Delegate::Delegate() = default; |
| 76 |
| 77 ArcProvisionNotificationService::Delegate::~Delegate() = default; |
| 78 |
| 79 ArcProvisionNotificationService::ArcProvisionNotificationService( |
| 80 ArcBridgeService* bridge_service) |
| 81 : ArcProvisionNotificationService(bridge_service, |
| 82 base::MakeUnique<DelegateImpl>()) {} |
| 83 |
| 84 ArcProvisionNotificationService::~ArcProvisionNotificationService() { |
| 85 // Make sure no notification is left being shown. |
| 86 delegate_->RemoveManagedProvisionNotification(); |
| 87 ArcSessionManager::Get()->RemoveObserver(this); |
| 88 } |
| 89 |
| 90 // static |
| 91 std::unique_ptr<ArcProvisionNotificationService> |
| 92 ArcProvisionNotificationService::CreateForTesting( |
| 93 ArcBridgeService* bridge_service, |
| 94 std::unique_ptr<Delegate> delegate) { |
| 95 return base::WrapUnique<ArcProvisionNotificationService>( |
| 96 new ArcProvisionNotificationService(bridge_service, std::move(delegate))); |
| 97 } |
| 98 |
| 99 ArcProvisionNotificationService::ArcProvisionNotificationService( |
| 100 ArcBridgeService* bridge_service, |
| 101 std::unique_ptr<Delegate> delegate) |
| 102 : ArcService(bridge_service), delegate_(std::move(delegate)) { |
| 103 ArcSessionManager::Get()->AddObserver(this); |
| 104 } |
| 105 |
| 106 void ArcProvisionNotificationService::OnArcPlayStoreEnabledChanged( |
| 107 bool enabled) { |
| 108 if (!enabled) { |
| 109 // Make sure no notification is shown after ARC gets disabled. |
| 110 delegate_->RemoveManagedProvisionNotification(); |
| 111 } |
| 112 } |
| 113 |
| 114 void ArcProvisionNotificationService::OnArcOptInManagementCheckStarted() { |
| 115 // This observer is notified at an early phase of the opt-in flow, so start |
| 116 // showing the notification if the opt-in flow happens silently (due to the |
| 117 // managed prefs), or ensure that no notification is shown otherwise. |
| 118 const Profile* const profile = ArcSessionManager::Get()->profile(); |
| 119 if (IsArcPlayStoreEnabledPreferenceManagedForProfile(profile) && |
| 120 AreArcAllOptInPreferencesManagedForProfile(profile)) { |
| 121 delegate_->ShowManagedProvisionNotification(); |
| 122 } else { |
| 123 delegate_->RemoveManagedProvisionNotification(); |
| 124 } |
| 125 } |
| 126 |
| 127 void ArcProvisionNotificationService::OnArcInitialStart() { |
| 128 // The opt-in flow finished successfully, so remove the notification. |
| 129 delegate_->RemoveManagedProvisionNotification(); |
| 130 } |
| 131 |
| 132 void ArcProvisionNotificationService::OnArcSessionStopped( |
| 133 ArcStopReason stop_reason) { |
| 134 // One of the reasons of ARC being stopped is a failure of the opt-in flow. |
| 135 // Therefore remove the notification if it is shown. |
| 136 delegate_->RemoveManagedProvisionNotification(); |
| 137 } |
| 138 |
| 139 void ArcProvisionNotificationService::OnArcErrorShowRequested( |
| 140 ArcSupportHost::Error error) { |
| 141 // If an error happens during the opt-in flow, then the provision fails, and |
| 142 // the notification should be therefore removed if it is shown. Do this here |
| 143 // unconditionally as there should be no notification displayed otherwise |
| 144 // anyway. |
| 145 delegate_->RemoveManagedProvisionNotification(); |
| 146 } |
| 147 |
| 148 } // namespace arc |
OLD | NEW |