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 #include "app_event.h"
16 
17 #include "ffrt.h"
18 #include "hiappevent_base.h"
19 #include "hiappevent_verify.h"
20 #include "hiappevent_write.h"
21 
22 namespace OHOS {
23 namespace HiviewDFX {
24 namespace HiAppEvent {
Event(const std::string & domain,const std::string & name,EventType type)25 Event::Event(const std::string& domain, const std::string& name, EventType type)
26 {
27     eventPack_ = std::make_shared<AppEventPack>(domain, name, type);
28 }
29 
AddParam(const std::string & key,bool value)30 void Event::AddParam(const std::string& key, bool value)
31 {
32     eventPack_->AddParam(key, value);
33 }
34 
AddParam(const std::string & key,int32_t value)35 void Event::AddParam(const std::string& key, int32_t value)
36 {
37     eventPack_->AddParam(key, value);
38 }
39 
AddParam(const std::string & key,int64_t value)40 void Event::AddParam(const std::string& key, int64_t value)
41 {
42     eventPack_->AddParam(key, value);
43 }
44 
AddParam(const std::string & key,double value)45 void Event::AddParam(const std::string& key, double value)
46 {
47     eventPack_->AddParam(key, value);
48 }
49 
AddParam(const std::string & key,const std::string & value)50 void Event::AddParam(const std::string& key, const std::string& value)
51 {
52     eventPack_->AddParam(key, value);
53 }
54 
AddParam(const std::string & key,const std::vector<bool> & value)55 void Event::AddParam(const std::string& key, const std::vector<bool>& value)
56 {
57     eventPack_->AddParam(key, value);
58 }
59 
AddParam(const std::string & key,const std::vector<int32_t> & value)60 void Event::AddParam(const std::string& key, const std::vector<int32_t>& value)
61 {
62     eventPack_->AddParam(key, value);
63 }
64 
AddParam(const std::string & key,const std::vector<int64_t> & value)65 void Event::AddParam(const std::string& key, const std::vector<int64_t>& value)
66 {
67     eventPack_->AddParam(key, value);
68 }
69 
AddParam(const std::string & key,const std::vector<double> & value)70 void Event::AddParam(const std::string& key, const std::vector<double>& value)
71 {
72     eventPack_->AddParam(key, value);
73 }
74 
AddParam(const std::string & key,const std::vector<std::string> & value)75 void Event::AddParam(const std::string& key, const std::vector<std::string>& value)
76 {
77     eventPack_->AddParam(key, value);
78 }
79 
Write(const Event & event)80 int Write(const Event& event)
81 {
82     if (!IsApp()) {
83         return ErrorCode::ERROR_NOT_APP;
84     }
85     int ret = VerifyAppEvent(event.eventPack_);
86     if (ret >= 0) {
87         auto appEventPack = event.eventPack_;
88         ffrt::submit([appEventPack]() {
89             WriteEvent(appEventPack);
90             }, {}, {}, ffrt::task_attr().name("app_event"));
91     }
92     return ret;
93 }
94 } // namespace HiAppEvent
95 } // namespace HiviewDFX
96 } // namespace OHOS
97