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 "cj_app_recovery.h"
17
18 #include "app_recovery.h"
19 #include "cj_ability_context.h"
20 #include "ffi_remote_data.h"
21 #include "hilog_tag_wrapper.h"
22 #include "want.h"
23 #include "want_params.h"
24
25 using namespace OHOS::AbilityRuntime;
26 using namespace OHOS::AppExecFwk;
27
FFIAppRecoveryEnable(int32_t restartFlag,int32_t saveOccasionFlag,int32_t saveModeFlag)28 CJ_EXPORT void FFIAppRecoveryEnable(int32_t restartFlag, int32_t saveOccasionFlag, int32_t saveModeFlag)
29 {
30 AppRecovery::GetInstance().EnableAppRecovery(static_cast<uint16_t>(restartFlag),
31 static_cast<uint16_t>(saveOccasionFlag), static_cast<uint16_t>(saveModeFlag));
32 }
33
FFIAppRecoveryRestartApp()34 CJ_EXPORT void FFIAppRecoveryRestartApp()
35 {
36 AppRecovery::GetInstance().ScheduleRecoverApp(StateReason::DEVELOPER_REQUEST);
37 }
38
FFIAppRecoverySaveAppState(int64_t ctxId)39 CJ_EXPORT bool FFIAppRecoverySaveAppState(int64_t ctxId)
40 {
41 uintptr_t ability = 0;
42 if (ctxId != 0) {
43 auto cjContext = OHOS::FFI::FFIData::GetData<CJAbilityContext>(ctxId);
44 if (cjContext == nullptr) {
45 TAG_LOGE(AAFwkTag::APPKIT, "CJAbilityContext id is invalid.");
46 return false;
47 }
48 ability = reinterpret_cast<uintptr_t>(cjContext->GetAbilityContext().get());
49 }
50 return AppRecovery::GetInstance().ScheduleSaveAppState(StateReason::DEVELOPER_REQUEST, ability);
51 }
52
FFIAppRecoverySetRestartWant(WantHandle want)53 CJ_EXPORT void FFIAppRecoverySetRestartWant(WantHandle want)
54 {
55 auto actualWant = reinterpret_cast<OHOS::AAFwk::Want*>(want);
56 if (!actualWant) {
57 return;
58 }
59 std::shared_ptr<OHOS::AAFwk::Want> paramWant = std::make_shared<OHOS::AAFwk::Want>();
60 *paramWant = *actualWant;
61 AppRecovery::GetInstance().SetRestartWant(paramWant);
62 }
63