• 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_param.h"
17 
18 #include "string_ex.h"
19 
20 #include "continuous_task_log.h"
21 
22 namespace OHOS {
23 namespace BackgroundTaskMgr {
ReadFromParcel(Parcel & parcel)24 bool ContinuousTaskParam::ReadFromParcel(Parcel &parcel)
25 {
26     if (!parcel.ReadBool(isNewApi_)) {
27         BGTASK_LOGE("Failed to read the flag which indicate whether is called from newApi");
28         return false;
29     }
30 
31     if (!parcel.ReadUint32(bgModeId_)) {
32         BGTASK_LOGE("Failed to read request background mode info");
33         return false;
34     }
35     bool valid = parcel.ReadBool();
36     if (valid) {
37         wantAgent_ = std::shared_ptr<AbilityRuntime::WantAgent::WantAgent>(
38             parcel.ReadParcelable<AbilityRuntime::WantAgent::WantAgent>());
39         if (!wantAgent_) {
40             BGTASK_LOGE("Failed to read wantAgent");
41             return false;
42         }
43     }
44 
45     std::u16string u16AbilityName;
46     if (!parcel.ReadString16(u16AbilityName)) {
47         BGTASK_LOGE("Failed to read ability name");
48         return false;
49     }
50     abilityName_ = Str16ToStr8(u16AbilityName);
51 
52     std::u16string u16AppName;
53     if (!parcel.ReadString16(u16AppName)) {
54         BGTASK_LOGE("Failed to read app name");
55         return false;
56     }
57     appName_ = Str16ToStr8(u16AppName);
58     return true;
59 }
60 
ReadFromParcel(Parcel & parcel)61 bool ContinuousTaskParamForInner::ReadFromParcel(Parcel &parcel)
62 {
63     if (!parcel.ReadBool(isStart_)) {
64         BGTASK_LOGE("Failed to read the flag which indicate keep or stop running background");
65         return false;
66     }
67 
68     if (!parcel.ReadUint32(bgModeId_)) {
69         BGTASK_LOGE("Failed to read request background mode info");
70         return false;
71     }
72 
73     if (!parcel.ReadInt32(uid_)) {
74         BGTASK_LOGE("Failed to read uid info");
75         return false;
76     }
77 
78     return true;
79 }
80 
Unmarshalling(Parcel & parcel)81 ContinuousTaskParam *ContinuousTaskParam::Unmarshalling(Parcel &parcel)
82 {
83     ContinuousTaskParam *param = new (std::nothrow) ContinuousTaskParam();
84     if (param && !param->ReadFromParcel(parcel)) {
85         BGTASK_LOGE("read from parcel failed");
86         delete param;
87         param = nullptr;
88     }
89     return param;
90 }
91 
Unmarshalling(Parcel & parcel)92 ContinuousTaskParamForInner *ContinuousTaskParamForInner::Unmarshalling(Parcel &parcel)
93 {
94     ContinuousTaskParamForInner *param = new (std::nothrow) ContinuousTaskParamForInner();
95     if (param && !param->ReadFromParcel(parcel)) {
96         BGTASK_LOGE("read from parcel failed");
97         delete param;
98         param = nullptr;
99     }
100     return param;
101 }
102 
Marshalling(Parcel & parcel) const103 bool ContinuousTaskParam::Marshalling(Parcel &parcel) const
104 {
105     if (!parcel.WriteBool(isNewApi_)) {
106         BGTASK_LOGE("Failed to write the flag which indicate whether is called from newApi");
107         return false;
108     }
109 
110     if (!parcel.WriteUint32(bgModeId_)) {
111         BGTASK_LOGE("Failed to write request background mode info");
112         return false;
113     }
114     bool valid = wantAgent_ != nullptr;
115     if (!parcel.WriteBool(valid)) {
116         BGTASK_LOGE("Failed to write the flag which indicate whether wantAgent is null");
117         return false;
118     }
119     if (valid) {
120         if (!parcel.WriteParcelable(wantAgent_.get())) {
121             BGTASK_LOGE("Failed to write wantAgent");
122             return false;
123         }
124     }
125 
126     std::u16string u16AbilityName = Str8ToStr16(abilityName_);
127     if (!parcel.WriteString16(u16AbilityName)) {
128         BGTASK_LOGE("Failed to write abilityName");
129         return false;
130     }
131     std::u16string u16AppName = Str8ToStr16(appName_);
132     if (!parcel.WriteString16(u16AppName)) {
133         BGTASK_LOGE("Failed to write appName");
134         return false;
135     }
136     return true;
137 }
138 
Marshalling(Parcel & parcel) const139 bool ContinuousTaskParamForInner::Marshalling(Parcel &parcel) const
140 {
141     if (!parcel.WriteBool(isStart_)) {
142         BGTASK_LOGE("Failed to write the flag which indicate keep or stop running background");
143         return false;
144     }
145 
146     if (!parcel.WriteUint32(bgModeId_)) {
147         BGTASK_LOGE("Failed to write request background mode info");
148         return false;
149     }
150 
151     if (!parcel.WriteInt32(uid_)) {
152         BGTASK_LOGE("Failed to write uid info");
153         return false;
154     }
155 
156     return true;
157 }
158 }  // namespace BackgroundTaskMgr
159 }  // namespace OHOS