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 OS_ACCOUNT_SERVICE_ABILITY_MANAGER_ADAPTER_H
17 #define OS_ACCOUNT_SERVICE_ABILITY_MANAGER_ADAPTER_H
18 
19 #include <mutex>
20 
21 #include "ability_connect_callback_interface.h"
22 #include "user_callback.h"
23 #include "want.h"
24 
25 namespace OHOS {
26 namespace AccountSA {
27 using namespace AAFwk;
28 /**
29  * @class AbilityManagerAdapter
30  * AbilityManagerAdapter is used to access ability manager services.
31  */
32 class AbilityManagerAdapter {
33 private:
34     AbilityManagerAdapter();
35     virtual ~AbilityManagerAdapter();
36     DISALLOW_COPY_AND_MOVE(AbilityManagerAdapter);
37 
38 public:
39     static AbilityManagerAdapter* GetInstance();
40 
41     /**
42      * ConnectAbility, connect session with service ability.
43      *
44      * @param want, Special want for service type's ability.
45      * @param connect, Callback used to notify caller the result of connecting or disconnecting.
46      * @param callerToken, caller ability token.
47      * @return Returns ERR_OK on success, others on failure.
48      */
49     ErrCode ConnectAbility(
50         const Want &want,
51         const sptr<IAbilityConnection> &connect,
52         const sptr<IRemoteObject> &callerToken,
53         int32_t userId = -1);
54 
55     /**
56      * DisconnectAbility, disconnect session with service ability.
57      *
58      * @param connect, Callback used to notify caller the result of connecting or disconnecting.
59      * @return Returns ERR_OK on success, others on failure.
60      */
61     ErrCode DisconnectAbility(const sptr<IAbilityConnection> &connect);
62 
63     /**
64      * @brief start user.
65      * @param accountId accountId.
66      * @param callback callback.
67      *
68      * @return Returns ERR_OK on success, others on failure.
69      */
70     ErrCode StartUser(int32_t accountId, const sptr<IUserCallback> &callback);
71 
72     /**
73      * @brief stop user.
74      * @param accountId accountId.
75      * @param callback callback.
76      *
77      * @return Returns ERR_OK on success, others on failure.
78      */
79     ErrCode StopUser(int32_t accountId, const sptr<IUserCallback> &callback);
80 
81     /**
82      * @brief logout user.
83      * @param accountId accountId.
84      *
85      * @return Returns ERR_OK on success, others on failure.
86      */
87     ErrCode LogoutUser(int32_t accountId);
88 
89 private:
90     void Connect();
91     ErrCode DoConnectAbility(
92         const sptr<IRemoteObject> proxy,
93         const Want &want,
94         const sptr<IAbilityConnection> &connect,
95         const sptr<IRemoteObject> &callerToken,
96         int32_t userId = -1);
97 
98     class AbilityMgrDeathRecipient : public IRemoteObject::DeathRecipient {
99     public:
100         AbilityMgrDeathRecipient() = default;
101         ~AbilityMgrDeathRecipient() override = default;
102         void OnRemoteDied(const wptr<IRemoteObject>& remote) override;
103     private:
104         DISALLOW_COPY_AND_MOVE(AbilityMgrDeathRecipient);
105     };
106 
107     sptr<IRemoteObject> GetAbilityManager();
108     void ResetProxy(const wptr<IRemoteObject>& remote);
109 
110     std::mutex proxyMutex_;
111     sptr<IRemoteObject> proxy_;
112     sptr<IRemoteObject::DeathRecipient> deathRecipient_;
113 };
114 }  // namespace AAFwk
115 }  // namespace OHOS
116 #endif  // OS_ACCOUNT_SERVICE_ABILITY_MANAGER_ADAPTER_H
117