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