1 /* 2 * Copyright (c) 2023 Huawei Device Co., Ltd. 3 * Licensed under the Apache License, Version 2.0 (the "License"); 4 * you may not use this file except in compliance with the License. 5 * You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software 10 * distributed under the License is distributed on an "AS IS" BASIS, 11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 * See the License for the specific language governing permissions and 13 * limitations under the License. 14 */ 15 16 #ifndef ANR_MANAGER_H 17 #define ANR_MANAGER_H 18 19 #include <functional> 20 #include <mutex> 21 #include <string> 22 #include <unordered_map> 23 24 #include "nocopyable.h" 25 #include "singleton.h" 26 27 #include "event_stage.h" 28 #include "ws_common.h" 29 30 namespace OHOS { 31 namespace Rosen { 32 33 class ANRManager final { 34 DECLARE_DELAYED_SINGLETON(ANRManager); 35 public: 36 DISALLOW_COPY_AND_MOVE(ANRManager); 37 void Init(); 38 void AddTimer(int32_t eventId, int32_t persistentId); 39 void MarkProcessed(int32_t eventId, int32_t persistentId); 40 bool IsANRTriggered(int32_t persistentId); 41 void SwitchAnr(bool status); 42 void OnSessionLost(int32_t persistentId); 43 void OnBackground(int32_t persistentId); 44 void SetApplicationInfo(int32_t persistentId, int32_t pid, const std::string& uid); 45 void SetAnrObserver(std::function<void(int32_t)> anrObserver); 46 void SetAppInfoGetter(std::function<void(int32_t, std::string&, int32_t)> callback); 47 std::string GetBundleName(int32_t pid, int32_t uid); 48 private: 49 struct AppInfo { 50 int32_t pid { -1 }; 51 std::string bundleName { "unknown" }; 52 }; 53 void RemoveTimers(int32_t persistentId); 54 void RemovePersistentId(int32_t persistentId); 55 void ExecuteAnrObserver(int32_t pid); 56 ANRManager::AppInfo GetAppInfoByPersistentId(int32_t persistentId); 57 private: 58 std::atomic_bool switcher_ { true }; 59 std::atomic_int32_t anrTimerCount_ { 0 }; 60 std::mutex mtx_; 61 std::unordered_map<int32_t, AppInfo> applicationMap_; 62 std::function<void(int32_t)> anrObserver_; 63 std::function<void(int32_t, std::string&, int32_t)> appInfoGetter_; 64 }; 65 } // namespace Rosen 66 } // namespace OHOS 67 #endif // ANR_MANAGER_H 68