blob: 35c4383cc31faf58877eec0435b6bf56be96886d [file] [log] [blame]
maxbogue93150a5d2015-08-31 21:08:321// Copyright 2015 The Chromium Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
maxbogue455a57e32016-08-14 00:08:325#include "components/sync/driver/sync_service.h"
maxbogue93150a5d2015-08-31 21:08:326
Ken Rockotcd98d952019-12-19 21:06:277#include <utility>
8
Marc Treibe712f652019-12-02 17:36:349#include "components/signin/public/identity_manager/account_info.h"
Marc Treib3accf29c2018-11-29 07:45:4810#include "components/sync/driver/sync_user_settings.h"
Marc Treib2b58eec2019-04-17 09:51:5211#include "components/sync/engine/cycle/sync_cycle_snapshot.h"
Marc Treib3accf29c2018-11-29 07:45:4812
maxbogue7e006db2016-10-03 19:48:2813namespace syncer {
maxbogue93150a5d2015-08-31 21:08:3214
Ken Rockotcd98d952019-12-19 21:06:2715SyncSetupInProgressHandle::SyncSetupInProgressHandle(
16 base::OnceClosure on_destroy)
17 : on_destroy_(std::move(on_destroy)) {}
tommyclif3a1551e2016-06-21 18:11:2318
19SyncSetupInProgressHandle::~SyncSetupInProgressHandle() {
Ken Rockotcd98d952019-12-19 21:06:2720 std::move(on_destroy_).Run();
tommyclif3a1551e2016-06-21 18:11:2321}
22
Marc Treib2b58eec2019-04-17 09:51:5223bool SyncService::HasCompletedSyncCycle() const {
Marc Treib22be1c02019-04-18 11:21:4324 // Stats on the last Sync cycle are only available in internal "for debugging"
25 // information. Better to access that here than making clients do it.
26 return GetLastCycleSnapshotForDebugging().is_initialized();
Marc Treib2b58eec2019-04-17 09:51:5227}
28
Marc Treibeedaa3e2018-08-02 18:53:5829bool SyncService::IsSyncFeatureEnabled() const {
Marc Treib135720d2018-08-29 12:11:4330 // Note: IsFirstSetupComplete() shouldn't usually be true if we don't have a
31 // primary account, but it could happen if the account changes from primary to
32 // secondary.
Marc Treib3accf29c2018-11-29 07:45:4833 return CanSyncFeatureStart() && GetUserSettings()->IsFirstSetupComplete();
Marc Treibeedaa3e2018-08-02 18:53:5834}
35
Marc Treib735cc0582018-09-17 17:08:3136bool SyncService::CanSyncFeatureStart() const {
Sergey Talantova30f44c2019-12-17 13:38:0037 return GetDisableReasons().Empty() && IsAuthenticatedAccountPrimary();
Marc Treibdb1e1a0f2018-07-07 15:30:4838}
39
Marc Treib90ce10aa2018-07-23 07:30:0340bool SyncService::IsEngineInitialized() const {
Marc Treib4b23757c2018-08-06 11:03:4741 switch (GetTransportState()) {
42 case TransportState::DISABLED:
Victor Hugo Vianna Silva9647e682020-07-22 09:18:2143 case TransportState::PAUSED:
Marc Treib4b23757c2018-08-06 11:03:4744 case TransportState::START_DEFERRED:
45 case TransportState::INITIALIZING:
Marc Treib90ce10aa2018-07-23 07:30:0346 return false;
Marc Treib4b23757c2018-08-06 11:03:4747 case TransportState::PENDING_DESIRED_CONFIGURATION:
48 case TransportState::CONFIGURING:
49 case TransportState::ACTIVE:
Marc Treib90ce10aa2018-07-23 07:30:0350 return true;
51 }
52 NOTREACHED();
53 return false;
54}
55
Marc Treib735cc0582018-09-17 17:08:3156bool SyncService::IsSyncFeatureActive() const {
Marc Treibeedaa3e2018-08-02 18:53:5857 if (!IsSyncFeatureEnabled()) {
58 return false;
59 }
Marc Treib4b23757c2018-08-06 11:03:4760 switch (GetTransportState()) {
61 case TransportState::DISABLED:
Victor Hugo Vianna Silva9647e682020-07-22 09:18:2162 case TransportState::PAUSED:
Marc Treib4b23757c2018-08-06 11:03:4763 case TransportState::START_DEFERRED:
64 case TransportState::INITIALIZING:
65 case TransportState::PENDING_DESIRED_CONFIGURATION:
Marc Treib90ce10aa2018-07-23 07:30:0366 return false;
Marc Treib4b23757c2018-08-06 11:03:4767 case TransportState::CONFIGURING:
68 case TransportState::ACTIVE:
Marc Treib90ce10aa2018-07-23 07:30:0369 return true;
70 }
71 NOTREACHED();
72 return false;
Marc Treib94702442018-07-19 09:04:4373}
74
Marc Treibdb1e1a0f2018-07-07 15:30:4875bool SyncService::HasUnrecoverableError() const {
76 return HasDisableReason(DISABLE_REASON_UNRECOVERABLE_ERROR);
77}
78
maxbogue7e006db2016-10-03 19:48:2879} // namespace syncer