1 /*
2 * Copyright (c) 2021-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 "status_receiver_impl.h"
17
18 #include "app_log_wrapper.h"
19
20 namespace OHOS {
21 namespace AppExecFwk {
22 namespace {
23 const int32_t MINIMUM_WAITTING_TIME = 180; // 3 mins
24 } // namespace
25
StatusReceiverImpl(int32_t waittingTime)26 StatusReceiverImpl::StatusReceiverImpl(int32_t waittingTime) : waittingTime_(waittingTime)
27 {
28 APP_LOGI("create status receiver instance");
29 }
30
StatusReceiverImpl()31 StatusReceiverImpl::StatusReceiverImpl() : waittingTime_(MINIMUM_WAITTING_TIME)
32 {
33 APP_LOGI("create status receiver instance");
34 }
35
~StatusReceiverImpl()36 StatusReceiverImpl::~StatusReceiverImpl()
37 {
38 APP_LOGI("destroy status receiver instance");
39 }
40
OnFinished(const int32_t resultCode,const std::string & resultMsg)41 void StatusReceiverImpl::OnFinished(const int32_t resultCode, const std::string &resultMsg)
42 {
43 APP_LOGI("on finished result is %{public}d, %{public}s", resultCode, resultMsg.c_str());
44 std::lock_guard<std::mutex> lock(setValueMutex_);
45 if (!isSetValue) {
46 isSetValue = true;
47 resultCodeSignal_.set_value(resultCode);
48 resultMsgSignal_.set_value(resultMsg);
49 } else {
50 APP_LOGW("resultCodeSignal_ is set");
51 }
52 }
53
OnStatusNotify(const int progress)54 void StatusReceiverImpl::OnStatusNotify(const int progress)
55 {
56 APP_LOGI("on OnStatusNotify is %{public}d", progress);
57 }
58
GetResultCode() const59 int32_t StatusReceiverImpl::GetResultCode() const
60 {
61 auto future = resultCodeSignal_.get_future();
62 if (future.wait_for(std::chrono::seconds(waittingTime_)) == std::future_status::ready) {
63 int32_t resultCode = future.get();
64 return resultCode;
65 }
66 return ERR_OPERATION_TIME_OUT;
67 }
68
GetResultMsg() const69 std::string StatusReceiverImpl::GetResultMsg() const
70 {
71 auto future = resultMsgSignal_.get_future();
72 if (future.wait_for(std::chrono::seconds(waittingTime_)) == std::future_status::ready) {
73 std::string resultMsg = future.get();
74 return resultMsg;
75 }
76 return "";
77 }
78 } // namespace AppExecFwk
79 } // namespace OHOS