• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #include "datashare_uri_utils.h"
25 #include "datashare_errno.h"
26 
27 namespace OHOS::DataShare {
28 std::mutex AmsMgrProxy::pmutex_;
29 
OnProxyDied()30 void AmsMgrProxy::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 
~AmsMgrProxy()40 AmsMgrProxy::~AmsMgrProxy()
41 {
42     std::lock_guard<std::mutex> lock(mutex_);
43     if (sa_ != nullptr) {
44         sa_->RemoveDeathRecipient(deathRecipient_);
45     }
46 }
47 
GetInstance()48 AmsMgrProxy* AmsMgrProxy::GetInstance()
49 {
50     std::lock_guard<std::mutex> lock(pmutex_);
51     static AmsMgrProxy* proxy = nullptr;
52     if (proxy != nullptr) {
53         return proxy;
54     }
55     proxy = new AmsMgrProxy();
56     if (proxy == nullptr) {
57         LOG_ERROR("new proxy failed");
58     }
59     return proxy;
60 }
61 
Connect(const std::string & uri,const sptr<IRemoteObject> & connect,const sptr<IRemoteObject> & callerToken)62 __attribute__ ((no_sanitize("cfi"))) int AmsMgrProxy::Connect(
63     const std::string &uri, const sptr<IRemoteObject> &connect, const sptr<IRemoteObject> &callerToken)
64 {
65     AAFwk::Want want;
66     auto [success, userId] = DataShareURIUtils::GetUserFromUri(uri);
67     if (!success) {
68         return E_INVALID_USER_ID;
69     }
70     want.SetUri(DataShareURIUtils::FormatUri(uri));
71     std::lock_guard<std::mutex> lock(mutex_);
72     if (ConnectSA()) {
73         LOG_INFO("connect start, uri = %{public}s", DataShareStringUtils::Change(uri).c_str());
74         return proxy_->ConnectAbilityCommon(want, connect, callerToken, AppExecFwk::ExtensionAbilityType::DATASHARE,
75             userId);
76     }
77     return -1;
78 }
79 
ConnectSA()80 bool AmsMgrProxy::ConnectSA()
81 {
82     if (proxy_ != nullptr) {
83         return true;
84     }
85     sptr<ISystemAbilityManager> systemAbilityManager =
86         SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
87     if (systemAbilityManager == nullptr) {
88         LOG_ERROR("Failed to get system ability mgr.");
89         return false;
90     }
91 
92     sa_ = systemAbilityManager->GetSystemAbility(ABILITY_MGR_SERVICE_ID);
93     if (sa_ == nullptr) {
94         LOG_ERROR("get ability manager service failed.");
95         return false;
96     }
97 
98     deathRecipient_ = new (std::nothrow) AmsMgrProxy::ServiceDeathRecipient(this);
99     if (deathRecipient_ == nullptr) {
100         LOG_ERROR("new death recipient failed.");
101         return false;
102     }
103     sa_->AddDeathRecipient(deathRecipient_);
104     proxy_ = new (std::nothrow)Proxy(sa_);
105     if (proxy_ == nullptr) {
106         LOG_ERROR("new proxy failed");
107         return false;
108     }
109     return true;
110 }
111 
DisConnect(sptr<IRemoteObject> connect)112 int AmsMgrProxy::DisConnect(sptr<IRemoteObject> connect)
113 {
114     std::lock_guard<std::mutex> lock(mutex_);
115     if (ConnectSA()) {
116         return proxy_->DisconnectAbility(connect);
117     }
118     return -1;
119 }
120 } // namespace OHOS::DataShare