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 #include "dlp_state_item.h"
17
18 #include "hilog_wrapper.h"
19
20 namespace OHOS {
21 namespace AAFwk {
22 namespace {
23 const std::string DLP_BUNDLE_NAME = "com.ohos.dlpmanager";
24 }
25
DlpStateItem(int32_t dlpUid,int32_t dlpPid)26 DlpStateItem::DlpStateItem(int32_t dlpUid, int32_t dlpPid) : dlpUid_(dlpUid), dlpPid_(dlpPid) {}
27
~DlpStateItem()28 DlpStateItem::~DlpStateItem() {}
29
AddDlpConnectionState(const std::shared_ptr<AbilityRecord> & record,AbilityRuntime::DlpStateData & data)30 bool DlpStateItem::AddDlpConnectionState(const std::shared_ptr<AbilityRecord> &record,
31 AbilityRuntime::DlpStateData &data)
32 {
33 return HandleDlpConnectionState(record, true, data);
34 }
35
RemoveDlpConnectionState(const std::shared_ptr<AbilityRecord> & record,AbilityRuntime::DlpStateData & data)36 bool DlpStateItem::RemoveDlpConnectionState(const std::shared_ptr<AbilityRecord> &record,
37 AbilityRuntime::DlpStateData &data)
38 {
39 return HandleDlpConnectionState(record, false, data);
40 }
41
GetDlpUid() const42 int32_t DlpStateItem::GetDlpUid() const
43 {
44 return dlpUid_;
45 }
46
GetOpenedAbilitySize() const47 int32_t DlpStateItem::GetOpenedAbilitySize() const
48 {
49 return static_cast<int32_t>(dlpAbilities_.size());
50 }
51
HandleDlpConnectionState(const std::shared_ptr<AbilityRecord> & record,bool isAdd,AbilityRuntime::DlpStateData & data)52 bool DlpStateItem::HandleDlpConnectionState(const std::shared_ptr<AbilityRecord> &record, bool isAdd,
53 AbilityRuntime::DlpStateData &data)
54 {
55 if (!record || record->GetAppIndex() == 0) {
56 HILOG_WARN("invalid dlp ability.");
57 return false;
58 }
59
60 if (dlpUid_ == 0 || dlpPid_ == 0) {
61 HILOG_WARN("invalid dlp manager state.");
62 return false;
63 }
64
65 sptr<IRemoteObject> tokenObj = nullptr;
66 if (record->GetToken()) {
67 tokenObj = record->GetToken()->AsObject();
68 }
69
70 if (!tokenObj) {
71 HILOG_WARN("invalid ability, no ability token.");
72 return false;
73 }
74
75 const auto &it = std::find_if(dlpAbilities_.begin(), dlpAbilities_.end(), [&tokenObj](const auto &item) {
76 return tokenObj == item;
77 });
78
79 if (isAdd) {
80 if (it != dlpAbilities_.end()) {
81 HILOG_INFO("dlp ability already reported.");
82 return false;
83 }
84 dlpAbilities_.emplace_back(tokenObj);
85 } else {
86 if (it == dlpAbilities_.end()) {
87 HILOG_INFO("find target dlp ability failed, not report closed.");
88 return false;
89 }
90 dlpAbilities_.erase(it);
91 }
92
93 GenerateDlpStateData(record, data);
94 return true;
95 }
96
GenerateDlpStateData(const std::shared_ptr<AbilityRecord> & dlpAbility,AbilityRuntime::DlpStateData & dlpData)97 void DlpStateItem::GenerateDlpStateData(
98 const std::shared_ptr<AbilityRecord> &dlpAbility, AbilityRuntime::DlpStateData &dlpData)
99 {
100 dlpData.callerUid = dlpUid_;
101 dlpData.callerPid = dlpPid_;
102 dlpData.callerName = DLP_BUNDLE_NAME;
103 dlpData.targetPid = dlpAbility->GetPid();
104 dlpData.targetUid = dlpAbility->GetUid();
105 dlpData.targetBundleName = dlpAbility->GetAbilityInfo().bundleName;
106 dlpData.targetModuleName = dlpAbility->GetAbilityInfo().moduleName;
107 dlpData.targetAbilityName = dlpAbility->GetAbilityInfo().name;
108 }
109 } // namespace AAFwk
110 } // namespace OHOS
111