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
ash / system / federated / federated_client_manager.cc [blame]
// Copyright 2023 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "ash/system/federated/federated_client_manager.h"
#include "ash/constants/ash_features.h"
#include "ash/shell.h"
#include "ash/system/federated/federated_service_controller_impl.h"
#include "ash/system/federated/test_federated_service_controller.h"
#include "base/notreached.h"
#include "chromeos/ash/services/federated/public/cpp/federated_example_util.h"
// TODO tidy up method naming conventions e.g. IsServiceAvailable is
// confusingly used by this class for itself, and by fed service for itself.
namespace ash::federated {
namespace {
using chromeos::federated::mojom::Example;
using chromeos::federated::mojom::ExamplePtr;
using chromeos::federated::mojom::Features;
using chromeos::federated::mojom::FederatedExampleTableId;
ExamplePtr CreateSingleStringExamplePtr(const std::string& example_feature_name,
const std::string& example_str) {
ExamplePtr example = Example::New();
example->features = Features::New();
auto& feature_map = example->features->feature;
feature_map[example_feature_name] = CreateStringList({example_str});
return example;
}
} // namespace
FederatedClientManager::FederatedClientManager() {}
FederatedClientManager::~FederatedClientManager() {
if (use_fake_controller_for_testing_) {
delete controller_;
}
}
void FederatedClientManager::UseFakeAshInteractionForTest() {
use_fake_controller_for_testing_ = true;
}
bool FederatedClientManager::IsFederatedServiceAvailable() {
// Lazy initialization. Useful for controlling mocked ash interaction in some
// unit test environments.
if (!initialized_) {
if (use_fake_controller_for_testing_) {
controller_ = new TestFederatedServiceController;
} else {
controller_ = Shell::Get()->federated_service_controller();
}
initialized_ = true;
}
return ash::features::IsFederatedServiceEnabled() && controller_ &&
controller_->IsServiceAvailable();
}
void FederatedClientManager::ReportExample(
const FederatedExampleTableId table_id,
ExamplePtr example) {
ReportExampleToFederatedService(table_id, std::move(example));
}
void FederatedClientManager::ReportSingleString(
const FederatedExampleTableId table_id,
const std::string& example_feature_name,
const std::string& example_str) {
ExamplePtr example =
CreateSingleStringExamplePtr(example_feature_name, example_str);
ReportExample(table_id, std::move(example));
}
bool FederatedClientManager::IsFederatedStringsServiceAvailable() {
return IsFederatedServiceAvailable() &&
ash::features::IsFederatedStringsServiceEnabled();
}
void FederatedClientManager::ReportStringViaStringsService(
const FederatedExampleTableId table_id,
const std::string& client_string) {
if (!ash::features::IsFederatedStringsServiceEnabled()) {
return;
}
// TODO(b/289140140): Use a less generic word than "query".
ReportExampleToFederatedService(
table_id, CreateSingleStringExamplePtr(
/*example_feature_name*/ "query", client_string));
}
void FederatedClientManager::TryToBindFederatedServiceIfNecessary() {
if (federated_service_.is_bound()) {
return;
}
if (IsFederatedServiceAvailable()) {
ash::federated::ServiceConnection::GetInstance()->BindReceiver(
federated_service_.BindNewPipeAndPassReceiver());
}
}
void FederatedClientManager::ReportExampleToFederatedService(
const FederatedExampleTableId table_id,
ExamplePtr example) {
TryToBindFederatedServiceIfNecessary();
if (!controller_->IsServiceAvailable()) {
// TODO(b/289140140): UMA metrics.
} else if (!federated_service_.is_connected()) {
// TODO(b/289140140): UMA metrics.
} else {
// Federated service available and connected.
federated_service_->ReportExampleToTable(table_id, std::move(example));
++successful_reports_for_test_;
// TODO(b/289140140): UMA metrics.
}
}
} // namespace ash::federated