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_create_window.h"
17 #include <cstring>
18 #include <securec.h>
19 #include <ui/rs_surface_node.h>
20 #include <unordered_map>
21
22 #include "nweb_adapter_helper.h"
23
24 using namespace OHOS;
25 namespace OHOS::NWeb {
26 namespace {
27 const int DEFAULT_WIDTH = 2560;
28 const int DEFAULT_HEIGHT = 1396;
29 const std::string ARG_URL = "--url";
30 const std::string ARG_DUMP = "--dump-path";
31 const std::string ARG_FRAME_INFO = "--frame-info";
32 const std::string ARG_ADD_WEB_ENGINE_ARG = "--add-args";
33 const std::string ARG_DELETE_WEB_ENGINE_ARG = "--delete-args";
34 const std::string ARG_MULTI_RENDER_PROCESS = "--multi-renderer-process";
35 const std::string ARG_NWEB_TEST_MOCK_BUNDLEPATH = "--bundle-installation-dir";
36 const std::string MOCK_INSTALLATION_DIR = "/data/app/el1/bundle/public/com.ohos.nweb";
37 const std::string ARG_WIDTH = "--width";
38 const std::string ARG_HEIGHT = "--height";
39 std::unordered_map<std::string, std::string> g_argsMap;
40 } // namespace
41
HasArg(const std::string & arg)42 static bool HasArg(const std::string& arg)
43 {
44 return (!g_argsMap.empty()) && (g_argsMap.find(arg) != g_argsMap.end());
45 }
46
GetArgValue(const std::string & arg)47 static std::string GetArgValue(const std::string& arg)
48 {
49 if (!HasArg(arg)) {
50 return "";
51 }
52 return g_argsMap.at(arg);
53 }
54
GetNumFromArgs(const std::string & arg)55 static uint32_t GetNumFromArgs(const std::string& arg)
56 {
57 if (!HasArg(arg)) {
58 return 0;
59 }
60 return std::stoi(GetArgValue(arg));
61 }
62
GetWebEngineArgs(const std::string & arg)63 static std::list<std::string> GetWebEngineArgs(const std::string& arg)
64 {
65 std::string webEngineArgValue = GetArgValue(arg);
66 std::list<std::string> webEngineArgList;
67 if (webEngineArgValue.empty()) {
68 return webEngineArgList;
69 }
70 uint32_t start = 0;
71 uint32_t pos = 0;
72 while (pos < webEngineArgValue.size()) {
73 if (webEngineArgValue[pos] == ',') {
74 webEngineArgList.emplace_back(webEngineArgValue.substr(start, pos - start));
75 pos++;
76 start = pos;
77 } else {
78 pos++;
79 }
80 }
81 webEngineArgList.emplace_back(webEngineArgValue.substr(start, pos - start));
82 webEngineArgList.emplace_back(ARG_NWEB_TEST_MOCK_BUNDLEPATH + "=" + MOCK_INSTALLATION_DIR);
83 return webEngineArgList;
84 }
85
GetInitArgs(void)86 NWebInitArgs GetInitArgs(void)
87 {
88 NWebInitArgs initArgs = {
89 .dump_path = GetArgValue(ARG_DUMP),
90 .frame_info_dump = HasArg(ARG_FRAME_INFO) ? true : false,
91 .web_engine_args_to_add = GetWebEngineArgs(ARG_ADD_WEB_ENGINE_ARG),
92 .web_engine_args_to_delete = GetWebEngineArgs(ARG_DELETE_WEB_ENGINE_ARG),
93 .multi_renderer_process = HasArg(ARG_MULTI_RENDER_PROCESS) ? true : false,
94 };
95 return initArgs;
96 }
97
CreateWindow(void)98 sptr<OHOS::Rosen::Window> CreateWindow(void)
99 {
100 sptr<OHOS::Rosen::WindowOption> option = new OHOS::Rosen::WindowOption();
101 int width = HasArg(ARG_WIDTH) ? GetNumFromArgs(ARG_WIDTH) : DEFAULT_WIDTH;
102 int height = HasArg(ARG_HEIGHT) ? GetNumFromArgs(ARG_HEIGHT) : DEFAULT_HEIGHT;
103 OHOS::Rosen::Rect windowRect = { 0, 0, width, height };
104 option->SetWindowRect(windowRect);
105 option->SetWindowType(Rosen::WindowType::WINDOW_TYPE_APP_LAUNCHING);
106 option->SetWindowMode(Rosen::WindowMode::WINDOW_MODE_FLOATING);
107 auto window = OHOS::Rosen::Window::Create("nweb_test_window", option);
108 return window;
109 }
110 }