1 /*
2 * Copyright (C) 2025 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 "ptp_special_handles.h"
17
18 namespace OHOS {
19 namespace Media {
20 using namespace std;
21 shared_ptr<PtpSpecialHandles> PtpSpecialHandles::instance_ = nullptr;
22 mutex PtpSpecialHandles::mutex_;
23
GetInstance()24 shared_ptr<PtpSpecialHandles> PtpSpecialHandles::GetInstance()
25 {
26 if (instance_ == nullptr) {
27 lock_guard<mutex> lock(mutex_);
28 if (instance_ == nullptr) {
29 instance_ = make_shared<PtpSpecialHandles>();
30 }
31 }
32 return instance_;
33 }
34
~PtpSpecialHandles()35 PtpSpecialHandles::~PtpSpecialHandles()
36 {
37 deletedHandleMap_.Clear();
38 }
39
AddHandleToMap(uint32_t deletedHandle,uint32_t realHandle)40 void PtpSpecialHandles::AddHandleToMap(uint32_t deletedHandle, uint32_t realHandle)
41 {
42 deletedHandleMap_.EnsureInsert(deletedHandle, realHandle);
43 }
44
HandleConvertToAdded(uint32_t deletedHandle)45 uint32_t PtpSpecialHandles::HandleConvertToAdded(uint32_t deletedHandle)
46 {
47 uint32_t addHandle = 0;
48 if (deletedHandleMap_.Find(deletedHandle, addHandle)) {
49 return addHandle;
50 }
51 return deletedHandle;
52 }
53
FindRealHandle(uint32_t realHandle)54 bool PtpSpecialHandles::FindRealHandle(uint32_t realHandle)
55 {
56 bool found = false;
57 deletedHandleMap_.Iterate([&found, realHandle](const uint32_t key, uint32_t& value) {
58 if (realHandle == value) {
59 found = true;
60 }
61 });
62 return found;
63 }
64
FindDeletedHandle(uint32_t deletedHandle)65 bool PtpSpecialHandles::FindDeletedHandle(uint32_t deletedHandle)
66 {
67 uint32_t value = 0;
68 return deletedHandleMap_.Find(deletedHandle, value);
69 }
70
HandleConvertToDeleted(uint32_t realHandle)71 uint32_t PtpSpecialHandles::HandleConvertToDeleted(uint32_t realHandle)
72 {
73 uint32_t deleteHandle = realHandle;
74 deletedHandleMap_.Iterate([&deleteHandle, realHandle](const uint32_t key, uint32_t& value) {
75 if (realHandle == value) {
76 deleteHandle = key;
77 }
78 });
79 return deleteHandle;
80 }
81
ClearDeletedHandles()82 void PtpSpecialHandles::ClearDeletedHandles()
83 {
84 deletedHandleMap_.Clear();
85 }
86 } // namespace Media
87 } // namespace OHOS