• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "downgrade_progress_napi.h"
17 
18 #include <cstddef>
19 #include <cstdint>
20 #include <sys/types.h>
21 
22 #include "async_work.h"
23 #include "cloud_sync_manager.h"
24 #include "dfs_error.h"
25 #include "utils_log.h"
26 #include "uv.h"
27 
28 namespace OHOS::FileManagement::CloudSync {
29 using namespace FileManagement::LibN;
30 using namespace std;
Constructor(napi_env env,napi_callback_info info)31 napi_value DowngradeProgressNapi::Constructor(napi_env env, napi_callback_info info)
32 {
33     NFuncArg funcArg(env, info);
34     funcArg.InitArgs(NARG_CNT::ZERO);
35 
36     auto progressEntity = make_unique<DowngradeProgressEntity>();
37     if (progressEntity == nullptr) {
38         LOGE("Failed to request heap memory.");
39         return nullptr;
40     }
41     if (!NClass::SetEntityFor<DowngradeProgressEntity>(env, funcArg.GetThisVar(), move(progressEntity))) {
42         LOGE("Failed to set progressEntity.");
43         return nullptr;
44     }
45     return funcArg.GetThisVar();
46 }
47 
GetState(napi_env env,napi_callback_info info)48 napi_value DowngradeProgressNapi::GetState(napi_env env, napi_callback_info info)
49 {
50     NFuncArg funcArg(env, info);
51     funcArg.InitArgs(NARG_CNT::ZERO);
52 
53     int32_t state = static_cast<int32_t>(DowngradeProgress::State::STOPPED);
54     auto progressEntity = NClass::GetEntityOf<DowngradeProgressEntity>(env, funcArg.GetThisVar());
55     if (progressEntity == nullptr || progressEntity->progress == nullptr) {
56         LOGE("Failed to get DowngradeProgressEntity.");
57     } else {
58         state = progressEntity->progress->state;
59     }
60 
61     return NVal::CreateInt32(env, state).val_;
62 }
63 
GetSuccessfulCount(napi_env env,napi_callback_info info)64 napi_value DowngradeProgressNapi::GetSuccessfulCount(napi_env env, napi_callback_info info)
65 {
66     NFuncArg funcArg(env, info);
67     funcArg.InitArgs(NARG_CNT::ZERO);
68 
69     int32_t succNum = -1;
70     auto progressEntity = NClass::GetEntityOf<DowngradeProgressEntity>(env, funcArg.GetThisVar());
71     if (progressEntity == nullptr || progressEntity->progress == nullptr) {
72         LOGE("Failed to get DowngradeProgressEntity.");
73     } else {
74         succNum = progressEntity->progress->successfulCount;
75     }
76 
77     return NVal::CreateInt32(env, succNum).val_;
78 }
79 
GetFailedCount(napi_env env,napi_callback_info info)80 napi_value DowngradeProgressNapi::GetFailedCount(napi_env env, napi_callback_info info)
81 {
82     NFuncArg funcArg(env, info);
83     funcArg.InitArgs(NARG_CNT::ZERO);
84 
85     int32_t failedNum = -1;
86     auto progressEntity = NClass::GetEntityOf<DowngradeProgressEntity>(env, funcArg.GetThisVar());
87     if (progressEntity == nullptr || progressEntity->progress == nullptr) {
88         LOGE("Failed to get DowngradeProgressEntity.");
89     } else {
90         failedNum = progressEntity->progress->failedCount;
91     }
92 
93     return NVal::CreateInt32(env, failedNum).val_;
94 }
95 
GetTotalCount(napi_env env,napi_callback_info info)96 napi_value DowngradeProgressNapi::GetTotalCount(napi_env env, napi_callback_info info)
97 {
98     NFuncArg funcArg(env, info);
99     funcArg.InitArgs(NARG_CNT::ZERO);
100 
101     int32_t totalNum = -1;
102     auto progressEntity = NClass::GetEntityOf<DowngradeProgressEntity>(env, funcArg.GetThisVar());
103     if (progressEntity == nullptr || progressEntity->progress == nullptr) {
104         LOGE("Failed to get DowngradeProgressEntity.");
105     } else {
106         totalNum = progressEntity->progress->totalCount;
107     }
108 
109     return NVal::CreateInt32(env, totalNum).val_;
110 }
111 
GetDownloadedSize(napi_env env,napi_callback_info info)112 napi_value DowngradeProgressNapi::GetDownloadedSize(napi_env env, napi_callback_info info)
113 {
114     NFuncArg funcArg(env, info);
115     funcArg.InitArgs(NARG_CNT::ZERO);
116 
117     int64_t downloadSize = INT64_MAX;
118     auto progressEntity = NClass::GetEntityOf<DowngradeProgressEntity>(env, funcArg.GetThisVar());
119     if (progressEntity == nullptr || progressEntity->progress == nullptr) {
120         LOGE("Failed to get DowngradeProgressEntity.");
121     } else {
122         downloadSize = progressEntity->progress->downloadedSize;
123     }
124 
125     return NVal::CreateInt64(env, downloadSize).val_;
126 }
127 
GetTotalSize(napi_env env,napi_callback_info info)128 napi_value DowngradeProgressNapi::GetTotalSize(napi_env env, napi_callback_info info)
129 {
130     NFuncArg funcArg(env, info);
131     funcArg.InitArgs(NARG_CNT::ZERO);
132 
133     int64_t totalSize = INT64_MAX;
134     auto progressEntity = NClass::GetEntityOf<DowngradeProgressEntity>(env, funcArg.GetThisVar());
135     if (progressEntity == nullptr || progressEntity->progress == nullptr) {
136         LOGE("Failed to get DowngradeProgressEntity.");
137     } else {
138         totalSize = progressEntity->progress->totalSize;
139     }
140 
141     return NVal::CreateInt64(env, totalSize).val_;
142 }
143 
GetStopReason(napi_env env,napi_callback_info info)144 napi_value DowngradeProgressNapi::GetStopReason(napi_env env, napi_callback_info info)
145 {
146     NFuncArg funcArg(env, info);
147     funcArg.InitArgs(NARG_CNT::ZERO);
148 
149     int32_t stopReason = DowngradeProgress::StopReason::OTHER_REASON;
150     auto progressEntity = NClass::GetEntityOf<DowngradeProgressEntity>(env, funcArg.GetThisVar());
151     if (progressEntity == nullptr || progressEntity->progress == nullptr) {
152         LOGE("Failed to get DowngradeProgressEntity.");
153     } else {
154         stopReason = progressEntity->progress->stopReason;
155     }
156 
157     return NVal::CreateInt32(env, stopReason).val_;
158 }
159 
GetClassName()160 std::string DowngradeProgressNapi::GetClassName()
161 {
162     return className_;
163 }
164 
Export()165 bool DowngradeProgressNapi::Export()
166 {
167     vector<napi_property_descriptor> props = {
168         NVal::DeclareNapiGetter("state", GetState),
169         NVal::DeclareNapiGetter("successfulCount", GetSuccessfulCount),
170         NVal::DeclareNapiGetter("failedCount", GetFailedCount),
171         NVal::DeclareNapiGetter("totalCount", GetTotalCount),
172         NVal::DeclareNapiGetter("downloadedSize", GetDownloadedSize),
173         NVal::DeclareNapiGetter("totalSize", GetTotalSize),
174         NVal::DeclareNapiGetter("stopReason", GetStopReason),
175     };
176 
177     string className = GetClassName();
178     bool succ = false;
179     napi_value classValue = nullptr;
180     tie(succ, classValue) =
181         NClass::DefineClass(exports_.env_, className, DowngradeProgressNapi::Constructor, move(props));
182     if (!succ) {
183         LOGE("Define class exceptions");
184         NError(Convert2JsErrNum(E_SERVICE_INNER_ERROR)).ThrowErr(exports_.env_);
185         return false;
186     }
187     succ = NClass::SaveClass(exports_.env_, className, classValue);
188     if (!succ) {
189         LOGE("Save class exceptions");
190         NError(Convert2JsErrNum(E_SERVICE_INNER_ERROR)).ThrowErr(exports_.env_);
191         return false;
192     }
193 
194     return exports_.AddProp(className, classValue);
195 }
196 } // namespace OHOS::FileManagement::CloudSync