1eace7efcSopenharmony_ci/*
2eace7efcSopenharmony_ci * Copyright (c) 2022 Huawei Device Co., Ltd.
3eace7efcSopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
4eace7efcSopenharmony_ci * you may not use this file except in compliance with the License.
5eace7efcSopenharmony_ci * You may obtain a copy of the License at
6eace7efcSopenharmony_ci *
7eace7efcSopenharmony_ci *     http://www.apache.org/licenses/LICENSE-2.0
8eace7efcSopenharmony_ci *
9eace7efcSopenharmony_ci * Unless required by applicable law or agreed to in writing, software
10eace7efcSopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
11eace7efcSopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12eace7efcSopenharmony_ci * See the License for the specific language governing permissions and
13eace7efcSopenharmony_ci * limitations under the License.
14eace7efcSopenharmony_ci */
15eace7efcSopenharmony_ci
16eace7efcSopenharmony_ci#ifndef OHOS_AAFWK_CONNECTION_STATE_ITME_H
17eace7efcSopenharmony_ci#define OHOS_AAFWK_CONNECTION_STATE_ITME_H
18eace7efcSopenharmony_ci
19eace7efcSopenharmony_ci#include <string>
20eace7efcSopenharmony_ci#include <map>
21eace7efcSopenharmony_ci#include <vector>
22eace7efcSopenharmony_ci
23eace7efcSopenharmony_ci#include "connection_record.h"
24eace7efcSopenharmony_ci#include "extension_ability_info.h"
25eace7efcSopenharmony_ci#include "connection_data.h"
26eace7efcSopenharmony_ci#include "data_ability_record.h"
27eace7efcSopenharmony_ci
28eace7efcSopenharmony_cinamespace OHOS {
29eace7efcSopenharmony_cinamespace AAFwk {
30eace7efcSopenharmony_ci/**
31eace7efcSopenharmony_ci * @class DataAbilityCaller
32eace7efcSopenharmony_ci * DataAbilityCaller,This class is used to record data ability caller info.
33eace7efcSopenharmony_ci */
34eace7efcSopenharmony_cistruct DataAbilityCaller {
35eace7efcSopenharmony_ci    bool isNotHap = false;
36eace7efcSopenharmony_ci    int32_t callerPid = 0;
37eace7efcSopenharmony_ci    int32_t callerUid = 0;
38eace7efcSopenharmony_ci    std::string callerName;
39eace7efcSopenharmony_ci    sptr<IRemoteObject> callerToken = nullptr;
40eace7efcSopenharmony_ci};
41eace7efcSopenharmony_ci
42eace7efcSopenharmony_ci/**
43eace7efcSopenharmony_ci * @class ConnectionStateItem
44eace7efcSopenharmony_ci * ConnectionStateItem,This class is used to record connection state of a process.
45eace7efcSopenharmony_ci */
46eace7efcSopenharmony_ciclass ConnectedExtension;
47eace7efcSopenharmony_ciclass ConnectedDataAbility;
48eace7efcSopenharmony_ciclass ConnectionStateItem : public std::enable_shared_from_this<ConnectionStateItem> {
49eace7efcSopenharmony_cipublic:
50eace7efcSopenharmony_ci    static std::shared_ptr<ConnectionStateItem> CreateConnectionStateItem(
51eace7efcSopenharmony_ci        const std::shared_ptr<ConnectionRecord> &record);
52eace7efcSopenharmony_ci
53eace7efcSopenharmony_ci    static std::shared_ptr<ConnectionStateItem> CreateConnectionStateItem(
54eace7efcSopenharmony_ci        const DataAbilityCaller &dataCaller);
55eace7efcSopenharmony_ci
56eace7efcSopenharmony_ci    ConnectionStateItem(int32_t callerUid, int32_t callerPid, const std::string &callerName);
57eace7efcSopenharmony_ci    virtual ~ConnectionStateItem();
58eace7efcSopenharmony_ci
59eace7efcSopenharmony_ci    /**
60eace7efcSopenharmony_ci     * add a connection to target extension.
61eace7efcSopenharmony_ci     *
62eace7efcSopenharmony_ci     * @param record the connection record which mark an connection.
63eace7efcSopenharmony_ci     * @param data output relationship data.
64eace7efcSopenharmony_ci     * @return Returns true if need report relationship.
65eace7efcSopenharmony_ci     */
66eace7efcSopenharmony_ci    bool AddConnection(std::shared_ptr<ConnectionRecord> record, AbilityRuntime::ConnectionData &data);
67eace7efcSopenharmony_ci
68eace7efcSopenharmony_ci    /**
69eace7efcSopenharmony_ci     * remove a connection to target extension.
70eace7efcSopenharmony_ci     *
71eace7efcSopenharmony_ci     * @param record the connection record which mark an connection.
72eace7efcSopenharmony_ci     * @param data output relationship data.
73eace7efcSopenharmony_ci     * @return Returns true if need report relationship.
74eace7efcSopenharmony_ci     */
75eace7efcSopenharmony_ci    bool RemoveConnection(std::shared_ptr<ConnectionRecord> record, AbilityRuntime::ConnectionData &data);
76eace7efcSopenharmony_ci
77eace7efcSopenharmony_ci    /**
78eace7efcSopenharmony_ci     * add a connection to target data ability.
79eace7efcSopenharmony_ci     *
80eace7efcSopenharmony_ci     * @param caller the caller of this data ability.
81eace7efcSopenharmony_ci     * @param dataAbility data ability that acquired.
82eace7efcSopenharmony_ci     * @param data output relationship data.
83eace7efcSopenharmony_ci     * @return Returns true if need report relationship.
84eace7efcSopenharmony_ci     */
85eace7efcSopenharmony_ci    bool AddDataAbilityConnection(const DataAbilityCaller &caller,
86eace7efcSopenharmony_ci        const std::shared_ptr<DataAbilityRecord> &dataAbility, AbilityRuntime::ConnectionData &data);
87eace7efcSopenharmony_ci
88eace7efcSopenharmony_ci    /**
89eace7efcSopenharmony_ci     * remove a connection to target data ability.
90eace7efcSopenharmony_ci     *
91eace7efcSopenharmony_ci     * @param caller the caller of this data ability.
92eace7efcSopenharmony_ci     * @param dataAbility data ability that acquired.
93eace7efcSopenharmony_ci     * @param data output relationship data.
94eace7efcSopenharmony_ci     * @return Returns true if need report relationship.
95eace7efcSopenharmony_ci     */
96eace7efcSopenharmony_ci    bool RemoveDataAbilityConnection(const DataAbilityCaller &caller,
97eace7efcSopenharmony_ci        const std::shared_ptr<DataAbilityRecord> &dataAbility, AbilityRuntime::ConnectionData &data);
98eace7efcSopenharmony_ci
99eace7efcSopenharmony_ci    /**
100eace7efcSopenharmony_ci     * handle died of data ability.
101eace7efcSopenharmony_ci     *
102eace7efcSopenharmony_ci     * @param token target token of data ability.
103eace7efcSopenharmony_ci     * @param data output relationship data.
104eace7efcSopenharmony_ci     * @return Returns true if need report relationship.
105eace7efcSopenharmony_ci     */
106eace7efcSopenharmony_ci    bool HandleDataAbilityDied(const sptr<IRemoteObject> &token, AbilityRuntime::ConnectionData &data);
107eace7efcSopenharmony_ci
108eace7efcSopenharmony_ci    /**
109eace7efcSopenharmony_ci     * generate all relationship data of this item.
110eace7efcSopenharmony_ci     *
111eace7efcSopenharmony_ci     * @param datas output relationship data.
112eace7efcSopenharmony_ci     */
113eace7efcSopenharmony_ci    void GenerateAllConnectionData(std::vector<AbilityRuntime::ConnectionData> &datas);
114eace7efcSopenharmony_ci
115eace7efcSopenharmony_ci    /**
116eace7efcSopenharmony_ci     * check if connections is empty.
117eace7efcSopenharmony_ci     *
118eace7efcSopenharmony_ci     * @return true if no connections.
119eace7efcSopenharmony_ci     */
120eace7efcSopenharmony_ci    bool IsEmpty() const;
121eace7efcSopenharmony_ci
122eace7efcSopenharmony_ciprivate:
123eace7efcSopenharmony_ci    DISALLOW_COPY_AND_MOVE(ConnectionStateItem);
124eace7efcSopenharmony_ci
125eace7efcSopenharmony_ci    void GenerateConnectionData(const std::shared_ptr<ConnectedExtension> &connectedExtension,
126eace7efcSopenharmony_ci        AbilityRuntime::ConnectionData &data);
127eace7efcSopenharmony_ci
128eace7efcSopenharmony_ci    void GenerateConnectionData(const std::shared_ptr<ConnectedDataAbility> &connectedDataAbility,
129eace7efcSopenharmony_ci        AbilityRuntime::ConnectionData &data);
130eace7efcSopenharmony_ci
131eace7efcSopenharmony_ci    int32_t callerUid_ = 0;
132eace7efcSopenharmony_ci    int32_t callerPid_ = 0;
133eace7efcSopenharmony_ci    std::string callerName_;
134eace7efcSopenharmony_ci    std::map<sptr<IRemoteObject>, std::shared_ptr<ConnectedExtension>> connectionMap_; // key:targetExtension token
135eace7efcSopenharmony_ci    std::map<sptr<IRemoteObject>, std::shared_ptr<ConnectedDataAbility>> dataAbilityMap_; // key:targetDatability token
136eace7efcSopenharmony_ci};
137eace7efcSopenharmony_ci}  // namespace AAFwk
138eace7efcSopenharmony_ci}  // namespace OHOS
139eace7efcSopenharmony_ci#endif  // OHOS_AAFWK_CONNECTION_STATE_ITME_H
140