• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 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 "continuous_task_record.h"
17 
18 #include "common_utils.h"
19 #include "iremote_object.h"
20 #include "background_mode.h"
21 #include "continuous_task_log.h"
22 
23 namespace OHOS {
24 namespace BackgroundTaskMgr {
25 const char *g_continuousTaskModeName[11] = {
26     "dataTransfer",
27     "audioPlayback",
28     "audioRecording",
29     "location",
30     "bluetoothInteraction",
31     "multiDeviceConnection",
32     "wifiInteraction",
33     "voip",
34     "taskKeeping",
35     "workout",
36     "default",
37 };
38 
ContinuousTaskRecord(const std::string & bundleName,const std::string & abilityName,int32_t uid,int32_t pid,uint32_t bgModeId,bool isBatchApi,const std::vector<uint32_t> & bgModeIds,int32_t abilityId)39 ContinuousTaskRecord::ContinuousTaskRecord(const std::string &bundleName, const std::string &abilityName, int32_t uid,
40     int32_t pid, uint32_t bgModeId, bool isBatchApi, const std::vector<uint32_t> &bgModeIds, int32_t abilityId)
41     : bundleName_(bundleName), abilityName_(abilityName), uid_(uid), pid_(pid), bgModeId_(bgModeId),
42       isBatchApi_(isBatchApi), bgModeIds_(bgModeIds), abilityId_(abilityId) {
43     if (isBatchApi_) {
44         auto findNonDataTransfer = [](const auto &target) {
45             return  target != BackgroundMode::DATA_TRANSFER;
46         };
47         auto iter = std::find_if(bgModeIds_.begin(), bgModeIds_.end(), findNonDataTransfer);
48         if (iter != bgModeIds_.end()) {
49             bgModeId_ = *iter;
50             BGTASK_LOGI("batch api, find non-datatransfer mode, set %{public}d", bgModeId_);
51         } else {
52             bgModeId_ = bgModeIds_[0];
53         }
54     } else {
55         bgModeIds_.push_back(bgModeId);
56     }
57 }
58 
GetBundleName() const59 std::string ContinuousTaskRecord::GetBundleName() const
60 {
61     return bundleName_;
62 }
63 
GetAbilityName() const64 std::string ContinuousTaskRecord::GetAbilityName() const
65 {
66     return abilityName_;
67 }
68 
IsNewApi() const69 bool ContinuousTaskRecord::IsNewApi() const
70 {
71     return isNewApi_;
72 }
73 
IsFromWebview() const74 bool ContinuousTaskRecord::IsFromWebview() const
75 {
76     return isFromWebview_;
77 }
78 
GetBgModeId() const79 uint32_t ContinuousTaskRecord::GetBgModeId() const
80 {
81     return bgModeId_;
82 }
83 
GetUserId() const84 int32_t ContinuousTaskRecord::GetUserId() const
85 {
86     return userId_;
87 }
88 
GetUid() const89 int32_t ContinuousTaskRecord::GetUid() const
90 {
91     return uid_;
92 }
93 
GetPid() const94 pid_t ContinuousTaskRecord::GetPid() const
95 {
96     return pid_;
97 }
98 
GetNotificationLabel() const99 std::string ContinuousTaskRecord::GetNotificationLabel() const
100 {
101     return notificationLabel_;
102 }
103 
GetNotificationId() const104 int32_t ContinuousTaskRecord::GetNotificationId() const
105 {
106     return notificationId_;
107 }
108 
GetContinuousTaskId() const109 int32_t ContinuousTaskRecord::GetContinuousTaskId() const
110 {
111     return continuousTaskId_;
112 }
113 
GetWantAgent() const114 std::shared_ptr<AbilityRuntime::WantAgent::WantAgent> ContinuousTaskRecord::GetWantAgent() const
115 {
116     return wantAgent_;
117 }
118 
ToString(std::vector<uint32_t> & bgmodes)119 std::string ContinuousTaskRecord::ToString(std::vector<uint32_t> &bgmodes)
120 {
121     std::string result;
122     for (auto it : bgmodes) {
123         result += std::to_string(it);
124         result += ",";
125     }
126     return result;
127 }
128 
ToVector(std::string & str)129 std::vector<uint32_t> ContinuousTaskRecord::ToVector(std::string &str)
130 {
131     std::vector<std::string> stringTokens;
132     std::vector<uint32_t> modeTokens;
133     OHOS::SplitStr(str, ",", stringTokens);
134     for (auto mode : stringTokens) {
135         modeTokens.push_back(std::atoi(mode.c_str()));
136     }
137     return modeTokens;
138 }
139 
GetAbilityId() const140 int32_t ContinuousTaskRecord::GetAbilityId() const
141 {
142     return abilityId_;
143 }
144 
IsSystem() const145 bool ContinuousTaskRecord::IsSystem() const
146 {
147     return isSystem_;
148 }
149 
ParseToJsonStr()150 std::string ContinuousTaskRecord::ParseToJsonStr()
151 {
152     nlohmann::json root;
153     root["bundleName"] = bundleName_;
154     root["abilityName"] = abilityName_;
155     root["userId"] = userId_;
156     root["uid"] = uid_;
157     root["pid"] = pid_;
158     root["bgModeId"] = bgModeId_;
159     root["isNewApi"] = isNewApi_;
160     root["isFromWebview"] = isFromWebview_;
161     root["notificationLabel"] = notificationLabel_;
162     root["notificationId"] = notificationId_;
163     root["isBatchApi"] = isBatchApi_;
164     root["bgModeIds"] = ToString(bgModeIds_);
165     root["bgSubModeIds"] = ToString(bgSubModeIds_);
166     root["isSystem"] = isSystem_;
167     if (wantAgentInfo_ != nullptr) {
168         nlohmann::json info;
169         info["bundleName"] = wantAgentInfo_->bundleName_;
170         info["abilityName"] = wantAgentInfo_->abilityName_;
171         root["wantAgentInfo"] = info;
172     }
173     root["continuousTaskId"] = continuousTaskId_;
174     root["abilityId"] = abilityId_;
175     return root.dump(CommonUtils::jsonFormat_);
176 }
177 
CheckContinuousRecod(const nlohmann::json & value)178 bool CheckContinuousRecod(const nlohmann::json &value)
179 {
180     return !value["bundleName"].is_string() || !value["abilityName"].is_string()
181         || !value["userId"].is_number_integer() || !value["uid"].is_number_integer()
182         || !value["pid"].is_number_integer() || !value["bgModeId"].is_number_integer()
183         || !value["isNewApi"].is_boolean() || !value["isFromWebview"].is_boolean()
184         || !value["notificationLabel"].is_string() || !value["isSystem"].is_boolean()
185         || !value["continuousTaskId"].is_number_integer()
186         || !value["abilityId"].is_number_integer();
187 }
188 
ParseFromJson(const nlohmann::json & value)189 bool ContinuousTaskRecord::ParseFromJson(const nlohmann::json &value)
190 {
191     if (value.is_null() || !value.is_object() || !CommonUtils::CheckJsonValue(value, { "bundleName",
192         "abilityName", "userId", "uid", "pid", "bgModeId", "isNewApi", "isFromWebview", "notificationLabel",
193         "isSystem", "continuousTaskId", "abilityId"})) {
194         BGTASK_LOGE("continuoustaskrecord no key");
195         return false;
196     }
197     if (CheckContinuousRecod(value)) {
198         BGTASK_LOGE("continuoustaskrecord parse from json fail");
199         return false;
200     }
201     this->bundleName_ = value.at("bundleName").get<std::string>();
202     this->abilityName_ = value.at("abilityName").get<std::string>();
203     this->userId_ = value.at("userId").get<int32_t>();
204     this->uid_ = value.at("uid").get<int32_t>();
205     this->pid_ = value.at("pid").get<int32_t>();
206     this->bgModeId_ = value.at("bgModeId").get<uint32_t>();
207     this->isNewApi_ = value.at("isNewApi").get<bool>();
208     this->isFromWebview_ = value.at("isFromWebview").get<bool>();
209     this->notificationLabel_ = value.at("notificationLabel").get<std::string>();
210     this->isSystem_ = value.at("isSystem").get<bool>();
211     this->continuousTaskId_ = value.at("continuousTaskId").get<int32_t>();
212     this->abilityId_ = value.at("abilityId").get<int32_t>();
213     if (value.contains("isBatchApi") && value["isBatchApi"].is_boolean()) {
214         this->isBatchApi_ = value.at("isBatchApi").get<bool>();
215     }
216     if (value.contains("bgModeIds") && value["bgModeIds"].is_string()) {
217         auto modes = value.at("bgModeIds").get<std::string>();
218         this->bgModeIds_ = ToVector(modes);
219     }
220     if (value.contains("bgSubModeIds") && value["bgSubModeIds"].is_string()) {
221         auto subModes = value.at("bgSubModeIds").get<std::string>();
222         this->bgSubModeIds_ = ToVector(subModes);
223     }
224     if (value.find("wantAgentInfo") != value.end()) {
225         nlohmann::json infoVal = value["wantAgentInfo"];
226         if (!CommonUtils::CheckJsonValue(infoVal, { "bundleName", "abilityName" })
227             || !infoVal["bundleName"].is_string() || !infoVal["abilityName"].is_string()) {
228             return false;
229         }
230         std::shared_ptr<WantAgentInfo> info = std::make_shared<WantAgentInfo>();
231         info->bundleName_ = infoVal.at("bundleName").get<std::string>();
232         info->abilityName_ = infoVal.at("abilityName").get<std::string>();
233         this->wantAgentInfo_ = info;
234     }
235     if (value.contains("notificationId") && value["notificationId"].is_number_integer()) {
236         this->notificationId_ = value.at("notificationId").get<int32_t>();
237     }
238     return true;
239 }
240 }  // namespace BackgroundTaskMgr
241 }  // namespace OHOS