• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2023-2024 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 "event_handler_factory.h"
17 
18 #include "distributed_device_profile_errors.h"
19 #include "distributed_device_profile_constants.h"
20 #include "distributed_device_profile_log.h"
21 
22 namespace OHOS {
23 namespace DistributedDeviceProfile {
24 IMPLEMENT_SINGLE_INSTANCE(EventHandlerFactory);
25 
26 namespace {
27     const std::string TAG = "EventHandlerFactory";
28 }
29 
Init()30 int32_t EventHandlerFactory::Init()
31 {
32     HILOGI("call!");
33     std::lock_guard<std::mutex> lock(eventHandlerMutex_);
34     if (eventHandler_ != nullptr) {
35         return DP_SUCCESS;
36     }
37     auto runner = AppExecFwk::EventRunner::Create(DP_HANDLER);
38     eventHandler_ = std::make_shared<AppExecFwk::EventHandler>(runner);
39     return DP_SUCCESS;
40 }
41 
UnInit()42 int32_t EventHandlerFactory::UnInit()
43 {
44     HILOGI("UnInit");
45     std::lock_guard<std::mutex> lock(eventHandlerMutex_);
46     if (eventHandler_ != nullptr) {
47         eventHandler_->RemoveAllEvents();
48         eventHandler_ = nullptr;
49     }
50     return DP_SUCCESS;
51 }
52 
GetEventHandler()53 std::shared_ptr<AppExecFwk::EventHandler> EventHandlerFactory::GetEventHandler()
54 {
55     HILOGD("call!");
56     std::lock_guard<std::mutex> lock(eventHandlerMutex_);
57     return eventHandler_;
58 }
59 } // namespace DeviceProfile
60 } // namespace OHOS
61