1 /*
2 * Copyright (c) 2024 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 #define LOG_TAG "JSClient"
16 #include "js_client.h"
17
18 #include <functional>
19 #include <string>
20 #include <vector>
21
22 #include "cloud_types.h"
23 #include "common_types.h"
24 #include "js_cloud_utils.h"
25 #include "js_error_utils.h"
26 #include "js_strategy_context.h"
27 #include "js_utils.h"
28 #include "logger.h"
29 #include "napi_queue.h"
30 #include "traits.h"
31
32 namespace OHOS {
33 namespace CloudData {
34 using namespace OHOS::AppDataMgrJsKit;
35 /*
36 * [JS API Prototype]
37 * function setCloudStrategy(strategy: StrategyType, param?: Array<commonType.ValueType>): Promise<void>;
38 */
SetCloudStrategy(napi_env env,napi_callback_info info)39 napi_value SetCloudStrategy(napi_env env, napi_callback_info info)
40 {
41 auto ctxt = std::make_shared<CloudStrategyContext>();
42 ctxt->GetCbInfo(env, info, [env, ctxt](size_t argc, napi_value *argv) {
43 // strategy 1 required parameter, param 1 Optional parameter
44 ASSERT_BUSINESS_ERR(ctxt, argc >= 1, Status::INVALID_ARGUMENT, "The number of parameters is incorrect.");
45 int32_t strategy = -1;
46 int status = JSUtils::Convert2ValueExt(env, argv[0], strategy);
47 ASSERT_BUSINESS_ERR(ctxt, status == JSUtils::OK && strategy >= 0 &&
48 strategy < static_cast<int32_t>(Strategy::STRATEGY_BUTT), Status::INVALID_ARGUMENT,
49 "The type of strategy must be StrategyType.");
50 ctxt->strategy = static_cast<Strategy>(strategy);
51 if (argc == 1 || JSUtils::IsNull(env, argv[1])) {
52 ctxt->SetDefault();
53 } else {
54 // 'argv[1]' represents a vector<CommonType::Value> param
55 status = JSUtils::Convert2Value(env, argv[1], ctxt->param);
56 ASSERT_BUSINESS_ERR(ctxt, status == JSUtils::OK, Status::INVALID_ARGUMENT,
57 "The type of param must be Array<commonType.ValueType>");
58 auto res = ctxt->CheckParam();
59 ASSERT_BUSINESS_ERR(ctxt, res.first == JSUtils::OK, Status::INVALID_ARGUMENT, res.second);
60 }
61 });
62 ASSERT_NULL(!ctxt->isThrowError, "SetCloudStrategy exit");
63 auto execute = [env, ctxt]() {
64 auto [status, proxy] = CloudManager::GetInstance().GetCloudService();
65 if (proxy == nullptr) {
66 if (status != CloudService::SERVER_UNAVAILABLE) {
67 status = CloudService::NOT_SUPPORT;
68 }
69 ctxt->status = (GenerateNapiError(status, ctxt->jsCode, ctxt->error) == Status::SUCCESS)
70 ? napi_ok: napi_generic_failure;
71 return;
72 }
73 LOG_DEBUG("SetCloudStrategy execute");
74
75 auto res = proxy->SetCloudStrategy(ctxt->strategy, ctxt->param);
76 ctxt->status =
77 (GenerateNapiError(res, ctxt->jsCode, ctxt->error) == Status::SUCCESS) ? napi_ok : napi_generic_failure;
78 };
79 return NapiQueue::AsyncWork(env, ctxt, std::string(__FUNCTION__), execute);
80 }
81
InitClient(napi_env env,napi_value exports)82 napi_value InitClient(napi_env env, napi_value exports)
83 {
84 napi_property_descriptor properties[] = { DECLARE_NAPI_FUNCTION("setCloudStrategy", SetCloudStrategy) };
85 NAPI_CALL(env, napi_define_properties(env, exports, sizeof(properties) / sizeof(*properties), properties));
86 return exports;
87 }
88 } // namespace CloudData
89 } // namespace OHOS
90