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 "adapter/ohos/entrance/cj_utils/cj_utils.h"
17
18 #include <regex>
19
20 #include "interfaces/inner_api/ace/utils.h"
21
22 #include "base/log/log.h"
23
24 namespace OHOS::Ace::CJUtils {
25 constexpr char CJ_FRONTEND_LIB_NAME[] = "libcj_frontend_ohos.z.so";
26 constexpr char CJ_FRONTEND_CREATE_FUNC[] = "OHOS_ACE_CreateCJFrontend";
27 using GetCJFrontendCreateFunc = void* (*)(bool, bool);
28
LoadCjFrontend(bool isNewPipeline,bool isStageModel)29 void* LoadCjFrontend(bool isNewPipeline, bool isStageModel)
30 {
31 LIBHANDLE handle = LOADLIB(CJ_FRONTEND_LIB_NAME);
32 if (handle == nullptr) {
33 LOGE("LOADLIB cj frontend lib failed.");
34 return nullptr;
35 }
36 auto createFunc = reinterpret_cast<GetCJFrontendCreateFunc>(LOADSYM(handle, CJ_FRONTEND_CREATE_FUNC));
37 if (createFunc == nullptr) {
38 LOGE("LOADSYM CJ_FRONTEND_CREATE_FUNC failed");
39 FREELIB(handle);
40 return nullptr;
41 }
42 return createFunc(isNewPipeline, isStageModel);
43 }
44
isCjAbility(const std::string & info)45 bool isCjAbility(const std::string& info)
46 {
47 // in cj application, the srcEntry format should be packageName.AbilityClassName.
48 std::string pattern = "^([a-zA-Z0-9_]+\\.)+[a-zA-Z0-9_]+$";
49 return std::regex_match(info, std::regex(pattern));
50 }
51
IsCJFrontendContext(OHOS::AbilityRuntime::Context * context)52 bool IsCJFrontendContext(OHOS::AbilityRuntime::Context* context)
53 {
54 LOGD("IsCJFrontendContext start.");
55 if (!context) {
56 return false;
57 }
58 auto moduleInfo = context->GetHapModuleInfo();
59 if (!moduleInfo) {
60 return false;
61 }
62 auto abilityInfos = moduleInfo->abilityInfos;
63 if (abilityInfos.empty()) {
64 return false;
65 }
66 return isCjAbility(abilityInfos.front().srcEntrance);
67 }
68 } // namespace OHOS::Ace::CJUtils