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
18 #include <cstring>
19 #include <securec.h>
20 #include <ui/rs_surface_node.h>
21 #include <unordered_map>
22
23 #include "nweb_adapter_helper.h"
24
25 using namespace OHOS;
26 namespace OHOS::NWeb {
27 namespace {
28 const std::string ARG_URL = "--url";
29 const std::string ARG_DUMP = "--dump-path";
30 const std::string ARG_FRAME_INFO = "--frame-info";
31 const std::string ARG_ADD_WEB_ENGINE_ARG = "--add-args";
32 const std::string ARG_DELETE_WEB_ENGINE_ARG = "--delete-args";
33 const std::string ARG_MULTI_RENDER_PROCESS = "--multi-renderer-process";
34 const std::string ARG_NWEB_TEST_MOCK_BUNDLEPATH = "--bundle-installation-dir";
35 const std::string MOCK_INSTALLATION_DIR = "/data/app/el1/bundle/public/com.ohos.nweb";
36 const std::string ARG_WIDTH = "--width";
37 const std::string ARG_HEIGHT = "--height";
38 std::unordered_map<std::string, std::string> g_argsMap;
39 sptr<Surface> g_surface = nullptr;
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
GetWebEngineArgs(const std::string & arg)55 static std::list<std::string> GetWebEngineArgs(const std::string& arg)
56 {
57 std::string webEngineArgValue = GetArgValue(arg);
58 std::list<std::string> webEngineArgList;
59 if (webEngineArgValue.empty()) {
60 return webEngineArgList;
61 }
62 uint32_t start = 0;
63 uint32_t pos = 0;
64 while (pos < webEngineArgValue.size()) {
65 if (webEngineArgValue[pos] == ',') {
66 webEngineArgList.emplace_back(webEngineArgValue.substr(start, pos - start));
67 pos++;
68 start = pos;
69 } else {
70 pos++;
71 }
72 }
73 webEngineArgList.emplace_back(webEngineArgValue.substr(start, pos - start));
74 webEngineArgList.emplace_back(ARG_NWEB_TEST_MOCK_BUNDLEPATH + "=" + MOCK_INSTALLATION_DIR);
75 return webEngineArgList;
76 }
77
GetInitArgs(void)78 std::shared_ptr<NWebEngineInitArgsImpl> GetInitArgs(void)
79 {
80 std::shared_ptr<NWebEngineInitArgsImpl> initArgs = std::make_shared<NWebEngineInitArgsImpl>();
81 initArgs->SetDumpPath(GetArgValue(ARG_DUMP));
82 initArgs->SetIsFrameInfoDump(HasArg(ARG_FRAME_INFO) ? true : false);
83 initArgs->SetIsMultiRendererProcess(HasArg(ARG_MULTI_RENDER_PROCESS) ? true : false);
84 initArgs->SetArgsToAdd(GetWebEngineArgs(ARG_ADD_WEB_ENGINE_ARG));
85 initArgs->SetArgsToDelete(GetWebEngineArgs(ARG_DELETE_WEB_ENGINE_ARG));
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