1 /*
2  * Copyright (c) 2022 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 "quick_fix_status_callback_host_impl.h"
17 
18 #include "app_log_wrapper.h"
19 
20 namespace OHOS {
21 namespace AppExecFwk {
22 namespace {
23 const int32_t WAITTING_TIME = 600; // Same with MAXIMUM_WAITTING_TIME, which means bundle install maximum waiting time.
24 } // namespace
25 
QuickFixStatusCallbackHostlmpl()26 QuickFixStatusCallbackHostlmpl::QuickFixStatusCallbackHostlmpl()
27 {
28     APP_LOGI("create status callback instance");
29 }
30 
~QuickFixStatusCallbackHostlmpl()31 QuickFixStatusCallbackHostlmpl::~QuickFixStatusCallbackHostlmpl()
32 {
33     APP_LOGI("destroy status callback instance");
34 }
35 
OnPatchDeployed(const std::shared_ptr<QuickFixResult> & result)36 void QuickFixStatusCallbackHostlmpl::OnPatchDeployed(const std::shared_ptr<QuickFixResult> &result)
37 {
38     APP_LOGD("on Patch deployed result is %{public}s", result->ToString().c_str());
39     quickFixPromise_.set_value(result);
40 }
41 
OnPatchSwitched(const std::shared_ptr<QuickFixResult> & result)42 void QuickFixStatusCallbackHostlmpl::OnPatchSwitched(const std::shared_ptr<QuickFixResult> &result)
43 {
44     APP_LOGD("on Patch switched result is %{public}s", result->ToString().c_str());
45     quickFixPromise_.set_value(result);
46 }
47 
OnPatchDeleted(const std::shared_ptr<QuickFixResult> & result)48 void QuickFixStatusCallbackHostlmpl::OnPatchDeleted(const std::shared_ptr<QuickFixResult> &result)
49 {
50     APP_LOGD("on Patch deleted result is %{public}s", result->ToString().c_str());
51     quickFixPromise_.set_value(result);
52 }
53 
GetResultCode(std::shared_ptr<QuickFixResult> & quickFixRes) const54 ErrCode QuickFixStatusCallbackHostlmpl::GetResultCode(std::shared_ptr<QuickFixResult> &quickFixRes) const
55 {
56     auto future = quickFixPromise_.get_future();
57     if (future.wait_for(std::chrono::seconds(WAITTING_TIME)) == std::future_status::ready) {
58         quickFixRes = future.get();
59         return quickFixRes->GetResCode();
60     }
61     return ERR_APPEXECFWK_OPERATION_TIME_OUT;
62 }
63 }  // namespace AppExecFwk
64 }  // namespace OHOS