1 /*
2  * Copyright (c) 2023 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 "scene_board_judgement.h"
17 
18 namespace OHOS {
19 namespace Rosen {
IsSceneBoardEnabled()20 bool SceneBoardJudgement::IsSceneBoardEnabled()
21 {
22     static bool isSceneBoardEnabled = false;
23     static bool initialized = false;
24     static std::mutex scbEnabledMutex;
25     if (!initialized) {
26         std::lock_guard<std::mutex> lock(scbEnabledMutex);
27         InitWithConfigFile("/etc/sceneboard.config", isSceneBoardEnabled);
28         initialized = true;
29     }
30     return isSceneBoardEnabled;
31 }
32 
SafeGetLine(std::ifstream & configFile,std::string & line)33 std::ifstream& SceneBoardJudgement::SafeGetLine(std::ifstream& configFile, std::string& line)
34 {
35     std::getline(configFile, line);
36     if (line.size() && line[line.size() - 1] == '\r') {
37         line = line.substr(0, line.size() - 1);
38     }
39     return configFile;
40 }
41 
InitWithConfigFile(const char * filePath,bool & enabled)42 void SceneBoardJudgement::InitWithConfigFile(const char* filePath, bool& enabled)
43 {
44     std::ifstream configFile(filePath);
45     std::string line;
46     if (configFile.is_open() && SafeGetLine(configFile, line) && line == "ENABLED") {
47         enabled = true;
48     }
49     configFile.close();
50 }
51 } // namespace Rosen
52 } // namespace OHOS
53