Improve code coverage
[platform/core/appfw/appcore-agent.git] / src / job.cc
1 /*
2  * Copyright (c) 2021 Samsung Electronics Co., Ltd All Rights Reserved
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 #include "job.hh"
18 #include "log-private.hh"
19
20 namespace tizen_cpp {
21
22 Job::Job(int status, std::string id, tizen_base::Bundle data)
23     : status_(status), id_(std::move(id)), data_(std::move(data)) {
24 }
25
26 Job::~Job() {
27   UnsetIdler();
28   UnsetTimer();
29 }
30
31 int Job::GetStatus() const {
32   return status_;
33 }
34
35 const std::string& Job::GetId() const {
36   return id_;
37 }
38
39 bundle* Job::GetData() const {
40   return data_.GetHandle();
41 }
42
43 void Job::SetEventListener(IEvent* listener) {
44   listener_ = listener;
45 }
46
47 // LCOV_EXCL_START
48 void Job::SetTimer() {
49   if (timer_ == 0) {
50     timer_ = g_timeout_add(5000, TimedOutCb, this);
51     if (timer_ == 0)
52       _E("g_timeout_add() is failed");
53   }
54 }
55 // LCOV_EXCL_STOP
56
57 void Job::UnsetTimer() {
58   if (timer_ != 0) {
59     // LCOV_EXCL_START
60     g_source_remove(timer_);
61     timer_ = 0;
62     // LCOV_EXCL_STOP
63   }
64 }
65
66 void Job::SetIdler() {
67   if (idler_ == 0) {
68     idler_ = g_idle_add(IdleCb, this);
69     if (idler_ == 0)
70       _E("g_idle_add() is failed");  // LCOV_EXCL_LINE
71   }
72 }
73
74 void Job::UnsetIdler() {
75   if (idler_ != 0) {
76     // LCOV_EXCL_START
77     g_source_remove(idler_);
78     idler_ = 0;
79     // LCOV_EXCL_STOP
80   }
81 }
82
83 // LCOV_EXCL_START
84 gboolean Job::TimedOutCb(gpointer data) {
85   auto* handle = static_cast<Job*>(data);
86   handle->timer_ = 0;
87
88   auto* listener = handle->listener_;
89   if (listener != nullptr)
90     listener->OnTimedOut(handle);
91
92   return G_SOURCE_REMOVE;
93 }
94 // LCOV_EXCL_STOP
95
96 gboolean Job::IdleCb(gpointer data) {
97   auto* handle = static_cast<Job*>(data);
98   handle->idler_ = 0;
99   auto* listener = handle->listener_;
100   if (listener != nullptr)
101     listener->OnRun(handle);
102
103   return G_SOURCE_REMOVE;
104 }
105
106 }  // namespace tizen_cpp