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