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 std::shared_ptr<NWebEngineInitArgsImpl> GetInitArgs(void)
78 {
79     std::shared_ptr<NWebEngineInitArgsImpl> initArgs = std::make_shared<NWebEngineInitArgsImpl>();
80     initArgs->SetDumpPath(GetArgValue(ARG_DUMP));
81     initArgs->SetIsFrameInfoDump(HasArg(ARG_FRAME_INFO) ? true : false);
82     initArgs->SetIsMultiRendererProcess(HasArg(ARG_MULTI_RENDER_PROCESS) ? true : false);
83     initArgs->SetArgsToAdd(GetWebEngineArgs(ARG_ADD_WEB_ENGINE_ARG));
84     initArgs->SetArgsToDelete(GetWebEngineArgs(ARG_DELETE_WEB_ENGINE_ARG));
85     return initArgs;
86 }
87 
GetNwebForTest()88 std::shared_ptr<NWeb> GetNwebForTest()
89 {
90     if (!g_surface) {
91         Rosen::RSSurfaceNodeConfig config;
92         config.SurfaceNodeName = "webTestSurfaceName";
93         auto surfaceNode = Rosen::RSSurfaceNode::Create(config, false);
94         if (surfaceNode == nullptr) {
95             return nullptr;
96         }
97         g_surface = surfaceNode->GetSurface();
98         if (g_surface== nullptr) {
99             return nullptr;
100         }
101     }
102     return NWebAdapterHelper::Instance().CreateNWeb(g_surface, GetInitArgs());
103 }
104 } // namespace OHOS::NWeb
105