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 #include "error_msg_util.h"
17
18 #include "freeze_util.h"
19
20 namespace OHOS::AbilityRuntime {
ErrorMsgGuard(sptr<IRemoteObject> token,uintptr_t scheduler,const std::string & name)21 ErrorMsgGuard::ErrorMsgGuard(sptr<IRemoteObject> token, uintptr_t scheduler, const std::string &name)
22 : token_(token), errorKey_(ErrorMgsUtil::BuildErrorKey(scheduler, name))
23 {
24 if (token != nullptr) {
25 ErrorMgsUtil::GetInstance().AddErrorMsg(errorKey_, "");
26 }
27 }
28
ErrorMsgGuard(pid_t pid,uintptr_t scheduler,const std::string & name)29 ErrorMsgGuard::ErrorMsgGuard(pid_t pid, uintptr_t scheduler, const std::string &name)
30 : pid_(pid), errorKey_(ErrorMgsUtil::BuildErrorKey(scheduler, name))
31 {
32 ErrorMgsUtil::GetInstance().AddErrorMsg(errorKey_, "");
33 }
34
~ErrorMsgGuard()35 ErrorMsgGuard::~ErrorMsgGuard()
36 {
37 auto errorMsg = ErrorMgsUtil::GetInstance().DeleteErrorMsg(errorKey_);
38 if (errorMsg.empty()) {
39 return;
40 }
41 if (token_) {
42 FreezeUtil::GetInstance().AppendLifecycleEvent(token_, errorMsg);
43 } else {
44 FreezeUtil::GetInstance().AddAppLifecycleEvent(pid_, errorMsg);
45 }
46 }
47
GetInstance()48 ErrorMgsUtil &ErrorMgsUtil::GetInstance()
49 {
50 static ErrorMgsUtil instance;
51 return instance;
52 }
53
BuildErrorKey(uintptr_t scheduler,const std::string & name)54 std::string ErrorMgsUtil::BuildErrorKey(uintptr_t scheduler, const std::string &name)
55 {
56 return std::to_string(scheduler) + "#" + name;
57 }
58
AddErrorMsg(const std::string & key,const std::string & errorMsg)59 void ErrorMgsUtil::AddErrorMsg(const std::string &key, const std::string &errorMsg)
60 {
61 std::lock_guard lock(errorMsgMapMutex_);
62 errorMsgMap_[key] = errorMsg;
63 }
64
UpdateErrorMsg(const std::string & key,const std::string & errorMsg)65 bool ErrorMgsUtil::UpdateErrorMsg(const std::string &key, const std::string &errorMsg)
66 {
67 std::lock_guard lock(errorMsgMapMutex_);
68 auto it = errorMsgMap_.find(key);
69 if (it == errorMsgMap_.end()) {
70 return false;
71 }
72 it->second = errorMsg;
73 return true;
74 }
75
DeleteErrorMsg(const std::string & key)76 std::string ErrorMgsUtil::DeleteErrorMsg(const std::string &key)
77 {
78 std::lock_guard lock(errorMsgMapMutex_);
79 auto it = errorMsgMap_.find(key);
80 if (it == errorMsgMap_.end()) {
81 return "";
82 }
83 auto result = std::move(it->second);
84 errorMsgMap_.erase(it);
85 return result;
86 }
87 } // namespace OHOS::AbilityRuntime