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 resultMsgSignal_.set_value(resultCode);
48 } else {
49 APP_LOGW("resultMsgSignal_ is set");
50 }
51 }
52
OnStatusNotify(const int progress)53 void StatusReceiverImpl::OnStatusNotify(const int progress)
54 {
55 APP_LOGI("on OnStatusNotify is %{public}d", progress);
56 }
57
GetResultCode() const58 int32_t StatusReceiverImpl::GetResultCode() const
59 {
60 auto future = resultMsgSignal_.get_future();
61 if (future.wait_for(std::chrono::seconds(waittingTime_)) == std::future_status::ready) {
62 int32_t resultCode = future.get();
63 return resultCode;
64 }
65 return ERR_OPERATION_TIME_OUT;
66 }
67 } // namespace AppExecFwk
68 } // namespace OHOS