• 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 {
25 
OnProxyDied()26 void AmsMgrProxy::OnProxyDied()
27 {
28     std::lock_guard<std::mutex> lock(mutex_);
29     if (sa_ != nullptr) {
30         sa_->RemoveDeathRecipient(deathRecipient_);
31     }
32     deathRecipient_ = nullptr;
33     proxy_ = nullptr;
34 }
35 
~AmsMgrProxy()36 AmsMgrProxy::~AmsMgrProxy()
37 {
38     std::lock_guard<std::mutex> lock(mutex_);
39     if (sa_ != nullptr) {
40         sa_->RemoveDeathRecipient(deathRecipient_);
41     }
42 }
43 
GetInstance()44 AmsMgrProxy* AmsMgrProxy::GetInstance()
45 {
46     static AmsMgrProxy* proxy = nullptr;
47     if (proxy != nullptr) {
48         return proxy;
49     }
50     std::mutex premutex_;
51     std::lock_guard<std::mutex> lock(premutex_);
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 int AmsMgrProxy::Connect(
63     const std::string &uri, const sptr<IRemoteObject> &connect, const sptr<IRemoteObject> &callerToken)
64 {
65     AAFwk::Want want;
66     want.SetUri(uri);
67     std::lock_guard<std::mutex> lock(mutex_);
68     if (ConnectSA()) {
69         return proxy_->ConnectAbilityCommon(want, connect, callerToken, AppExecFwk::ExtensionAbilityType::DATASHARE);
70     }
71     return -1;
72 }
73 
ConnectSA()74 bool AmsMgrProxy::ConnectSA()
75 {
76     if (proxy_ != nullptr) {
77         return true;
78     }
79     sptr<ISystemAbilityManager> systemAbilityManager =
80         SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
81     if (systemAbilityManager == nullptr) {
82         LOG_ERROR("Failed to get system ability mgr.");
83         return false;
84     }
85 
86     sa_ = systemAbilityManager->GetSystemAbility(ABILITY_MGR_SERVICE_ID);
87     if (sa_ == nullptr) {
88         LOG_ERROR("get ability manager service failed.");
89         return false;
90     }
91 
92     deathRecipient_ = new (std::nothrow) AmsMgrProxy::ServiceDeathRecipient(this);
93     if (deathRecipient_ == nullptr) {
94         LOG_ERROR("new death recipient failed.");
95         return false;
96     }
97     sa_->AddDeathRecipient(deathRecipient_);
98     proxy_ = new (std::nothrow)Proxy(sa_);
99     if (proxy_ == nullptr) {
100         LOG_ERROR("new proxy failed");
101         return false;
102     }
103     return true;
104 }
105 
DisConnect(sptr<IRemoteObject> connect)106 int AmsMgrProxy::DisConnect(sptr<IRemoteObject> connect)
107 {
108     std::lock_guard<std::mutex> lock(mutex_);
109     if (ConnectSA()) {
110         return proxy_->DisconnectAbility(connect);
111     }
112     return -1;
113 }
114 } // namespace OHOS::DataShare