1 /*
2 * Copyright (c) 2023-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 "ace_forward_compatibility.h"
17
18 #include <fstream>
19 #include <unordered_set>
20
21 #include "arkui_log.h"
22 #ifdef OHOS_PLATFORM
23 #include "parameters.h"
24 #endif
25
26 namespace OHOS {
27 namespace Ace {
28 namespace {
29 constexpr uint32_t ARKUI_NEW_PIPELINE_MIN_VERSION = 9;
30 #if defined(WINDOWS_PLATFORM)
31 constexpr char ARKUI_LIB_NAME_COMPATIBLE[] = "libace_compatible.dll";
32 constexpr char ARKUI_LIB_NAME[] = "libace.dll";
33 #elif defined(MAC_PLATFORM)
34 constexpr char ARKUI_LIB_NAME_COMPATIBLE[] = "libace_compatible.dylib";
35 constexpr char ARKUI_LIB_NAME[] = "libace.dylib";
36 #elif defined(LINUX_PLATFORM)
37 constexpr char ARKUI_LIB_NAME_COMPATIBLE[] = "libace_compatible.so";
38 constexpr char ARKUI_LIB_NAME[] = "libace.so";
39 #else
40 constexpr char ARKUI_LIB_NAME_COMPATIBLE[] = "libace_compatible.z.so";
41 constexpr char ARKUI_LIB_NAME[] = "libace.z.so";
42 #endif
43 const std::unordered_set<std::string> FORCE_OLD_PIPELINE {
44 "com.ohos.launcher",
45 "com.ohos.sceneboard"
46 };
47 #ifdef OHOS_PLATFORM
48 const std::string KERNEL_TYPE_HM = "hongmeng";
49 const std::string RECLAIM_FILEPAGE_STRING_FOR_HM = "1";
50 #endif
51 const std::string RECLAIM_FILEPAGE_STRING_FOR_LINUX = "file";
52 } // namespace
53
Init(const std::string & bundleName,const uint32_t apiCompatibleVersion,bool forceFullUpdate)54 void AceForwardCompatibility::Init(const std::string& bundleName, const uint32_t apiCompatibleVersion, bool forceFullUpdate)
55 {
56 if (FORCE_OLD_PIPELINE.find(bundleName) != FORCE_OLD_PIPELINE.end()) {
57 isForceOldPipeline_ = true;
58 } else {
59 #ifdef OHOS_PLATFORM
60 isForceOldPipeline_ = OHOS::system::GetBoolParameter("persist.arkui.libace.og", true);
61 #else
62 isForceOldPipeline_ = true;
63 #endif
64 }
65
66 isNewPipeline_ = (apiCompatibleVersion >= ARKUI_NEW_PIPELINE_MIN_VERSION) && !forceFullUpdate;
67 isInited_ = true;
68 LOGI("AceForwardCompatibility [%{public}s] force:%{public}d newpipe:%{public}d", bundleName.c_str(),
69 isForceOldPipeline_, isNewPipeline_);
70 }
71
IsForceOldPipeline()72 bool AceForwardCompatibility::IsForceOldPipeline()
73 {
74 if (isInited_) {
75 return isForceOldPipeline_;
76 }
77 #ifdef OHOS_PLATFORM
78 return OHOS::system::GetBoolParameter("persist.arkui.libace.og", true);
79 #else
80 return true;
81 #endif
82 }
83
IsNewPipeline()84 bool AceForwardCompatibility::IsNewPipeline()
85 {
86 if (isInited_) {
87 return isNewPipeline_ && !isForceOldPipeline_;
88 }
89 isNewAppspawn_ = !IsForceOldPipeline();
90 return !IsForceOldPipeline();
91 }
92
IsUseNG()93 bool AceForwardCompatibility::IsUseNG()
94 {
95 return isNewPipeline_;
96 }
97
GetAceLibName()98 const char* AceForwardCompatibility::GetAceLibName()
99 {
100 const char* libName;
101 if (IsNewPipeline()) {
102 libName = ARKUI_LIB_NAME;
103 } else {
104 libName = ARKUI_LIB_NAME_COMPATIBLE;
105 }
106 return libName;
107 }
108
PipelineChanged()109 bool AceForwardCompatibility::PipelineChanged()
110 {
111 return (isNewPipeline_ && !isForceOldPipeline_) != isNewAppspawn_;
112 }
113
ReclaimFileCache(int32_t pid)114 void AceForwardCompatibility::ReclaimFileCache(int32_t pid)
115 {
116 LOGI("ReclaimFileCache start pid:%{public}d", pid);
117 if (pid <= 0) {
118 LOGE("get invalid pid:%{public}d", pid);
119 return;
120 }
121 std::string path = "/proc/" + std::to_string(pid) +"/reclaim";
122 std::string content = RECLAIM_FILEPAGE_STRING_FOR_LINUX;
123 #ifdef OHOS_PLATFORM
124
125 if (system::GetParameter("ohos.boot.kernel", "") == KERNEL_TYPE_HM) {
126 content = RECLAIM_FILEPAGE_STRING_FOR_HM;
127 }
128 #endif
129 std::ofstream outfile(path);
130 if (outfile.is_open()) {
131 outfile << content;
132 outfile.close();
133 } else {
134 LOGE("ace reclaim exception");
135 }
136 LOGI("ReclaimFileCache end");
137 }
138 } // namespace Ace
139 } // namespace OHOS
140