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 #define LOG_TAG "AppConnectManager"
17 #include "extension_mgr_proxy.h"
18
19 #include "app_connect_manager.h"
20 #include "extension_ability_info.h"
21 #include "if_system_ability_manager.h"
22 #include "iservice_registry.h"
23 #include "log_print.h"
24 #include "system_ability_definition.h"
25 #include "want.h"
26 namespace OHOS::DataShare {
OnProxyDied()27 void ExtensionMgrProxy::OnProxyDied()
28 {
29 std::lock_guard<std::mutex> lock(mutex_);
30 if (sa_ != nullptr) {
31 sa_->RemoveDeathRecipient(deathRecipient_);
32 }
33 deathRecipient_ = nullptr;
34 proxy_ = nullptr;
35 }
36
~ExtensionMgrProxy()37 ExtensionMgrProxy::~ExtensionMgrProxy()
38 {
39 std::lock_guard<std::mutex> lock(mutex_);
40 if (sa_ != nullptr) {
41 sa_->RemoveDeathRecipient(deathRecipient_);
42 }
43 }
44
GetInstance()45 std::shared_ptr<ExtensionMgrProxy> ExtensionMgrProxy::GetInstance()
46 {
47 static std::shared_ptr<ExtensionMgrProxy> proxy = std::make_shared<ExtensionMgrProxy>();
48 return proxy;
49 }
50
Connect(const std::string & uri,const sptr<IRemoteObject> & connect,const sptr<IRemoteObject> & callerToken)51 int ExtensionMgrProxy::Connect(
52 const std::string &uri, const sptr<IRemoteObject> &connect, const sptr<IRemoteObject> &callerToken)
53 {
54 AAFwk::Want want;
55 want.SetUri(uri);
56 std::lock_guard<std::mutex> lock(mutex_);
57 if (ConnectSA()) {
58 int ret = proxy_->ConnectAbilityCommon(want, connect, callerToken, AppExecFwk::ExtensionAbilityType::DATASHARE);
59 if (ret != ERR_OK) {
60 ZLOGE("ConnectAbilityCommon failed, %{public}d", ret);
61 }
62 return ret;
63 }
64 return -1;
65 }
66
ConnectSA()67 bool ExtensionMgrProxy::ConnectSA()
68 {
69 if (proxy_ != nullptr) {
70 return true;
71 }
72 sptr<ISystemAbilityManager> systemAbilityManager =
73 SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
74 if (systemAbilityManager == nullptr) {
75 ZLOGE("Failed to get system ability mgr.");
76 return false;
77 }
78
79 sa_ = systemAbilityManager->GetSystemAbility(ABILITY_MGR_SERVICE_ID);
80 if (sa_ == nullptr) {
81 ZLOGE("Failed to GetSystemAbility.");
82 return false;
83 }
84 deathRecipient_ = new (std::nothrow) ExtensionMgrProxy::ServiceDeathRecipient(weak_from_this());
85 if (deathRecipient_ == nullptr) {
86 ZLOGE("deathRecipient alloc failed.");
87 return false;
88 }
89 sa_->AddDeathRecipient(deathRecipient_);
90 proxy_ = new (std::nothrow)Proxy(sa_);
91 if (proxy_ == nullptr) {
92 ZLOGE("proxy_ null, new failed");
93 return false;
94 }
95 return true;
96 }
97
DisConnect(sptr<IRemoteObject> connect)98 int ExtensionMgrProxy::DisConnect(sptr<IRemoteObject> connect)
99 {
100 std::lock_guard<std::mutex> lock(mutex_);
101 if (ConnectSA()) {
102 int ret = proxy_->DisconnectAbility(connect);
103 if (ret != ERR_OK) {
104 ZLOGE("DisconnectAbility failed, %{public}d", ret);
105 }
106 return ret;
107 }
108 return -1;
109 }
110 } // namespace OHOS::DataShare