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