blob: eb8b71b633a46795aa9ab180f753f2f8af965518 [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
Marc Treibe712f652019-12-02 17:36:347#include "components/signin/public/identity_manager/account_info.h"
Marc Treib3accf29c2018-11-29 07:45:488#include "components/sync/driver/sync_user_settings.h"
Marc Treib2b58eec2019-04-17 09:51:529#include "components/sync/engine/cycle/sync_cycle_snapshot.h"
Marc Treib3accf29c2018-11-29 07:45:4810
maxbogue7e006db2016-10-03 19:48:2811namespace syncer {
maxbogue93150a5d2015-08-31 21:08:3212
tommyclif3a1551e2016-06-21 18:11:2313SyncSetupInProgressHandle::SyncSetupInProgressHandle(base::Closure on_destroy)
14 : on_destroy_(on_destroy) {}
15
16SyncSetupInProgressHandle::~SyncSetupInProgressHandle() {
17 on_destroy_.Run();
18}
19
Marc Treibe712f652019-12-02 17:36:3420CoreAccountId SyncService::GetAuthenticatedAccountId() const {
21 return GetAuthenticatedAccountInfo().account_id;
22}
23
Marc Treib2b58eec2019-04-17 09:51:5224bool SyncService::HasCompletedSyncCycle() const {
Marc Treib22be1c02019-04-18 11:21:4325 // Stats on the last Sync cycle are only available in internal "for debugging"
26 // information. Better to access that here than making clients do it.
27 return GetLastCycleSnapshotForDebugging().is_initialized();
Marc Treib2b58eec2019-04-17 09:51:5228}
29
Marc Treibeedaa3e2018-08-02 18:53:5830bool SyncService::IsSyncFeatureEnabled() const {
Marc Treib135720d2018-08-29 12:11:4331 // Note: IsFirstSetupComplete() shouldn't usually be true if we don't have a
32 // primary account, but it could happen if the account changes from primary to
33 // secondary.
Marc Treib3accf29c2018-11-29 07:45:4834 return CanSyncFeatureStart() && GetUserSettings()->IsFirstSetupComplete();
Marc Treibeedaa3e2018-08-02 18:53:5835}
36
Marc Treib735cc0582018-09-17 17:08:3137bool SyncService::CanSyncFeatureStart() const {
Sergey Talantova30f44c2019-12-17 13:38:0038 return GetDisableReasons().Empty() && IsAuthenticatedAccountPrimary();
Marc Treibdb1e1a0f2018-07-07 15:30:4839}
40
Marc Treib90ce10aa2018-07-23 07:30:0341bool SyncService::IsEngineInitialized() const {
Marc Treib4b23757c2018-08-06 11:03:4742 switch (GetTransportState()) {
43 case TransportState::DISABLED:
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:
Marc Treib4b23757c2018-08-06 11:03:4762 case TransportState::START_DEFERRED:
63 case TransportState::INITIALIZING:
64 case TransportState::PENDING_DESIRED_CONFIGURATION:
Marc Treib90ce10aa2018-07-23 07:30:0365 return false;
Marc Treib4b23757c2018-08-06 11:03:4766 case TransportState::CONFIGURING:
67 case TransportState::ACTIVE:
Marc Treib90ce10aa2018-07-23 07:30:0368 return true;
69 }
70 NOTREACHED();
71 return false;
Marc Treib94702442018-07-19 09:04:4372}
73
Marc Treibdb1e1a0f2018-07-07 15:30:4874bool SyncService::HasUnrecoverableError() const {
75 return HasDisableReason(DISABLE_REASON_UNRECOVERABLE_ERROR);
76}
77
maxbogue7e006db2016-10-03 19:48:2878} // namespace syncer