• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021 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 "common_event_death_recipient.h"
17 
18 #include "common_event.h"
19 #include "iservice_registry.h"
20 #include "event_log_wrapper.h"
21 #include "refbase.h"
22 #include "system_ability_definition.h"
23 #include <memory>
24 #include <mutex>
25 
26 namespace OHOS {
27 namespace EventFwk {
~CommonEventDeathRecipient()28 CommonEventDeathRecipient::~CommonEventDeathRecipient()
29 {
30     if (statusChangeListener_ != nullptr) {
31         auto samgrProxy = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
32         if (samgrProxy == nullptr) {
33             EVENT_LOGE("GetSystemAbilityManager failed");
34             return;
35         }
36 
37         int32_t ret = samgrProxy->UnSubscribeSystemAbility(COMMON_EVENT_SERVICE_ID, statusChangeListener_);
38         if (ret != ERR_OK) {
39             EVENT_LOGE("SubscribeSystemAbility to sa manager failed");
40             return;
41         }
42         statusChangeListener_ = nullptr;
43     }
44 }
45 
SubscribeSAManager()46 void CommonEventDeathRecipient::SubscribeSAManager()
47 {
48     if (statusChangeListener_ == nullptr) {
49         std::lock_guard<ffrt::mutex> lock(listenerMutex_);
50         if (statusChangeListener_ == nullptr) {
51             auto samgrProxy = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
52             if (samgrProxy == nullptr) {
53                 EVENT_LOGE("GetSystemAbilityManager failed");
54                 return;
55             }
56 
57             sptr<SystemAbilityStatusChangeListener> listener =
58                 new (std::nothrow) CommonEventDeathRecipient::SystemAbilityStatusChangeListener();
59             if (listener == nullptr) {
60                 EVENT_LOGE("new SystemAbilityStatusChangeListener failed");
61                 return;
62             }
63             int32_t ret = samgrProxy->SubscribeSystemAbility(COMMON_EVENT_SERVICE_ID, listener);
64             if (ret != ERR_OK) {
65                 EVENT_LOGE("SubscribeSystemAbility to sa manager failed");
66                 return;
67             }
68             statusChangeListener_ = listener;
69         }
70     }
71 }
72 
SystemAbilityStatusChangeListener()73 CommonEventDeathRecipient::SystemAbilityStatusChangeListener::SystemAbilityStatusChangeListener()
74 {
75     queue_ = std::make_shared<ffrt::queue>("cesResubMain");
76 }
77 
OnAddSystemAbility(int32_t systemAbilityId,const std::string & deviceId)78 void CommonEventDeathRecipient::SystemAbilityStatusChangeListener::OnAddSystemAbility(
79     int32_t systemAbilityId, const std::string& deviceId)
80 {
81     std::lock_guard<ffrt::mutex> lock(mutex_);
82     if (!isSAOffline_) {
83         return;
84     }
85     EVENT_LOGI("CES restarted, try to reconnect");
86     if (CommonEvent::GetInstance()->Reconnect()) {
87         auto resubscrebeFund = [] () {
88             CommonEvent::GetInstance()->Resubscribe();
89         };
90         queue_->submit(resubscrebeFund);
91         isSAOffline_ = false;
92     }
93 }
94 
OnRemoveSystemAbility(int32_t systemAbilityId,const std::string & deviceId)95 void CommonEventDeathRecipient::SystemAbilityStatusChangeListener::OnRemoveSystemAbility(
96     int32_t systemAbilityId, const std::string& deviceId)
97 {
98     EVENT_LOGI("CES died");
99     std::lock_guard<ffrt::mutex> lock(mutex_);
100     isSAOffline_ = true;
101 }
102 }  // namespace EventFwk
103 }  // namespace OHOS
104