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      *
67      * @return Returns ERR_OK on success, others on failure.
68      */
69     ErrCode StartUser(int32_t accountId, const sptr<IUserCallback> &callback);
70 
71     /**
72      * @brief stop user.
73      * @param accountId accountId.
74      * @param callback callback.
75      *
76      * @return Returns ERR_OK on success, others on failure.
77      */
78     ErrCode StopUser(int32_t accountId, const sptr<IUserCallback> &callback);
79 
80     /**
81      * @brief logout user.
82      * @param accountId accountId.
83      *
84      * @return Returns ERR_OK on success, others on failure.
85      */
86     ErrCode LogoutUser(int32_t accountId);
87 
88 private:
89     void Connect();
90     ErrCode DoConnectAbility(
91         const sptr<IRemoteObject> proxy,
92         const Want &want,
93         const sptr<IAbilityConnection> &connect,
94         const sptr<IRemoteObject> &callerToken,
95         int32_t userId = -1);
96 
97     class AbilityMgrDeathRecipient : public IRemoteObject::DeathRecipient {
98         public:
99             AbilityMgrDeathRecipient() = default;
100             ~AbilityMgrDeathRecipient() override = default;
101             void OnRemoteDied(const wptr<IRemoteObject>& remote) override;
102         private:
103             DISALLOW_COPY_AND_MOVE(AbilityMgrDeathRecipient);
104     };
105 
106     sptr<IRemoteObject> GetAbilityManager();
107     void ResetProxy(const wptr<IRemoteObject>& remote);
108 
109     std::mutex proxyMutex_;
110     sptr<IRemoteObject> proxy_;
111     sptr<IRemoteObject::DeathRecipient> deathRecipient_;
112 };
113 }  // namespace AAFwk
114 }  // namespace OHOS
115 #endif  // OS_ACCOUNT_SERVICE_ABILITY_MANAGER_ADAPTER_H
116