-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfederated_library.cc
79 lines (66 loc) · 2.95 KB
/
federated_library.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
// Copyright 2021 The ChromiumOS Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "federated/federated_library.h"
#include <cstddef>
#include <absl/status/status.h>
#include <base/files/file_path.h>
#include <base/logging.h>
#include <base/native_library.h>
#include <base/no_destructor.h>
#include "federated/federated_client.h"
#include "federated/metrics.h"
namespace federated {
FederatedLibrary* FederatedLibrary::GetInstance(const std::string& lib_path) {
static base::NoDestructor<FederatedLibrary> instance(lib_path);
return instance.get();
}
FederatedLibrary::FederatedLibrary(const std::string& lib_path)
: run_plan_(nullptr), free_run_plan_result_(nullptr) {
base::NativeLibraryLoadError error;
library_.emplace(base::LoadNativeLibraryWithOptions(
base::FilePath(lib_path),
/* options */ {.prefer_own_symbols = true}, &error));
if (!library_->is_valid()) {
LOG(ERROR) << "Failed to load library, error message: " << error.ToString();
status_ = absl::FailedPreconditionError(
"Failed to load library, error message: " + error.ToString());
Metrics::GetInstance()->LogServiceEvent(ServiceEvent::kInvalidLibraryError);
return;
}
// Helper macro to look up functions from the library, assuming the function
// pointer type is named as (name+"Fn"), which is the case in
// <fcp/fcp.h>.
#define FEDERATED_LOOKUP_FUNCTION(function_ptr, name) \
function_ptr = \
reinterpret_cast<name##Fn>(library_->GetFunctionPointer(#name)); \
if (function_ptr == nullptr) { \
LOG(ERROR) << "Failed to lookup function " << #name; \
status_ = absl::InternalError("Failed to lookup function"); \
Metrics::GetInstance()->LogServiceEvent( \
ServiceEvent::kFunctionMissingError); \
return; \
}
// Look up the function pointers.
FEDERATED_LOOKUP_FUNCTION(run_plan_, FlRunPlan);
FEDERATED_LOOKUP_FUNCTION(free_run_plan_result_, FlFreeRunPlanResult);
#undef FEDERATED_LOOKUP_FUNCTION
status_ = absl::OkStatus();
Metrics::GetInstance()->LogServiceEvent(ServiceEvent::kLibraryLoadingSuccess);
}
FederatedLibrary::~FederatedLibrary() = default;
absl::Status FederatedLibrary::GetStatus() const {
return status_;
}
FederatedClient FederatedLibrary::CreateClient(
const std::string& service_uri,
const std::string& api_key,
const std::string& brella_lib_version,
const ClientConfigMetadata client_config,
DeviceStatusMonitor* const device_status_monitor) {
DCHECK(status_.ok());
return FederatedClient(run_plan_, free_run_plan_result_, service_uri, api_key,
brella_lib_version, client_config,
device_status_monitor);
}
} // namespace federated