1 /*
2  * Copyright (c) 2023 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.h"
17 
18 #include <chrono>
19 #include <functional>
20 #include <gtest/gtest.h>
21 
22 #include "sdk_helper.h"
23 
24 namespace OHOS {
25 namespace FileManagement {
26 namespace CloudSync {
27 using namespace std;
28 
29 /* task runner */
TaskRunner(function<void ()> callback)30 TaskRunner::TaskRunner(function<void()> callback) : callback_(callback) {}
31 
~TaskRunner()32 TaskRunner::~TaskRunner() {}
33 
GenerateTaskId()34 int32_t TaskRunner::GenerateTaskId()
35 {
36     return currentId_.fetch_add(1);
37 }
38 
AddTask(shared_ptr<Task> t)39 int32_t TaskRunner::AddTask(shared_ptr<Task> t)
40 {
41     return E_OK;
42 }
43 
StartTask(shared_ptr<Task> t,TaskAction action)44 int32_t TaskRunner::StartTask(shared_ptr<Task> t, TaskAction action)
45 {
46     GTEST_LOG_(INFO) << "StartTask Start";
47     return E_OK;
48 }
49 
CommitTask(shared_ptr<Task> t)50 int32_t TaskRunner::CommitTask(shared_ptr<Task> t)
51 {
52     GTEST_LOG_(INFO) << "CommitTask Start";
53     return E_OK;
54 }
55 
CompleteTask(int32_t id)56 void TaskRunner::CompleteTask(int32_t id) {}
57 
ReleaseTask()58 bool TaskRunner::ReleaseTask()
59 {
60     return false;
61 }
62 
SetStopFlag(std::shared_ptr<bool> stopFlag)63 void TaskRunner::SetStopFlag(std::shared_ptr<bool> stopFlag)
64 {
65 }
66 
Reset()67 void TaskRunner::Reset()
68 {
69     currentId_.store(0);
70 }
71 
SetCommitFunc(function<int32_t (shared_ptr<TaskRunner>,shared_ptr<Task>)> func)72 void TaskRunner::SetCommitFunc(function<int32_t(shared_ptr<TaskRunner>, shared_ptr<Task>)> func) {}
73 
CommitDummyTask()74 void TaskRunner::CommitDummyTask() {}
75 
CompleteDummyTask()76 void TaskRunner::CompleteDummyTask() {}
77 
78 /* TaskManager */
TaskManager()79 TaskManager::TaskManager() {}
80 
~TaskManager()81 TaskManager::~TaskManager() {}
82 
AllocRunner(int32_t userId,const std::string & bundleName,function<void ()> callback)83 shared_ptr<TaskRunner> TaskManager::AllocRunner(int32_t userId,
84                                                 const std::string &bundleName,
85                                                 function<void()> callback)
86 {
87     string key = GetKey(userId, bundleName);
88     unique_lock wlock(mapMutex_);
89     if (map_.find(key) == map_.end()) {
90         auto runner = make_shared<TaskRunner>(callback);
91         InitRunner(*runner);
92         map_.insert({key, runner});
93     }
94     return map_[key];
95 }
96 
ReleaseRunner(int32_t userId,const std::string & bundleName)97 void TaskManager::ReleaseRunner(int32_t userId, const std::string &bundleName)
98 {
99     string key = GetKey(userId, bundleName);
100     unique_lock wlock(mapMutex_);
101     map_.erase(key);
102 }
103 
GetRunner(int32_t userId,const std::string & bundleName)104 shared_ptr<TaskRunner> TaskManager::GetRunner(int32_t userId, const std::string &bundleName)
105 {
106     string key = GetKey(userId, bundleName);
107     shared_lock rlock(mapMutex_);
108     if (map_.find(key) == map_.end()) {
109         return nullptr;
110     }
111     return map_[key];
112 }
113 
InitRunner(TaskRunner & runner)114 void TaskManager::InitRunner(TaskRunner &runner)
115 {
116     runner.SetCommitFunc(bind(&TaskManager::CommitTask, this, placeholders::_1, placeholders::_2));
117 }
118 
CommitTask(shared_ptr<TaskRunner> runner,shared_ptr<Task> t)119 int32_t TaskManager::CommitTask(shared_ptr<TaskRunner> runner, shared_ptr<Task> t)
120 {
121     GTEST_LOG_(INFO) << "CommitTask Start";
122     return E_OK;
123 }
124 
GetKey(int32_t userId,const string & bundleName)125 string TaskManager::GetKey(int32_t userId, const string &bundleName)
126 {
127     return to_string(userId) + bundleName;
128 }
129 } // namespace CloudSync
130 } // namespace FileManagement
131 } // namespace OHOS
132