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