blob: d76205b586614fd4648b06025381907cde8355fe [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 {
Marc Treib19e2f142018-11-16 14:48:0238 return GetDisableReasons() == DISABLE_REASON_NONE &&
39 IsAuthenticatedAccountPrimary();
Marc Treibdb1e1a0f2018-07-07 15:30:4840}
41
Marc Treib90ce10aa2018-07-23 07:30:0342bool SyncService::IsEngineInitialized() const {
Marc Treib4b23757c2018-08-06 11:03:4743 switch (GetTransportState()) {
44 case TransportState::DISABLED:
Marc Treib4b23757c2018-08-06 11:03:4745 case TransportState::START_DEFERRED:
46 case TransportState::INITIALIZING:
Marc Treib90ce10aa2018-07-23 07:30:0347 return false;
Marc Treib4b23757c2018-08-06 11:03:4748 case TransportState::PENDING_DESIRED_CONFIGURATION:
49 case TransportState::CONFIGURING:
50 case TransportState::ACTIVE:
Marc Treib90ce10aa2018-07-23 07:30:0351 return true;
52 }
53 NOTREACHED();
54 return false;
55}
56
Marc Treib735cc0582018-09-17 17:08:3157bool SyncService::IsSyncFeatureActive() const {
Marc Treibeedaa3e2018-08-02 18:53:5858 if (!IsSyncFeatureEnabled()) {
59 return false;
60 }
Marc Treib4b23757c2018-08-06 11:03:4761 switch (GetTransportState()) {
62 case TransportState::DISABLED:
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