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