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