1 /*
2 * Copyright (c) 2025 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 "environment_ani.h"
17
18 #include "environment_core.h"
19 #include "error_handler.h"
20 #include "filemgmt_libhilog.h"
21 #include "type_converter.h"
22
23 namespace OHOS {
24 namespace FileManagement {
25 namespace ModuleFileIO {
26 namespace ANI {
27
28 using namespace std;
29 using namespace OHOS::FileManagement::ModuleFileIO;
30 using namespace OHOS::FileManagement::ModuleEnvironment;
31
GetStorageDataDirSync(ani_env * env,ani_class clazz)32 ani_string EnvironmentAni::GetStorageDataDirSync(ani_env *env, [[maybe_unused]] ani_class clazz)
33 {
34 auto ret = DoGetStorageDataDir();
35 if (!ret.IsSuccess()) {
36 HILOGE("Get storage data dir failed");
37 const auto &err = ret.GetError();
38 ErrorHandler::Throw(env, err);
39 return nullptr;
40 }
41
42 string res = ret.GetData().value();
43 auto [succ, result] = TypeConverter::ToAniString(env, res);
44 if (!succ) {
45 HILOGE("Create ani_string error");
46 ErrorHandler::Throw(env, UNKNOWN_ERR);
47 return nullptr;
48 }
49 return result;
50 }
51
GetUserDataDirSync(ani_env * env,ani_class clazz)52 ani_string EnvironmentAni::GetUserDataDirSync(ani_env *env, [[maybe_unused]] ani_class clazz)
53 {
54 auto ret = DoGetUserDataDir();
55 if (!ret.IsSuccess()) {
56 HILOGE("Get user data dir failed");
57 const auto &err = ret.GetError();
58 ErrorHandler::Throw(env, err);
59 return nullptr;
60 }
61
62 string res = ret.GetData().value();
63 auto [succ, result] = TypeConverter::ToAniString(env, res);
64 if (!succ) {
65 HILOGE("Create ani_string error");
66 ErrorHandler::Throw(env, UNKNOWN_ERR);
67 return nullptr;
68 }
69 return result;
70 }
71
GetUserDownloadDirSync(ani_env * env,ani_class clazz)72 ani_string EnvironmentAni::GetUserDownloadDirSync(ani_env *env, [[maybe_unused]] ani_class clazz)
73 {
74 auto ret = DoGetUserDownloadDir();
75 if (!ret.IsSuccess()) {
76 HILOGE("Get user download dir failed");
77 const auto &err = ret.GetError();
78 ErrorHandler::Throw(env, err);
79 return nullptr;
80 }
81
82 string res = ret.GetData().value();
83 auto [succ, result] = TypeConverter::ToAniString(env, res);
84 if (!succ) {
85 HILOGE("Create ani_string error");
86 ErrorHandler::Throw(env, UNKNOWN_ERR);
87 return nullptr;
88 }
89 return result;
90 }
91
GetUserDesktopDirSync(ani_env * env,ani_class clazz)92 ani_string EnvironmentAni::GetUserDesktopDirSync(ani_env *env, [[maybe_unused]] ani_class clazz)
93 {
94 auto ret = DoGetUserDesktopDir();
95 if (!ret.IsSuccess()) {
96 HILOGE("Get user desktop dir failed");
97 const auto &err = ret.GetError();
98 ErrorHandler::Throw(env, err);
99 return nullptr;
100 }
101
102 string res = ret.GetData().value();
103 auto [succ, result] = TypeConverter::ToAniString(env, res);
104 if (!succ) {
105 HILOGE("Create ani_string error");
106 ErrorHandler::Throw(env, UNKNOWN_ERR);
107 return nullptr;
108 }
109 return result;
110 }
111
GetUserDocumentDirSync(ani_env * env,ani_class clazz)112 ani_string EnvironmentAni::GetUserDocumentDirSync(ani_env *env, [[maybe_unused]] ani_class clazz)
113 {
114 auto ret = DoGetUserDocumentDir();
115 if (!ret.IsSuccess()) {
116 HILOGE("Get user document dir failed");
117 const auto &err = ret.GetError();
118 ErrorHandler::Throw(env, err);
119 return nullptr;
120 }
121
122 string res = ret.GetData().value();
123 auto [succ, result] = TypeConverter::ToAniString(env, res);
124 if (!succ) {
125 HILOGE("Create ani_string error");
126 ErrorHandler::Throw(env, UNKNOWN_ERR);
127 return nullptr;
128 }
129 return result;
130 }
131
GetExternalStorageDirSync(ani_env * env,ani_class clazz)132 ani_string EnvironmentAni::GetExternalStorageDirSync(ani_env *env, [[maybe_unused]] ani_class clazz)
133 {
134 auto ret = DoGetExternalStorageDir();
135 if (!ret.IsSuccess()) {
136 HILOGE("Get external storage dir failed");
137 const auto &err = ret.GetError();
138 ErrorHandler::Throw(env, err);
139 return nullptr;
140 }
141
142 string res = ret.GetData().value();
143 auto [succ, result] = TypeConverter::ToAniString(env, res);
144 if (!succ) {
145 HILOGE("Create ani_string error");
146 ErrorHandler::Throw(env, UNKNOWN_ERR);
147 return nullptr;
148 }
149 return result;
150 }
151
GetUserHomeDirSync(ani_env * env,ani_class clazz)152 ani_string EnvironmentAni::GetUserHomeDirSync(ani_env *env, [[maybe_unused]] ani_class clazz)
153 {
154 auto ret = DoGetUserHomeDir();
155 if (!ret.IsSuccess()) {
156 HILOGE("Get user home dir failed");
157 const auto &err = ret.GetError();
158 ErrorHandler::Throw(env, err);
159 return nullptr;
160 }
161
162 string res = ret.GetData().value();
163 auto [succ, result] = TypeConverter::ToAniString(env, res);
164 if (!succ) {
165 HILOGE("Create ani_string error");
166 ErrorHandler::Throw(env, UNKNOWN_ERR);
167 return nullptr;
168 }
169 return result;
170 }
171
172 } // namespace ANI
173 } // namespace ModuleFileIO
174 } // namespace FileManagement
175 } // namespace OHOS