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 std::string ARG_URL = "--url";
28 const std::string ARG_DUMP = "--dump-path";
29 const std::string ARG_FRAME_INFO = "--frame-info";
30 const std::string ARG_ADD_WEB_ENGINE_ARG = "--add-args";
31 const std::string ARG_DELETE_WEB_ENGINE_ARG = "--delete-args";
32 const std::string ARG_MULTI_RENDER_PROCESS = "--multi-renderer-process";
33 const std::string ARG_NWEB_TEST_MOCK_BUNDLEPATH = "--bundle-installation-dir";
34 const std::string MOCK_INSTALLATION_DIR = "/data/app/el1/bundle/public/com.ohos.nweb";
35 const std::string ARG_WIDTH = "--width";
36 const std::string ARG_HEIGHT = "--height";
37 std::unordered_map<std::string, std::string> g_argsMap;
38 sptr<Surface> g_surface = nullptr;
39 } // namespace
40
HasArg(const std::string & arg)41 static bool HasArg(const std::string& arg)
42 {
43 return (!g_argsMap.empty()) && (g_argsMap.find(arg) != g_argsMap.end());
44 }
45
GetArgValue(const std::string & arg)46 static std::string GetArgValue(const std::string& arg)
47 {
48 if (!HasArg(arg)) {
49 return "";
50 }
51 return g_argsMap.at(arg);
52 }
53
GetWebEngineArgs(const std::string & arg)54 static std::list<std::string> GetWebEngineArgs(const std::string& arg)
55 {
56 std::string webEngineArgValue = GetArgValue(arg);
57 std::list<std::string> webEngineArgList;
58 if (webEngineArgValue.empty()) {
59 return webEngineArgList;
60 }
61 uint32_t start = 0;
62 uint32_t pos = 0;
63 while (pos < webEngineArgValue.size()) {
64 if (webEngineArgValue[pos] == ',') {
65 webEngineArgList.emplace_back(webEngineArgValue.substr(start, pos - start));
66 pos++;
67 start = pos;
68 } else {
69 pos++;
70 }
71 }
72 webEngineArgList.emplace_back(webEngineArgValue.substr(start, pos - start));
73 webEngineArgList.emplace_back(ARG_NWEB_TEST_MOCK_BUNDLEPATH + "=" + MOCK_INSTALLATION_DIR);
74 return webEngineArgList;
75 }
76
GetInitArgs(void)77 NWebInitArgs GetInitArgs(void)
78 {
79 NWebInitArgs initArgs = {
80 .dump_path = GetArgValue(ARG_DUMP),
81 .frame_info_dump = HasArg(ARG_FRAME_INFO) ? true : false,
82 .web_engine_args_to_add = GetWebEngineArgs(ARG_ADD_WEB_ENGINE_ARG),
83 .web_engine_args_to_delete = GetWebEngineArgs(ARG_DELETE_WEB_ENGINE_ARG),
84 .multi_renderer_process = HasArg(ARG_MULTI_RENDER_PROCESS) ? true : false,
85 };
86 return initArgs;
87 }
88
GetNwebForTest()89 std::shared_ptr<NWeb> GetNwebForTest()
90 {
91 if (!g_surface) {
92 Rosen::RSSurfaceNodeConfig config;
93 config.SurfaceNodeName = "webTestSurfaceName";
94 auto surfaceNode = Rosen::RSSurfaceNode::Create(config, false);
95 if (surfaceNode == nullptr) {
96 return nullptr;
97 }
98 g_surface = surfaceNode->GetSurface();
99 if (g_surface== nullptr) {
100 return nullptr;
101 }
102 }
103 return NWebAdapterHelper::Instance().CreateNWeb(g_surface, GetInitArgs());
104 }
105 } // namespace OHOS::NWeb
106