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/interfaces/cj_ffi/concurrency/cj_concurrency_ffi.h"
17 
18 #include "cj_lambda.h"
19 #include "bridge/common/utils/engine_helper.h"
20 #include "frameworks/core/common/container.h"
21 #include "frameworks/core/common/container_scope.h"
22 
23 using namespace OHOS::Ace;
24 using namespace OHOS::Ace::Framework;
25 
26 extern "C" {
FfiOHOSAceFrameworkPostTaskToMainThread(void (* callback)())27 void FfiOHOSAceFrameworkPostTaskToMainThread(void(*callback)())
28 {
29     auto currentObj = Container::CurrentSafely();
30     if (!currentObj) {
31         LOGE("Can not found valid container");
32         return;
33     }
34     auto executor = currentObj->GetTaskExecutor();
35     if (!executor) {
36         LOGE("can not get executor.");
37         return;
38     }
39     executor->PostTask(
40         [task = CJLambda::Create(callback), instanceId = currentObj->GetInstanceId()] {
41             ContainerScope scope(instanceId);
42             task();
43         },
44         TaskExecutor::TaskType::UI, "CJSPAWNMAIN");
45 }
46 
FfiOHOSAceFrameworkHasContainer()47 bool FfiOHOSAceFrameworkHasContainer()
48 {
49     auto currentObj = Container::Current();
50     if (!currentObj) {
51         LOGI("Can not get Current Container");
52         return false;
53     } else {
54         return true;
55     }
56 }
57 }
58