1 /*
2 * Copyright (c) 2024 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 "adapter/ohos/osal/thp_extra_manager_impl.h"
17
18 #include <dlfcn.h>
19
20 #include "adapter/ohos/entrance/utils.h"
21
22 namespace OHOS::Ace::NG {
23 void* THPExtraManagerImpl::lib_ = nullptr;
24
Init()25 bool THPExtraManagerImpl::Init()
26 {
27 ThpExtraRunCommand_ = [](const char* command, const char* parameters) -> const char* {
28 return "ok";
29 };
30 if (lib_ == nullptr) {
31 lib_ = dlopen("/system/lib64/libthp_extra_innerapi.z.so", RTLD_LAZY);
32 }
33 if (lib_ == nullptr) {
34 LOGI("Failed to open libthp_extra_innerapi.z.so, reason: %{public}s", dlerror());
35 return false;
36 }
37 ThpExtraRunCommand_ = reinterpret_cast<ThpExtraRunCommandFunc>(dlsym(lib_, "ThpExtraRunCommand"));
38 if (ThpExtraRunCommand_ == nullptr) {
39 ThpExtraRunCommand_ = [](const char* command, const char* parameters) -> const char* {
40 return "ok";
41 };
42 LOGI("Failed to load ThpExtraRunCommand");
43 return false;
44 }
45 auto ThpExtraGetConfigStr = reinterpret_cast<ThpExtraGetConfigStrFunc>(dlsym(lib_, "ThpExtraGetConfigStr"));
46 if (ThpExtraGetConfigStr == nullptr) {
47 LOGI("Load ThpExtraGetConfigStr failed");
48 return false;
49 }
50
51 std::string jsonStr = std::string(ThpExtraGetConfigStr());
52 if (jsonStr.empty()) {
53 return false;
54 }
55 auto rootJson = JsonUtil::ParseJsonString(jsonStr);
56 if (!rootJson || !rootJson->IsValid()) {
57 return false;
58 }
59 auto hotzone = rootJson->GetObject("hotzone");
60 if (hotzone == nullptr) {
61 return false;
62 }
63 enable_ = hotzone->GetBool("enable", false);
64 height_ = hotzone->GetInt("height");
65 width_ = hotzone->GetInt("width");
66 if (!enable_) {
67 return false;
68 }
69 return true;
70 }
71
ThpExtraRunCommand(const char * command,const char * parameters)72 const char* THPExtraManagerImpl::ThpExtraRunCommand(const char* command, const char* parameters)
73 {
74 if (ThpExtraRunCommand_) {
75 return ThpExtraRunCommand_(command, parameters);
76 }
77 return "";
78 }
79
GetHeight(void) const80 int32_t THPExtraManagerImpl::GetHeight(void) const
81 {
82 return height_;
83 }
84
GetWidth(void) const85 int32_t THPExtraManagerImpl::GetWidth(void) const
86 {
87 return width_;
88 }
89 } // namespace OHOS::Ace::NG
90