-
Notifications
You must be signed in to change notification settings - Fork 6.6k
/
password_sync_controller_delegate_android.cc
181 lines (157 loc) · 7.14 KB
/
password_sync_controller_delegate_android.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
// Copyright 2022 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "chrome/browser/password_manager/android/password_sync_controller_delegate_android.h"
#include "base/memory/weak_ptr.h"
#include "base/metrics/histogram_functions.h"
#include "base/task/sequenced_task_runner.h"
#include "components/password_manager/core/browser/android_backend_error.h"
#include "components/password_manager/core/browser/password_sync_util.h"
#include "components/sync/base/user_selectable_type.h"
#include "components/sync/driver/sync_service.h"
#include "components/sync/engine/data_type_activation_response.h"
#include "components/sync/model/model_type_controller_delegate.h"
#include "components/sync/model/proxy_model_type_controller_delegate.h"
#include "components/sync/model/type_entities_count.h"
namespace password_manager {
namespace {
using sync_util::IsPasswordSyncEnabled;
std::string BuildCredentialManagerNotificationMetricName(
const std::string& suffix) {
return "PasswordManager.SyncControllerDelegateNotifiesCredentialManager." +
suffix;
}
} // namespace
PasswordSyncControllerDelegateAndroid::PasswordSyncControllerDelegateAndroid(
std::unique_ptr<PasswordSyncControllerDelegateBridge> bridge,
base::OnceClosure on_sync_shutdown)
: bridge_(std::move(bridge)),
on_sync_shutdown_(std::move(on_sync_shutdown)) {
DCHECK(bridge_);
bridge_->SetConsumer(weak_ptr_factory_.GetWeakPtr());
}
PasswordSyncControllerDelegateAndroid::
~PasswordSyncControllerDelegateAndroid() = default;
std::unique_ptr<syncer::ProxyModelTypeControllerDelegate>
PasswordSyncControllerDelegateAndroid::CreateProxyModelControllerDelegate() {
return std::make_unique<syncer::ProxyModelTypeControllerDelegate>(
base::SequencedTaskRunner::GetCurrentDefault(),
base::BindRepeating(
&PasswordSyncControllerDelegateAndroid::GetWeakPtrToBaseClass,
base::Unretained(this)));
}
void PasswordSyncControllerDelegateAndroid::OnSyncServiceInitialized(
syncer::SyncService* sync_service) {
sync_service_ = sync_service;
sync_observation_.Observe(sync_service);
is_sync_enabled_ = IsSyncEnabled(IsPasswordSyncEnabled(sync_service_));
}
void PasswordSyncControllerDelegateAndroid::OnSyncStarting(
const syncer::DataTypeActivationRequest& request,
StartCallback callback) {
// React on sync starting only if we know that sync was disabled. Otherwise,
// we either couldn't obtain sync status before OnSyncStarting was called, or
// sync was already active and this is called on browser start up. In either
// case we shouldn't react.
// TODO(crbug.com/1260837): Record whether OnSyncStarting is called before
// |is_sync_enabled_| holds value.
if (is_sync_enabled_.has_value() &&
is_sync_enabled_.value() == IsSyncEnabled(false)) {
// TODO(crbug.com/1260837): Sync was enabled. Move passwords from local
// storage to syncing storage.
NOTIMPLEMENTED();
}
is_sync_enabled_ = IsSyncEnabled(true);
// Set |skip_engine_connection| to true to indicate that, actually, this sync
// datatype doesn't depend on the built-in SyncEngine to communicate changes
// to/from the Sync server. Instead, Android specific functionality is
// leveraged to achieve similar behavior.
auto activation_response =
std::make_unique<syncer::DataTypeActivationResponse>();
activation_response->skip_engine_connection = true;
std::move(callback).Run(std::move(activation_response));
}
void PasswordSyncControllerDelegateAndroid::OnSyncStopping(
syncer::SyncStopMetadataFate metadata_fate) {
switch (metadata_fate) {
case syncer::KEEP_METADATA:
// Sync got temporarily paused. Just ignore.
break;
case syncer::CLEAR_METADATA:
// The user (or something equivalent like an enterprise policy)
// permanently disrabled sync, either fully or specifically for passwords.
// This also includes more advanced cases like the user having cleared all
// sync data in the dashboard (birthday reset) or, at least in theory, the
// sync server reporting that all sync metadata is obsolete (i.e.
// CLIENT_DATA_OBSOLETE in the sync protocol).
// TODO(crbug.com/1260837): Sync was disabled. Move passwords from syncing
// storage to local storage.
NOTIMPLEMENTED();
is_sync_enabled_ = IsSyncEnabled(false);
break;
}
}
void PasswordSyncControllerDelegateAndroid::GetAllNodesForDebugging(
AllNodesCallback callback) {
// This is not implemented because it's not worth the hassle just to display
// debug information in chrome://sync-internals.
std::move(callback).Run(syncer::PASSWORDS, base::Value::List());
}
void PasswordSyncControllerDelegateAndroid::GetTypeEntitiesCountForDebugging(
base::OnceCallback<void(const syncer::TypeEntitiesCount&)> callback) const {
// This is not implemented because it's not worth the hassle just to display
// debug information in chrome://sync-internals.
std::move(callback).Run(syncer::TypeEntitiesCount(syncer::PASSWORDS));
}
void PasswordSyncControllerDelegateAndroid::
RecordMemoryUsageAndCountsHistograms() {
// This is not implemented because it's not worth the hassle. Password sync
// module on Android doesn't hold any password. Instead passwords are
// requested on demand from the GMS Core.
}
void PasswordSyncControllerDelegateAndroid::OnStateChanged(
syncer::SyncService* sync) {
// Notify credential manager about current account on startup or if
// password sync setting has changed.
if (sync_util::IsPasswordSyncEnabled(sync) &&
(!credential_manager_sync_setting_.has_value() ||
credential_manager_sync_setting_ == IsSyncEnabled(false))) {
bridge_->NotifyCredentialManagerWhenSyncing();
credential_manager_sync_setting_ = IsSyncEnabled(true);
}
if (!sync_util::IsPasswordSyncEnabled(sync) &&
(!credential_manager_sync_setting_.has_value() ||
credential_manager_sync_setting_ == IsSyncEnabled(true))) {
bridge_->NotifyCredentialManagerWhenNotSyncing();
credential_manager_sync_setting_ = IsSyncEnabled(false);
}
}
void PasswordSyncControllerDelegateAndroid::OnSyncShutdown(
syncer::SyncService* sync) {
sync_service_ = nullptr;
if (!on_sync_shutdown_)
return;
std::move(on_sync_shutdown_).Run();
}
void PasswordSyncControllerDelegateAndroid::OnCredentialManagerNotified() {
base::UmaHistogramBoolean(
BuildCredentialManagerNotificationMetricName("Success"), 1);
}
void PasswordSyncControllerDelegateAndroid::OnCredentialManagerError(
const AndroidBackendError& error,
int api_error_code) {
base::UmaHistogramBoolean(
BuildCredentialManagerNotificationMetricName("Success"), 0);
base::UmaHistogramEnumeration(
BuildCredentialManagerNotificationMetricName("ErrorCode"), error.type);
if (error.type == AndroidBackendErrorType::kExternalError) {
base::UmaHistogramSparse(
BuildCredentialManagerNotificationMetricName("APIErrorCode"),
api_error_code);
}
}
base::WeakPtr<syncer::ModelTypeControllerDelegate>
PasswordSyncControllerDelegateAndroid::GetWeakPtrToBaseClass() {
return weak_ptr_factory_.GetWeakPtr();
}
} // namespace password_manager