1 /*
2  * Copyright (c) 2024 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 "task_signal.h"
17 
18 #include "distributed_file_daemon_manager.h"
19 #include "filemgmt_libhilog.h"
20 
21 namespace OHOS {
22 namespace DistributedFS {
23 namespace ModuleTaskSignal {
24 using namespace FileManagement;
25 constexpr int CANCEL_ERR = -3;
Cancel()26 int32_t TaskSignal::Cancel()
27 {
28     HILOGD("TaskSignal Cancel in.");
29     if (remoteTask_.load()) {
30         if (sessionName_.empty()) {
31             HILOGE("TaskSignal::Cancel sessionName is empty");
32             return CANCEL_ERR;
33         }
34         auto ret = Storage::DistributedFile::DistributedFileDaemonManager::GetInstance().
35                 CancelCopyTask(sessionName_);
36         HILOGD("taskSignal.cancel sessionName = %{public}s", sessionName_.c_str());
37         if (ret != 0) {
38             HILOGI("CancelCopyTask failed, ret = %{public}d", ret);
39             return ret;
40         }
41         OnCancel();
42         return ret;
43     }
44     needCancel_.store(true);
45     return 0;
46 }
47 
IsCanceled()48 bool TaskSignal::IsCanceled()
49 {
50     return needCancel_.load() || remoteTask_.load();
51 }
52 
SetTaskSignalListener(TaskSignalListener * signalListener)53 void TaskSignal::SetTaskSignalListener(TaskSignalListener *signalListener)
54 {
55     if (signalListener_ == nullptr) {
56         signalListener_ = std::move(signalListener);
57     }
58 }
59 
OnCancel()60 void TaskSignal::OnCancel()
61 {
62     if (signalListener_ != nullptr) {
63         signalListener_->OnCancel();
64     }
65 }
66 
CheckCancelIfNeed(const std::string & path)67 bool TaskSignal::CheckCancelIfNeed(const std::string &path)
68 {
69     if (!needCancel_.load()) {
70         return false;
71     }
72     OnCancel();
73     return true;
74 }
75 
MarkRemoteTask()76 void TaskSignal::MarkRemoteTask()
77 {
78     remoteTask_.store(true);
79 }
80 
SetFileInfoOfRemoteTask(const std::string & sessionName,const std::string & filePath)81 void TaskSignal::SetFileInfoOfRemoteTask(const std::string &sessionName, const std::string &filePath)
82 {
83     HILOGD("SetFileInfoOfRemoteTask sessionName = %{public}s", sessionName.c_str());
84     sessionName_ = sessionName;
85     filePath_ = filePath;
86 }
87 } // namespace ModuleTaskSignal
88 } // namespace DistributedFS
89 } // namespace OHOS