1 // 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 
5 #include "components/sync/driver/sync_service_utils.h"
6 
7 #include "base/metrics/histogram_functions.h"
8 #include "base/metrics/histogram_macros.h"
9 #include "components/sync/driver/sync_service.h"
10 #include "components/sync/driver/sync_user_settings.h"
11 #include "google_apis/gaia/google_service_auth_error.h"
12 
13 namespace syncer {
14 
GetUploadToGoogleState(const SyncService * sync_service,ModelType type)15 UploadState GetUploadToGoogleState(const SyncService* sync_service,
16                                    ModelType type) {
17   // Note: Before configuration is done, GetPreferredDataTypes returns
18   // "everything" (i.e. the default setting). If a data type is missing there,
19   // it must be because the user explicitly disabled it.
20   if (!sync_service || sync_service->IsLocalSyncEnabled() ||
21       !sync_service->CanSyncFeatureStart() ||
22       !sync_service->GetPreferredDataTypes().Has(type)) {
23     return UploadState::NOT_ACTIVE;
24   }
25 
26   // If the given ModelType is encrypted with a custom passphrase, we also
27   // consider uploading inactive, since Google can't read the data.
28   // Note that encryption is tricky: Some data types (e.g. PASSWORDS) are always
29   // encrypted, but not necessarily with a custom passphrase. On the other hand,
30   // some data types are never encrypted (e.g. DEVICE_INFO), even if the
31   // "encrypt everything" setting is enabled.
32   if (sync_service->GetUserSettings()->GetEncryptedDataTypes().Has(type) &&
33       sync_service->GetUserSettings()->IsUsingSecondaryPassphrase()) {
34     return UploadState::NOT_ACTIVE;
35   }
36 
37   // Persistent auth errors always map to NOT_ACTIVE. For transient errors, we
38   // give the benefit of the doubt and may still say we're INITIALIZING.
39   if (sync_service->GetAuthError().IsPersistentError()) {
40     return UploadState::NOT_ACTIVE;
41   }
42 
43   switch (sync_service->GetTransportState()) {
44     case SyncService::TransportState::DISABLED:
45     case SyncService::TransportState::PAUSED:
46       return UploadState::NOT_ACTIVE;
47 
48     case SyncService::TransportState::START_DEFERRED:
49     case SyncService::TransportState::INITIALIZING:
50     case SyncService::TransportState::PENDING_DESIRED_CONFIGURATION:
51     case SyncService::TransportState::CONFIGURING:
52       return UploadState::INITIALIZING;
53 
54     case SyncService::TransportState::ACTIVE:
55       // If sync is active, but the data type in question still isn't, then
56       // something must have gone wrong with that data type.
57       if (!sync_service->GetActiveDataTypes().Has(type)) {
58         return UploadState::NOT_ACTIVE;
59       }
60       if (sync_service->GetAuthError().IsTransientError()) {
61         return UploadState::INITIALIZING;
62       }
63       // TODO(crbug.com/831579): We only know if the refresh token is actually
64       // valid (no auth error) after we've tried talking to the Sync server.
65       if (!sync_service->HasCompletedSyncCycle()) {
66         return UploadState::INITIALIZING;
67       }
68       return UploadState::ACTIVE;
69   }
70   NOTREACHED();
71   return UploadState::NOT_ACTIVE;
72 }
73 
RecordKeyRetrievalTrigger(KeyRetrievalTriggerForUMA trigger)74 void RecordKeyRetrievalTrigger(KeyRetrievalTriggerForUMA trigger) {
75   base::UmaHistogramEnumeration("Sync.TrustedVaultKeyRetrievalTrigger",
76                                 trigger);
77 }
78 
79 }  // namespace syncer
80