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 "aafwk_browser_host_impl.h"
17 
18 #include "nweb_log.h"
19 #include "graphic_adapter.h"
20 #include "ibuffer_consumer_listener.h"
21 #include "iconsumer_surface.h"
22 #include "nweb_adapter_helper.h"
23 #include "ipc_types.h"
24 #include "surface_utils.h"
25 #include "foundation/graphic/graphic_surface/interfaces/inner_api/surface/window.h"
26 #include "foundation/graphic/graphic_surface/surface/include/native_window.h"
27 
28 namespace OHOS::NWeb {
29 
BrowserHost()30 BrowserHost::BrowserHost()
31 {
32     memberFuncMap_[static_cast<uint32_t>(IBrowser::Message::QUERY_RENDER_SURFACE)] =
33         [](BrowserHost* that, MessageParcel &data, MessageParcel &reply) {
34             return that->HandleQueryRenderSurface(data, reply);
35         };
36     memberFuncMap_[static_cast<uint32_t>(IBrowser::Message::REPORT_THREAD)] =
37         [](BrowserHost* that, MessageParcel &data, MessageParcel &reply) {
38             return that->HandleReportThread(data, reply);
39         };
40     memberFuncMap_[static_cast<uint32_t>(IBrowser::Message::PASS_SURFACE)] =
41         [](BrowserHost* that, MessageParcel &data, MessageParcel &reply) {
42             return that->HandlePassSurface(data, reply);
43         };
44     memberFuncMap_[static_cast<uint32_t>(IBrowser::Message::DESTROY_RENDER_SURFACE)] =
45         [](BrowserHost* that, MessageParcel &data, MessageParcel &reply) {
46             return that->HandleDestroyRenderSurface(data, reply);
47         };
48 }
49 
~BrowserHost()50 BrowserHost::~BrowserHost()
51 {
52     memberFuncMap_.clear();
53 }
54 
OnRemoteRequest(uint32_t code,MessageParcel & data,MessageParcel & reply,MessageOption & option)55 int BrowserHost::OnRemoteRequest(uint32_t code, MessageParcel &data,
56     MessageParcel& reply, MessageOption& option)
57 {
58     std::u16string descriptor = BrowserHost::GetDescriptor();
59     std::u16string remoteDescriptor = data.ReadInterfaceToken();
60     if (descriptor != remoteDescriptor) {
61         WVLOG_E("local descriptor is not equal to remote");
62         return ERR_INVALID_STATE;
63     }
64 
65     auto itFunc = memberFuncMap_.find(code);
66     if (itFunc != memberFuncMap_.end()) {
67         auto& memberFunc = itFunc->second;
68         if (memberFunc) {
69             return memberFunc(this, data, reply);
70         }
71     }
72     return IPCObjectStub::OnRemoteRequest(code, data, reply, option);
73 }
74 
HandleQueryRenderSurface(MessageParcel & data,MessageParcel & reply)75 int BrowserHost::HandleQueryRenderSurface(MessageParcel &data, MessageParcel &reply)
76 {
77     int surface_id = data.ReadInt32();
78 
79     // call child class, get surface void* from kelnel
80     // -> aafwk_browser_host_impl.cpp
81     sptr<IRemoteObject> surfaceObj = QueryRenderSurface(surface_id);
82     reply.WriteRemoteObject(surfaceObj);
83     return 0;
84 }
85 
HandleReportThread(MessageParcel & data,MessageParcel & reply)86 int BrowserHost::HandleReportThread(MessageParcel &data, MessageParcel &reply)
87 {
88     int status = data.ReadInt32();
89     int process_id = data.ReadInt32();
90     int thread_id = data.ReadInt32();
91     int role = data.ReadInt32();
92 
93     // call child class, report thread
94     // -> aafwk_browser_host_impl.cpp
95     ReportThread(status, process_id, thread_id, role);
96     return 0;
97 }
98 
HandlePassSurface(MessageParcel & data,MessageParcel & reply)99 int BrowserHost::HandlePassSurface(MessageParcel &data, MessageParcel &reply)
100 {
101     sptr<IRemoteObject> surfaceObject = data.ReadRemoteObject();
102     sptr<IBufferProducer> bufferProducer = iface_cast<IBufferProducer>(surfaceObject);
103     if (bufferProducer == nullptr) {
104         WVLOG_E("HandlePass buffer failed.");
105         return 0;
106     }
107     sptr<Surface> surface = Surface::CreateSurfaceAsProducer(bufferProducer);
108     int64_t surface_id = data.ReadInt64();
109     PassSurface(surface, surface_id);
110     return 0;
111 }
112 
HandleDestroyRenderSurface(MessageParcel & data,MessageParcel & reply)113 int BrowserHost::HandleDestroyRenderSurface(MessageParcel &data, MessageParcel &reply)
114 {
115     int32_t surface_id = data.ReadInt32();
116     DestroyRenderSurface(surface_id);
117     return 0;
118 }
119 
AafwkBrowserHostImpl(std::shared_ptr<AafwkBrowserHostAdapter> adapter)120 AafwkBrowserHostImpl::AafwkBrowserHostImpl(std::shared_ptr<AafwkBrowserHostAdapter> adapter)
121     : browserHostAdapter_(adapter) {}
122 
QueryRenderSurface(int32_t surface_id)123 sptr<IRemoteObject> AafwkBrowserHostImpl::QueryRenderSurface(int32_t surface_id)
124 {
125     WVLOG_D("browser host impl get request for window id = %{public}d", surface_id);
126     if (browserHostAdapter_ == nullptr) {
127         return nullptr;
128     }
129     // send to kernel (Browser)
130     void* window = browserHostAdapter_->GetSurfaceFromKernel(surface_id);
131     if (window) {
132         OHNativeWindow* ohNativeWindow = reinterpret_cast<OHNativeWindow*>(window);
133         sptr<Surface> surface = ohNativeWindow->surface;
134         WVLOG_D("browser host impl get request window");
135         return surface->GetProducer()->AsObject();
136     }
137     WVLOG_E("browser host impl get surface from kernel failed");
138     return nullptr;
139 }
140 
ReportThread(int32_t status,int32_t process_id,int32_t thread_id,int32_t role)141 void AafwkBrowserHostImpl::ReportThread(int32_t status, int32_t process_id, int32_t thread_id, int32_t role)
142 {
143     ResSchedStatusAdapter resSchedStatus = static_cast<ResSchedStatusAdapter>(status);
144     ResSchedRoleAdapter resSchedRole = static_cast<ResSchedRoleAdapter>(role);
145     OHOS::NWeb::ResSchedClientAdapter::ReportKeyThread(
146         resSchedStatus, process_id, thread_id, resSchedRole);
147 }
148 
PassSurface(sptr<Surface> surface,int64_t surface_id)149 void AafwkBrowserHostImpl::PassSurface(sptr<Surface> surface, int64_t surface_id)
150 {
151     sptr<Surface> surfaceTmp = surface;
152     SurfaceUtils* utils = SurfaceUtils::GetInstance();
153     if (!utils) {
154         WVLOG_E("get surfaceUtils failed.");
155         return;
156     }
157     surface_map_.emplace(surface_id, surfaceTmp);
158     utils->Add(surface_id, surfaceTmp);
159 }
160 
DestroyRenderSurface(int32_t surface_id)161 void AafwkBrowserHostImpl::DestroyRenderSurface(int32_t surface_id)
162 {
163     if (browserHostAdapter_ == nullptr) {
164         return;
165     }
166     browserHostAdapter_->DestroySurfaceFromKernel(surface_id);
167     WVLOG_D("Destroy render surface id is %{public}d", surface_id);
168 }
169 } // namespace OHOS::NWeb