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
16 #include "ext_backup_context_js.h"
17
18 #include "filemgmt_libhilog.h"
19 #include "js_extension_context.h"
20 #include "js_runtime.h"
21
22 namespace OHOS::FileManagement::Backup {
CreateExtBackupJsContext(napi_env env,std::shared_ptr<ExtBackupContext> context)23 napi_value CreateExtBackupJsContext(napi_env env, std::shared_ptr<ExtBackupContext> context)
24 {
25 if (context == nullptr) {
26 HILOGE("Failed to CreateExtBackupJsContext, context is nullptr.");
27 return nullptr;
28 }
29 std::shared_ptr<OHOS::AppExecFwk::AbilityInfo> abilityInfo = context->GetAbilityInfo();
30 napi_value object = CreateJsExtensionContext(env, context, abilityInfo);
31 if (object == nullptr) {
32 HILOGE("Failed to CreateJsServiceExtensionContext, context is nullptr.");
33 return nullptr;
34 }
35 std::unique_ptr<ExtBackupContextJS> jsContext = std::make_unique<ExtBackupContextJS>(context);
36 napi_wrap(env, object, jsContext.release(), ExtBackupContextJS::Finalizer, nullptr, nullptr);
37 AbilityRuntime::BindNativeProperty(env, object, "backupDir", ExtBackupContextJS::GetBackupDir);
38 return object;
39 }
40
GetBackupDir(napi_env env,napi_callback_info info)41 napi_value ExtBackupContextJS::GetBackupDir(napi_env env, napi_callback_info info)
42 {
43 using namespace AbilityRuntime;
44 HILOGI("GetBackupDir Called.");
45 GET_NAPI_INFO_AND_CALL(env, info, ExtBackupContextJS, OnGetBackupDir);
46 }
47
Finalizer(napi_env env,void * data,void * hint)48 void ExtBackupContextJS::Finalizer(napi_env env, void* data, void* hint)
49 {
50 HILOGI("Finalizer Called.");
51 std::unique_ptr<ExtBackupContextJS>(static_cast<ExtBackupContextJS*>(data));
52 }
53
54 napi_value ExtBackupContextJS::OnGetBackupDir(napi_env env, [[maybe_unused]]AbilityRuntime::NapiCallbackInfo& info)
55 {
56 napi_value result = nullptr;
57 auto context = context_.lock();
58 if (!context) {
59 HILOGE("context is nullptr.");
60 return nullptr;
61 }
62 std::string backupDir = context->GetBackupDir();
63 napi_create_string_utf8(env, backupDir.c_str(), backupDir.size(), &result);
64 return result;
65 }
66 } // namespace OHOS::FileManagement::Backup