• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "ability_post_event_timeout.h"
17 
18 #include "ability_handler.h"
19 #include "hilog_wrapper.h"
20 
21 namespace OHOS {
22 namespace AppExecFwk {
23 const int64_t AbilityPostEventTimeout::defalutDelayTime = 5000;
24 
25 std::atomic<uint32_t> AbilityPostEventTimeout::allocationId_ = 0;
26 
AbilityPostEventTimeout(std::string str,std::shared_ptr<AbilityHandler> & eventHandler)27 AbilityPostEventTimeout::AbilityPostEventTimeout(std::string str, std::shared_ptr<AbilityHandler> &eventHandler)
28 {
29     uint32_t taskId = allocationId_++;
30     std::string strId = std::to_string(taskId);
31     task_ = str + strId;
32     taskExec_ = false;
33     handler_ = eventHandler;
34 }
~AbilityPostEventTimeout()35 AbilityPostEventTimeout::~AbilityPostEventTimeout()
36 {
37     handler_.reset();
38 }
39 
TimingBegin(int64_t delaytime)40 void AbilityPostEventTimeout::TimingBegin(int64_t delaytime)
41 {
42     HILOG_INFO("AbilityPostEventTimeout::TimingBegin() call %{public}s", task_.c_str());
43     if (handler_ == nullptr) {
44         HILOG_ERROR("AbilityPostEventTimeout::TimingBegin %{public}s handler_ is nullptr", task_.c_str());
45         return;
46     }
47 
48     auto task = [timeOutTask = shared_from_this()]() { timeOutTask->TimeOutProc(); };
49     handler_->PostTask(task, task_, delaytime);
50 }
TimeEnd()51 void AbilityPostEventTimeout::TimeEnd()
52 {
53     if (handler_ == nullptr) {
54         HILOG_ERROR("AbilityPostEventTimeout::TimeEnd %{public}s handler_ is nullptr", task_.c_str());
55         return;
56     }
57 
58     std::lock_guard<std::mutex> lck(mtx_);
59     if (!taskExec_) {
60         taskExec_ = true;
61         handler_->RemoveTask(task_);
62     }
63 }
64 
TimeOutProc()65 void AbilityPostEventTimeout::TimeOutProc()
66 {
67     HILOG_INFO("AbilityPostEventTimeout::TimeOutProc() call %{public}s", task_.c_str());
68     if (handler_ == nullptr) {
69         HILOG_ERROR("AbilityPostEventTimeout::TimeEnd %{public}s handler_ is nullptr", task_.c_str());
70         return;
71     }
72 
73     std::lock_guard<std::mutex> lck(mtx_);
74     if (!taskExec_) {
75         taskExec_ = true;
76         HILOG_WARN("TimeOutProc %{public}s Event TimeOut", task_.c_str());
77         handler_->RemoveTask(task_);
78     } else {
79         HILOG_WARN("AbilityPostEventTimeout::TimeOutProc Exec Failed, The Event is %{public}s", task_.c_str());
80     }
81 }
82 }  // namespace AppExecFwk
83 }  // namespace OHOS
84