1 /*
2 * Copyright (c) 2024 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 "ability_manager_client.h"
17 #include "application_context.h"
18 #include "hisysevent.h"
19 #include "nweb_hisysevent.h"
20 #include "arkweb_utils.h"
21 #include "parameters.h"
22
23 namespace OHOS::NWeb {
24 namespace {
25 const HiviewDFX::HiSysEvent::EventType EVENT_TYPES[] = {
26 OHOS::HiviewDFX::HiSysEvent::EventType::FAULT,
27 OHOS::HiviewDFX::HiSysEvent::EventType::STATISTIC,
28 OHOS::HiviewDFX::HiSysEvent::EventType::SECURITY,
29 OHOS::HiviewDFX::HiSysEvent::EventType::BEHAVIOR,
30 };
31
32 enum EventType {
33 FAULT = 0,
34 STATISTIC,
35 SECURITY,
36 BEHAVIOR,
37 };
38 }
39
40 constexpr char INSTANCE_INITIALIZE_TIME[] = "INSTANCE_INITIALIZE_TIME";
41 constexpr char INSTANCE_ID[] = "INSTANCE_ID";
42 constexpr char USED_TIME[] = "USED_TIME";
43
44 static std::string g_currentBundleName = "";
45 static std::string g_apiCompatibleVersion = "";
46 static std::string g_webEngineType = "";
47 static std::string g_defaultWebEngineType = "";
48 template<typename... Args>
ForwardToHiSysEvent(const std::string & eventName,EventType type,const std::tuple<Args...> & tp)49 static int ForwardToHiSysEvent(const std::string& eventName, EventType type, const std::tuple<Args...>& tp)
50 {
51 if (g_currentBundleName.empty() || g_apiCompatibleVersion.empty()) {
52 auto appInfo = AbilityRuntime::ApplicationContext::GetInstance()->GetApplicationInfo();
53 if (appInfo != nullptr) {
54 g_currentBundleName = appInfo->bundleName;
55 g_apiCompatibleVersion = std::to_string(appInfo->apiCompatibleVersion);
56 }
57 }
58
59 if (g_webEngineType.empty()) {
60 g_webEngineType = std::to_string(static_cast<int>(OHOS::ArkWeb::getActiveWebEngineType()));
61 }
62
63 if (g_defaultWebEngineType.empty()) {
64 g_defaultWebEngineType = std::to_string(OHOS::system::GetIntParameter("web.engine.default",
65 static_cast<int>(OHOS::ArkWeb::ArkWebEngineType::EVERGREEN)));
66 }
67
68 auto sysData = std::make_tuple("BUNDLE_NAME", g_currentBundleName,
69 "API_COMPATIBLE_VERSION", g_apiCompatibleVersion,
70 "WEB_ENGINE_TYPE", g_webEngineType,
71 "DEFAULT_WEB_ENGINE_TYPE", g_defaultWebEngineType);
72 auto mergeData = std::tuple_cat(sysData, tp);
73
74 return std::apply(
75 [&](auto&&... args) {
76 return HiSysEventWrite(OHOS::HiviewDFX::HiSysEvent::Domain::WEBVIEW, eventName, EVENT_TYPES[type], args...);
77 },
78 mergeData);
79 }
80
ReportCreateWebInstanceTime(uint32_t nwebId,int64_t usedTime)81 int EventReport::ReportCreateWebInstanceTime(uint32_t nwebId, int64_t usedTime)
82 {
83 auto data = std::make_tuple(INSTANCE_ID, nwebId, USED_TIME, usedTime);
84 return ForwardToHiSysEvent(INSTANCE_INITIALIZE_TIME, STATISTIC, data);
85 }
86 } // namespace OHOS::NWeb