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 "ability_runtime/cj_ability_ffi.h"
17 
18 #include "ability_runtime/cj_ui_ability.h"
19 #include "ability_runtime/cj_ability_context.h"
20 #include "cj_common_ffi.h"
21 #include "hilog_tag_wrapper.h"
22 
23 namespace OHOS {
24 namespace AbilityRuntime {
25 extern "C" {
FFIAbilityGetAbilityContext(AbilityHandle abilityHandle)26 int64_t FFIAbilityGetAbilityContext(AbilityHandle abilityHandle)
27 {
28     if (abilityHandle == nullptr) {
29         TAG_LOGE(AAFwkTag::CONTEXT, "null abilityHandle");
30         return ERR_INVALID_INSTANCE_CODE;
31     }
32     auto ability = static_cast<CJUIAbility*>(abilityHandle);
33     auto context = ability->GetAbilityContext();
34     if (context == nullptr) {
35         TAG_LOGE(AAFwkTag::CONTEXT, "null abilityContext");
36         return ERR_INVALID_INSTANCE_CODE;
37     }
38     auto cjContext = FFI::FFIData::Create<CJAbilityContext>(context);
39     if (cjContext == nullptr) {
40         TAG_LOGE(AAFwkTag::CONTEXT, "GetAbilityContext failed, abilityContext is null");
41         return ERR_INVALID_INSTANCE_CODE;
42     }
43     return cjContext->GetID();
44 }
45 
FFIAbilityContextGetFilesDir(int64_t id,void (* accept)(const char *))46 void FFIAbilityContextGetFilesDir(int64_t id, void(*accept)(const char*))
47 {
48     auto cjContext = FFI::FFIData::GetData<CJAbilityContext>(id);
49     if (cjContext == nullptr) {
50         TAG_LOGE(AAFwkTag::CONTEXT, "null cj abilityContext");
51         return;
52     }
53     auto context = cjContext->GetAbilityContext();
54     if (context == nullptr) {
55         TAG_LOGE(AAFwkTag::CONTEXT, "null context");
56         return;
57     }
58     auto filesDir = context->GetFilesDir();
59     accept(filesDir.c_str());
60 }
61 }
62 } // namespace AbilityRuntime
63 } // namespace OHOS
64