• 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[10] = {
26     "dataTransfer",
27     "audioPlayback",
28     "audioRecording",
29     "location",
30     "bluetoothInteraction",
31     "multiDeviceConnection",
32     "wifiInteraction",
33     "voip",
34     "taskKeeping",
35     "default",
36 };
37 
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)38 ContinuousTaskRecord::ContinuousTaskRecord(const std::string &bundleName, const std::string &abilityName, int32_t uid,
39     int32_t pid, uint32_t bgModeId, bool isBatchApi, const std::vector<uint32_t> &bgModeIds, int32_t abilityId)
40     : bundleName_(bundleName), abilityName_(abilityName), uid_(uid), pid_(pid), bgModeId_(bgModeId),
41       isBatchApi_(isBatchApi), bgModeIds_(bgModeIds), abilityId_(abilityId) {
42     if (isBatchApi_) {
43         auto findNonDataTransfer = [](const auto &target) {
44             return  target != BackgroundMode::DATA_TRANSFER;
45         };
46         auto iter = std::find_if(bgModeIds_.begin(), bgModeIds_.end(), findNonDataTransfer);
47         if (iter != bgModeIds_.end()) {
48             bgModeId_ = *iter;
49             BGTASK_LOGI("batch api, find non-datatransfer mode, set %{public}d", bgModeId_);
50         } else {
51             bgModeId_ = bgModeIds_[0];
52         }
53     } else {
54         bgModeIds_.push_back(bgModeId);
55     }
56 }
57 
GetBundleName() const58 std::string ContinuousTaskRecord::GetBundleName() const
59 {
60     return bundleName_;
61 }
62 
GetAbilityName() const63 std::string ContinuousTaskRecord::GetAbilityName() const
64 {
65     return abilityName_;
66 }
67 
IsNewApi() const68 bool ContinuousTaskRecord::IsNewApi() const
69 {
70     return isNewApi_;
71 }
72 
IsFromWebview() const73 bool ContinuousTaskRecord::IsFromWebview() const
74 {
75     return isFromWebview_;
76 }
77 
GetBgModeId() const78 uint32_t ContinuousTaskRecord::GetBgModeId() const
79 {
80     return bgModeId_;
81 }
82 
GetUserId() const83 int32_t ContinuousTaskRecord::GetUserId() const
84 {
85     return userId_;
86 }
87 
GetUid() const88 int32_t ContinuousTaskRecord::GetUid() const
89 {
90     return uid_;
91 }
92 
GetPid() const93 pid_t ContinuousTaskRecord::GetPid() const
94 {
95     return pid_;
96 }
97 
GetNotificationLabel() const98 std::string ContinuousTaskRecord::GetNotificationLabel() const
99 {
100     return notificationLabel_;
101 }
102 
GetNotificationId() const103 int32_t ContinuousTaskRecord::GetNotificationId() const
104 {
105     return notificationId_;
106 }
107 
GetContinuousTaskId() const108 int32_t ContinuousTaskRecord::GetContinuousTaskId() const
109 {
110     return continuousTaskId_;
111 }
112 
GetWantAgent() const113 std::shared_ptr<AbilityRuntime::WantAgent::WantAgent> ContinuousTaskRecord::GetWantAgent() const
114 {
115     return wantAgent_;
116 }
117 
ToString(std::vector<uint32_t> & bgmodes)118 std::string ContinuousTaskRecord::ToString(std::vector<uint32_t> &bgmodes)
119 {
120     std::string result;
121     for (auto it : bgmodes) {
122         result += std::to_string(it);
123         result += ",";
124     }
125     return result;
126 }
127 
ToVector(std::string & str)128 std::vector<uint32_t> ContinuousTaskRecord::ToVector(std::string &str)
129 {
130     std::vector<std::string> stringTokens;
131     std::vector<uint32_t> modeTokens;
132     OHOS::SplitStr(str, ",", stringTokens);
133     for (auto mode : stringTokens) {
134         modeTokens.push_back(std::atoi(mode.c_str()));
135     }
136     return modeTokens;
137 }
138 
GetAbilityId() const139 int32_t ContinuousTaskRecord::GetAbilityId() const
140 {
141     return abilityId_;
142 }
143 
IsSystem() const144 bool ContinuousTaskRecord::IsSystem() const
145 {
146     return isSystem_;
147 }
148 
ParseToJsonStr()149 std::string ContinuousTaskRecord::ParseToJsonStr()
150 {
151     nlohmann::json root;
152     root["bundleName"] = bundleName_;
153     root["abilityName"] = abilityName_;
154     root["userId"] = userId_;
155     root["uid"] = uid_;
156     root["pid"] = pid_;
157     root["bgModeId"] = bgModeId_;
158     root["isNewApi"] = isNewApi_;
159     root["isFromWebview"] = isFromWebview_;
160     root["notificationLabel"] = notificationLabel_;
161     root["notificationId"] = notificationId_;
162     root["isBatchApi"] = isBatchApi_;
163     root["bgModeIds"] = ToString(bgModeIds_);
164     root["isSystem"] = isSystem_;
165     if (wantAgentInfo_ != nullptr) {
166         nlohmann::json info;
167         info["bundleName"] = wantAgentInfo_->bundleName_;
168         info["abilityName"] = wantAgentInfo_->abilityName_;
169         root["wantAgentInfo"] = info;
170     }
171     root["continuousTaskId"] = continuousTaskId_;
172     root["abilityId"] = abilityId_;
173     return root.dump(CommonUtils::jsonFormat_);
174 }
175 
CheckContinuousRecod(const nlohmann::json & value)176 bool CheckContinuousRecod(const nlohmann::json &value)
177 {
178     return !value["bundleName"].is_string() || !value["abilityName"].is_string()
179         || !value["userId"].is_number_integer() || !value["uid"].is_number_integer()
180         || !value["pid"].is_number_integer() || !value["bgModeId"].is_number_integer()
181         || !value["isNewApi"].is_boolean() || !value["isFromWebview"].is_boolean()
182         || !value["notificationLabel"].is_string() || !value["isSystem"].is_boolean()
183         || !value["continuousTaskId"].is_number_integer()
184         || !value["abilityId"].is_number_integer();
185 }
186 
ParseFromJson(const nlohmann::json & value)187 bool ContinuousTaskRecord::ParseFromJson(const nlohmann::json &value)
188 {
189     if (value.is_null() || !value.is_object() || !CommonUtils::CheckJsonValue(value, { "bundleName",
190         "abilityName", "userId", "uid", "pid", "bgModeId", "isNewApi", "isFromWebview", "notificationLabel",
191         "isSystem", "continuousTaskId", "abilityId"})) {
192         BGTASK_LOGE("continuoustaskrecord no key");
193         return false;
194     }
195     if (CheckContinuousRecod(value)) {
196         BGTASK_LOGE("continuoustaskrecord parse from json fail");
197         return false;
198     }
199     this->bundleName_ = value.at("bundleName").get<std::string>();
200     this->abilityName_ = value.at("abilityName").get<std::string>();
201     this->userId_ = value.at("userId").get<int32_t>();
202     this->uid_ = value.at("uid").get<int32_t>();
203     this->pid_ = value.at("pid").get<int32_t>();
204     this->bgModeId_ = value.at("bgModeId").get<uint32_t>();
205     this->isNewApi_ = value.at("isNewApi").get<bool>();
206     this->isFromWebview_ = value.at("isFromWebview").get<bool>();
207     this->notificationLabel_ = value.at("notificationLabel").get<std::string>();
208     this->isSystem_ = value.at("isSystem").get<bool>();
209     this->continuousTaskId_ = value.at("continuousTaskId").get<int32_t>();
210     this->abilityId_ = value.at("abilityId").get<int32_t>();
211     if (value.contains("isBatchApi") && value["isBatchApi"].is_boolean()) {
212         this->isBatchApi_ = value.at("isBatchApi").get<bool>();
213     }
214     if (value.contains("bgModeIds") && value["bgModeIds"].is_string()) {
215         auto modes = value.at("bgModeIds").get<std::string>();
216         this->bgModeIds_ = ToVector(modes);
217     }
218     if (value.find("wantAgentInfo") != value.end()) {
219         nlohmann::json infoVal = value["wantAgentInfo"];
220         if (!CommonUtils::CheckJsonValue(infoVal, { "bundleName", "abilityName" })
221             || !infoVal["bundleName"].is_string() || !infoVal["abilityName"].is_string()) {
222             return false;
223         }
224         std::shared_ptr<WantAgentInfo> info = std::make_shared<WantAgentInfo>();
225         info->bundleName_ = infoVal.at("bundleName").get<std::string>();
226         info->abilityName_ = infoVal.at("abilityName").get<std::string>();
227         this->wantAgentInfo_ = info;
228     }
229     if (value.contains("notificationId") && value["notificationId"].is_number_integer()) {
230         this->notificationId_ = value.at("notificationId").get<int32_t>();
231     }
232     return true;
233 }
234 }  // namespace BackgroundTaskMgr
235 }  // namespace OHOS