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 {
44 return;
45 }
46 eventCallback_(accountEventInfo);
47 }
48
SetEventCallback(EventCallback callback)49 void EventSubscriber::SetEventCallback(EventCallback callback)
50 {
51 eventCallback_ = callback;
52 }
53
~AccountDelegateImpl()54 AccountDelegateImpl::~AccountDelegateImpl()
55 {
56 observerMap_.Clear();
57 }
58
NotifyAccountChanged(const AccountEventInfo & accountEventInfo)59 void AccountDelegateImpl::NotifyAccountChanged(const AccountEventInfo &accountEventInfo)
60 {
61 observerMap_.ForEach([&accountEventInfo] (const auto& key, auto& val) {
62 if (val->GetLevel() == AccountDelegate::Observer::LevelType::HIGH) {
63 val->OnAccountChanged(accountEventInfo);
64 }
65 return false;
66 });
67 observerMap_.ForEach([&accountEventInfo] (const auto& key, auto& val) {
68 if (val->GetLevel() == AccountDelegate::Observer::LevelType::LOW) {
69 val->OnAccountChanged(accountEventInfo);
70 }
71 return false;
72 });
73 }
74
Subscribe(std::shared_ptr<Observer> observer)75 Status AccountDelegateImpl::Subscribe(std::shared_ptr<Observer> observer)
76 {
77 ZLOGD("start");
78 if (observer == nullptr || observer->Name().empty()) {
79 return Status::INVALID_ARGUMENT;
80 }
81 if (observerMap_.Contains(observer->Name())) {
82 return Status::INVALID_ARGUMENT;
83 }
84
85 auto ret = observerMap_.Insert(observer->Name(), observer);
86 if (ret) {
87 ZLOGD("end");
88 return Status::SUCCESS;
89 }
90 ZLOGE("fail");
91 return Status::ERROR;
92 }
93
Unsubscribe(std::shared_ptr<Observer> observer)94 Status AccountDelegateImpl::Unsubscribe(std::shared_ptr<Observer> observer)
95 {
96 ZLOGD("start");
97 if (observer == nullptr || observer->Name().empty()) {
98 return Status::INVALID_ARGUMENT;
99 }
100 if (!observerMap_.Contains(observer->Name())) {
101 return Status::INVALID_ARGUMENT;
102 }
103
104 auto ret = observerMap_.Erase(observer->Name());
105 if (ret) {
106 ZLOGD("end");
107 return Status::SUCCESS;
108 }
109 ZLOGD("fail");
110 return Status::ERROR;
111 }
112
RegisterHashFunc(HashFunc hash)113 bool AccountDelegateImpl::RegisterHashFunc(HashFunc hash)
114 {
115 hash_ = hash;
116 return true;
117 }
118
DoHash(const void * data,size_t size,bool isUpper) const119 std::string AccountDelegateImpl::DoHash(const void *data, size_t size, bool isUpper) const
120 {
121 if (hash_ == nullptr) {
122 return std::string(static_cast<const char *>(data), size);
123 }
124 return hash_(data, size, isUpper);
125 }
126 } // namespace DistributedKv
127 } // namespace OHOS
128