-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcpp_feature_check_example.cc
96 lines (81 loc) · 3.18 KB
/
cpp_feature_check_example.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
// 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 <base/barrier_closure.h>
#include <base/files/file_descriptor_watcher_posix.h>
#include <base/functional/callback.h>
#include <base/logging.h>
#include <base/run_loop.h>
#include <base/task/single_thread_task_executor.h>
#include <base/task/thread_pool.h>
#include <base/task/thread_pool/thread_pool_instance.h>
#include <dbus/bus.h>
#include "featured/feature_library.h"
const struct VariationsFeature kCrOSLateBootMyAwesomeFeature = {
.name = "CrOSLateBootMyAwesomeFeature",
.default_state = FEATURE_DISABLED_BY_DEFAULT,
};
void EnabledCallback(base::RepeatingClosure barrier_closure, bool enabled) {
LOG(INFO) << "Enabled? " << enabled;
barrier_closure.Run();
}
void GetParamsCallback(base::RepeatingClosure barrier_closure,
feature::PlatformFeatures::ParamsResult result) {
for (const auto& [name, entry] : result) {
LOG(INFO) << "Feature: " << name;
LOG(INFO) << " Enabled?: " << entry.enabled;
LOG(INFO) << " Params?:";
if (entry.params.empty()) {
LOG(INFO) << " No params";
break;
}
for (const auto& [key, value] : entry.params) {
LOG(INFO) << " params['" << key << "'] = '" << value << "'";
}
}
barrier_closure.Run();
}
void FetchState(feature::PlatformFeatures* feature_lib,
base::RepeatingClosure barrier_closure) {
feature_lib->IsEnabled(kCrOSLateBootMyAwesomeFeature,
base::BindOnce(&EnabledCallback, barrier_closure));
feature_lib->GetParamsAndEnabled(
{&kCrOSLateBootMyAwesomeFeature},
base::BindOnce(&GetParamsCallback, barrier_closure));
}
void Refetch(feature::PlatformFeatures* feature_lib,
base::RepeatingClosure barrier_closure) {
LOG(INFO) << "Refetch";
FetchState(feature_lib, barrier_closure);
}
void Ready(bool ready) {
LOG(INFO) << "ready: " << std::boolalpha << ready;
}
int main(int argc, char* argv[]) {
base::ThreadPoolInstance::CreateAndStartWithDefaultParams(
"cpp_feature_check_example");
base::SingleThreadTaskExecutor task_executor(base::MessagePumpType::IO);
base::FileDescriptorWatcher watcher(task_executor.task_runner());
LOG(INFO) << "Creating bus";
dbus::Bus::Options options;
options.bus_type = dbus::Bus::SYSTEM;
options.dbus_task_runner =
base::ThreadPool::CreateSequencedTaskRunner({base::MayBlock()});
scoped_refptr<dbus::Bus> bus(new dbus::Bus(options));
base::RunLoop loop;
// 2 calls for the initial plus 2 for after chrome restarts.
base::RepeatingClosure barrier_closure =
base::BarrierClosure(4, loop.QuitClosure());
LOG(INFO) << "Creating lib";
CHECK(feature::PlatformFeatures::Initialize(bus))
<< "Failed to initialize lib";
feature::PlatformFeatures* feature_lib = feature::PlatformFeatures::Get();
LOG(INFO) << "ListenForRefetch";
feature_lib->ListenForRefetchNeeded(
base::BindRepeating(&Refetch, feature_lib, barrier_closure),
base::BindOnce(&Ready));
LOG(INFO) << "FetchState";
FetchState(feature_lib, barrier_closure);
loop.Run();
base::ThreadPoolInstance::Get()->Shutdown();
}