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 #define LOG_TAG "EVENT_HANDLER"
17
18 #include "account_delegate_impl.h"
19 #include <thread>
20 #include <unistd.h>
21
22 namespace OHOS {
23 namespace DistributedKv {
24 using namespace OHOS::EventFwk;
25 using namespace OHOS::AAFwk;
26 using namespace OHOS::DistributedData;
27
EventSubscriber(const CommonEventSubscribeInfo & info)28 EventSubscriber::EventSubscriber(const CommonEventSubscribeInfo &info) : CommonEventSubscriber(info) {}
29
OnReceiveEvent(const CommonEventData & event)30 void EventSubscriber::OnReceiveEvent(const CommonEventData &event)
31 {
32 const auto want = event.GetWant();
33 AccountEventInfo accountEventInfo {};
34 std::string action = want.GetAction();
35 ZLOGI("Want Action is %{public}s", action.c_str());
36
37 if (action == CommonEventSupport::COMMON_EVENT_USER_REMOVED) {
38 accountEventInfo.status = AccountStatus::DEVICE_ACCOUNT_DELETE;
39 accountEventInfo.userId = std::to_string(event.GetCode());
40 } else if (action == CommonEventSupport::COMMON_EVENT_USER_SWITCHED) {
41 accountEventInfo.status = AccountStatus::DEVICE_ACCOUNT_SWITCHED;
42 accountEventInfo.userId = std::to_string(event.GetCode());
43 } else if (action == CommonEventSupport::COMMON_EVENT_USER_UNLOCKED) {
44 accountEventInfo.status = AccountStatus::DEVICE_ACCOUNT_UNLOCKED;
45 accountEventInfo.userId = std::to_string(event.GetCode());
46 } else {
47 return;
48 }
49 eventCallback_(accountEventInfo);
50 }
51
SetEventCallback(EventCallback callback)52 void EventSubscriber::SetEventCallback(EventCallback callback)
53 {
54 eventCallback_ = callback;
55 }
56
~AccountDelegateImpl()57 AccountDelegateImpl::~AccountDelegateImpl()
58 {
59 observerMap_.Clear();
60 }
61
NotifyAccountChanged(const AccountEventInfo & accountEventInfo)62 void AccountDelegateImpl::NotifyAccountChanged(const AccountEventInfo &accountEventInfo)
63 {
64 observerMap_.ForEach([&accountEventInfo] (const auto& key, auto& val) {
65 if (val->GetLevel() == AccountDelegate::Observer::LevelType::HIGH) {
66 val->OnAccountChanged(accountEventInfo);
67 }
68 return false;
69 });
70 observerMap_.ForEach([&accountEventInfo] (const auto& key, auto& val) {
71 if (val->GetLevel() == AccountDelegate::Observer::LevelType::LOW) {
72 val->OnAccountChanged(accountEventInfo);
73 }
74 return false;
75 });
76 }
77
Subscribe(std::shared_ptr<Observer> observer)78 Status AccountDelegateImpl::Subscribe(std::shared_ptr<Observer> observer)
79 {
80 ZLOGD("start");
81 if (observer == nullptr || observer->Name().empty()) {
82 return Status::INVALID_ARGUMENT;
83 }
84 if (observerMap_.Contains(observer->Name())) {
85 return Status::INVALID_ARGUMENT;
86 }
87
88 auto ret = observerMap_.Insert(observer->Name(), observer);
89 if (ret) {
90 ZLOGD("end");
91 return Status::SUCCESS;
92 }
93 ZLOGE("fail");
94 return Status::ERROR;
95 }
96
Unsubscribe(std::shared_ptr<Observer> observer)97 Status AccountDelegateImpl::Unsubscribe(std::shared_ptr<Observer> observer)
98 {
99 ZLOGD("start");
100 if (observer == nullptr || observer->Name().empty()) {
101 return Status::INVALID_ARGUMENT;
102 }
103 if (!observerMap_.Contains(observer->Name())) {
104 return Status::INVALID_ARGUMENT;
105 }
106
107 auto ret = observerMap_.Erase(observer->Name());
108 if (ret) {
109 ZLOGD("end");
110 return Status::SUCCESS;
111 }
112 ZLOGD("fail");
113 return Status::ERROR;
114 }
115
RegisterHashFunc(HashFunc hash)116 bool AccountDelegateImpl::RegisterHashFunc(HashFunc hash)
117 {
118 hash_ = hash;
119 return true;
120 }
121
DoHash(const void * data,size_t size,bool isUpper) const122 std::string AccountDelegateImpl::DoHash(const void *data, size_t size, bool isUpper) const
123 {
124 if (hash_ == nullptr) {
125 return std::string(static_cast<const char *>(data), size);
126 }
127 return hash_(data, size, isUpper);
128 }
129 } // namespace DistributedKv
130 } // namespace OHOS
131