Commit 970e36a8 authored by Paula Vidas's avatar Paula Vidas Committed by Commit Bot

[SyncInvalidations] Clear FCM token on StopListeningPermanently().

StopListeningPermanently() gets called during sign-out. The FCM token
needs to be cleared so that the change can be propagated to device info.
Empty token in device info signals that the device is not listening to
invalidations.

Change-Id: I6d0a4e2007af6d7457cafed265877ec9810cb4f7
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2465844
Commit-Queue: Paula Vidas <paulavidas@google.com>
Reviewed-by: default avatarMarc Treib <treib@chromium.org>
Reviewed-by: default avatarRushan Suleymanov <rushans@google.com>
Cr-Commit-Position: refs/heads/master@{#816173}
parent ad71401e
......@@ -59,6 +59,10 @@ void FCMHandler::StopListeningPermanently() {
if (instance_id_driver_->ExistsInstanceID(app_id_)) {
instance_id_driver_->GetInstanceID(app_id_)->DeleteID(
/*callback=*/base::DoNothing());
fcm_registration_token_.clear();
for (FCMRegistrationTokenObserver& token_observer : token_observers_) {
token_observer.OnFCMRegistrationTokenChanged();
}
}
StopListening();
}
......
......@@ -51,8 +51,8 @@ class FCMHandler : public gcm::GCMAppHandler {
// shutdown.
void StopListening();
// Stop handling incoming invalidations and delete Instance ID. This method
// gets called during sign-out.
// Stop handling incoming invalidations and delete Instance ID. It clears the
// FCM registration token. This method gets called during sign-out.
void StopListeningPermanently();
// Returns if the handler is listening for incoming invalidations.
......@@ -68,7 +68,7 @@ class FCMHandler : public gcm::GCMAppHandler {
void RemoveTokenObserver(FCMRegistrationTokenObserver* observer);
// Used to get an obtained FCM token. Returns empty string if it hasn't
// received yet.
// been received yet, or if the handler has stopped listening permanently.
const std::string& GetFCMRegistrationToken() const;
// GCMAppHandler overrides.
......
......@@ -27,6 +27,7 @@ using instance_id::InstanceID;
using testing::_;
using testing::Invoke;
using testing::NiceMock;
using testing::Return;
using testing::WithArg;
namespace syncer {
......@@ -214,5 +215,27 @@ TEST_F(FCMHandlerTest, ShouldScheduleTokenValidationAndNotActOnSameToken) {
fcm_handler_.RemoveTokenObserver(&mock_token_observer);
}
TEST_F(FCMHandlerTest, ShouldClearTokenOnStopListeningPermanently) {
// Check that the handler gets the token through GetToken.
EXPECT_CALL(mock_instance_id_, GetToken(_, _, _, _, _, _))
.WillOnce(WithArg<5>(Invoke([](InstanceID::GetTokenCallback callback) {
std::move(callback).Run("token", InstanceID::Result::SUCCESS);
})));
fcm_handler_.StartListening();
NiceMock<MockTokenObserver> mock_token_observer;
fcm_handler_.AddTokenObserver(&mock_token_observer);
EXPECT_CALL(mock_instance_id_driver_,
ExistsInstanceID(kSyncInvalidationsAppId))
.WillOnce(Return(true));
// Token should be cleared when StopListeningPermanently() is called.
EXPECT_CALL(mock_token_observer, OnFCMRegistrationTokenChanged());
fcm_handler_.StopListeningPermanently();
EXPECT_EQ("", fcm_handler_.GetFCMRegistrationToken());
fcm_handler_.RemoveTokenObserver(&mock_token_observer);
}
} // namespace
} // namespace syncer
......@@ -42,7 +42,7 @@ class SyncInvalidationsService : public KeyedService {
virtual void RemoveTokenObserver(FCMRegistrationTokenObserver* observer) = 0;
// Used to get an obtained FCM token. Returns empty string if it hasn't been
// received yet.
// received yet, or if the device has stopped listening to invalidations.
virtual const std::string& GetFCMRegistrationToken() const = 0;
// Set the interested data types change handler. |handler| can be nullptr to
......
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