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 "bridge/cj_frontend/cppview/lazy_foreach_func.h"
17 
18 #include "bridge/cj_frontend/cppview/data_change_listener.h"
19 #include "bridge/cj_frontend/runtime/cj_runtime_delegate.h"
20 
21 using namespace OHOS::Ace;
22 using namespace OHOS::Ace::Framework;
23 using namespace OHOS::FFI;
24 using namespace std;
25 
26 namespace OHOS::Ace::Framework {
27 
GenerateKey(int64_t index)28 string LazyForEachFuncs::GenerateKey(int64_t index)
29 {
30     auto funcs = CJRuntimeDelegate::GetInstance()->GetCJFuncs();
31     if (!funcs.atCOHOSAceFrameworkLazyForEachFuncsGenerateKey) {
32         LOGE("Failed to invoke cj function: LazyForEachFuncs GenerateKey, return empty id!");
33         return "";
34     }
35     auto cstr = funcs.atCOHOSAceFrameworkLazyForEachFuncsGenerateKey(GetID(), index);
36     std::string res = cstr.value;
37     cstr.free(cstr.value);
38     return res;
39 }
40 
GenerateItem(int64_t index)41 void LazyForEachFuncs::GenerateItem(int64_t index)
42 {
43     auto funcs = CJRuntimeDelegate::GetInstance()->GetCJFuncs();
44     void (*cjFunc)(int64_t, int64_t) = funcs.atCOHOSAceFrameworkLazyForEachFuncsGenerateItem;
45     if (!cjFunc) {
46         LOGE("Failed to invoke cj function: LazyForEachFuncs GenerateItem!");
47         return;
48     }
49     cjFunc(GetID(), index);
50 }
51 
GetTotalCount()52 int64_t LazyForEachFuncs::GetTotalCount()
53 {
54     auto funcs = CJRuntimeDelegate::GetInstance()->GetCJFuncs();
55     auto cjFunc = funcs.atCOHOSAceFrameworkLazyForEachFuncsGetTotalCount;
56     if (!cjFunc) {
57         LOGE("Failed to invoke cj function: LazyForEachFuncs GetTotalCount, return 0!");
58         return 0;
59     }
60     int64_t count = cjFunc(GetID());
61     return count;
62 }
63 
RegisterListenerFunc(const sptr<CJDataChangeListener> & listener)64 void LazyForEachFuncs::RegisterListenerFunc(const sptr<CJDataChangeListener>& listener)
65 {
66     auto funcs = CJRuntimeDelegate::GetInstance()->GetCJFuncs();
67     auto cjFunc = funcs.atCOHOSAceFrameworkLazyForEachFuncsDataChangeListenerRegister;
68     if (!cjFunc) {
69         LOGE("Failed to invoke cj function: LazyForEachFuncs DataChangeListenerRegister!");
70         return;
71     }
72     cjFunc(GetID(), listener->GetID());
73 }
74 
UnRegisterListenerFunc(const sptr<CJDataChangeListener> & listener)75 void LazyForEachFuncs::UnRegisterListenerFunc(const sptr<CJDataChangeListener>& listener)
76 {
77     auto funcs = CJRuntimeDelegate::GetInstance()->GetCJFuncs();
78     auto cjFunc = funcs.atCOHOSAceFrameworkLazyForEachFuncsDataChangeListenerUnregister;
79     if (!cjFunc) {
80         LOGE("Failed to invoke cj function: LazyForEachFuncs DataChangeListenerUnregister!");
81         return;
82     }
83     cjFunc(GetID(), listener->GetID());
84 }
85 
MarkLazyForEachProcess(const std::string & key)86 void LazyForEachFuncs::MarkLazyForEachProcess(const std::string& key)
87 {
88     auto funcs = CJRuntimeDelegate::GetInstance()->GetCJFuncs();
89     auto cjFunc = funcs.atCOHOSAceFrameworkLazyForEachFuncsMarkLazy;
90     if (!cjFunc) {
91         LOGE("Failed to invoke cj function: LazyForEachFuncs MarkLazy!");
92         return;
93     }
94     cjFunc(GetID(), key.c_str());
95 }
96 
ResetLazyForEachProcess()97 void LazyForEachFuncs::ResetLazyForEachProcess()
98 {
99     auto funcs = CJRuntimeDelegate::GetInstance()->GetCJFuncs();
100     auto cjFunc = funcs.atCOHOSAceFrameworkLazyForEachFuncsResetLazy;
101     if (!cjFunc) {
102         LOGE("Failed to invoke cj function: LazyForEachFuncs ResetLazy!");
103         return;
104     }
105     cjFunc(GetID());
106 }
107 
RemoveChildGroupById(const std::string & composedId)108 void LazyForEachFuncs::RemoveChildGroupById(const std::string& composedId)
109 {
110     auto funcs = CJRuntimeDelegate::GetInstance()->GetCJFuncs();
111     auto cjFunc = funcs.atCOHOSAceFrameworkLazyForEachFuncsRemoveChildGroup;
112     if (!cjFunc) {
113         LOGE("Failed to invoke cj function: LazyForEachFuncs RemoveChildGroup!");
114         return;
115     }
116     cjFunc(GetID(), composedId.c_str());
117 }
118 
119 } // namespace OHOS::Ace::Framework
120