1 /*
2 * Copyright (c) 2021 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_preferences_helper.h"
16
17 #include <linux/limits.h>
18
19 #include <string>
20
21 #include "js_ability.h"
22 #include "js_logger.h"
23 #include "js_utils.h"
24 #include "napi_async_proxy.h"
25 #include "napi_preferences.h"
26 #include "preferences_errno.h"
27 #include "securec.h"
28
29 using namespace OHOS::NativePreferences;
30 using namespace OHOS::AppDataMgrJsKit;
31
32 namespace OHOS {
33 namespace PreferencesJsKit {
34 struct HelperAysncContext : NapiAsyncProxy<HelperAysncContext>::AysncContext {
35 std::string path;
36 std::shared_ptr<Context> context;
37 };
38
ParseContext(const napi_env & env,const napi_value & object,HelperAysncContext * asyncContext)39 void ParseContext(const napi_env &env, const napi_value &object, HelperAysncContext *asyncContext)
40 {
41 auto context = JSAbility::GetContext(env, object);
42 NAPI_ASSERT_RETURN_VOID(env, context != nullptr, "ParseContext get context failed.");
43 asyncContext->context = context;
44 }
45
ParseName(const napi_env & env,const napi_value & value,HelperAysncContext * asyncContext)46 void ParseName(const napi_env &env, const napi_value &value, HelperAysncContext *asyncContext)
47 {
48 LOG_DEBUG("ParseName start");
49 NAPI_ASSERT_RETURN_VOID(env, asyncContext->context != nullptr, "ParseName context is null.");
50 std::string name = JSUtils::Convert2String(env, value);
51 NAPI_ASSERT_RETURN_VOID(env, !name.empty(), "Get preferences name empty.");
52
53 size_t pos = name.find_first_of('/');
54 NAPI_ASSERT_RETURN_VOID(env, pos == std::string::npos, "A name without path should be input.");
55 std::string databaseDir = asyncContext->context->GetDatabaseDir();
56 asyncContext->path = databaseDir.append("/").append(name);
57 }
58
GetPreferences(napi_env env,napi_callback_info info)59 napi_value GetPreferences(napi_env env, napi_callback_info info)
60 {
61 LOG_DEBUG("GetPreferences start");
62 NapiAsyncProxy<HelperAysncContext> proxy;
63 proxy.Init(env, info);
64 std::vector<NapiAsyncProxy<HelperAysncContext>::InputParser> parsers;
65 parsers.push_back(ParseContext);
66 parsers.push_back(ParseName);
67 proxy.ParseInputs(parsers);
68
69 return proxy.DoAsyncWork(
70 "GetPreferences",
71 [](HelperAysncContext *asyncContext) {
72 int errCode = OK;
73 OHOS::NativePreferences::PreferencesHelper::GetPreferences(asyncContext->path, errCode);
74 LOG_DEBUG("GetPreferences return %{public}d", errCode);
75 return errCode;
76 },
77 [](HelperAysncContext *asyncContext, napi_value &output) {
78 napi_value path = nullptr;
79 napi_create_string_utf8(asyncContext->env, asyncContext->path.c_str(), NAPI_AUTO_LENGTH, &path);
80 auto ret = PreferencesProxy::NewInstance(asyncContext->env, path, &output);
81 return (ret == napi_ok) ? OK : ERR;
82 });
83 }
84
DeletePreferences(napi_env env,napi_callback_info info)85 napi_value DeletePreferences(napi_env env, napi_callback_info info)
86 {
87 NapiAsyncProxy<HelperAysncContext> proxy;
88 proxy.Init(env, info);
89 std::vector<NapiAsyncProxy<HelperAysncContext>::InputParser> parsers;
90 parsers.push_back(ParseContext);
91 parsers.push_back(ParseName);
92 proxy.ParseInputs(parsers);
93
94 return proxy.DoAsyncWork(
95 "DeletePreferences",
96 [](HelperAysncContext *asyncContext) {
97 int errCode = PreferencesHelper::DeletePreferences(asyncContext->path);
98 LOG_DEBUG("DeletePreferences return %{public}d", errCode);
99 return errCode;
100 },
101 [](HelperAysncContext *asyncContext, napi_value &output) {
102 napi_get_undefined(asyncContext->env, &output);
103 return OK;
104 });
105 }
106
RemovePreferencesFromCache(napi_env env,napi_callback_info info)107 napi_value RemovePreferencesFromCache(napi_env env, napi_callback_info info)
108 {
109 NapiAsyncProxy<HelperAysncContext> proxy;
110 proxy.Init(env, info);
111 std::vector<NapiAsyncProxy<HelperAysncContext>::InputParser> parsers;
112 parsers.push_back(ParseContext);
113 parsers.push_back(ParseName);
114 proxy.ParseInputs(parsers);
115
116 return proxy.DoAsyncWork(
117 "RemovePreferencesFromCache",
118 [](HelperAysncContext *asyncContext) {
119 int errCode = PreferencesHelper::RemovePreferencesFromCache(asyncContext->path);
120 LOG_DEBUG("RemovePreferencesFromCache return %{public}d", errCode);
121 return errCode;
122 },
123 [](HelperAysncContext *asyncContext, napi_value &output) {
124 napi_get_undefined(asyncContext->env, &output);
125 return OK;
126 });
127 }
128
InitPreferencesHelper(napi_env env,napi_value exports)129 napi_value InitPreferencesHelper(napi_env env, napi_value exports)
130 {
131 napi_property_descriptor properties[] = {
132 DECLARE_NAPI_FUNCTION("getPreferences", GetPreferences),
133 DECLARE_NAPI_FUNCTION("deletePreferences", DeletePreferences),
134 DECLARE_NAPI_FUNCTION("removePreferencesFromCache", RemovePreferencesFromCache),
135 };
136 NAPI_CALL(env, napi_define_properties(env, exports, sizeof(properties) / sizeof(*properties), properties));
137 return exports;
138 }
139 } // namespace PreferencesJsKit
140 } // namespace OHOS
141