1 /* 2 * Copyright (c) 2022 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 DEVICE_SECURITY_LEVEL_CALLBACK_HELPER 17 #define DEVICE_SECURITY_LEVEL_CALLBACK_HELPER 18 19 #include <cstdint> 20 #include <map> 21 #include <mutex> 22 23 #include "message_option.h" 24 #include "message_parcel.h" 25 #include "nocopyable.h" 26 #include "refbase.h" 27 #include "singleton.h" 28 #include "timer.h" 29 30 #include "device_security_defines.h" 31 #include "device_security_level_callback_stub.h" 32 #include "device_security_level_defines.h" 33 34 namespace OHOS { 35 namespace Security { 36 namespace DeviceSecurityLevel { 37 using namespace OHOS; 38 39 class DeviceSecurityLevelCallbackHelper : public DelayedRefSingleton<DeviceSecurityLevelCallbackHelper> { 40 public: 41 DeviceSecurityLevelCallbackHelper(); 42 ~DeviceSecurityLevelCallbackHelper() override; 43 bool Publish(const DeviceIdentify &identity, const ResultCallback &callback, uint32_t keep, 44 sptr<DeviceSecurityLevelCallbackStub> &stub, uint32_t &cookie); 45 bool Withdraw(uint32_t cookie); 46 47 private: 48 class CallbackInfoHolder final : public NoCopyable { 49 struct CallbackInfo { 50 DeviceIdentify identity; 51 ResultCallback callback; 52 uint64_t cookie {0}; 53 }; 54 55 public: 56 CallbackInfoHolder(); 57 ~CallbackInfoHolder() override; 58 bool PushCallback(const DeviceIdentify &identity, const ResultCallback &callback, uint32_t keep, 59 uint32_t &cookie); 60 bool PopCallback(uint32_t cookie, uint32_t result, uint32_t level); 61 bool PopCallback(uint32_t cookie); 62 63 private: 64 std::map<uint32_t, CallbackInfo> map_; 65 uint32_t generate_ {0}; 66 std::mutex mutex_; 67 OHOS::Utils::Timer timer_; 68 }; 69 70 int32_t OnRemoteRequest(uint32_t code, MessageParcel &data, MessageParcel &reply, MessageOption &option); 71 CallbackInfoHolder holder_; 72 sptr<DeviceSecurityLevelCallbackStub> stub_; 73 }; 74 } // namespace DeviceSecurityLevel 75 } // namespace Security 76 } // namespace OHOS 77 78 #endif // DEVICE_SECURITY_LEVEL_CALLBACK_HELPER