1 /* 2 * Copyright (c) 2024 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 OHOS_ABILITY_RUNTIME_ERROR_MSG_UTIL_H 17 #define OHOS_ABILITY_RUNTIME_ERROR_MSG_UTIL_H 18 19 #include <mutex> 20 #include <string> 21 #include <unordered_map> 22 23 #include "iremote_object.h" 24 25 namespace OHOS::AbilityRuntime { 26 class ErrorMsgGuard { 27 public: 28 ErrorMsgGuard(sptr<IRemoteObject> token, uintptr_t scheduler, const std::string &name); // for ability 29 ErrorMsgGuard(pid_t pid, uintptr_t scheduler, const std::string &name); // for app 30 ~ErrorMsgGuard(); 31 32 ErrorMsgGuard(const ErrorMsgGuard &) = delete; 33 void operator=(const ErrorMsgGuard &) = delete; 34 private: 35 sptr<IRemoteObject> token_; // token, determines ability or app 36 pid_t pid_ = 0; 37 std::string errorKey_; 38 }; 39 40 class ErrorMgsUtil { 41 public: 42 ErrorMgsUtil& operator=(const ErrorMgsUtil&) = delete; 43 ErrorMgsUtil(const ErrorMgsUtil&) = delete; 44 virtual ~ErrorMgsUtil() = default; 45 static ErrorMgsUtil& GetInstance(); 46 static std::string BuildErrorKey(uintptr_t scheduler, const std::string &name); 47 48 void AddErrorMsg(const std::string &key, const std::string &errorMsg); 49 bool UpdateErrorMsg(const std::string &key, const std::string &errorMsg); 50 std::string DeleteErrorMsg(const std::string &key); 51 private: 52 ErrorMgsUtil() = default; 53 54 std::mutex errorMsgMapMutex_; 55 std::unordered_map<std::string, std::string> errorMsgMap_; 56 }; 57 } // namespace OHOS::AbilityRuntime 58 #endif // OHOS_ABILITY_RUNTIME_ERROR_MSG_UTIL_H