• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "remote_object_holder_impl.h"
17 
18 #include <string_ex.h>
19 
20 #include "ipc_utils_ffi.h"
21 
22 namespace OHOS {
RemoteObjectHolderImpl(const std::u16string & descriptor)23 RemoteObjectHolderImpl::RemoteObjectHolderImpl(const std::u16string& descriptor)
24     : descriptor_(descriptor), sptrCachedObject_(nullptr), wptrCachedObject_(nullptr), attachCount_(1)
25 {
26     jsThreadId_ = std::this_thread::get_id();
27 }
28 
~RemoteObjectHolderImpl()29 RemoteObjectHolderImpl::~RemoteObjectHolderImpl() {}
30 
Get()31 sptr<IRemoteObject> RemoteObjectHolderImpl::Get()
32 {
33     std::lock_guard<std::mutex> lockGuard(mutex_);
34     if (sptrCachedObject_ != nullptr) {
35         return sptrCachedObject_;
36     }
37     sptr<IRemoteObject> tmp = wptrCachedObject_.promote();
38     if (tmp == nullptr) {
39         tmp = new (std::nothrow) RemoteObjectImpl(jsThreadId_, descriptor_);
40         if (tmp == nullptr) {
41             ZLOGE(LOG_LABEL, "new RemoteObjectImpl failed");
42             return nullptr;
43         }
44         wptrCachedObject_ = tmp;
45     }
46     return tmp;
47 }
48 
Set(sptr<IRemoteObject> object)49 void RemoteObjectHolderImpl::Set(sptr<IRemoteObject> object)
50 {
51     std::lock_guard<std::mutex> lockGuard(mutex_);
52     IPCObjectStub* tmp = static_cast<IPCObjectStub*>(object.GetRefPtr());
53     if (tmp->GetObjectType() == IPCObjectStub::OBJECT_TYPE_JAVASCRIPT) {
54         wptrCachedObject_ = object;
55     } else {
56         sptrCachedObject_ = object;
57     }
58 }
59 
attachLocalInterface(std::string & descriptor)60 void RemoteObjectHolderImpl::attachLocalInterface(std::string& descriptor)
61 {
62     descriptor_ = Str8ToStr16(descriptor);
63 }
64 } // namespace OHOS