/* * Copyright (c) 2021 Huawei Device Co., Ltd. * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #ifndef FREEZE_RULE_CLUSTER_H #define FREEZE_RULE_CLUSTER_H #include #include #include #include #include #include #include "watch_point.h" namespace OHOS { namespace HiviewDFX { class FreezeResult { public: FreezeResult() : window_(0), code_(0), scope_(""), samePackage_(""), domain_(""), stringId_(""), action_("and"), ffrt_("") {}; FreezeResult(long window, const std::string& domain, const std::string& stringId) : window_(window), code_(0), scope_(""), samePackage_(""), domain_(domain), stringId_(stringId), action_("and"), ffrt_("") {}; FreezeResult(unsigned long code, const std::string& scope) : window_(0), code_(code), scope_(scope), samePackage_(""), domain_(""), stringId_(""), action_("and"), ffrt_("") {}; ~FreezeResult() {}; std::string GetDomain() const { return domain_; } std::string GetStringId() const { return stringId_; } unsigned long GetId() const { return code_; } void SetId(unsigned long code) { code_ = code; } std::string GetScope() const { return scope_; } void SetScope(const std::string& scope) { scope_ = scope; } long GetWindow() const { return window_; } std::string GetSamePackage() const { return samePackage_; } void SetSamePackage(const std::string& samePackage) { samePackage_ = samePackage; } std::string GetAction() const { return action_; } void SetAction(const std::string& action) { action_ = action; } std::string GetFfrt() const { return ffrt_; } void SetFfrt(const std::string& ffrt) { ffrt_ = ffrt; } private: long window_; unsigned long code_; std::string scope_; std::string samePackage_; std::string domain_; std::string stringId_; std::string action_; std::string ffrt_; }; class FreezeRule { public: FreezeRule() : domain_(""), stringId_("") {}; FreezeRule(const std::string& domain, const std::string& stringId) : domain_(domain), stringId_(stringId) {}; ~FreezeRule() { results_.clear(); } std::string GetDomain() const { return domain_; } void SetDomain(const std::string& domain) { domain_ = domain; } std::string GetStringId() const { return stringId_; } void SetStringId(const std::string& stringId) { stringId_ = stringId; } std::map GetMap() const { return results_; } void AddResult(const std::string& domain, const std::string& stringId, const FreezeResult& result); bool GetResult(const std::string& domain, const std::string& stringId, FreezeResult& result); private: std::string domain_; std::string stringId_; std::map results_; }; class FreezeRuleCluster { public: FreezeRuleCluster(); ~FreezeRuleCluster(); FreezeRuleCluster& operator=(const FreezeRuleCluster&) = delete; FreezeRuleCluster(const FreezeRuleCluster&) = delete; bool Init(); bool CheckFileSize(const std::string& path); bool ParseRuleFile(const std::string& file); void ParseTagFreeze(xmlNode* tag); void ParseTagRules(xmlNode* tag); void ParseTagRule(xmlNode* tag); void ParseTagLinks(xmlNode* tag, FreezeRule& rule); void ParseTagEvent(xmlNode* tag, FreezeResult& result); void ParseTagResult(xmlNode* tag, FreezeResult& result); void ParseTagRelevance(xmlNode* tag, FreezeResult& result); template T GetAttributeValue(xmlNode* node, const std::string& name); bool GetResult(const WatchPoint& watchPoint, std::vector& list); std::map> GetApplicationPairs() const { return applicationPairs_; } std::map> GetSystemPairs() const { return systemPairs_; } std::map> GetSysWarningPairs() const; private: static const inline std::string DEFAULT_RULE_FILE = "/system/etc/hiview/freeze_rules.xml"; static const inline std::string TAG_FREEZE = "freeze"; static const inline std::string TAG_RULES = "rules"; static const inline std::string TAG_RULE = "rule"; static const inline std::string TAG_LINKS = "links"; static const inline std::string TAG_EVENT = "event"; static const inline std::string TAG_RESULT = "result"; static const inline std::string TAG_RELEVANCE = "relevance"; static const inline std::string ATTRIBUTE_ID = "id"; static const inline std::string ATTRIBUTE_WINDOW = "window"; static const inline std::string ATTRIBUTE_DOMAIN = "domain"; static const inline std::string ATTRIBUTE_STRINGID = "stringid"; static const inline std::string ATTRIBUTE_TYPE = "type"; static const inline std::string ATTRIBUTE_USER = "user"; static const inline std::string ATTRIBUTE_WATCHPOINT = "watchpoint"; static const inline std::string ATTRIBUTE_CODE = "code"; static const inline std::string ATTRIBUTE_SCOPE = "scope"; static const inline std::string ATTRIBUTE_SAME_PACKAGE = "samePackage"; static const inline std::string attributeAction = "action"; static const inline std::string attributeFfrt = "ffrt"; static const inline std::string ATTRIBUTE_APPLICATION = "application"; static const inline std::string ATTRIBUTE_SYSTEM = "system"; static const int MAX_FILE_SIZE = 512 * 1024; std::map rules_; std::map> applicationPairs_; std::map> systemPairs_; std::map> sysWarningPairs_; }; } // namespace HiviewDFX } // namespace OHOS #endif