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 "common_event_manager_service_ability.h"
17 
18 #include "common_event_manager_service.h"
19 #include "event_log_wrapper.h"
20 #include <new>
21 
22 namespace OHOS {
23 namespace EventFwk {
24 namespace {
25 REGISTER_SYSTEM_ABILITY_BY_ID(CommonEventManagerServiceAbility, COMMON_EVENT_SERVICE_ID, true);
26 }
27 
CommonEventManagerServiceAbility(const int32_t systemAbilityId,bool runOnCreate)28 CommonEventManagerServiceAbility::CommonEventManagerServiceAbility(const int32_t systemAbilityId, bool runOnCreate)
29     : SystemAbility(systemAbilityId, runOnCreate), service_(nullptr)
30 {}
31 
~CommonEventManagerServiceAbility()32 CommonEventManagerServiceAbility::~CommonEventManagerServiceAbility()
33 {}
34 
OnStart()35 void CommonEventManagerServiceAbility::OnStart()
36 {
37     EVENT_LOGD("OnStart called.");
38     if (service_ != nullptr) {
39         EVENT_LOGD("The CommonEventManagerService has existed.");
40         return;
41     }
42 
43     service_ = CommonEventManagerService::GetInstance();
44     ErrCode errorCode = service_->Init();
45     if (errorCode != ERR_OK) {
46         EVENT_LOGE("Failed to init the commonEventManagerService instance.");
47         return;
48     }
49 
50     if (!Publish(service_)) {
51         EVENT_LOGE("Failed to publish CommonEventManagerService to SystemAbilityMgr");
52         return;
53     }
54 }
55 
OnStop()56 void CommonEventManagerServiceAbility::OnStop()
57 {
58     EVENT_LOGD("onStop called.");
59     service_ = nullptr;
60 }
61 }  // namespace EventFwk
62 }  // namespace OHOS