Commit dfc85f6b authored by Anthony Vallee-Dubois's avatar Anthony Vallee-Dubois Committed by Commit Bot

Refactor FCMInvalidationService

FCMInvalidationService currently does 2 things: it observes changes to
its IdentityProvider and maintains invalidations-related state. The seam
between those responsibilities is pretty clear.

In order to support invalidations for Machine-Level cloud policies on
desktop browsers, we need to use the invalidations-related bits of
FCMInvalidationService in a context where there is no IdentityProvider.
This is different than the ChromeOS case where there's no Profile but a
Device Level Robot Account is available (See
AffiliatedInvalidationServiceProvider). Desktop browsers have no such
robot account, therefore no IdentityProvider in the context of Machine-
Level policies.

This CL splits off the profile-agnostic parts of FCMInvalidationService
into their own base class: FCMInvalidationServiceBase. This refactor
should be a no-op, except it will allow us to add a second implementation
of FCMInvalidationServiceBase that doesn't care about the presence of an
account.

The linked bug and the design doc (go/cbcm-machine-policy-invalidations)
contain more context about this change.

Bug: 1019791
Change-Id: Ib659aaa7d52f18797c67eaa9faae6d78d746942c
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/1910423Reviewed-by: default avatarMarc Treib <treib@chromium.org>
Reviewed-by: default avatarTatiana Gornak <melandory@chromium.org>
Commit-Queue: anthonyvd <anthonyvd@chromium.org>
Cr-Commit-Position: refs/heads/master@{#714938}
parent 326f43ed
...@@ -27,6 +27,8 @@ static_library("impl") { ...@@ -27,6 +27,8 @@ static_library("impl") {
"fcm_invalidation_listener.h", "fcm_invalidation_listener.h",
"fcm_invalidation_service.cc", "fcm_invalidation_service.cc",
"fcm_invalidation_service.h", "fcm_invalidation_service.h",
"fcm_invalidation_service_base.cc",
"fcm_invalidation_service_base.h",
"fcm_network_handler.cc", "fcm_network_handler.cc",
"fcm_network_handler.h", "fcm_network_handler.h",
"fcm_sync_network_channel.cc", "fcm_sync_network_channel.cc",
......
...@@ -6,48 +6,16 @@ ...@@ -6,48 +6,16 @@
#define COMPONENTS_INVALIDATION_IMPL_FCM_INVALIDATION_SERVICE_H_ #define COMPONENTS_INVALIDATION_IMPL_FCM_INVALIDATION_SERVICE_H_
#include "base/macros.h" #include "base/macros.h"
#include "base/time/time.h" #include "components/invalidation/impl/fcm_invalidation_service_base.h"
#include "components/gcm_driver/instance_id/instance_id.h"
#include "components/invalidation/impl/fcm_invalidation_listener.h"
#include "components/invalidation/impl/invalidation_listener.h"
#include "components/invalidation/impl/invalidation_logger.h"
#include "components/invalidation/impl/invalidator_registrar_with_memory.h"
#include "components/invalidation/public/identity_provider.h" #include "components/invalidation/public/identity_provider.h"
#include "components/invalidation/public/invalidation_handler.h"
#include "components/invalidation/public/invalidation_service.h"
#include "components/keyed_service/core/keyed_service.h"
#include "net/base/backoff_entry.h"
class PrefService;
class PrefRegistrySimple;
namespace instance_id {
class InstanceIDDriver;
}
namespace syncer {
class FCMNetworkHandler;
class PerUserTopicRegistrationManager;
} // namespace syncer
namespace invalidation { namespace invalidation {
using FCMNetworkHandlerCallback = // This concrete implementation of FCMInvalidationServiceBase starts the
base::RepeatingCallback<std::unique_ptr<syncer::FCMNetworkHandler>( // invalidation service machinery once an account is signed in and conversely
const std::string& sender_id, // stops it when the account is signed out.
const std::string& app_id)>; class FCMInvalidationService : public FCMInvalidationServiceBase,
public IdentityProvider::Observer {
using PerUserTopicRegistrationManagerCallback =
base::RepeatingCallback<std::unique_ptr<
syncer::PerUserTopicRegistrationManager>(const std::string& project_id,
bool migrate_prefs)>;
// This InvalidationService wraps the C++ Invalidation Client (FCM) library.
// It provides invalidations for desktop platforms (Win, Mac, Linux).
class FCMInvalidationService
: public InvalidationService,
public IdentityProvider::Observer,
public syncer::FCMInvalidationListener::Delegate {
public: public:
FCMInvalidationService(IdentityProvider* identity_provider, FCMInvalidationService(IdentityProvider* identity_provider,
FCMNetworkHandlerCallback fcm_network_handler_callback, FCMNetworkHandlerCallback fcm_network_handler_callback,
...@@ -58,21 +26,8 @@ class FCMInvalidationService ...@@ -58,21 +26,8 @@ class FCMInvalidationService
const std::string& sender_id = {}); const std::string& sender_id = {});
~FCMInvalidationService() override; ~FCMInvalidationService() override;
void Init(); void Init() override;
static void RegisterPrefs(PrefRegistrySimple* registry);
// InvalidationService implementation.
// It is an error to have registered handlers when the service is destroyed.
void RegisterInvalidationHandler(
syncer::InvalidationHandler* handler) override;
bool UpdateRegisteredInvalidationIds(syncer::InvalidationHandler* handler,
const syncer::ObjectIdSet& ids) override;
void UnregisterInvalidationHandler(
syncer::InvalidationHandler* handler) override;
syncer::InvalidatorState GetInvalidatorState() const override;
std::string GetInvalidatorClientId() const override;
InvalidationLogger* GetInvalidationLogger() override;
void RequestDetailedStatus( void RequestDetailedStatus(
base::RepeatingCallback<void(const base::DictionaryValue&)> caller) base::RepeatingCallback<void(const base::DictionaryValue&)> caller)
const override; const override;
...@@ -82,76 +37,26 @@ class FCMInvalidationService ...@@ -82,76 +37,26 @@ class FCMInvalidationService
void OnActiveAccountLogin() override; void OnActiveAccountLogin() override;
void OnActiveAccountLogout() override; void OnActiveAccountLogout() override;
// syncer::FCMInvalidationListener::Delegate implementation.
void OnInvalidate(
const syncer::TopicInvalidationMap& invalidation_map) override;
void OnInvalidatorStateChange(syncer::InvalidatorState state) override;
protected: protected:
friend class FCMInvalidationServiceTestDelegate; friend class FCMInvalidationServiceTestDelegate;
// Initializes with an injected invalidator. base::DictionaryValue CollectDebugData() const override;
void InitForTest(
std::unique_ptr<syncer::FCMInvalidationListener> invalidation_listener);
private: private:
struct Diagnostics { struct Diagnostics {
Diagnostics();
// Collect all the internal variables in a single readable dictionary.
base::DictionaryValue CollectDebugData() const;
base::Time active_account_login; base::Time active_account_login;
base::Time active_account_token_updated; base::Time active_account_token_updated;
base::Time active_account_logged_out; base::Time active_account_logged_out;
base::Time instance_id_requested;
base::Time instance_id_received;
base::Time service_was_stopped;
base::Time service_was_started;
bool was_already_started_on_login = false; bool was_already_started_on_login = false;
bool was_ready_to_start_on_login = false; bool was_ready_to_start_on_login = false;
CoreAccountId active_account_id; CoreAccountId active_account_id;
}; };
bool IsReadyToStart(); bool IsReadyToStart();
bool IsStarted() const;
void StartInvalidator();
void StopInvalidator();
void PopulateClientID();
void ResetClientID();
void OnInstanceIdRecieved(const std::string& id);
void OnDeleteIDCompleted(instance_id::InstanceID::Result);
void DoUpdateRegisteredIdsIfNeeded();
const std::string GetApplicationName();
const std::string sender_id_;
syncer::InvalidatorRegistrarWithMemory invalidator_registrar_;
// The invalidation logger object we use to record state changes
// and invalidations.
InvalidationLogger logger_;
FCMNetworkHandlerCallback fcm_network_handler_callback_;
PerUserTopicRegistrationManagerCallback
per_user_topic_registration_manager_callback_;
instance_id::InstanceIDDriver* const instance_id_driver_;
std::string client_id_;
IdentityProvider* const identity_provider_; IdentityProvider* const identity_provider_;
PrefService* const pref_service_;
bool update_was_requested_ = false;
Diagnostics diagnostic_info_; Diagnostics diagnostic_info_;
// The invalidation listener.
std::unique_ptr<syncer::FCMInvalidationListener> invalidation_listener_;
SEQUENCE_CHECKER(sequence_checker_); SEQUENCE_CHECKER(sequence_checker_);
DISALLOW_COPY_AND_ASSIGN(FCMInvalidationService); DISALLOW_COPY_AND_ASSIGN(FCMInvalidationService);
......
This diff is collapsed.
// Copyright 2019 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.
#ifndef COMPONENTS_INVALIDATION_IMPL_FCM_INVALIDATION_SERVICE_BASE_H_
#define COMPONENTS_INVALIDATION_IMPL_FCM_INVALIDATION_SERVICE_BASE_H_
#include "base/macros.h"
#include "base/time/time.h"
#include "components/gcm_driver/instance_id/instance_id.h"
#include "components/invalidation/impl/fcm_invalidation_listener.h"
#include "components/invalidation/impl/invalidation_listener.h"
#include "components/invalidation/impl/invalidation_logger.h"
#include "components/invalidation/impl/invalidator_registrar_with_memory.h"
#include "components/invalidation/public/invalidation_handler.h"
#include "components/invalidation/public/invalidation_service.h"
class PrefService;
class PrefRegistrySimple;
namespace instance_id {
class InstanceIDDriver;
}
namespace syncer {
class FCMNetworkHandler;
class PerUserTopicRegistrationManager;
} // namespace syncer
namespace invalidation {
using FCMNetworkHandlerCallback =
base::RepeatingCallback<std::unique_ptr<syncer::FCMNetworkHandler>(
const std::string& sender_id,
const std::string& app_id)>;
using PerUserTopicRegistrationManagerCallback =
base::RepeatingCallback<std::unique_ptr<
syncer::PerUserTopicRegistrationManager>(const std::string& project_id,
bool migrate_prefs)>;
// This InvalidationService wraps the C++ Invalidation Client (FCM) library.
// It provides invalidations for desktop platforms (Win, Mac, Linux).
// Subclasses should implement Init to set up their initial state and call
// StartInvalidator/StopInvalidator when they want to start/stop receiving
// invalidations.
class FCMInvalidationServiceBase
: public InvalidationService,
public syncer::FCMInvalidationListener::Delegate {
public:
FCMInvalidationServiceBase(
FCMNetworkHandlerCallback fcm_network_handler_callback,
PerUserTopicRegistrationManagerCallback
per_user_topic_registration_manager_callback,
instance_id::InstanceIDDriver* client_id_driver,
PrefService* pref_service,
const std::string& sender_id = {});
~FCMInvalidationServiceBase() override;
virtual void Init() = 0;
static void RegisterPrefs(PrefRegistrySimple* registry);
// InvalidationService implementation.
// It is an error to have registered handlers when the service is destroyed.
void RegisterInvalidationHandler(
syncer::InvalidationHandler* handler) override;
bool UpdateRegisteredInvalidationIds(syncer::InvalidationHandler* handler,
const syncer::ObjectIdSet& ids) override;
void UnregisterInvalidationHandler(
syncer::InvalidationHandler* handler) override;
syncer::InvalidatorState GetInvalidatorState() const override;
std::string GetInvalidatorClientId() const override;
InvalidationLogger* GetInvalidationLogger() override;
void RequestDetailedStatus(
base::RepeatingCallback<void(const base::DictionaryValue&)> caller)
const override;
// syncer::FCMInvalidationListener::Delegate implementation.
void OnInvalidate(
const syncer::TopicInvalidationMap& invalidation_map) override;
void OnInvalidatorStateChange(syncer::InvalidatorState state) override;
protected:
// Initializes with an injected invalidator.
void InitForTest(
std::unique_ptr<syncer::FCMInvalidationListener> invalidation_listener);
virtual base::DictionaryValue CollectDebugData() const;
void ReportInvalidatorState(syncer::InvalidatorState state);
// Returns true if the service is currently started and able to receive
// invalidations.
bool IsStarted() const;
// Subclasses should be calling StartInvalidator and StopInvalidator when it
// is appropriate for their use case. This class will call StopInvalidator
// when it's destroyed if it's still started at that point.
// Start the invalidation service to start receiving invalidations.
void StartInvalidator();
// Stop the invalidation service to stop receiving invalidations. It's
// appropriate to call this when the service is expected to be started again
// with the same client ID (such as during shutdown).
void StopInvalidator();
// Stops the invalidation service to stop receiving invalidations. This also
// resets the Instance ID driver's client ID and clears the client ID cache.
void StopInvalidatorPermanently();
private:
struct Diagnostics {
base::Time instance_id_requested;
base::Time instance_id_received;
base::Time service_was_stopped;
base::Time service_was_started;
};
void PopulateClientID();
void ResetClientID();
void OnInstanceIdReceived(const std::string& id);
void OnDeleteIDCompleted(instance_id::InstanceID::Result);
void DoUpdateRegisteredIdsIfNeeded();
const std::string GetApplicationName();
const std::string sender_id_;
syncer::InvalidatorRegistrarWithMemory invalidator_registrar_;
// The invalidation logger object we use to record state changes
// and invalidations.
InvalidationLogger logger_;
FCMNetworkHandlerCallback fcm_network_handler_callback_;
PerUserTopicRegistrationManagerCallback
per_user_topic_registration_manager_callback_;
instance_id::InstanceIDDriver* const instance_id_driver_;
std::string client_id_;
PrefService* const pref_service_;
bool update_was_requested_ = false;
Diagnostics diagnostic_info_;
// The invalidation listener.
std::unique_ptr<syncer::FCMInvalidationListener> invalidation_listener_;
SEQUENCE_CHECKER(sequence_checker_);
DISALLOW_COPY_AND_ASSIGN(FCMInvalidationServiceBase);
};
} // namespace invalidation
#endif // COMPONENTS_INVALIDATION_IMPL_FCM_INVALIDATION_SERVICE_BASE_H_
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