blob: 40c4540f40a0c5a706053a9f5c04691c65007b38 [file] [log] [blame]
Andrei Onea037d2822020-11-19 00:20:04 +00001/*
2 * Copyright (C) 2020 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#include "compat_framework.h"
18
19#include "android-base/logging.h"
20#include <sys/types.h>
21#include <unistd.h>
22
23namespace art {
24
25// Compat change states as strings.
26static constexpr char kUnknownChangeState[] = "UNKNOWN";
27static constexpr char kEnabledChangeState[] = "ENABLED";
28static constexpr char kDisabledChangeState[] = "DISABLED";
29static constexpr char kLoggedState[] = "LOGGED";
30
31bool CompatFramework::IsChangeEnabled(uint64_t change_id) {
32 const auto enabled = disabled_compat_changes_.count(change_id) == 0;
33 ReportChange(change_id, enabled ? ChangeState::kEnabled : ChangeState::kDisabled);
34 return enabled;
35}
36
37void CompatFramework::LogChange(uint64_t change_id) {
38 ReportChange(change_id, ChangeState::kLogged);
39}
40
41void CompatFramework::ReportChange(uint64_t change_id, ChangeState state) {
42 bool already_reported = reported_compat_changes_.count(change_id) != 0;
43 if (already_reported) {
44 return;
45 }
46 LOG(DEBUG) << "Compat change id reported: " << change_id << "; UID " << getuid()
47 << "; state: " << ChangeStateToString(state);
48 // TODO(145743810): add an up call to java to log to statsd
49 reported_compat_changes_.emplace(change_id);
50}
51
52std::string_view CompatFramework::ChangeStateToString(ChangeState state) {
53 switch (state) {
54 case ChangeState::kUnknown:
55 return kUnknownChangeState;
56 case ChangeState::kEnabled:
57 return kEnabledChangeState;
58 case ChangeState::kDisabled:
59 return kDisabledChangeState;
60 case ChangeState::kLogged:
61 return kLoggedState;
62 }
63}
64
65} // namespace art