Chromium Code Reviews| Index: chrome/browser/chromeos/arc/notification/arc_provision_notification_service.cc |
| diff --git a/chrome/browser/chromeos/arc/notification/arc_provision_notification_service.cc b/chrome/browser/chromeos/arc/notification/arc_provision_notification_service.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..4ac25893e1f34c3de781a0aa8f898872d9a14b6c |
| --- /dev/null |
| +++ b/chrome/browser/chromeos/arc/notification/arc_provision_notification_service.cc |
| @@ -0,0 +1,131 @@ |
| +// Copyright 2017 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +#include "chrome/browser/chromeos/arc/notification/arc_provision_notification_service.h" |
| + |
| +#include "ash/common/system/chromeos/devicetype_utils.h" |
| +#include "base/memory/ptr_util.h" |
| +#include "base/strings/utf_string_conversions.h" |
| +#include "chrome/browser/chromeos/arc/arc_util.h" |
| +#include "chrome/grit/generated_resources.h" |
| +#include "chrome/grit/theme_resources.h" |
| +#include "components/signin/core/account_id/account_id.h" |
| +#include "components/user_manager/user.h" |
| +#include "components/user_manager/user_manager.h" |
| +#include "ui/base/l10n/l10n_util.h" |
| +#include "ui/base/resource/resource_bundle.h" |
| +#include "ui/chromeos/resources/grit/ui_chromeos_resources.h" |
| +#include "ui/gfx/image/image.h" |
| +#include "ui/message_center/message_center.h" |
| +#include "ui/message_center/notification.h" |
| +#include "ui/message_center/notification_types.h" |
| +#include "ui/message_center/notifier_settings.h" |
| +#include "url/gurl.h" |
| + |
| +namespace arc { |
| + |
| +namespace { |
| + |
| +const char kManagedProvisionNotificationId[] = "arc_managed_provision"; |
|
Luis Héctor Chávez
2017/03/13 15:50:41
nit: constexpr
emaxx
2017/03/16 18:32:12
Done.
|
| +const char kManagedProvisionNotifierId[] = "arc_managed_provision"; |
| +const char kManagedProvisionDisplaySource[] = "arc_managed_provision"; |
| + |
| +class DelegateImpl : public ArcProvisionNotificationService::Delegate { |
| + public: |
| + void ShowManagedProvisionNotification() override; |
| + void RemoveManagedProvisionNotification() override; |
| +}; |
| + |
| +void DelegateImpl::ShowManagedProvisionNotification() { |
| + message_center::NotifierId notifier_id( |
| + message_center::NotifierId::SYSTEM_COMPONENT, |
| + kManagedProvisionNotifierId); |
| + const AccountId& account_id = |
| + user_manager::UserManager::Get()->GetPrimaryUser()->GetAccountId(); |
| + notifier_id.profile_id = account_id.GetUserEmail(); |
| + message_center::RichNotificationData optional_fields; |
| + optional_fields.clickable = false; |
| + optional_fields.never_timeout = true; |
| + |
| + message_center::MessageCenter::Get()->AddNotification( |
| + base::MakeUnique<message_center::Notification>( |
| + message_center::NOTIFICATION_TYPE_SIMPLE, |
| + kManagedProvisionNotificationId, |
| + l10n_util::GetStringUTF16( |
| + IDS_ARC_MANAGED_PROVISION_NOTIFICATION_TITLE), |
| + l10n_util::GetStringFUTF16( |
| + IDS_ARC_MANAGED_PROVISION_NOTIFICATION_MESSAGE, |
| + ash::GetChromeOSDeviceName()), |
| + gfx::Image(ui::ResourceBundle::GetSharedInstance().GetImageNamed( |
| + IDR_ARC_PLAY_STORE_NOTIFICATION)), |
| + base::UTF8ToUTF16(kManagedProvisionDisplaySource), GURL(), |
| + notifier_id, optional_fields, nullptr)); |
| +} |
| + |
| +void DelegateImpl::RemoveManagedProvisionNotification() { |
| + message_center::MessageCenter::Get()->RemoveNotification( |
| + kManagedProvisionNotificationId, false); |
| +} |
| + |
| +} // namespace |
| + |
| +ArcProvisionNotificationService::ArcProvisionNotificationService( |
| + ArcBridgeService* bridge_service) |
| + : ArcProvisionNotificationService(bridge_service, |
| + base::MakeUnique<DelegateImpl>()) {} |
| + |
| +ArcProvisionNotificationService::ArcProvisionNotificationService( |
| + ArcBridgeService* bridge_service, |
| + std::unique_ptr<Delegate> delegate) |
| + : ArcService(bridge_service), delegate_(std::move(delegate)) { |
| + ArcSessionManager::Get()->AddObserver(this); |
| +} |
| + |
| +ArcProvisionNotificationService::~ArcProvisionNotificationService() { |
| + // Make sure no notification is left being shown. |
| + delegate_->RemoveManagedProvisionNotification(); |
| + ArcSessionManager::Get()->RemoveObserver(this); |
| +} |
| + |
| +void ArcProvisionNotificationService::OnArcPlayStoreEnabledChanged( |
| + bool enabled) { |
| + if (!enabled) { |
| + // Make sure no notification is shown after ARC gets disabled. |
| + delegate_->RemoveManagedProvisionNotification(); |
| + } |
| +} |
| + |
| +void ArcProvisionNotificationService::OnArcStartOptInAndroidManagementCheck() { |
| + // This observer is notified at an early phase of the OptIn flow, so start |
| + // showing the notification if the OptIn flow happens silently (due to the |
| + // managed prefs), or ensure that no notification is shown otherwise. |
| + const Profile* const profile = ArcSessionManager::Get()->profile(); |
| + if (IsArcPlayStoreEnabledPreferenceManagedForProfile(profile) && |
| + AreArcAllOptInPreferencesManagedForProfile(profile)) { |
| + delegate_->ShowManagedProvisionNotification(); |
| + } else { |
| + delegate_->RemoveManagedProvisionNotification(); |
| + } |
| +} |
| + |
| +void ArcProvisionNotificationService::OnArcInitialStart() { |
| + // The OptIn flow finished successfully, so remove the notification. |
| + delegate_->RemoveManagedProvisionNotification(); |
| +} |
| + |
| +void ArcProvisionNotificationService::OnArcSessionStopped( |
| + ArcStopReason stop_reason) { |
| + // One of the reasons of ARC being stopped is a failure of the opt-in flow. |
| + // Therefore remove the notification if it is shown. |
| + delegate_->RemoveManagedProvisionNotification(); |
| +} |
| + |
| +void ArcProvisionNotificationService::OnArcSupportHostErrorShown( |
| + ArcSupportHost::Error error) { |
| + // After the error is displayed, there is no reason to keep showing the |
| + // notification, if one was shown. |
| + delegate_->RemoveManagedProvisionNotification(); |
| +} |
| + |
| +} // namespace arc |