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 #ifndef DISTRIBUTEDDATAMGR_ACCOUNT_DELEGATE_IMPL_H
17 #define DISTRIBUTEDDATAMGR_ACCOUNT_DELEGATE_IMPL_H
18 
19 #include "account_delegate.h"
20 #include <mutex>
21 #include <memory.h>
22 #include "common_event_manager.h"
23 #include "common_event_subscriber.h"
24 #include "common_event_support.h"
25 #include "concurrent_map.h"
26 #include "log_print.h"
27 
28 namespace OHOS {
29 namespace DistributedKv {
30 using namespace OHOS::EventFwk;
31 using EventCallback = std::function<void(AccountEventInfo &account)>;
32 
33 class EventSubscriber final : public CommonEventSubscriber {
34 public:
~EventSubscriber()35     ~EventSubscriber() {}
36     explicit EventSubscriber(const CommonEventSubscribeInfo &info);
37     void SetEventCallback(EventCallback callback);
38     void OnReceiveEvent(const CommonEventData &event) override;
39 private:
40     EventCallback eventCallback_ {};
41 };
42 
43 class AccountDelegateImpl : public AccountDelegate {
44 public:
45     ~AccountDelegateImpl();
46     Status Subscribe(std::shared_ptr<Observer> observer) override __attribute__((no_sanitize("cfi")));
47     Status Unsubscribe(std::shared_ptr<Observer> observer) override __attribute__((no_sanitize("cfi")));
48     void NotifyAccountChanged(const AccountEventInfo &accountEventInfo);
49     bool RegisterHashFunc(HashFunc hash) override;
50 
51 protected:
52     std::string DoHash(const void *data, size_t size, bool isUpper) const;
53 private:
54     ConcurrentMap<std::string, std::shared_ptr<Observer>> observerMap_ {};
55     HashFunc hash_ = nullptr;
56 };
57 }  // namespace DistributedKv
58 }  // namespace OHOS
59 #endif // DISTRIBUTEDDATAMGR_ACCOUNT_DELEGATE_IMPL_H
60