1 /*
2 * Copyright (c) 2023 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 "cloud_sync_manager_napi.h"
17 #include "cloud_sync_manager_n_exporter.h"
18 #include "downgrade_download_napi.h"
19 #include "downgrade_progress_napi.h"
20 #include "utils_log.h"
21 namespace OHOS::FileManagement::CloudSync {
22 using namespace FileManagement::LibN;
23 /***********************************************
24 * Module export and register
25 ***********************************************/
InitDownloadState(napi_env env,napi_value exports)26 void InitDownloadState(napi_env env, napi_value exports)
27 {
28 const char *propertyName = "DownloadState";
29 napi_value obj = nullptr;
30 napi_create_object(env, &obj);
31 napi_property_descriptor desc[] = {
32 DECLARE_NAPI_STATIC_PROPERTY("RUNNING", NVal::CreateInt32(env, (int32_t)DowngradeProgress::RUNNING).val_),
33 DECLARE_NAPI_STATIC_PROPERTY("COMPLETED", NVal::CreateInt32(env, (int32_t)DowngradeProgress::COMPLETED).val_),
34 DECLARE_NAPI_STATIC_PROPERTY("STOPPED", NVal::CreateInt32(env, (int32_t)DowngradeProgress::STOPPED).val_),
35 };
36 napi_define_properties(env, obj, sizeof(desc) / sizeof(desc[0]), desc);
37 napi_set_named_property(env, exports, propertyName, obj);
38 }
39
InitDownloadStopReason(napi_env env,napi_value exports)40 void InitDownloadStopReason(napi_env env, napi_value exports)
41 {
42 char propertyName[] = "DownloadStopReason";
43 napi_value obj = nullptr;
44 napi_create_object(env, &obj);
45 napi_property_descriptor desc[] = {
46 DECLARE_NAPI_STATIC_PROPERTY("NO_STOP", NVal::CreateInt32(env, (int32_t)DowngradeProgress::NO_STOP).val_),
47 DECLARE_NAPI_STATIC_PROPERTY("NETWORK_UNAVAILABLE",
48 NVal::CreateInt32(env, (int32_t)DowngradeProgress::NETWORK_UNAVAILABLE).val_),
49 DECLARE_NAPI_STATIC_PROPERTY("LOCAL_STORAGE_FULL",
50 NVal::CreateInt32(env, (int32_t)DowngradeProgress::LOCAL_STORAGE_FULL).val_),
51 DECLARE_NAPI_STATIC_PROPERTY("TEMPERATURE_LIMIT",
52 NVal::CreateInt32(env, (int32_t)DowngradeProgress::TEMPERATURE_LIMIT).val_),
53 DECLARE_NAPI_STATIC_PROPERTY("USER_STOPPED",
54 NVal::CreateInt32(env, (int32_t)DowngradeProgress::USER_STOPPED).val_),
55 DECLARE_NAPI_STATIC_PROPERTY("APP_UNLOAD", NVal::CreateInt32(env, (int32_t)DowngradeProgress::APP_UNLOAD).val_),
56 DECLARE_NAPI_STATIC_PROPERTY("OTHER_REASON",
57 NVal::CreateInt32(env, (int32_t)DowngradeProgress::OTHER_REASON).val_),
58 };
59 napi_define_properties(env, obj, sizeof(desc) / sizeof(desc[0]), desc);
60 napi_set_named_property(env, exports, propertyName, obj);
61 }
62
Init(napi_env env,napi_value exports)63 static napi_value Init(napi_env env, napi_value exports)
64 {
65 CloudSyncManagerExport(env, exports);
66 InitENumACtions(env, exports);
67 InitDownloadState(env, exports);
68 InitDownloadStopReason(env, exports);
69 std::vector<std::unique_ptr<NExporter>> products;
70 products.emplace_back(std::make_unique<DowngradeDownloadNapi>(env, exports));
71 products.emplace_back(std::make_unique<DowngradeProgressNapi>(env, exports));
72 for (auto &&product : products) {
73 if (!product->Export()) {
74 LOGE("INNER BUG. Failed to export class %{public}s for module cloudSyncDownload ",
75 product->GetClassName().c_str());
76 return nullptr;
77 } else {
78 LOGI("Class %{public}s for module cloudSyncDownload has been exported", product->GetClassName().c_str());
79 }
80 }
81
82 return exports;
83 }
84
CloudSyncManagerExport(napi_env env,napi_value exports)85 void CloudSyncManagerExport(napi_env env, napi_value exports)
86 {
87 static napi_property_descriptor desc[] = {
88 DECLARE_NAPI_FUNCTION("changeAppCloudSwitch", ChangeAppCloudSwitch),
89 DECLARE_NAPI_FUNCTION("clean", Clean),
90 DECLARE_NAPI_FUNCTION("notifyDataChange", NotifyDataChange),
91 DECLARE_NAPI_FUNCTION("enableCloud", EnableCloud),
92 DECLARE_NAPI_FUNCTION("disableCloud", DisableCloud),
93 };
94 napi_define_properties(env, exports, sizeof(desc) / sizeof(desc[0]), desc);
95 }
96
InitENumACtions(napi_env env,napi_value exports)97 void InitENumACtions(napi_env env, napi_value exports)
98 {
99 char propertyName[] = "Action";
100 napi_value actionObj = nullptr;
101 napi_create_object(env, &actionObj);
102 napi_property_descriptor desc[] = {
103 DECLARE_NAPI_STATIC_PROPERTY("RETAIN_DATA", NVal::CreateInt32(env, (int32_t)Action::RETAIN_DATA).val_),
104 DECLARE_NAPI_STATIC_PROPERTY("CLEAR_DATA", NVal::CreateInt32(env, (int32_t)Action::CLEAR_DATA).val_),
105 };
106 napi_define_properties(env, actionObj, sizeof(desc) / sizeof(desc[0]), desc);
107 napi_set_named_property(env, exports, propertyName, actionObj);
108 }
109
110 static napi_module _module = {.nm_version = 1,
111 .nm_flags = 0,
112 .nm_filename = nullptr,
113 .nm_register_func = Init,
114 .nm_modname = "file.cloudSyncManager",
115 .nm_priv = ((void *)0),
116 .reserved = {0}};
117
RegisterModule(void)118 extern "C" __attribute__((constructor)) void RegisterModule(void)
119 {
120 napi_module_register(&_module);
121 }
122 } // namespace OHOS::FileManagement::CloudSync
123