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 
21 namespace OHOS::NWeb {
22 namespace {
23 const HiviewDFX::HiSysEvent::EventType EVENT_TYPES[] = {
24     OHOS::HiviewDFX::HiSysEvent::EventType::FAULT,
25     OHOS::HiviewDFX::HiSysEvent::EventType::STATISTIC,
26     OHOS::HiviewDFX::HiSysEvent::EventType::SECURITY,
27     OHOS::HiviewDFX::HiSysEvent::EventType::BEHAVIOR,
28 };
29 
30 enum EventType {
31     FAULT = 0,
32     STATISTIC,
33     SECURITY,
34     BEHAVIOR,
35 };
36 }
37 
38 constexpr char INSTANCE_INITIALIZE_TIME[] = "INSTANCE_INITIALIZE_TIME";
39 constexpr char INSTANCE_ID[] = "INSTANCE_ID";
40 constexpr char USED_TIME[] = "USED_TIME";
41 
42 static std::string g_currentBundleName = "";
43 template<typename... Args>
ForwardToHiSysEvent(const std::string & eventName,EventType type,const std::tuple<Args...> & tp)44 static int ForwardToHiSysEvent(const std::string& eventName, EventType type, const std::tuple<Args...>& tp)
45 {
46     if (g_currentBundleName.empty()) {
47         auto appInfo = AbilityRuntime::ApplicationContext::GetInstance()->GetApplicationInfo();
48         if (appInfo != nullptr) {
49             g_currentBundleName = appInfo->bundleName.c_str();
50         }
51     }
52     std::tuple<const std::string, const std::string> sysData("BUNDLE_NAME", g_currentBundleName.c_str());
53     auto mergeData = std::tuple_cat(sysData, tp);
54 
55     return std::apply(
56         [&](auto&&... args) {
57             return HiSysEventWrite(OHOS::HiviewDFX::HiSysEvent::Domain::WEBVIEW, eventName, EVENT_TYPES[type], args...);
58         },
59         mergeData);
60 }
61 
ReportCreateWebInstanceTime(uint32_t nwebId,int64_t usedTime)62 int EventReport::ReportCreateWebInstanceTime(uint32_t nwebId, int64_t usedTime)
63 {
64     auto data = std::make_tuple(INSTANCE_ID, nwebId, USED_TIME, usedTime);
65     return ForwardToHiSysEvent(INSTANCE_INITIALIZE_TIME, STATISTIC, data);
66 }
67 } // namespace OHOS::NWeb