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,AAFwk::WantParams & wantParams)51 int ExtensionMgrProxy::Connect(
52 const std::string &uri, const sptr<IRemoteObject> &connect, const sptr<IRemoteObject> &callerToken,
53 AAFwk::WantParams &wantParams)
54 {
55 AAFwk::Want want;
56 want.SetUri(uri);
57 want.SetParams(wantParams);
58 std::lock_guard<std::mutex> lock(mutex_);
59 if (ConnectSA()) {
60 int ret = proxy_->ConnectAbilityCommon(want, connect, callerToken, AppExecFwk::ExtensionAbilityType::DATASHARE);
61 if (ret != ERR_OK) {
62 ZLOGE("ConnectAbilityCommon failed, %{public}d", ret);
63 }
64 return ret;
65 }
66 return -1;
67 }
68
ConnectSA()69 bool ExtensionMgrProxy::ConnectSA()
70 {
71 if (proxy_ != nullptr) {
72 return true;
73 }
74 sptr<ISystemAbilityManager> systemAbilityManager =
75 SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
76 if (systemAbilityManager == nullptr) {
77 ZLOGE("Failed to get system ability mgr.");
78 return false;
79 }
80
81 sa_ = systemAbilityManager->GetSystemAbility(ABILITY_MGR_SERVICE_ID);
82 if (sa_ == nullptr) {
83 ZLOGE("Failed to GetSystemAbility.");
84 return false;
85 }
86 deathRecipient_ = new (std::nothrow) ExtensionMgrProxy::ServiceDeathRecipient(weak_from_this());
87 if (deathRecipient_ == nullptr) {
88 ZLOGE("deathRecipient alloc failed.");
89 return false;
90 }
91 sa_->AddDeathRecipient(deathRecipient_);
92 proxy_ = new (std::nothrow)Proxy(sa_);
93 if (proxy_ == nullptr) {
94 ZLOGE("proxy_ null, new failed");
95 return false;
96 }
97 return true;
98 }
99
DisConnect(sptr<IRemoteObject> connect)100 int ExtensionMgrProxy::DisConnect(sptr<IRemoteObject> connect)
101 {
102 std::lock_guard<std::mutex> lock(mutex_);
103 if (ConnectSA()) {
104 int ret = proxy_->DisconnectAbility(connect);
105 if (ret != ERR_OK) {
106 ZLOGE("DisconnectAbility failed, %{public}d", ret);
107 }
108 return ret;
109 }
110 return -1;
111 }
112 } // namespace OHOS::DataShare