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 #include "napi_app_event_holder.h"
16
17 #include "app_event_cache.h"
18 #include "hiappevent_base.h"
19 #include "hilog/log.h"
20 #include "napi_error.h"
21 #include "napi_util.h"
22
23 namespace OHOS {
24 namespace HiviewDFX {
25 namespace {
26 const HiLogLabel LABEL = { LOG_CORE, HIAPPEVENT_DOMAIN, "Napi_HiAppEvent_Holder" };
27 constexpr size_t PARAM_NUM = 1;
28 const std::string HOLDER_CLASS_NAME = "AppEventPackageHolder";
29 }
30 thread_local napi_ref NapiAppEventHolder::constructor_ = nullptr;
31
NapiAppEventHolder(std::string name)32 NapiAppEventHolder::NapiAppEventHolder(std::string name) : name_(name)
33 {
34 takeSize_ = 512 * 1024; // 512 * 1024: 512KB
35 packageId_ = 0; // id is incremented from 0
36 }
37
NapiConstructor(napi_env env,napi_callback_info info)38 napi_value NapiAppEventHolder::NapiConstructor(napi_env env, napi_callback_info info)
39 {
40 size_t paramNum = PARAM_NUM;
41 napi_value params[PARAM_NUM] = { 0 };
42 napi_value thisVar = nullptr;
43 NAPI_CALL(env, napi_get_cb_info(env, info, ¶mNum, params, &thisVar, nullptr));
44
45 if (paramNum < PARAM_NUM) {
46 HiLog::Error(LABEL, "hodler failed to construct: invalid param num");
47 return thisVar;
48 }
49 auto holder = new(std::nothrow) NapiAppEventHolder(NapiUtil::GetString(env, params[0]));
50 napi_wrap(
51 env, thisVar, holder,
52 [](napi_env env, void* data, void* hint) {
53 NapiAppEventHolder* holder = (NapiAppEventHolder*)data;
54 delete holder;
55 },
56 nullptr, nullptr);
57 return thisVar;
58 }
59
NapiExport(napi_env env,napi_value exports)60 napi_value NapiAppEventHolder::NapiExport(napi_env env, napi_value exports)
61 {
62 napi_property_descriptor properties[] = {
63 DECLARE_NAPI_FUNCTION("setSize", NapiSetSize),
64 DECLARE_NAPI_FUNCTION("takeNext", NapiTakeNext)
65 };
66 napi_value holderClass = nullptr;
67 napi_define_class(env, HOLDER_CLASS_NAME.c_str(), HOLDER_CLASS_NAME.size(), NapiConstructor, nullptr,
68 sizeof(properties) / sizeof(properties[0]), properties, &holderClass);
69 NapiUtil::SetNamedProperty(env, exports, HOLDER_CLASS_NAME, holderClass);
70 constructor_ = NapiUtil::CreateReference(env, holderClass);
71 return exports;
72 }
73
NapiSetSize(napi_env env,napi_callback_info info)74 napi_value NapiAppEventHolder::NapiSetSize(napi_env env, napi_callback_info info)
75 {
76 size_t paramNum = PARAM_NUM;
77 napi_value params[PARAM_NUM] = { 0 };
78 napi_value thisVar = nullptr;
79 NAPI_CALL(env, napi_get_cb_info(env, info, ¶mNum, params, &thisVar, nullptr));
80 if (paramNum < PARAM_NUM) {
81 NapiUtil::ThrowError(env, NapiError::ERR_PARAM, NapiUtil::CreateErrMsg("size"));
82 return nullptr;
83 }
84 if (!NapiUtil::IsNumber(env, params[0])) {
85 NapiUtil::ThrowError(env, NapiError::ERR_PARAM, NapiUtil::CreateErrMsg("size", "number"));
86 return nullptr;
87 }
88 if (int num = NapiUtil::GetInt32(env, params[0]); num >= 0) {
89 NapiAppEventHolder* holder = nullptr;
90 napi_unwrap(env, thisVar, (void**)&holder);
91 holder->SetSize(num);
92 } else {
93 NapiUtil::ThrowError(env, NapiError::ERR_INVALID_SIZE, "Invalid size value.");
94 }
95 return NapiUtil::CreateUndefined(env);
96 }
97
NapiTakeNext(napi_env env,napi_callback_info info)98 napi_value NapiAppEventHolder::NapiTakeNext(napi_env env, napi_callback_info info)
99 {
100 napi_value thisVar = nullptr;
101 NAPI_CALL(env, napi_get_cb_info(env, info, nullptr, nullptr, &thisVar, nullptr));
102 NapiAppEventHolder* holder = nullptr;
103 napi_unwrap(env, thisVar, (void**)&holder);
104 auto package = holder->TakeNext();
105 if (package == nullptr) {
106 return NapiUtil::CreateNull(env);
107 }
108 napi_value packageObj = NapiUtil::CreateObject(env);
109 NapiUtil::SetNamedProperty(env, packageObj, "packageId", NapiUtil::CreateInt32(env, package->packageId));
110 NapiUtil::SetNamedProperty(env, packageObj, "row", NapiUtil::CreateInt32(env, package->row));
111 NapiUtil::SetNamedProperty(env, packageObj, "size", NapiUtil::CreateInt32(env, package->size));
112 NapiUtil::SetNamedProperty(env, packageObj, "data", NapiUtil::CreateStrings(env, package->events));
113 return packageObj;
114 }
115
SetSize(int size)116 void NapiAppEventHolder::SetSize(int size)
117 {
118 HiLog::Info(LABEL, "hodler=%{public}s set size=%{public}d", name_.c_str(), size);
119 takeSize_ = size;
120 }
121
TakeNext()122 std::shared_ptr<AppEventPackage> NapiAppEventHolder::TakeNext()
123 {
124 auto block = AppEventCache::GetInstance()->GetBlock(name_);
125 if (block == nullptr) {
126 HiLog::Error(LABEL, "hodler=%{public}s failed to get block", name_.c_str());
127 return nullptr;
128 }
129 std::vector<std::string> events;
130 int result = block->Take(takeSize_, events);
131 if (result <= 0) {
132 HiLog::Info(LABEL, "hodler=%{public}s end to take data", name_.c_str());
133 return nullptr;
134 }
135 auto package = std::make_shared<AppEventPackage>();
136 package->packageId = packageId_++;
137 package->row = static_cast<int>(events.size());
138 package->size = result;
139 package->events = events;
140 return package;
141 }
142 } // namespace HiviewDFX
143 } // namespace OHOS
144