• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #include "subscribe_info_checker.h"
17 
18 #include "device_profile_log.h"
19 #include "profile_event.h"
20 
21 namespace OHOS {
22 namespace DeviceProfile {
23 namespace {
24 const std::string TAG = "SubscribeInfoChecker";
25 }
26 
27 IMPLEMENT_SINGLE_INSTANCE(SubscribeInfoChecker);
28 
SubscribeInfoChecker()29 SubscribeInfoChecker::SubscribeInfoChecker()
30 {
31     checkersMap_[EVENT_SYNC_COMPLETED] = &SubscribeInfoChecker::CheckSyncEventInner;
32     checkersMap_[EVENT_PROFILE_CHANGED] = &SubscribeInfoChecker::CheckProfileChangeInner;
33 }
34 
Check(const std::list<SubscribeInfo> & subscribeInfos)35 bool SubscribeInfoChecker::Check(const std::list<SubscribeInfo>& subscribeInfos)
36 {
37     auto checker = [this](const auto& subscribeInfo) {
38         auto profileEvent = subscribeInfo.profileEvent;
39         auto iter = checkersMap_.find(profileEvent);
40         if (iter != checkersMap_.end()) {
41             auto func = iter->second;
42             if (func != nullptr) {
43                 return (this->*func)(subscribeInfo);
44             }
45         }
46         HILOGE("unknown profile event = %{public}u", static_cast<uint32_t>(profileEvent));
47         return false;
48     };
49     return std::all_of(subscribeInfos.begin(), subscribeInfos.end(), checker);
50 }
51 
CheckSyncEventInner(const SubscribeInfo & subscribeInfo)52 bool SubscribeInfoChecker::CheckSyncEventInner([[maybe_unused]] const SubscribeInfo& subscribeInfo)
53 {
54     // currently no need to check for sync event
55     return true;
56 }
57 
CheckProfileChangeInner(const SubscribeInfo & subscribeInfo)58 bool SubscribeInfoChecker::CheckProfileChangeInner(const SubscribeInfo& subscribeInfo)
59 {
60     const ExtraInfo& extraInfo = subscribeInfo.extraInfo;
61     const auto& deviceIdJson = extraInfo["deviceId"];
62     if (deviceIdJson == nullptr) {
63         return false;
64     }
65     HILOGD("check deviceId");
66     if (deviceIdJson.type() != ExtraInfo::value_t::string) {
67         return false;
68     }
69 
70     const auto& serviceIdsJson = extraInfo["serviceIds"];
71     if (serviceIdsJson == nullptr) {
72         return false;
73     }
74     HILOGD("check serviceIds");
75     auto type = serviceIdsJson.type();
76     if (type != ExtraInfo::value_t::array) {
77         return false;
78     } else {
79         return std::all_of(serviceIdsJson.begin(), serviceIdsJson.end(),
80             [](const auto& serviceIdJson) { return serviceIdJson.type() == ExtraInfo::value_t::string; });
81     }
82     return true;
83 }
84 } // namespace DeviceProfile
85 } // namespace OHOS