blob: e6b2f70f637faf1c789a866817263f840ad4f04b [file] [log] [blame]
// Copyright 2018 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 <memory>
#include "base/macros.h"
#include "chromeos/services/device_sync/proto/cryptauth_api.pb.h"
#include "chromeos/services/device_sync/public/cpp/device_sync_client.h"
namespace base {
class OneShotTimer;
} // namespace base
namespace chromeos {
namespace device_sync {
class GcmDeviceInfoProvider;
} // namespace device_sync
namespace multidevice_setup {
// This class re-enrolls the device if the set of supported SoftwareFeatures in
// the GCM device info differs from that of the local device metadata. This
// condition is checked in the constructor and any time the
// DeviceSyncClient::Observer callbacks--OnEnrollmentFinished() and
// OnNewDevicesSync()--are called.
// The supported software features listed in GCM device info should be
// considered the new source of truth. Enrollment updates a device's list of
// supported software features (among other things) on the backend to conform
// with the GCM device info. Then, a device sync is necessary to update the
// cache of device information with the latest backend data. The local device
// metadata is part of this cache.
// The flow of the class is as follows:
// +-------------------------------------------------------------------------+
// | |
// | (From external enrollment) (From external device sync)|
// | | | |
// V V V |
// Start-->Enrollment-->OnEnrollmentFinished-->DeviceSync-->OnNewDevicesSynced-+
// | |
// | |
// V V
// If features agree, then done
// A five-minute retry timer is started at the beginning of the flow. Should any
// step fail, the process will be re-started when the timer fires.
class DeviceReenroller : public device_sync::DeviceSyncClient::Observer {
class Factory {
static Factory* Get();
static void SetFactoryForTesting(Factory* test_factory);
virtual ~Factory();
virtual std::unique_ptr<DeviceReenroller> BuildInstance(
device_sync::DeviceSyncClient* device_sync_client,
const device_sync::GcmDeviceInfoProvider* gcm_device_info_provider,
std::unique_ptr<base::OneShotTimer> timer =
static Factory* test_factory_;
~DeviceReenroller() override;
device_sync::DeviceSyncClient* device_sync_client,
const device_sync::GcmDeviceInfoProvider* gcm_device_info_provider,
std::unique_ptr<base::OneShotTimer> timer);
void AttemptReenrollmentIfNecessary();
// Returns a sorted and deduped list of the supported or enabled software
// features from DeviceSyncClient::GetLocalDeviceMetadata().
std::vector<cryptauth::SoftwareFeature> GetSupportedFeaturesForLocalDevice();
// device_sync::DeviceSyncClient::Observer:
void OnEnrollmentFinished() override;
void OnNewDevicesSynced() override;
device_sync::DeviceSyncClient* device_sync_client_;
const std::vector<cryptauth::SoftwareFeature>
std::unique_ptr<base::OneShotTimer> timer_;
} // namespace multidevice_setup
} // namespace chromeos