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 #ifndef OHOS_ABILITY_RUNTIME_CALL_CONTAINER_H
17 #define OHOS_ABILITY_RUNTIME_CALL_CONTAINER_H
18 
19 #include <map>
20 #include <string>
21 #include <mutex>
22 
23 #include "ability_connect_callback_interface.h"
24 #include "call_record.h"
25 #include "element_name.h"
26 #include "iremote_object.h"
27 #include "nocopyable.h"
28 
29 namespace OHOS {
30 namespace AAFwk {
31 class CallRecord;
32 /**
33  * @class CallContainer
34  * CallContainer provides a facility for managing the call records of ability.
35  */
36 class CallContainer : public std::enable_shared_from_this<CallContainer> {
37 public:
38     using CallMapType = std::map<sptr<IRemoteObject>, std::shared_ptr<CallRecord>>;
39     using RecipientMapType = std::map<sptr<IRemoteObject>, sptr<IRemoteObject::DeathRecipient>>;
40 
41     CallContainer();
42     virtual ~CallContainer();
43 
44     void AddCallRecord(const sptr<IAbilityConnection> &connect, const std::shared_ptr<CallRecord> &callRecord);
45     std::shared_ptr<CallRecord> GetCallRecord(const sptr<IAbilityConnection> &connect) const;
46     bool RemoveCallRecord(const sptr<IAbilityConnection> &connect);
47     std::shared_ptr<CallRecord> FindCallRecordMap(sptr<IRemoteObject> object) const;
48     std::shared_ptr<CallRecord> RemoveCallRecordMap(sptr<IRemoteObject> object);
49     void InsertCallRecordMap(sptr<IRemoteObject> object, std::shared_ptr<CallRecord> callRecord);
50     CallMapType CopyCallRecordMap() const;
51     bool EmptyCallRecordMap();
52     bool CallRequestDone(const sptr<IRemoteObject> &callStub);
53     void Dump(std::vector<std::string> &info) const;
54     bool IsNeedToCallRequest() const;
55     bool IsExistConnection(const sptr<IAbilityConnection> &connect);
56 
57 private:
58     void RemoveConnectDeathRecipient(const sptr<IAbilityConnection> &connect);
59     void AddConnectDeathRecipient(const sptr<IAbilityConnection> &connect);
60     void OnConnectionDied(const wptr<IRemoteObject> &remote);
61 
62 private:
63     CallMapType callRecordMap_;
64     mutable std::mutex callRecordMapLock_;
65     RecipientMapType deathRecipientMap_;
66 
67     DISALLOW_COPY_AND_MOVE(CallContainer);
68 };
69 }  // namespace AAFwk
70 }  // namespace OHOS
71 #endif  // OHOS_ABILITY_RUNTIME_CALL_CONTAINER_H
72 
73