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