1 /*
2  * Copyright (c) 2022 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 #include "service_center_connection.h"
17 
18 #include "app_log_tag_wrapper.h"
19 
20 namespace OHOS {
21 namespace AppExecFwk {
~ServiceCenterConnection()22 ServiceCenterConnection::~ServiceCenterConnection()
23 {
24     LOG_I(BMS_TAG_DEFAULT, "ServiceCenterConnection destory");
25 }
26 
OnAbilityConnectDone(const AppExecFwk::ElementName & element,const sptr<IRemoteObject> & remoteObject,int32_t resultCode)27 void ServiceCenterConnection::OnAbilityConnectDone(
28     const AppExecFwk::ElementName &element, const sptr<IRemoteObject> &remoteObject, int32_t resultCode)
29 {
30     LOG_I(BMS_TAG_DEFAULT, "OnAbilityConnectDone start");
31     if (resultCode != ERR_OK) {
32         LOG_E(BMS_TAG_DEFAULT, "OnAbilityConnectDone resultCode = %{public}d", resultCode);
33         connectState_ = ServiceCenterConnectState::DISCONNECTED;
34         cv_.notify_all();
35         return;
36     }
37 
38     if (remoteObject == nullptr) {
39         LOG_E(BMS_TAG_DEFAULT, "OnAbilityConnectDone remoteObject is nullptr");
40         connectState_ = ServiceCenterConnectState::DISCONNECTED;
41         cv_.notify_all();
42         return;
43     }
44     serviceCenterRemoteObject_ = remoteObject;
45     connectState_ = ServiceCenterConnectState::CONNECTED;
46 
47     deathRecipient_ = new (std::nothrow) ServiceCenterDeathRecipient(connectAbilityMgr_);
48     if (deathRecipient_ == nullptr) {
49         LOG_E(BMS_TAG_DEFAULT, "Failed to create ServiceCenterDeathRecipient");
50         cv_.notify_all();
51         return;
52     }
53 
54     if (!serviceCenterRemoteObject_->AddDeathRecipient(deathRecipient_)) {
55         LOG_E(BMS_TAG_DEFAULT, "Failed to add AbilityManagerDeathRecipient");
56     }
57     cv_.notify_all();
58     LOG_I(BMS_TAG_DEFAULT, "OnAbilityConnectDone end");
59 }
60 
OnAbilityDisconnectDone(const AppExecFwk::ElementName & element,int32_t resultCode)61 void ServiceCenterConnection::OnAbilityDisconnectDone(const AppExecFwk::ElementName &element, int32_t resultCode)
62 {
63     LOG_I(BMS_TAG_DEFAULT, "OnAbilityDisconnectDone start");
64     if (serviceCenterRemoteObject_ != nullptr && deathRecipient_ != nullptr) {
65         serviceCenterRemoteObject_->RemoveDeathRecipient(deathRecipient_);
66     }
67 
68     connectState_ = ServiceCenterConnectState::DISCONNECTED;
69     serviceCenterRemoteObject_ = nullptr;
70 
71     cv_.notify_all();
72     LOG_I(BMS_TAG_DEFAULT, "OnAbilityDisconnectDone end");
73 }
74 
GetRemoteObject()75 sptr<IRemoteObject> ServiceCenterConnection::GetRemoteObject()
76 {
77     return serviceCenterRemoteObject_;
78 }
79 }  // namespace AppExecFwk
80 }  // namespace OHOS