1 /*
2  * Copyright (c) 2022 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/ace_new_pipe_judgement.h"
17 
18 #include <fstream>
19 
20 #include "base/utils/utils.h"
21 
22 namespace OHOS::Ace {
23 namespace {
24 
25 const std::string CONFIG_PATH = "/etc/";
26 const std::string ACE_NEW_PIPE_CONFIG_FILE_NAME = "acenewpipe.config";
27 const std::string ACE_NEW_PIPE_DISABLED_TAG = "DISABLED";
28 const std::string ACE_NEW_PIPE_ENABLED_FOR_ALL_TAG = "ENABLED_FOR_ALL";
29 const std::string NEW_PIPE_ENABLED_RELEASE_TYPE = "Beta4";
30 const std::string NEW_PIPE_ENABLED_RELEASE_TYPE_NEW = "Beta5";
31 const std::string NEW_PIPE_ENABLED_RELEASE_TYPE_RELEASE = "Release";
32 constexpr int32_t NEW_PIPE_MIN_VERSION = 9;
33 
34 } // namespace
35 
StartsWith(const std::string& str, const std::string& prefix)36 bool StartsWith(const std::string& str, const std::string& prefix)
37 {
38     return str.size() >= prefix.size() && str.substr(0, prefix.size()) == prefix;
39 }
40 
SafeGetLine(std::ifstream& configFile, std::string& line)41 std::ifstream& AceNewPipeJudgement::SafeGetLine(std::ifstream& configFile, std::string& line)
42 {
43     std::string myline;
44     std::getline(configFile, myline);
45     if (!myline.empty() && myline[myline.size() - 1] == '\r') {
46         line = myline.substr(0, myline.size() - 1);
47     } else {
48         line = myline;
49     }
50     return configFile;
51 }
52 
QueryAceNewPipeEnabledFA(const std::string& packagename, uint32_t apiCompatibleVersion, uint32_t apiTargetVersion, const std::string& apiReleaseType)53 bool AceNewPipeJudgement::QueryAceNewPipeEnabledFA(const std::string& packagename, uint32_t apiCompatibleVersion,
54     uint32_t apiTargetVersion, const std::string& apiReleaseType)
55 {
56     if (((apiTargetVersion == NEW_PIPE_MIN_VERSION &&
57             (apiReleaseType == NEW_PIPE_ENABLED_RELEASE_TYPE || apiReleaseType == NEW_PIPE_ENABLED_RELEASE_TYPE_NEW ||
58                 apiReleaseType == NEW_PIPE_ENABLED_RELEASE_TYPE_RELEASE ||
59                 SystemProperties::GetExtSurfaceEnabled())) ||
60             apiTargetVersion > NEW_PIPE_MIN_VERSION) &&
61         apiCompatibleVersion >= NEW_PIPE_MIN_VERSION) {
62         return true;
63     }
64     switch (aceNewPipeEnabledType_) {
65         case AceNewPipeEnabledType::ACE_NEW_PIPE_PARTIALLY_ENABLED:
66             return aceNewPipeEnabledList_.find(packagename) != aceNewPipeEnabledList_.end();
67         case AceNewPipeEnabledType::ACE_NEW_PIPE_ENABLED_FOR_ALL:
68             return true;
69         case AceNewPipeEnabledType::ACE_NEW_PIPE_DISABLED:
70             return false;
71         default:
72             return false;
73     }
74 }
75 
QueryAceNewPipeEnabledStage(const std::string& packagename, uint32_t apiCompatibleVersion, uint32_t apiTargetVersion, const std::string& apiReleaseType, bool closeArkTSPartialUpdate)76 bool AceNewPipeJudgement::QueryAceNewPipeEnabledStage(const std::string& packagename, uint32_t apiCompatibleVersion,
77     uint32_t apiTargetVersion, const std::string& apiReleaseType, bool closeArkTSPartialUpdate)
78 {
79     if (closeArkTSPartialUpdate) {
80         return false;
81     }
82     if (((apiTargetVersion == NEW_PIPE_MIN_VERSION &&
83              (apiReleaseType == NEW_PIPE_ENABLED_RELEASE_TYPE || apiReleaseType == NEW_PIPE_ENABLED_RELEASE_TYPE_NEW ||
84                  apiReleaseType == NEW_PIPE_ENABLED_RELEASE_TYPE_RELEASE ||
85                  SystemProperties::GetExtSurfaceEnabled())) ||
86             apiTargetVersion > NEW_PIPE_MIN_VERSION) &&
87         apiCompatibleVersion >= NEW_PIPE_MIN_VERSION) {
88         return true;
89     }
90     switch (aceNewPipeEnabledType_) {
91         case AceNewPipeEnabledType::ACE_NEW_PIPE_PARTIALLY_ENABLED:
92             return aceNewPipeEnabledList_.find(packagename) != aceNewPipeEnabledList_.end();
93         case AceNewPipeEnabledType::ACE_NEW_PIPE_ENABLED_FOR_ALL:
94             return true;
95         case AceNewPipeEnabledType::ACE_NEW_PIPE_DISABLED:
96             return false;
97         default:
98             return false;
99     }
100 }
101 
InitAceNewPipeConfig()102 void AceNewPipeJudgement::InitAceNewPipeConfig()
103 {
104     if (!InitedAceNewPipeConfig_) {
105         InitAceNewPipeWithConfigFile();
106         LOGI("Init RenderService UniRender Type:%{public}d", aceNewPipeEnabledType_);
107     }
108 }
109 
InitAceNewPipeWithConfigFile()110 void AceNewPipeJudgement::InitAceNewPipeWithConfigFile()
111 {
112     // open config file
113     std::string configFilePath = CONFIG_PATH + ACE_NEW_PIPE_CONFIG_FILE_NAME;
114     std::ifstream configFile = std::ifstream(configFilePath.c_str());
115     std::string line;
116     // first line, init aceNewPipeEnabledType_
117     if (!configFile.is_open() || !SafeGetLine(configFile, line) || line.empty()) {
118         aceNewPipeEnabledType_ = AceNewPipeEnabledType::ACE_NEW_PIPE_DISABLED;
119     } else if (line == ACE_NEW_PIPE_DISABLED_TAG) {
120         aceNewPipeEnabledType_ = AceNewPipeEnabledType::ACE_NEW_PIPE_DISABLED;
121     } else if (line == ACE_NEW_PIPE_ENABLED_FOR_ALL_TAG) {
122         aceNewPipeEnabledType_ = AceNewPipeEnabledType::ACE_NEW_PIPE_ENABLED_FOR_ALL;
123     } else {
124         while (SafeGetLine(configFile, line)) {
125             if (line.empty() || StartsWith(line, "//")) {
126                 continue;
127             }
128             aceNewPipeEnabledList_.insert(line);
129         }
130         if (!aceNewPipeEnabledList_.empty()) {
131             aceNewPipeEnabledType_ = AceNewPipeEnabledType::ACE_NEW_PIPE_PARTIALLY_ENABLED;
132         }
133     }
134     configFile.close();
135     InitedAceNewPipeConfig_ = true;
136 }
137 } // namespace OHOS::Ace