blob: a03f13f093213dc048f3f64fbd6e406b0a095df1 [file] [log] [blame]
// Copyright 2017 The Fuchsia 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 <fuchsia/ledger/cloud/cpp/fidl.h>
#include <lib/backoff/backoff.h>
#include <lib/callback/scoped_task_runner.h>
#include <lib/fidl/cpp/binding.h>
#include <lib/fit/function.h>
#include <memory>
#include <set>
#include "src/ledger/bin/cloud_sync/impl/aggregator.h"
#include "src/ledger/bin/cloud_sync/impl/ledger_sync_impl.h"
#include "src/ledger/bin/cloud_sync/public/user_sync.h"
#include "src/ledger/bin/environment/environment.h"
namespace cloud_sync {
class UserSyncImpl : public UserSync, cloud_provider::DeviceSetWatcher {
// Parameters:
// |on_version_mismatch| is called when the local state is detected to be
// incompatible with the state in the cloud and has to be erased.
UserSyncImpl(ledger::Environment* environment, UserConfig user_config,
std::unique_ptr<backoff::Backoff> backoff,
fit::closure on_version_mismatch);
~UserSyncImpl() override;
// UserSync:
void SetSyncWatcher(SyncStateWatcher* watcher) override;
void Start() override;
std::unique_ptr<LedgerSync> CreateLedgerSync(
fxl::StringView app_id,
encryption::EncryptionService* encryption_service) override;
// Returns the path where the device fingerprint is stored.
ledger::DetachedPath GetFingerprintPath();
// cloud_provider::DeviceSetWatcher:
void OnCloudErased() override;
void OnError(cloud_provider::Status status) override;
// Checks that the cloud was not erased since the last sync using the device
// fingerprint.
void CheckCloudNotErased();
void CreateFingerprint();
void HandleDeviceSetResult(cloud_provider::Status status);
// Sets a watcher to detect that the cloud is cleared while sync is running.
void SetCloudErasedWatcher();
// Enables sync upload.
void EnableUpload();
ledger::Environment* environment_;
const UserConfig user_config_;
std::unique_ptr<backoff::Backoff> backoff_;
fit::closure on_version_mismatch_;
// UserSyncImpl must be started before it can be used.
bool started_ = false;
// Whether uploads should be enabled. It is false until the cloud version has
// been checked.
bool upload_enabled_ = false;
cloud_provider::DeviceSetPtr device_set_;
fidl::Binding<cloud_provider::DeviceSetWatcher> watcher_binding_;
// Fingerprint of the device in the cloud device list.
std::string fingerprint_;
std::set<LedgerSyncImpl*> active_ledger_syncs_;
// Aggregates the synchronization state of multiple ledgers into one
// notification stream.
Aggregator aggregator_;
// This must be the last member of this class.
callback::ScopedTaskRunner task_runner_;
} // namespace cloud_sync