• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #include "object_collector.h"
17 #include <iremote_object.h>
18 
19 #include "hdf_base.h"
20 #include "hdf_log.h"
21 #include "osal_time.h"
22 
23 using namespace OHOS::HDI;
24 
25 ObjectCollector *ObjectCollector::instance_ = new ObjectCollector();
26 
GetInstance()27 ObjectCollector &ObjectCollector::GetInstance()
28 {
29     return *instance_;
30 }
31 
ConstructorRegister(const std::u16string & interfaceName,const Constructor & constructor)32 bool ObjectCollector::ConstructorRegister(const std::u16string &interfaceName, const Constructor &constructor)
33 {
34     if (interfaceName.empty()) {
35         return false;
36     }
37     std::lock_guard<std::mutex> lock(mutex_);
38     constructorMapper_.emplace(interfaceName, std::move(constructor));
39     return true;
40 }
41 
ConstructorUnRegister(const std::u16string & interfaceName)42 void ObjectCollector::ConstructorUnRegister(const std::u16string &interfaceName)
43 {
44     std::lock_guard<std::mutex> lock(mutex_);
45     constructorMapper_.erase(interfaceName);
46 }
47 
NewObjectLocked(const OHOS::sptr<HdiBase> & interface,const std::u16string & interfaceName)48 OHOS::sptr<OHOS::IRemoteObject> ObjectCollector::NewObjectLocked(
49     const OHOS::sptr<HdiBase> &interface, const std::u16string &interfaceName)
50 {
51     if (interface == nullptr) {
52         return nullptr;
53     }
54     auto constructor = constructorMapper_.find(interfaceName);
55     if (constructor != constructorMapper_.end()) {
56         return constructor->second(interface);
57     }
58 
59     return nullptr;
60 }
61 
NewObject(const OHOS::sptr<HdiBase> & interface,const std::u16string & interfaceName)62 OHOS::sptr<OHOS::IRemoteObject> ObjectCollector::NewObject(
63     const OHOS::sptr<HdiBase> &interface, const std::u16string &interfaceName)
64 {
65     std::lock_guard<std::mutex> lock(mutex_);
66     return NewObjectLocked(interface, interfaceName);
67 }
68 
GetOrNewObject(const OHOS::sptr<HdiBase> & interface,const std::u16string & interfaceName)69 OHOS::sptr<OHOS::IRemoteObject> ObjectCollector::GetOrNewObject(
70     const OHOS::sptr<HdiBase> &interface, const std::u16string &interfaceName)
71 {
72     if (interface == nullptr) {
73         return nullptr;
74     }
75     mutex_.lock();
76 RETRY:
77     auto it = interfaceObjectCollector_.find(interface.GetRefPtr());
78     if (it != interfaceObjectCollector_.end()) {
79         if (it->second->GetSptrRefCount() == 0) {
80             // may object is releasing, yield to sync
81             mutex_.unlock();
82             OsalMSleep(1);
83             mutex_.lock();
84             goto RETRY;
85         }
86         mutex_.unlock();
87         return it->second;
88     }
89     sptr<IRemoteObject> object = NewObjectLocked(interface, interfaceName);
90     interfaceObjectCollector_[interface.GetRefPtr()] = object.GetRefPtr();
91     mutex_.unlock();
92     return object;
93 }
94 
RemoveObject(const OHOS::sptr<HdiBase> & interface)95 bool ObjectCollector::RemoveObject(const OHOS::sptr<HdiBase> &interface)
96 {
97     std::lock_guard<std::mutex> lock(mutex_);
98     auto it = interfaceObjectCollector_.find(interface.GetRefPtr());
99     if (it == interfaceObjectCollector_.end()) {
100         return false;
101     }
102     interfaceObjectCollector_.erase(it);
103     return true;
104 }
105