• 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 "nweb_handler_impl_test.h"
17 
18 #include <cstring>
19 #include <fstream>
20 #include "nweb_test_log.h"
21 #include "nweb_url_resource_request.h"
22 #include "nweb_url_resource_response.h"
23 #include "securec.h"
24 
25 namespace {
26     const std::string EXECUTE_JAVASCRIPT_CALLBACK_HTML = "execute_javaScript_test.html";
27 
28     constexpr uint8_t BITS_PER_PIXEL = 4; /* 4 bits per pixel */
29     constexpr uint8_t LAST_SECOND_CHANNEL_OF_PIXEL = 2;
30     constexpr uint8_t LAST_THIRD_CHANNEL_OF_PIXEL = 3;
31     struct BmpFileHeader {
32         unsigned int bf_size;             /* Size of file */
33         unsigned short bf_reserved_1 = 0; /* Reserved */
34         unsigned short bf_reserved_2 = 0; /* ... */
35         unsigned int bf_offbits = 0x36;   /* Offset to bitmap data */
36     };
37     /**** BMP file info structure ****/
38     struct BmpInfoHeader {
39         unsigned int bi_size;                 /* Size of info header */
40         int bi_width;                         /* Width of image */
41         int bi_height;                        /* Height of image */
42         unsigned short bi_planes = 1;         /* Number of color planes */
43         unsigned short bi_bit_count = 24;     /* 24 Number of bits per pixel */
44         unsigned int bi_compression = 0;      /* Type of compression to use */
45         unsigned int bi_size_image = 0;       /* Size of image data */
46         int bi_x_pixels_per_meter = 5000;     /* 5000 X pixels per meter */
47         int bi_y_pixels_per_meter = 5000;     /* 5000 Y pixels per meter */
48         unsigned int bi_colors_used = 0;      /* Number of colors used */
49         unsigned int bi_colors_important = 0; /* Number of important colors */
50     };
51 
RgbaToRgb(char * buf,int width,int height)52     void RgbaToRgb(char* buf, int width, int height)
53     {
54         char* p_rgba = buf;
55         char* p_rgb = buf;
56         uint64_t len = width * height * BITS_PER_PIXEL;
57         for (uint64_t i = 0; i < len; i++, p_rgba++) {
58             if (i % BITS_PER_PIXEL == LAST_THIRD_CHANNEL_OF_PIXEL) {
59                 // check alpha value, if 0, set related color to white
60                 if (buf[i] == 0) {
61                     *(p_rgb - LAST_THIRD_CHANNEL_OF_PIXEL) = 0xff;
62                     *(p_rgb - LAST_SECOND_CHANNEL_OF_PIXEL) = 0xff;
63                     *(p_rgb - 1) = 0xff;
64                 }
65                 continue;
66             }
67             *p_rgb++ = *p_rgba;
68             if (i % BITS_PER_PIXEL == LAST_SECOND_CHANNEL_OF_PIXEL) {
69                 char tmp = *(p_rgb - 1);
70                 *(p_rgb - 1) = *(p_rgb - LAST_THIRD_CHANNEL_OF_PIXEL);
71                 *(p_rgb - LAST_THIRD_CHANNEL_OF_PIXEL) = tmp;
72             }
73         }
74     }
75 
WriteToBmp(const std::string & filename,char * buf,int width,int height)76     void WriteToBmp(const std::string& filename, char* buf, int width, int height)
77     {
78         BmpFileHeader bmp_file_header;
79         BmpInfoHeader bmp_info_header;
80         // Magic number for file. It does not fit in the header structure
81         // due to alignment requirements, so put it outside
82         unsigned short bmp_file_type = 0x4d42;
83         constexpr int rgb_bits_per_pixel = LAST_THIRD_CHANNEL_OF_PIXEL;
84         int rgb_buf_size = width * height * rgb_bits_per_pixel;
85         bmp_file_header.bf_size =
86             sizeof(BmpFileHeader) + sizeof(BmpInfoHeader) + rgb_buf_size;
87         bmp_info_header.bi_size = sizeof(BmpInfoHeader);
88         bmp_info_header.bi_width = width;
89         bmp_info_header.bi_height = -height;
90         FILE* file = fopen(filename.c_str(), "wb");
91         if (!file) {
92             return;
93         }
94         // Write headers
95         (void)fwrite(&bmp_file_type, sizeof(bmp_file_type), 1, file);
96         (void)fwrite(&bmp_file_header, sizeof(bmp_file_header), 1, file);
97         (void)fwrite(&bmp_info_header, sizeof(bmp_info_header), 1, file);
98         (void)fwrite(buf, rgb_buf_size, 1, file);
99         (void)fclose(file);
100     }
101 
DumpToBmp(const std::string & filename,char * buf,int width,int height)102     void DumpToBmp(const std::string& filename, char* buf, int width, int height)
103     {
104         RgbaToRgb(buf, width, height);
105         WriteToBmp(filename, buf, width, height);
106     }
107 }
108 
109 namespace OHOS::NWeb {
110 class JavaScriptResultCb : public NWebValueCallback<std::string> {
OnReceiveValue(std::string result)111     void OnReceiveValue(std::string result) override
112     {
113         TESTLOG_I("JavaScript execute result = %{public}s", result.c_str());
114     }
115 };
OnProxyDied()116 void NWebHandlerImplTest::OnProxyDied()
117 {
118     TESTLOG_I("NWebHandlerImplTest::OnProxyDied called");
119 }
120 
SetNWeb(std::shared_ptr<NWeb> nweb)121 void NWebHandlerImplTest::SetNWeb(std::shared_ptr<NWeb> nweb)
122 {
123     nwebweak_ = nweb;
124 }
125 
OnPageLoadEnd(int httpStatusCode,const std::string & url)126 void NWebHandlerImplTest::OnPageLoadEnd(int httpStatusCode, const std::string& url)
127 {
128     TESTLOG_I("NWebHandlerImplTest::OnPageLoadend called, url=%{public}s", url.c_str());
129     auto nwebShared = nwebweak_.lock();
130     if (nwebShared == nullptr) {
131         TESTLOG_E("nwebShared is nullptr");
132         return;
133     }
134 
135     if (url.find(EXECUTE_JAVASCRIPT_CALLBACK_HTML) != std::string::npos) {
136         // invoke js function which is defined in html, test case 106
137         std::string ss = "javascript:ExecuteJavaScriptTest()";
138         std::shared_ptr<NWebValueCallback<std::string>> callback = std::make_shared<JavaScriptResultCb>();
139         nwebShared->ExecuteJavaScript(ss, callback);
140     }
141 }
142 
OnPageLoadBegin(const std::string & url)143 void NWebHandlerImplTest::OnPageLoadBegin(const std::string& url)
144 {
145     TESTLOG_I("NWebHandlerImplTest::OnPageLoadBegin called, url=%{public}s", url.c_str());
146 }
147 
OnPageLoadError(int errorCode,const std::string & description,const std::string & failingUrl)148 void NWebHandlerImplTest::OnPageLoadError(int errorCode,
149                                           const std::string& description,
150                                           const std::string& failingUrl)
151 {
152     TESTLOG_I("NWebHandlerImplTest::OnPageLoadError called, url=%{public}s", failingUrl.c_str());
153 }
154 
OnHandleInterceptUrlLoading(const std::string & url)155 bool NWebHandlerImplTest::OnHandleInterceptUrlLoading(const std::string& url)
156 {
157     TESTLOG_I("NWebHandlerImplTest::OnHandleInterceptUrlLoading called, url=%{public}s", url.c_str());
158     return false;
159 }
160 
OnRouterPush(const std::string & param)161 void NWebHandlerImplTest::OnRouterPush(const std::string& param)
162 {
163     TESTLOG_I("NWebHandlerImplTest::OnRouterPush called, url=%{public}s", param.c_str());
164 }
165 
OnMessage(const std::string & param)166 void NWebHandlerImplTest::OnMessage(const std::string& param)
167 {
168     TESTLOG_I("NWebHandlerImplTest::OnMessage called, message=%{public}s", param.c_str());
169 }
170 
VisitedUrlHistory()171 const std::vector<std::string> NWebHandlerImplTest::VisitedUrlHistory()
172 {
173     std::vector<std::string> vector_string;
174     vector_string.push_back("https://www.qq.com");
175     vector_string.push_back("file:///data/local/cef/cef_user_data/jingtai.html");
176     return vector_string;
177 }
178 
OnResourceLoadError(std::shared_ptr<NWebUrlResourceRequest> request,std::shared_ptr<NWebUrlResourceError> error)179 void NWebHandlerImplTest::OnResourceLoadError(std::shared_ptr<NWebUrlResourceRequest> request,
180                                               std::shared_ptr<NWebUrlResourceError> error)
181 {
182     TESTLOG_I("OnPageLoadError, url=%{public}s, errorCode=%{public}d, desc=%{public}s", request->Url().c_str(),
183         error->ErrorCode(), error->ErrorInfo().c_str());
184 }
OnHttpError(std::shared_ptr<NWebUrlResourceRequest> request,std::shared_ptr<NWebUrlResourceResponse> errorResponse)185 void NWebHandlerImplTest::OnHttpError(std::shared_ptr<NWebUrlResourceRequest> request,
186                                       std::shared_ptr<NWebUrlResourceResponse> errorResponse)
187 {
188     TESTLOG_I("OnHttpError, url=%{public}s, mimeType=%{public}s, is_main_frame=%{public}d," \
189         "has_user_gesture=%{public}d", request->Url().c_str(),
190         errorResponse->ResponseMimeType().c_str(), request->IsAboutMainFrame(), request->FromGesture());
191 }
OnPageIcon(const void * data,size_t width,size_t height,ImageColorType colorType,ImageAlphaType alphaType)192 void NWebHandlerImplTest::OnPageIcon(const void* data,
193                                      size_t width,
194                                      size_t height,
195                                      ImageColorType colorType,
196                                      ImageAlphaType alphaType)
197 {
198     TESTLOG_I("OnPageIcon, width=%{public}zu, height=%{public}zu", width, height);
199     size_t len = width * height * BITS_PER_PIXEL;
200     char* data_temp = new char[len];
201     if (memcpy_s(data_temp, len, data, len) == 0) {
202         DumpToBmp("/system/etc/webview/icon.bmp", data_temp, width, height);
203     }
204     delete[] data_temp;
205     data_temp = nullptr;
206 }
207 
OnDesktopIconUrl(const std::string & iconUrl,bool precomposed)208 void NWebHandlerImplTest::OnDesktopIconUrl(const std::string& iconUrl, bool precomposed)
209 {
210     TESTLOG_I("OnDesktopIconUrl, iconUrl=%{public}s, precomposed=%{public}d", iconUrl.c_str(), precomposed);
211 }
212 
OnFocus()213 void NWebHandlerImplTest::OnFocus()
214 {
215     TESTLOG_I("NWebHandlerImplTest::OnFocus");
216 }
217 
OnLoadingProgress(int newProgress)218 void NWebHandlerImplTest::OnLoadingProgress(int newProgress)
219 {
220     TESTLOG_I("NWebHandlerImplTest::OnLoadingProgress progress=%{public}d", newProgress);
221 }
222 
OnPageTitle(const std::string & title)223 void NWebHandlerImplTest::OnPageTitle(const std::string &title)
224 {
225     TESTLOG_I("NWebHandlerImplTest::OnTitle title=%{public}s", title.c_str());
226 }
227 
OnResource(const std::string & url)228 void NWebHandlerImplTest::OnResource(const std::string &url)
229 {
230     TESTLOG_I("NWebHandlerImplTest::OnResource url=%{public}s", url.c_str());
231 }
232 
OnGeolocationShow(const std::string & origin,std::shared_ptr<NWebGeolocationCallbackInterface> callback)233 void NWebHandlerImplTest::OnGeolocationShow(const std::string& origin,
234                                             std::shared_ptr<NWebGeolocationCallbackInterface> callback)
235 {
236     TESTLOG_I("NWebHandlerImplTest::OnGeolocationShow called, origin=%{public}s", origin.c_str());
237     callback->GeolocationCallbackInvoke(origin, true, true);
238 }
239 
OnGeolocationHide()240 void NWebHandlerImplTest::OnGeolocationHide()
241 {
242     TESTLOG_I("NWebHandlerImplTest::OnGeolocationHide called");
243 }
244 
OnPermissionRequest(std::shared_ptr<NWebAccessRequest> request)245 void NWebHandlerImplTest::OnPermissionRequest(std::shared_ptr<NWebAccessRequest> request)
246 {
247     int id = request->ResourceAcessId();
248     TESTLOG_I("NWebHandlerImplTest::OnPermissionRequest called, origin=%{public}s, resourceId=%{public}d",
249               request->Origin().c_str(), id);
250     request->Agree(id);
251 }
252 
OnPermissionRequestCanceled(std::shared_ptr<NWebAccessRequest> request)253 void NWebHandlerImplTest::OnPermissionRequestCanceled(std::shared_ptr<NWebAccessRequest> request)
254 {
255     int id = request->ResourceAcessId();
256     TESTLOG_I("NWebHandlerImplTest::OnPermissionRequestCanceled called, origin=%{public}s, resourceId=%{public}d",
257               request->Origin().c_str(), id);
258     request->Refuse();
259 }
260 } // namespace OHOS::NWeb
261