1 /*
2 * Copyright (c) 2023 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_adapter_impl.h"
19 #include "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 template<typename... Args>
ForwardToHiSysEvent(const std::string & eventName,HiSysEventAdapter::EventType type,const std::tuple<Args...> & tp)31 int ForwardToHiSysEvent(const std::string& eventName, HiSysEventAdapter::EventType type, const std::tuple<Args...>& tp)
32 {
33 return std::apply(
34 [&](auto&&... args) {
35 return HiSysEventWrite(OHOS::HiviewDFX::HiSysEvent::Domain::WEBVIEW, eventName, EVENT_TYPES[type], args...);
36 },
37 tp);
38 }
39 } // namespace
40
GetInstance()41 HiSysEventAdapterImpl& HiSysEventAdapterImpl::GetInstance()
42 {
43 static HiSysEventAdapterImpl instance;
44 return instance;
45 }
46
Write(const std::string & eventName,EventType type,const std::tuple<const std::string,const int,const std::string,const int,const std::string,const int,const std::string,const int,const std::string,const float> & data)47 int HiSysEventAdapterImpl::Write(const std::string& eventName, EventType type,
48 const std::tuple<const std::string, const int, const std::string, const int, const std::string, const int,
49 const std::string, const int, const std::string, const float>& data)
50 {
51 return ForwardToHiSysEvent(eventName, type, data);
52 }
53
Write(const std::string & eventName,EventType type,const std::tuple<const std::string,const int,const std::string,const int,const std::string,const int> & data)54 int HiSysEventAdapterImpl::Write(const std::string& eventName, EventType type,
55 const std::tuple<const std::string, const int, const std::string, const int, const std::string, const int>& data)
56 {
57 return ForwardToHiSysEvent(eventName, type, data);
58 }
59
Write(const std::string & eventName,EventType type,const std::tuple<const std::string,const int,const std::string,const std::string,const std::string,const int,const std::string,const std::string> & data)60 int HiSysEventAdapterImpl::Write(const std::string& eventName, EventType type,
61 const std::tuple<const std::string, const int, const std::string, const std::string, const std::string, const int,
62 const std::string, const std::string>& data)
63 {
64 return ForwardToHiSysEvent(eventName, type, data);
65 }
66
67 using systemData = std::tuple<const std::string, const int, const std::string, const std::string, const std::string,
68 const std::string, const std::string, const std::string>;
69
Write(const std::string & eventName,EventType type,const std::tuple<const std::string,const int64_t,const std::string,const int,const std::string,const std::vector<uint16_t>,const std::string,const int> & data)70 int HiSysEventAdapterImpl::Write(const std::string& eventName, EventType type,
71 const std::tuple<const std::string, const int64_t, const std::string, const int,
72 const std::string, const std::vector<uint16_t>, const std::string, const int>& data)
73 {
74 auto appInfo = AbilityRuntime::ApplicationContext::GetInstance()->GetApplicationInfo();
75
76 AppExecFwk::ElementName elementName = AAFwk::AbilityManagerClient::GetInstance()->GetTopAbility();
77
78 systemData sysData = {
79 "VERSION_CODE", appInfo->versionCode,
80 "VERSION_NAME", appInfo->versionName.c_str(),
81 "BUNDLE_NAME", appInfo->bundleName.c_str(),
82 "ABILITY_NAME", elementName.GetAbilityName()
83 };
84
85 auto mergeData = std::tuple_cat(data, sysData);
86 return ForwardToHiSysEvent(eventName, type, mergeData);
87 }
88 } // namespace OHOS::NWeb
89