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 <cstring>
17 #include "cj_error_manager_ffi.h"
18 #include "cj_error_observer.h"
19 #include "application_data_manager.h"
20 #include "hilog_tag_wrapper.h"
21 #include "event_runner.h"
22
23 using namespace OHOS::AbilityRuntime;
24 using namespace OHOS::FFI;
25
26 namespace {
27 constexpr int ERR_PARAM = 401;
28 constexpr int ERROR_CODE_INVALID_ID = 16000003;
29 constexpr int ERROR_CODE_INVALID_CALLER = 16200001;
30 constexpr const char* ON_OFF_TYPE = "error";
31 int32_t g_serialNumber = 0;
32 std::shared_ptr<ErrorObserver> g_observer;
33
34 struct CJLoopObserver {
35 std::shared_ptr<OHOS::AppExecFwk::EventRunner> mainRunner;
36 std::function<void(int64_t)> callbackOnLoopTimeout = nullptr;
37 };
38 static std::shared_ptr<CJLoopObserver> g_loopObserver;
39 }
40
41 extern "C" {
FfiOHOSErrorManagerOn(char * onType,CErrorObserver observer)42 RetDataI32 FfiOHOSErrorManagerOn(char* onType, CErrorObserver observer)
43 {
44 TAG_LOGI(AAFwkTag::APPKIT, "FfiOHOSErrorManagerOn.");
45 RetDataI32 ret = { .code = ERR_PARAM, .data = 0 };
46 if (strcmp(onType, ON_OFF_TYPE) != 0) {
47 TAG_LOGE(AAFwkTag::APPKIT, "FfiOHOSErrorManagerOn failed.");
48 return ret;
49 }
50 int32_t observerId = g_serialNumber;
51 if (g_serialNumber < INT32_MAX) {
52 g_serialNumber++;
53 } else {
54 g_serialNumber = 0;
55 }
56
57 if (g_observer == nullptr) {
58 TAG_LOGI(AAFwkTag::APPKIT, "g_observer is null.");
59 g_observer = std::make_shared<ErrorObserver>();
60 OHOS::AppExecFwk::ApplicationDataManager::GetInstance().AddErrorObserver(g_observer);
61 }
62
63 g_observer->AddObserverObject(observerId, observer);
64 TAG_LOGI(AAFwkTag::APPKIT, "FfiOHOSErrorManagerOn success.");
65 ret.code = SUCCESS_CODE;
66 ret.data = observerId;
67 return ret;
68 }
69
FfiOHOSErrorManagerOff(char * offType,int observerId)70 int FfiOHOSErrorManagerOff(char* offType, int observerId)
71 {
72 TAG_LOGI(AAFwkTag::APPKIT, "FfiOHOSErrorManagerOff.");
73 int ret = ERR_PARAM;
74 TAG_LOGI(AAFwkTag::APPKIT, "unregister errorObserver called, observer:%{public}d", observerId);
75 if (strcmp(offType, ON_OFF_TYPE) != 0) {
76 TAG_LOGE(AAFwkTag::APPKIT, "FfiOHOSErrorManagerOff failed.");
77 return ret;
78 }
79
80 TAG_LOGI(AAFwkTag::APPKIT, "Unregister errorObserver called.");
81 if (g_observer == nullptr || !g_observer->RemoveObserverObject(observerId)) {
82 ret = ERROR_CODE_INVALID_ID;
83 return ret;
84 }
85
86 if (g_observer && g_observer->IsEmpty()) {
87 OHOS::AppExecFwk::ApplicationDataManager::GetInstance().RemoveErrorObserver();
88 g_observer = nullptr;
89 }
90 TAG_LOGI(AAFwkTag::APPKIT, "FfiOHOSErrorManagerOff success.");
91 ret = SUCCESS_CODE;
92 return ret;
93 }
94
CallbackTimeout(int64_t timeout)95 static void CallbackTimeout(int64_t timeout)
96 {
97 if (g_loopObserver != nullptr && g_loopObserver->callbackOnLoopTimeout != nullptr) {
98 g_loopObserver->callbackOnLoopTimeout(timeout);
99 }
100 }
101
FfiOHOSErrorManagerLoopObserverOn(int64_t timeout,CLoopObserver observer)102 int32_t FfiOHOSErrorManagerLoopObserverOn(int64_t timeout, CLoopObserver observer)
103 {
104 TAG_LOGD(AAFwkTag::APPKIT, "FfiOHOSErrorManagerLoopObserverOn.");
105 if (!OHOS::AppExecFwk::EventRunner::IsAppMainThread()) {
106 TAG_LOGE(AAFwkTag::APPKIT, "not mainThread");
107 return ERROR_CODE_INVALID_CALLER;
108 }
109 if (timeout <= 0) {
110 TAG_LOGE(AAFwkTag::APPKIT, "Param invalid, timeout<=0");
111 return ERR_PARAM;
112 }
113 if (g_loopObserver == nullptr) {
114 g_loopObserver = std::make_shared<CJLoopObserver>();
115 }
116 if (observer.callbackOnLoopTimeout != nullptr) {
117 g_loopObserver->callbackOnLoopTimeout = CJLambda::Create(observer.callbackOnLoopTimeout);
118 }
119 g_loopObserver->mainRunner = OHOS::AppExecFwk::EventRunner::GetMainEventRunner();
120 if (g_loopObserver->mainRunner == nullptr) {
121 return ERR_PARAM;
122 }
123 g_loopObserver->mainRunner->SetTimeout(timeout);
124 g_loopObserver->mainRunner->SetTimeoutCallback(CallbackTimeout);
125 return SUCCESS_CODE;
126 }
127
FfiOHOSErrorManagerLoopObserverOff()128 int32_t FfiOHOSErrorManagerLoopObserverOff()
129 {
130 TAG_LOGD(AAFwkTag::APPKIT, "FfiOHOSErrorManagerLoopObserverOff.");
131 if (!OHOS::AppExecFwk::EventRunner::IsAppMainThread()) {
132 TAG_LOGE(AAFwkTag::APPKIT, "not mainThread");
133 return ERROR_CODE_INVALID_CALLER;
134 }
135 if (g_loopObserver) {
136 g_loopObserver.reset();
137 g_loopObserver = nullptr;
138 TAG_LOGI(AAFwkTag::APPKIT, "LoopObserverOff success");
139 }
140 return SUCCESS_CODE;
141 }
142 }