summaryrefslogtreecommitdiff
path: root/runtime/compat_framework.cc
blob: 17d423bbed6c99a8d51f4bcb250c196a3af0cb2a (plain)
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
/*
 * Copyright (C) 2020 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include "compat_framework.h"

#include <sys/types.h>
#include <unistd.h>

#include "android-base/logging.h"
#include "thread-current-inl.h"

namespace art {

// Compat change states as strings.
static constexpr char kUnknownChangeState[] = "UNKNOWN";
static constexpr char kEnabledChangeState[] = "ENABLED";
static constexpr char kDisabledChangeState[] = "DISABLED";
static constexpr char kLoggedState[] = "LOGGED";

CompatFramework::CompatFramework()
    : reported_compat_changes_lock_("reported compat changes lock") {}

CompatFramework::~CompatFramework() {}

bool CompatFramework::IsChangeEnabled(uint64_t change_id) {
  const auto enabled = disabled_compat_changes_.count(change_id) == 0;
  ReportChange(change_id, enabled ? ChangeState::kEnabled : ChangeState::kDisabled);
  return enabled;
}

void CompatFramework::LogChange(uint64_t change_id) {
  ReportChange(change_id, ChangeState::kLogged);
}

void CompatFramework::ReportChange(uint64_t change_id, ChangeState state) {
  MutexLock mu(Thread::Current(), reported_compat_changes_lock_);
  bool already_reported = reported_compat_changes_.count(change_id) != 0;
  if (already_reported) {
    return;
  }
  LOG(DEBUG) << "Compat change id reported: " << change_id << "; UID " << getuid()
            << "; state: " << ChangeStateToString(state);
  // TODO(145743810): add an up call to java to log to statsd
  reported_compat_changes_.emplace(change_id);
}

std::string_view CompatFramework::ChangeStateToString(ChangeState state) {
  switch (state) {
    case ChangeState::kUnknown:
      return kUnknownChangeState;
    case ChangeState::kEnabled:
      return kEnabledChangeState;
    case ChangeState::kDisabled:
      return kDisabledChangeState;
    case ChangeState::kLogged:
      return kLoggedState;
  }
}

}  // namespace art