1 /*
2  * Copyright (c) 2022-2023 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_LOCAL_CALL_CONTAINER_H
17 #define OHOS_ABILITY_RUNTIME_LOCAL_CALL_CONTAINER_H
18 
19 #include <mutex>
20 
21 #include "ability_context.h"
22 #include "ability_connect_callback_stub.h"
23 #include "ability_connect_callback_proxy.h"
24 #include "local_call_record.h"
25 #include "want.h"
26 
27 namespace OHOS {
28 namespace AbilityRuntime {
29 using Want = OHOS::AAFwk::Want;
30 using AbilityConnectionStub = OHOS::AAFwk::AbilityConnectionStub;
31 class CallerConnection;
32 class LocalCallContainer : public std::enable_shared_from_this<LocalCallContainer> {
33 public:
34     LocalCallContainer() = default;
35     virtual ~LocalCallContainer() = default;
36 
37     int StartAbilityByCallInner(const Want &want, std::shared_ptr<CallerCallBack> callback,
38         sptr<IRemoteObject> callerToken, int32_t accountId = DEFAULT_INVAL_VALUE);
39 
40     int ReleaseCall(const std::shared_ptr<CallerCallBack> &callback);
41 
42     void ClearFailedCallConnection(const std::shared_ptr<CallerCallBack> &callback);
43 
44     void DumpCalls(std::vector<std::string> &info);
45 
46     void SetCallLocalRecord(
47         const AppExecFwk::ElementName& element, const std::shared_ptr<LocalCallRecord> &localCallRecord);
48     void SetMultipleCallLocalRecord(
49         const AppExecFwk::ElementName& element, const std::shared_ptr<LocalCallRecord> &localCallRecord);
50 
51     void OnCallStubDied(const wptr<IRemoteObject> &remote);
52 
53 private:
54     bool GetCallLocalRecord(const AppExecFwk::ElementName &elementName,
55         std::shared_ptr<LocalCallRecord> &localCallRecord, int32_t accountId);
56     void OnSingletonCallStubDied(const wptr<IRemoteObject> &remote);
57     int32_t RemoveSingletonCallLocalRecord(const std::shared_ptr<LocalCallRecord> &record);
58     int32_t RemoveMultipleCallLocalRecord(const std::shared_ptr<LocalCallRecord> &record);
59     int32_t GetCurrentUserId();
60     int32_t GetValidUserId(int32_t accountId);
61     bool IsCallBackCalled(const std::vector<std::shared_ptr<CallerCallBack>> &callers) const;
62 
63 private:
64     int32_t currentUserId_ = DEFAULT_INVAL_VALUE;
65     // used to store single instance call records
66     std::map<std::string, std::set<std::shared_ptr<LocalCallRecord>>> callProxyRecords_;
67     // used to store multi instance call records
68     std::map<std::string, std::set<std::shared_ptr<LocalCallRecord>>> multipleCallProxyRecords_;
69     std::set<sptr<CallerConnection>> connections_;
70     std::mutex mutex_;
71     std::mutex multipleMutex_;
72 };
73 
74 class CallerConnection : public AbilityConnectionStub {
75 public:
76     CallerConnection() = default;
77     virtual ~CallerConnection() = default;
78 
79     void ClearCallRecord();
80 
81     void SetRecordAndContainer(const std::shared_ptr<LocalCallRecord> &localCallRecord,
82         const std::weak_ptr<LocalCallContainer> &container);
83 
84     void OnAbilityConnectDone(
85         const AppExecFwk::ElementName &element, const sptr<IRemoteObject> &remoteObject, int code) override;
86 
87     void OnAbilityDisconnectDone(const AppExecFwk::ElementName &element, int code) override;
88 
89     void OnRemoteStateChanged(const AppExecFwk::ElementName &element, int32_t abilityState) override;
90 private:
91     std::shared_ptr<LocalCallRecord> localCallRecord_;
92     std::weak_ptr<LocalCallContainer> container_;
93 };
94 } // namespace AbilityRuntime
95 } // namespace OHOS
96 #endif // OHOS_ABILITY_RUNTIME_LOCAL_CALL_CONTAINER_H
97