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 #ifndef SECURITY_GUARD_DATA_FORMAT_H 17 #define SECURITY_GUARD_DATA_FORMAT_H 18 19 #include <string> 20 21 #include "security_guard_define.h" 22 23 namespace OHOS::Security::SecurityGuard { 24 class DataFormatInterface { 25 public: 26 virtual ~DataFormatInterface() = default; 27 virtual bool CheckRiskContent(std::string content) = 0; 28 virtual void ParseConditions(std::string conditions, RequestCondition &reqCondition) = 0; 29 }; 30 31 class MockDataFormatInterface : public DataFormatInterface { 32 public: 33 MockDataFormatInterface() = default; 34 ~MockDataFormatInterface() override = default; 35 MOCK_METHOD1(CheckRiskContent, bool(std::string content)); 36 MOCK_METHOD2(ParseConditions, void(std::string conditions, RequestCondition &reqCondition)); 37 }; 38 39 class DataFormat { 40 public: CheckRiskContent(std::string content)41 static bool CheckRiskContent(std::string content) 42 { 43 if (instance_ == nullptr) { 44 return false; 45 } 46 return instance_->CheckRiskContent(content); 47 } 48 ParseConditions(std::string conditions,RequestCondition & reqCondition)49 static void ParseConditions(std::string conditions, RequestCondition &reqCondition) 50 { 51 if (instance_ == nullptr) { 52 return; 53 } 54 return instance_->ParseConditions(conditions, reqCondition); 55 } 56 GetInterface()57 static std::shared_ptr<MockDataFormatInterface> GetInterface() 58 { 59 if (instance_ == nullptr) { 60 std::lock_guard<std::mutex> lock(mutex_); 61 if (instance_ == nullptr) { 62 instance_ = std::make_shared<MockDataFormatInterface>(); 63 } 64 } 65 return instance_; 66 }; 67 DelInterface()68 static void DelInterface() 69 { 70 if (instance_ != nullptr) { 71 instance_.reset(); 72 } 73 }; 74 75 private: 76 static std::shared_ptr<MockDataFormatInterface> instance_; 77 static std::mutex mutex_; 78 }; 79 } // namespace OHOS::Security::SecurityGuard 80 81 #endif // SECURITY_GUARD_DATA_FORMAT_H 82