1/*
2 * Copyright (c) 2024 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 "privacy_controller.h"
17
18#include "hiview_logger.h"
19#include "plugin_factory.h"
20
21namespace OHOS {
22namespace HiviewDFX {
23REGISTER(PrivacyController);
24DEFINE_LOG_TAG("PrivacyController");
25
26void PrivacyController::OnLoad()
27{
28    InitAreaPolicy();
29}
30
31void PrivacyController::InitAreaPolicy()
32{
33    auto context = GetHiviewContext();
34    if (context == nullptr) {
35        HIVIEW_LOGW("context is null");
36        return;
37    }
38
39    std::string configPath = context->GetHiViewDirectory(HiviewContext::DirectoryType::CONFIG_DIRECTORY);
40    const std::string configFileName = "area_policy.json";
41    areaPolicy_ = std::make_unique<AreaPolicy>(configPath.append(configFileName));
42}
43
44bool PrivacyController::OnEvent(std::shared_ptr<Event>& event)
45{
46    if (event == nullptr) {
47        return false;
48    }
49
50    if (areaPolicy_ == nullptr) {
51        return true;
52    }
53
54    auto sysEvent = std::static_pointer_cast<SysEvent>(event);
55    if (!areaPolicy_->IsAllowed(sysEvent)) {
56        HIVIEW_LOGD("event[%{public}s|%{public}s] is not allowed",
57            sysEvent->domain_.c_str(), sysEvent->eventName_.c_str());
58        return sysEvent->OnFinish();
59    }
60    return true;
61}
62} // namespace HiviewDFX
63} // namespace OHOS
64