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 "cj_application_context.h"
17 
18 #include "ability_delegator_registry.h"
19 #include "application_context.h"
20 #include "cj_utils_ffi.h"
21 #include "hilog_tag_wrapper.h"
22 
23 namespace OHOS {
24 namespace ApplicationContextCJ {
25 using namespace OHOS::FFI;
26 using namespace OHOS::AbilityRuntime;
27 
GetArea()28 int CJApplicationContext::GetArea()
29 {
30     auto context = applicationContext_.lock();
31     if (context == nullptr) {
32         TAG_LOGE(AAFwkTag::CONTEXT, "null context");
33         return INVALID_CODE;
34     }
35     return context->GetArea();
36 }
37 
GetApplicationInfo()38 std::shared_ptr<AppExecFwk::ApplicationInfo> CJApplicationContext::GetApplicationInfo()
39 {
40     auto context = applicationContext_.lock();
41     if (context == nullptr) {
42         TAG_LOGE(AAFwkTag::CONTEXT, "null context");
43         return nullptr;
44     }
45     return context->GetApplicationInfo();
46 }
47 
48 extern "C" {
FFIGetArea(int64_t id)49 int64_t FFIGetArea(int64_t id)
50 {
51     auto context = FFI::FFIData::GetData<CJApplicationContext>(id);
52     if (context == nullptr) {
53         TAG_LOGE(AAFwkTag::CONTEXT, "null context");
54         return INVALID_CODE;
55     }
56     return context->GetArea();
57 }
58 
FFICJApplicationInfo(int64_t id)59 CApplicationInfo* FFICJApplicationInfo(int64_t id)
60 {
61     auto context = FFI::FFIData::GetData<CJApplicationContext>(id);
62     if (context == nullptr) {
63         TAG_LOGE(AAFwkTag::CONTEXT, "null context");
64         return nullptr;
65     }
66     auto appInfo = context->GetApplicationInfo();
67     CApplicationInfo* buffer = static_cast<CApplicationInfo*>(malloc(sizeof(CApplicationInfo)));
68     if (buffer == nullptr) {
69         TAG_LOGE(AAFwkTag::CONTEXT, "malloc appinfo fail");
70         return nullptr;
71     }
72     buffer->name = CreateCStringFromString(appInfo->name);
73     buffer->bundleName = CreateCStringFromString(appInfo->bundleName);
74     return buffer;
75 }
76 }
77 }
78 }