• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021 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 "data_ability_impl.h"
17 #include "app_log_wrapper.h"
18 #include "abs_shared_result_set.h"
19 #include "data_ability_predicates.h"
20 #include "values_bucket.h"
21 
22 namespace OHOS {
23 namespace AppExecFwk {
24 using AbilityManagerClient = OHOS::AAFwk::AbilityManagerClient;
25 /**
26  * @brief Handling the life cycle switching of PageAbility.
27  *
28  * @param want Indicates the structure containing information about the ability.
29  * @param targetState The life cycle state to switch to.
30  *
31  */
HandleAbilityTransaction(const Want & want,const AAFwk::LifeCycleStateInfo & targetState)32 void DataAbilityImpl::HandleAbilityTransaction(const Want &want, const AAFwk::LifeCycleStateInfo &targetState)
33 {
34     APP_LOGI("DataAbilityImpl::sourceState:%{public}d; targetState: %{public}d; isNewWant: %{public}d",
35         lifecycleState_,
36         targetState.state,
37         targetState.isNewWant);
38     if ((lifecycleState_ == targetState.state) && !targetState.isNewWant) {
39         APP_LOGE("Org lifeCycleState equals to Dst lifeCycleState.");
40         return;
41     }
42 
43     switch (targetState.state) {
44         case AAFwk::ABILITY_STATE_ACTIVE: {
45             if (lifecycleState_ == AAFwk::ABILITY_STATE_INITIAL) {
46                 SerUriString(targetState.caller.deviceId + "/" + targetState.caller.bundleName + "/" +
47                              targetState.caller.abilityName);
48                 Start(want);
49             } else {
50                 return;
51             }
52             break;
53         }
54         default: {
55             APP_LOGE("DataAbilityImpl::HandleAbilityTransaction state is error");
56             return;
57             break;
58         }
59     }
60 
61     AbilityManagerClient::GetInstance()->AbilityTransitionDone(token_, targetState.state);
62 }
63 
64 /**
65  * @brief Obtains the MIME types of files supported.
66  *
67  * @param uri Indicates the path of the files to obtain.
68  * @param mimeTypeFilter Indicates the MIME types of the files to obtain. This parameter cannot be null.
69  *
70  * @return Returns the matched MIME types. If there is no match, null is returned.
71  */
GetFileTypes(const Uri & uri,const std::string & mimeTypeFilter)72 std::vector<std::string> DataAbilityImpl::GetFileTypes(const Uri &uri, const std::string &mimeTypeFilter)
73 {
74     std::vector<std::string> types;
75     if (ability_ == nullptr) {
76         APP_LOGE("DataAbilityImpl::GetFileTypes ability_ is nullptr");
77         return types;
78     }
79 
80     types = ability_->GetFileTypes(uri, mimeTypeFilter);
81     return types;
82 }
83 
84 /**
85  * @brief Opens a file in a specified remote path.
86  *
87  * @param uri Indicates the path of the file to open.
88  * @param mode Indicates the file open mode, which can be "r" for read-only access, "w" for write-only access
89  * (erasing whatever data is currently in the file), "wt" for write access that truncates any existing file,
90  * "wa" for write-only access to append to any existing data, "rw" for read and write access on any existing data,
91  *  or "rwt" for read and write access that truncates any existing file.
92  *
93  * @return Returns the file descriptor.
94  */
OpenFile(const Uri & uri,const std::string & mode)95 int DataAbilityImpl::OpenFile(const Uri &uri, const std::string &mode)
96 {
97     int fd = -1;
98     if (ability_ == nullptr) {
99         APP_LOGE("DataAbilityImpl::OpenFile ability_ is nullptr");
100         return fd;
101     }
102 
103     fd = ability_->OpenFile(uri, mode);
104     return fd;
105 }
106 
107 /**
108  * @brief This is like openFile, open a file that need to be able to return sub-sections of files,often assets
109  * inside of their .hap.
110  *
111  * @param uri Indicates the path of the file to open.
112  * @param mode Indicates the file open mode, which can be "r" for read-only access, "w" for write-only access
113  * (erasing whatever data is currently in the file), "wt" for write access that truncates any existing file,
114  * "wa" for write-only access to append to any existing data, "rw" for read and write access on any existing
115  * data, or "rwt" for read and write access that truncates any existing file.
116  *
117  * @return Returns the RawFileDescriptor object containing file descriptor.
118  */
OpenRawFile(const Uri & uri,const std::string & mode)119 int DataAbilityImpl::OpenRawFile(const Uri &uri, const std::string &mode)
120 {
121     int fd = -1;
122     if (ability_ == nullptr) {
123         APP_LOGE("DataAbilityImpl::OpenRawFile ability_ is nullptr");
124         return fd;
125     }
126 
127     fd = ability_->OpenRawFile(uri, mode);
128     return fd;
129 }
130 
131 /**
132  * @brief Inserts a single data record into the database.
133  *
134  * @param uri Indicates the path of the data to operate.
135  * @param value  Indicates the data record to insert. If this parameter is null, a blank row will be inserted.
136  *
137  * @return Returns the index of the inserted data record.
138  */
Insert(const Uri & uri,const NativeRdb::ValuesBucket & value)139 int DataAbilityImpl::Insert(const Uri &uri, const NativeRdb::ValuesBucket &value)
140 {
141     int index = -1;
142     if (ability_ == nullptr) {
143         APP_LOGE("DataAbilityImpl::Insert ability_ is nullptr");
144         return index;
145     }
146 
147     index = ability_->Insert(uri, value);
148     return index;
149 }
150 
151 /**
152  * @brief Updates data records in the database.
153  *
154  * @param uri Indicates the path of data to update.
155  * @param value Indicates the data to update. This parameter can be null.
156  * @param predicates Indicates filter criteria. You should define the processing logic when this parameter is null.
157  *
158  * @return Returns the number of data records updated.
159  */
Update(const Uri & uri,const NativeRdb::ValuesBucket & value,const NativeRdb::DataAbilityPredicates & predicates)160 int DataAbilityImpl::Update(
161     const Uri &uri, const NativeRdb::ValuesBucket &value, const NativeRdb::DataAbilityPredicates &predicates)
162 {
163     int index = -1;
164     if (ability_ == nullptr) {
165         APP_LOGE("DataAbilityImpl::Update ability_ is nullptr");
166         return index;
167     }
168 
169     index = ability_->Update(uri, value, predicates);
170     return index;
171 }
172 
173 /**
174  * @brief Deletes one or more data records from the database.
175  *
176  * @param uri Indicates the path of the data to operate.
177  * @param predicates Indicates filter criteria. You should define the processing logic when this parameter is null.
178  *
179  * @return Returns the number of data records deleted.
180  */
Delete(const Uri & uri,const NativeRdb::DataAbilityPredicates & predicates)181 int DataAbilityImpl::Delete(const Uri &uri, const NativeRdb::DataAbilityPredicates &predicates)
182 {
183     int index = -1;
184     if (ability_ == nullptr) {
185         APP_LOGE("DataAbilityImpl::Delete ability_ is nullptr");
186         return index;
187     }
188 
189     index = ability_->Delete(uri, predicates);
190     return index;
191 }
192 
193 /**
194  * @brief Deletes one or more data records from the database.
195  *
196  * @param uri Indicates the path of data to query.
197  * @param columns Indicates the columns to query. If this parameter is null, all columns are queried.
198  * @param predicates Indicates filter criteria. You should define the processing logic when this parameter is null.
199  *
200  * @return Returns the query result.
201  */
Query(const Uri & uri,std::vector<std::string> & columns,const NativeRdb::DataAbilityPredicates & predicates)202 std::shared_ptr<NativeRdb::AbsSharedResultSet> DataAbilityImpl::Query(
203     const Uri &uri, std::vector<std::string> &columns, const NativeRdb::DataAbilityPredicates &predicates)
204 {
205     // std::shared_ptr<NativeRdb::AbsSharedResultSet> resultSet = nullptr;
206     if (ability_ == nullptr) {
207         APP_LOGE("DataAbilityImpl::Query ability_ is nullptr");
208         return nullptr;
209     }
210 
211     return ability_->Query(uri, columns, predicates);
212 }
213 
214 /**
215  * @brief Obtains the MIME type matching the data specified by the URI of the Data ability. This method should be
216  * implemented by a Data ability. Data abilities supports general data types, including text, HTML, and JPEG.
217  *
218  * @param uri Indicates the URI of the data.
219  *
220  * @return Returns the MIME type that matches the data specified by uri.
221  */
GetType(const Uri & uri)222 std::string DataAbilityImpl::GetType(const Uri &uri)
223 {
224     std::string type;
225     if (ability_ == nullptr) {
226         APP_LOGE("DataAbilityImpl::GetType ability_ is nullptr");
227         return type;
228     }
229     type = ability_->GetType(uri);
230     return type;
231 }
232 
233 /**
234  * @brief Reloads data in the database.
235  *
236  * @param uri Indicates the position where the data is to reload. This parameter is mandatory.
237  * @param extras Indicates the PacMap object containing the additional parameters to be passed in this call. This
238  * parameter can be null. If a custom Sequenceable object is put in the PacMap object and will be transferred across
239  * processes, you must call BasePacMap.setClassLoader(ClassLoader) to set a class loader for the custom object.
240  *
241  * @return Returns true if the data is successfully reloaded; returns false otherwise.
242  */
Reload(const Uri & uri,const PacMap & extras)243 bool DataAbilityImpl::Reload(const Uri &uri, const PacMap &extras)
244 {
245     bool ret = false;
246     if (ability_ == nullptr) {
247         APP_LOGE("DataAbilityImpl::Reload ability_ is nullptr");
248         return ret;
249     }
250     ret = ability_->Reload(uri, extras);
251     return ret;
252 }
253 
254 /**
255  * @brief Inserts multiple data records into the database.
256  *
257  * @param uri Indicates the path of the data to operate.
258  * @param values Indicates the data records to insert.
259  *
260  * @return Returns the number of data records inserted.
261  */
BatchInsert(const Uri & uri,const std::vector<NativeRdb::ValuesBucket> & values)262 int DataAbilityImpl::BatchInsert(const Uri &uri, const std::vector<NativeRdb::ValuesBucket> &values)
263 {
264     int ret = -1;
265     if (ability_ == nullptr) {
266         APP_LOGE("DataAbilityImpl::BatchInsert​ ability_ is nullptr");
267         return ret;
268     }
269     ret = ability_->BatchInsert(uri, values);
270     return ret;
271 }
272 /**
273  * @brief Converts the given uri that refer to the Data ability into a normalized URI. A normalized URI can be used
274  * across devices, persisted, backed up, and restored. It can refer to the same item in the Data ability even if the
275  * context has changed. If you implement URI normalization for a Data ability, you must also implement
276  * denormalizeUri(ohos.utils.net.Uri) to enable URI denormalization. After this feature is enabled, URIs passed to any
277  * method that is called on the Data ability must require normalization verification and denormalization. The default
278  * implementation of this method returns null, indicating that this Data ability does not support URI normalization.
279  *
280  * @param uri Indicates the Uri object to normalize.
281  *
282  * @return Returns the normalized Uri object if the Data ability supports URI normalization; returns null otherwise.
283  */
NormalizeUri(const Uri & uri)284 Uri DataAbilityImpl::NormalizeUri(const Uri &uri)
285 {
286     Uri urivalue("");
287     if (ability_ == nullptr) {
288         APP_LOGE("DataAbilityImpl::NormalizeUri ability_ is nullptr");
289         return urivalue;
290     }
291     urivalue = ability_->NormalizeUri(uri);
292     return urivalue;
293 }
294 
295 /**
296  * @brief Converts the given uri that refer to the Data ability into a normalized URI. A normalized URI can be used
297  * across devices, persisted, backed up, and restored. It can refer to the same item in the Data ability even if the
298  * context has changed. If you implement URI normalization for a Data ability, you must also implement
299  * denormalizeUri(ohos.utils.net.Uri) to enable URI denormalization. After this feature is enabled, URIs passed to any
300  * method that is called on the Data ability must require normalization verification and denormalization. The default
301  * implementation of this method returns null, indicating that this Data ability does not support URI normalization.
302  *
303  * @param uri Indicates the Uri object to normalize.
304  *
305  * @return Returns the normalized Uri object if the Data ability supports URI normalization; returns null otherwise.
306  */
DenormalizeUri(const Uri & uri)307 Uri DataAbilityImpl::DenormalizeUri(const Uri &uri)
308 {
309     Uri urivalue("");
310     if (ability_ == nullptr) {
311         APP_LOGE("DataAbilityImpl::DenormalizeUri ability_ is nullptr");
312         return urivalue;
313     }
314     urivalue = ability_->DenormalizeUri(uri);
315     return urivalue;
316 }
317 
ExecuteBatch(const std::vector<std::shared_ptr<DataAbilityOperation>> & operations)318 std::vector<std::shared_ptr<DataAbilityResult>> DataAbilityImpl::ExecuteBatch(
319     const std::vector<std::shared_ptr<DataAbilityOperation>> &operations)
320 {
321     APP_LOGI("DataAbilityImpl::ExecuteBatch start");
322     std::vector<std::shared_ptr<DataAbilityResult>> results;
323     if (ability_ == nullptr) {
324         APP_LOGE("DataAbilityImpl::ExecuteBatch ability_ is nullptr");
325         results.clear();
326         return results;
327     }
328 
329     results = ability_->ExecuteBatch(operations);
330     APP_LOGI("DataAbilityImpl::ExecuteBatch end, results size:%{public}zu", results.size());
331     return results;
332 }
333 
334 }  // namespace AppExecFwk
335 }  // namespace OHOS
336