• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2022-2025 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 #include "app_event_watcher.h"
16 
17 #include "event_json_util.h"
18 #include "hiappevent_common.h"
19 #include "hilog/log.h"
20 
21 #undef LOG_DOMAIN
22 #define LOG_DOMAIN 0xD002D07
23 
24 #undef LOG_TAG
25 #define LOG_TAG "Watcher"
26 
27 namespace OHOS {
28 namespace HiviewDFX {
AppEventWatcher(const std::string & name)29 AppEventWatcher::AppEventWatcher(const std::string& name) : AppEventObserver(name) {};
30 
AppEventWatcher(const std::string & name,const std::vector<AppEventFilter> & filters,TriggerCondition cond)31 AppEventWatcher::AppEventWatcher(
32     const std::string& name,
33     const std::vector<AppEventFilter>& filters,
34     TriggerCondition cond)
35     : AppEventObserver(name, filters, cond) {};
36 
GetOsEventsMask()37 uint64_t AppEventWatcher::GetOsEventsMask()
38 {
39     uint64_t mask = 0;
40     auto filters = GetFilters();
41     std::for_each(filters.begin(), filters.end(), [&mask](const auto& filter) {
42         mask |= filter.GetOsEventsMask();
43     });
44     return mask;
45 }
46 
GetFiltersStr()47 std::string AppEventWatcher::GetFiltersStr()
48 {
49     std::lock_guard<std::mutex> lockGuard(mutex_);
50     if (!filtersStr_.empty()) {
51         return filtersStr_;
52     }
53     Json::Value filtersJson(Json::arrayValue);
54     auto filters = GetFilters();
55     for (const auto& filter : filters) {
56         Json::Value filterJson;
57         filterJson[HiAppEvent::DOMAIN_PROPERTY] = filter.domain;
58         Json::Value namesJson(Json::arrayValue);
59         for (const auto& name : filter.names) {
60             namesJson.append(name);
61         }
62         filterJson[HiAppEvent::NAMES_PROPERTY] = namesJson;
63         filterJson[HiAppEvent::TYPES_PROPERTY] = filter.types;
64         filtersJson.append(filterJson);
65     }
66     filtersStr_ = Json::FastWriter().write(filtersJson);
67     return filtersStr_;
68 }
69 
SetFiltersStr(const std::string & jsonStr)70 void AppEventWatcher::SetFiltersStr(const std::string& jsonStr)
71 {
72     std::vector<AppEventFilter> filters;
73     if (jsonStr.empty()) {
74         return;
75     }
76     Json::Value filtersJson;
77     Json::Reader reader(Json::Features::strictMode());
78     if (!reader.parse(jsonStr, filtersJson)) {
79         HILOG_ERROR(LOG_CORE, "parse filters failed, please check the style of json");
80         return;
81     }
82     if (!filtersJson.isArray() || filtersJson.empty()) {
83         HILOG_WARN(LOG_CORE, "filters is empty");
84         return;
85     }
86     for (Json::ArrayIndex i = 0; i < filtersJson.size(); ++i) {
87         if (filtersJson[i].isObject()) {
88             std::string domain = EventJsonUtil::ParseString(filtersJson[i], HiAppEvent::DOMAIN_PROPERTY);
89             std::unordered_set<std::string> names;
90             EventJsonUtil::ParseStrings(filtersJson[i], HiAppEvent::NAMES_PROPERTY, names);
91             uint32_t types = EventJsonUtil::ParseUInt32(filtersJson[i], HiAppEvent::TYPES_PROPERTY);
92             filters.emplace_back(AppEventFilter(domain, names, types));
93         }
94     }
95     SetFilters(filters);
96     std::lock_guard<std::mutex> lockGuard(mutex_);
97     filtersStr_ = jsonStr;
98 }
99 } // namespace HiviewDFX
100 } // namespace OHOS
101