Commit 7b8f74c3 authored by Jimmy Gong's avatar Jimmy Gong Committed by Commit Bot

Implement NotificationAccessManager

- Responsible for sending requests for displaying the notification
  access setup UI to the user's phone.

Bug: 1106937
Test: chromeos_components_unittests

Change-Id: If00b19664b4dd6240558e48b4bf5bb45c98e2723
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2453511
Commit-Queue: Jimmy Gong <jimmyxgong@chromium.org>
Reviewed-by: default avatarKyle Horimoto <khorimoto@chromium.org>
Cr-Commit-Position: refs/heads/master@{#815448}
parent 4bbaf6c4
......@@ -30,7 +30,7 @@ NotificationAccessManager::AttemptNotificationSetup(
weak_ptr_factory_.GetWeakPtr(), operation_id)));
id_to_operation_map_.emplace(operation_id, operation.get());
OnSetupAttemptStarted();
OnSetupRequested();
return operation;
}
......@@ -70,8 +70,9 @@ void NotificationAccessManager::OnSetupOperationDeleted(int operation_id) {
return;
id_to_operation_map_.erase(it);
if (id_to_operation_map_.empty())
OnSetupAttemptEnded();
PA_LOG(INFO) << "Notification access setup operation has ended.";
}
} // namespace phonehub
......
......@@ -66,8 +66,7 @@ class NotificationAccessManager {
bool IsSetupOperationInProgress() const;
virtual void OnSetupAttemptStarted() {}
virtual void OnSetupAttemptEnded() {}
virtual void OnSetupRequested() {}
private:
friend class NotificationAccessManagerImplTest;
......
......@@ -5,6 +5,8 @@
#include "chromeos/components/phonehub/notification_access_manager_impl.h"
#include "chromeos/components/multidevice/logging/logging.h"
#include "chromeos/components/phonehub/connection_scheduler.h"
#include "chromeos/components/phonehub/message_sender.h"
#include "chromeos/components/phonehub/pref_names.h"
#include "components/prefs/pref_registry_simple.h"
#include "components/prefs/pref_service.h"
......@@ -19,10 +21,24 @@ void NotificationAccessManagerImpl::RegisterPrefs(
}
NotificationAccessManagerImpl::NotificationAccessManagerImpl(
PrefService* pref_service)
: pref_service_(pref_service) {}
PrefService* pref_service,
FeatureStatusProvider* feature_status_provider,
MessageSender* message_sender,
ConnectionScheduler* connection_scheduler)
: pref_service_(pref_service),
feature_status_provider_(feature_status_provider),
message_sender_(message_sender),
connection_scheduler_(connection_scheduler) {
DCHECK(feature_status_provider_);
DCHECK(message_sender_);
NotificationAccessManagerImpl::~NotificationAccessManagerImpl() = default;
current_feature_status_ = feature_status_provider_->GetStatus();
feature_status_provider_->AddObserver(this);
}
NotificationAccessManagerImpl::~NotificationAccessManagerImpl() {
feature_status_provider_->RemoveObserver(this);
}
bool NotificationAccessManagerImpl::HasAccessBeenGranted() const {
return pref_service_->GetBoolean(prefs::kNotificationAccessGranted);
......@@ -30,19 +46,84 @@ bool NotificationAccessManagerImpl::HasAccessBeenGranted() const {
void NotificationAccessManagerImpl::SetHasAccessBeenGrantedInternal(
bool has_access_been_granted) {
if (has_access_been_granted == HasAccessBeenGranted())
return;
PA_LOG(INFO) << "Notification access state has been set to: "
<< has_access_been_granted;
// TODO(jimmyxgong): Implement this stub function.
pref_service_->SetBoolean(prefs::kNotificationAccessGranted,
has_access_been_granted);
NotifyNotificationAccessChanged();
if (IsSetupOperationInProgress() && has_access_been_granted) {
SetNotificationSetupOperationStatus(
NotificationAccessSetupOperation::Status::kCompletedSuccessfully);
}
}
void NotificationAccessManagerImpl::OnSetupAttemptStarted() {
void NotificationAccessManagerImpl::OnSetupRequested() {
PA_LOG(INFO) << "Notification access setup flow started.";
// TODO(khorimoto): Attempt notification setup flow.
switch (feature_status_provider_->GetStatus()) {
// We're already connected, so request that the UI be shown on the phone.
case FeatureStatus::kEnabledAndConnected:
SendShowNotificationAccessSetupRequest();
break;
// We're already connecting, so wait until a connection succeeds before
// trying to send a message
case FeatureStatus::kEnabledAndConnecting:
break;
// We are not connected, so schedule a connection; once the
// connection succeeds, we'll send the message in OnFeatureStatusChanged().
case FeatureStatus::kEnabledButDisconnected:
connection_scheduler_->ScheduleConnectionNow();
break;
default:
NOTREACHED();
break;
}
}
void NotificationAccessManagerImpl::OnFeatureStatusChanged() {
if (!IsSetupOperationInProgress())
return;
const FeatureStatus previous_feature_status = current_feature_status_;
current_feature_status_ = feature_status_provider_->GetStatus();
if (previous_feature_status == current_feature_status_)
return;
// If we were previously connecting and could not establish a connection,
// send a timeout state.
if (previous_feature_status == FeatureStatus::kEnabledAndConnecting &&
current_feature_status_ != FeatureStatus::kEnabledAndConnected) {
SetNotificationSetupOperationStatus(
NotificationAccessSetupOperation::Status::kTimedOutConnecting);
return;
}
// If we were previously connected and are now no longer connected, send a
// connection disconnected state.
if (previous_feature_status == FeatureStatus::kEnabledAndConnected &&
current_feature_status_ != FeatureStatus::kEnabledAndConnected) {
SetNotificationSetupOperationStatus(
NotificationAccessSetupOperation::Status::kConnectionDisconnected);
return;
}
if (current_feature_status_ == FeatureStatus::kEnabledAndConnected) {
SendShowNotificationAccessSetupRequest();
return;
}
}
void NotificationAccessManagerImpl::OnSetupAttemptEnded() {
PA_LOG(INFO) << "Notification access setup flow ended.";
// TODO(khorimoto): Stop ongoing notification setup flow.
void NotificationAccessManagerImpl::SendShowNotificationAccessSetupRequest() {
message_sender_->SendShowNotificationAccessSetupRequest();
SetNotificationSetupOperationStatus(
NotificationAccessSetupOperation::Status::
kSentMessageToPhoneAndWaitingForResponse);
}
} // namespace phonehub
......
......@@ -7,32 +7,52 @@
#include "chromeos/components/phonehub/notification_access_manager.h"
#include "chromeos/components/phonehub/feature_status_provider.h"
class PrefRegistrySimple;
class PrefService;
namespace chromeos {
namespace phonehub {
class MessageSender;
class ConnectionScheduler;
// Implements NotificationAccessManager by persisting the last-known
// notification access value to user prefs.
// TODO(khorimoto): Currently HasAccessBeenGranted() always returns false. Have
// it return true once the phone has sent a message indicating that it has
// granted access.
class NotificationAccessManagerImpl : public NotificationAccessManager {
class NotificationAccessManagerImpl : public NotificationAccessManager,
public FeatureStatusProvider::Observer {
public:
static void RegisterPrefs(PrefRegistrySimple* registry);
explicit NotificationAccessManagerImpl(PrefService* pref_service);
explicit NotificationAccessManagerImpl(
PrefService* pref_service,
FeatureStatusProvider* feature_status_provider,
MessageSender* message_sender,
ConnectionScheduler* connection_scheduler);
~NotificationAccessManagerImpl() override;
private:
friend class NotificationAccessManagerImplTest;
// NotificationAccessManager:
bool HasAccessBeenGranted() const override;
void SetHasAccessBeenGrantedInternal(bool has_access_been_granted) override;
void OnSetupAttemptStarted() override;
void OnSetupAttemptEnded() override;
void OnSetupRequested() override;
// FeatureStatusProvider::Observer:
void OnFeatureStatusChanged() override;
void SendShowNotificationAccessSetupRequest();
FeatureStatus current_feature_status_;
PrefService* pref_service_;
FeatureStatusProvider* feature_status_provider_;
MessageSender* message_sender_;
ConnectionScheduler* connection_scheduler_;
};
} // namespace phonehub
......
......@@ -58,7 +58,11 @@ PhoneHubManagerImpl::PhoneHubManagerImpl(
do_not_disturb_controller_.get(),
message_sender_.get())),
notification_access_manager_(
std::make_unique<NotificationAccessManagerImpl>(pref_service)),
std::make_unique<NotificationAccessManagerImpl>(
pref_service,
feature_status_provider_.get(),
message_sender_.get(),
connection_scheduler_.get())),
notification_manager_(std::make_unique<NotificationManagerImpl>()),
onboarding_ui_tracker_(std::make_unique<OnboardingUiTrackerImpl>(
pref_service,
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment