1 /*
2 * Copyright (c) 2021-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_hiappevent_config.h"
16
17 #include <map>
18 #include <string>
19
20 #include "hiappevent_config.h"
21 #include "napi_error.h"
22 #include "napi_util.h"
23
24 namespace OHOS {
25 namespace HiviewDFX {
26 namespace NapiHiAppEventConfig {
27 namespace {
28 const std::map<std::string, napi_valuetype> CONFIG_OPTION_MAP = {
29 { "disable", napi_boolean },
30 { "maxStorage", napi_string },
31 };
32 }
Configure(const napi_env env,const napi_value configObj,bool isThrow)33 bool Configure(const napi_env env, const napi_value configObj, bool isThrow)
34 {
35 if (!NapiUtil::IsObject(env, configObj)) {
36 NapiUtil::ThrowError(env, NapiError::ERR_PARAM, NapiUtil::CreateErrMsg("config", "ConfigOption"), isThrow);
37 return false;
38 }
39
40 std::vector<std::string> keys;
41 NapiUtil::GetPropertyNames(env, configObj, keys);
42 for (auto key : keys) {
43 if (CONFIG_OPTION_MAP.find(key) == CONFIG_OPTION_MAP.end()) {
44 continue;
45 }
46 napi_value value = NapiUtil::GetProperty(env, configObj, key);
47 if (CONFIG_OPTION_MAP.at(key) != NapiUtil::GetType(env, value)) {
48 std::string errMsg = NapiUtil::CreateErrMsg(key, CONFIG_OPTION_MAP.at(key));
49 NapiUtil::ThrowError(env, NapiError::ERR_PARAM, errMsg, isThrow);
50 return false;
51 }
52 if (!HiAppEventConfig::GetInstance().SetConfigurationItem(key, NapiUtil::ConvertToString(env, value))) {
53 std::string errMsg = "Invalid max storage quota value.";
54 NapiUtil::ThrowError(env, NapiError::ERR_INVALID_MAX_STORAGE, errMsg, isThrow);
55 return false;
56 }
57 }
58 return true;
59 }
60
IsDisable()61 bool IsDisable()
62 {
63 return HiAppEventConfig::GetInstance().GetDisable();
64 }
65
GetStorageDir()66 std::string GetStorageDir()
67 {
68 return HiAppEventConfig::GetInstance().GetStorageDir();
69 }
70 } // namespace NapiHiAppEventConfig
71 } // namespace HiviewDFX
72 } // namespace OHOS
73