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 DATASHARESERVICE_EXTENSION_PROXY_H
17 #define DATASHARESERVICE_EXTENSION_PROXY_H
18 
19 #include <memory>
20 #include <string>
21 
22 #include "extension_manager_proxy.h"
23 #include "want.h"
24 namespace OHOS::DataShare {
25 class ExtensionMgrProxy final : public std::enable_shared_from_this<ExtensionMgrProxy> {
26 public:
27     // do not use
28     ExtensionMgrProxy() = default;
29     ~ExtensionMgrProxy();
30     static std::shared_ptr<ExtensionMgrProxy> GetInstance();
31     int Connect(const std::string &uri, const sptr<IRemoteObject> &connect,
32         const sptr<IRemoteObject> &callerToken, AAFwk::WantParams &wantParams);
33     int DisConnect(sptr<IRemoteObject> connect);
34 private:
35     using Proxy = AAFwk::ExtensionManagerProxy;
36     class ServiceDeathRecipient : public IRemoteObject::DeathRecipient {
37     public:
ServiceDeathRecipient(std::weak_ptr<ExtensionMgrProxy> owner)38         explicit ServiceDeathRecipient(std::weak_ptr<ExtensionMgrProxy> owner) : owner_(owner)
39         {
40         }
OnRemoteDied(const wptr<IRemoteObject> & object)41         void OnRemoteDied(const wptr<IRemoteObject> &object) override
42         {
43             auto owner = owner_.lock();
44             if (owner != nullptr) {
45                 owner->OnProxyDied();
46             }
47         }
48 
49     private:
50         std::weak_ptr<ExtensionMgrProxy> owner_;
51     };
52     void OnProxyDied();
53     bool ConnectSA();
54     std::mutex mutex_;
55     sptr<IRemoteObject> sa_;
56     sptr<Proxy> proxy_;
57     sptr<ExtensionMgrProxy::ServiceDeathRecipient> deathRecipient_;
58 };
59 } // namespace OHOS::DataShare
60 #endif // DATASHARESERVICE_BUNDLEMGR_PROXY_H
61