1 /*
2  * Copyright (c) 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_EXTENSION_MANAGER_CLIENT_H
17 #define OHOS_ABILITY_RUNTIME_EXTENSION_MANAGER_CLIENT_H
18 
19 #include <mutex>
20 
21 #include "extension_manager_interface.h"
22 
23 #include "iremote_object.h"
24 
25 namespace OHOS {
26 namespace AAFwk {
27 /**
28  * @class ExtensionManagerClient
29  * ExtensionManagerClient is used to access ability manager services.
30  */
31 class ExtensionManagerClient {
32 public:
33     ExtensionManagerClient() = default;
34     virtual ~ExtensionManagerClient() = default;
35     static ExtensionManagerClient& GetInstance();
36 
37     ErrCode ConnectServiceExtensionAbility(const Want &want, const sptr<IRemoteObject> &connect, int32_t userId);
38 
39     ErrCode ConnectServiceExtensionAbility(const Want &want, const sptr<IRemoteObject> &connect,
40         const sptr<IRemoteObject> &callerToken, int32_t userId);
41 
42     ErrCode ConnectEnterpriseAdminExtensionAbility(const Want &want,
43         const sptr<IRemoteObject> &connect, const sptr<IRemoteObject> &callerToken, int32_t userId);
44 
45     /**
46      * Connect extension ability.
47      *
48      * @param want special want for the extension ability.
49      * @param connect callback used to notify caller the result of connecting.
50      * @param userId the extension runs in.
51      * @return Returns ERR_OK on success, others on failure.
52      */
53     ErrCode ConnectExtensionAbility(const Want &want, const sptr<IRemoteObject> &connect,
54         int32_t userId = DEFAULT_INVALID_USER_ID);
55 
56     /**
57      * Disconnect session with extension ability.
58      *
59      * @param connect Callback used to notify caller the result of disconnecting.
60      * @return Returns ERR_OK on success, others on failure.
61      */
62     ErrCode DisconnectAbility(const sptr<IRemoteObject> &connect);
63 
64 private:
65     class ExtensionMgrDeathRecipient : public IRemoteObject::DeathRecipient {
66     public:
67         ExtensionMgrDeathRecipient() = default;
68         ~ExtensionMgrDeathRecipient() = default;
69         void OnRemoteDied(const wptr<IRemoteObject>& remote) override;
70     private:
71         DISALLOW_COPY_AND_MOVE(ExtensionMgrDeathRecipient);
72     };
73 
74     sptr<IExtensionManager> GetExtensionManager();
75     void Connect();
76     void ResetProxy(const wptr<IRemoteObject>& remote);
77 
78     std::mutex mutex_;
79     sptr<IExtensionManager> proxy_;
80     sptr<IRemoteObject::DeathRecipient> deathRecipient_;
81 };
82 }  // namespace AAFwk
83 }  // namespace OHOS
84 #endif  // OHOS_ABILITY_RUNTIME_EXTENSION_MANAGER_CLIENT_H
85