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