Andrei Onea | 037d282 | 2020-11-19 00:20:04 +0000 | [diff] [blame] | 1 | /* |
| 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 | #ifndef ART_RUNTIME_COMPAT_FRAMEWORK_H_ |
| 18 | #define ART_RUNTIME_COMPAT_FRAMEWORK_H_ |
| 19 | |
| 20 | #include <set> |
| 21 | |
| 22 | #include "base/string_view_cpp20.h" |
| 23 | |
| 24 | namespace art { |
| 25 | |
| 26 | // ART counterpart of the compat framework (go/compat-framework). |
| 27 | // Created in order to avoid repeated up-calls to Java. |
| 28 | class CompatFramework { |
| 29 | public: |
| 30 | // Compat change reported state |
| 31 | // This must be kept in sync with AppCompatibilityChangeReported.State in |
Jeffrey Huang | a3148d2 | 2020-12-04 11:42:55 -0800 | [diff] [blame^] | 32 | // frameworks/proto_logging/stats/atoms.proto |
Andrei Onea | 037d282 | 2020-11-19 00:20:04 +0000 | [diff] [blame] | 33 | enum class ChangeState { |
| 34 | kUnknown, |
| 35 | kEnabled, |
| 36 | kDisabled, |
| 37 | kLogged |
| 38 | }; |
| 39 | |
| 40 | void SetDisabledCompatChanges(const std::set<uint64_t>& disabled_changes) { |
| 41 | disabled_compat_changes_ = disabled_changes; |
| 42 | } |
| 43 | |
| 44 | const std::set<uint64_t>& GetDisabledCompatChanges() const { |
| 45 | return disabled_compat_changes_; |
| 46 | } |
| 47 | // Query if a given compatibility change is enabled for the current process. |
| 48 | // This also gets logged to logcat, and we add the information we logged in |
| 49 | // reported_compat_changes_. This ensures we only log once per change id for the app's lifetime. |
| 50 | bool IsChangeEnabled(uint64_t change_id); |
| 51 | |
| 52 | // Logs that the code path for this compatibility change has been reached. |
| 53 | // This also gets logged to logcat, and we add the information we logged in |
| 54 | // reported_compat_changes_. This ensures we only log once per change id for the app's lifetime. |
| 55 | void LogChange(uint64_t change_id); |
| 56 | |
| 57 | private: |
| 58 | // Get a string equivalent for a compatibility change state. |
| 59 | static std::string_view ChangeStateToString(ChangeState s); |
| 60 | // Report the state of a compatibility change to logcat. |
| 61 | // TODO(145743810): also report to statsd. |
| 62 | void ReportChange(uint64_t change_id, ChangeState state); |
| 63 | |
| 64 | // A set of disabled compat changes for the running app, all other changes are enabled. |
| 65 | std::set<uint64_t> disabled_compat_changes_; |
| 66 | |
| 67 | // A set of repoted compat changes for the running app. |
| 68 | std::set<uint64_t> reported_compat_changes_; |
| 69 | }; |
| 70 | |
| 71 | } // namespace art |
| 72 | |
| 73 | #endif // ART_RUNTIME_COMPAT_FRAMEWORK_H_ |