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