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.h>
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 
22 using namespace OHOS::AbilityRuntime;
23 using namespace OHOS::FFI;
24 
25 namespace {
26     constexpr int ERR_PARAM = 401;
27     constexpr int ERROR_CODE_INVALID_ID = 16000003;
28     constexpr const char* ON_OFF_TYPE = "error";
29     int32_t g_serialNumber = 0;
30     std::shared_ptr<ErrorObserver> g_observer;
31 }
32 
33 extern "C" {
FfiOHOSErrorManagerOn(char * onType,CErrorObserver observer)34 RetDataI32 FfiOHOSErrorManagerOn(char* onType, CErrorObserver observer)
35 {
36     TAG_LOGI(AAFwkTag::APPKIT, "FfiOHOSErrorManagerOn.");
37     RetDataI32 ret = { .code = ERR_PARAM, .data = 0 };
38     if (strcmp(onType, ON_OFF_TYPE) != 0) {
39         TAG_LOGE(AAFwkTag::APPKIT, "FfiOHOSErrorManagerOn failed.");
40         return ret;
41     }
42     int32_t observerId = g_serialNumber;
43     if (g_serialNumber < INT32_MAX) {
44         g_serialNumber++;
45     } else {
46         g_serialNumber = 0;
47     }
48 
49     if (g_observer == nullptr) {
50         TAG_LOGI(AAFwkTag::APPKIT, "g_observer is null.");
51         g_observer = std::make_shared<ErrorObserver>();
52         OHOS::AppExecFwk::ApplicationDataManager::GetInstance().AddErrorObserver(g_observer);
53     }
54 
55     g_observer->AddObserverObject(observerId, observer);
56     TAG_LOGI(AAFwkTag::APPKIT, "FfiOHOSErrorManagerOn success.");
57     ret.code = SUCCESS_CODE;
58     ret.data = observerId;
59     return ret;
60 }
61 
FfiOHOSErrorManagerOff(char * offType,int observerId)62 int FfiOHOSErrorManagerOff(char* offType, int observerId)
63 {
64     TAG_LOGI(AAFwkTag::APPKIT, "FfiOHOSErrorManagerOff.");
65     int ret = ERR_PARAM;
66     TAG_LOGI(AAFwkTag::APPKIT, "unregister errorObserver called, observer:%{public}d", observerId);
67     if (strcmp(offType, ON_OFF_TYPE) != 0) {
68         TAG_LOGE(AAFwkTag::APPKIT, "FfiOHOSErrorManagerOff failed.");
69         return ret;
70     }
71 
72     TAG_LOGI(AAFwkTag::APPKIT, "Unregister errorObserver called.");
73     if (g_observer == nullptr || !g_observer->RemoveObserverObject(observerId)) {
74         ret = ERROR_CODE_INVALID_ID;
75         return ret;
76     }
77 
78     if (g_observer && g_observer->IsEmpty()) {
79         OHOS::AppExecFwk::ApplicationDataManager::GetInstance().RemoveErrorObserver();
80         g_observer = nullptr;
81     }
82     TAG_LOGI(AAFwkTag::APPKIT, "FfiOHOSErrorManagerOff success.");
83     ret = SUCCESS_CODE;
84     return ret;
85 }
86 }