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 "cj_error_observer.h"
17 #include "hilog_tag_wrapper.h"
18
19 namespace OHOS {
20 namespace AbilityRuntime {
21
MallocCString(const std::string & origin)22 char *MallocCString(const std::string &origin)
23 {
24 if (origin.empty()) {
25 return nullptr;
26 }
27 auto len = origin.length() + 1;
28 char* res = (char*)malloc(sizeof(char) * len);
29 if (res == nullptr) {
30 return nullptr;
31 }
32 return std::char_traits<char>::copy(res, origin.c_str(), len);
33 }
34
ErrorObserver()35 ErrorObserver::ErrorObserver() {};
36
OnExceptionObject(const AppExecFwk::ErrorObject & errorObj)37 void ErrorObserver::OnExceptionObject(const AppExecFwk::ErrorObject &errorObj)
38 {
39 TAG_LOGI(AAFwkTag::APPKIT, "OnExceptionObject come.");
40 std::weak_ptr<ErrorObserver> thisWeakPtr(shared_from_this());
41 std::shared_ptr<ErrorObserver> observer = thisWeakPtr.lock();
42 if (observer) {
43 observer->HandleException(errorObj);
44 }
45 }
46
OnUnhandledException(const std::string errMsg)47 void ErrorObserver::OnUnhandledException(const std::string errMsg)
48 {
49 TAG_LOGI(AAFwkTag::APPKIT, "OnUnhandledException come.");
50 std::weak_ptr<ErrorObserver> thisWeakPtr(shared_from_this());
51 std::shared_ptr<ErrorObserver> observer = thisWeakPtr.lock();
52 if (observer) {
53 observer->HandleOnUnhandledException(errMsg);
54 }
55 }
56
HandleOnUnhandledException(const std::string & errMsg)57 void ErrorObserver::HandleOnUnhandledException(const std::string &errMsg)
58 {
59 TAG_LOGI(AAFwkTag::APPKIT, "HandleOnUnhandledException come.");
60 auto tmpMap = observerObjectMap_;
61 for (auto &item : tmpMap) {
62 auto obj = item.second;
63 char* cstr = MallocCString(errMsg);
64 if (cstr == nullptr) {
65 TAG_LOGE(AAFwkTag::APPKIT, "HandleOnUnhandledException failed.");
66 continue;
67 }
68 obj.callbackOnUnhandledException(cstr);
69 }
70 }
71
HandleException(const AppExecFwk::ErrorObject & errorObj)72 void ErrorObserver::HandleException(const AppExecFwk::ErrorObject &errorObj)
73 {
74 TAG_LOGI(AAFwkTag::APPKIT, "HandleException come.");
75 auto tmpMap = observerObjectMap_;
76 for (auto &item : tmpMap) {
77 auto obj = item.second;
78 if (obj.callbackOnException == nullptr) {
79 return;
80 }
81 CErrorObject cjErrorObj;
82 cjErrorObj.name = MallocCString(errorObj.name);
83 cjErrorObj.message = MallocCString(errorObj.message);
84 cjErrorObj.stack = MallocCString(errorObj.stack);
85 obj.callbackOnException(cjErrorObj);
86 }
87 }
88
AddObserverObject(const int32_t observerId,CErrorObserver observer)89 void ErrorObserver::AddObserverObject(const int32_t observerId, CErrorObserver observer)
90 {
91 MapErrorObserver mObserver;
92 mObserver.callbackOnUnhandledException = CJLambda::Create(observer.callbackOnUnhandledException);
93 if (observer.callbackOnException == nullptr) {
94 mObserver.callbackOnException = nullptr;
95 } else {
96 mObserver.callbackOnException = CJLambda::Create(observer.callbackOnException);
97 }
98 observerObjectMap_.emplace(observerId, mObserver);
99 }
100
IsEmpty()101 bool ErrorObserver::IsEmpty()
102 {
103 bool isEmpty = observerObjectMap_.empty();
104 return isEmpty;
105 }
106
RemoveObserverObject(const int32_t observerId)107 bool ErrorObserver::RemoveObserverObject(const int32_t observerId)
108 {
109 bool result = false;
110 result = (observerObjectMap_.erase(observerId) == 1);
111 return result;
112 }
113
114 }
115 }