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