1 /*
2  * Copyright (c) 2021-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 #ifndef OHOS_ABILITY_RUNTIME_DATA_ABILITY_IMPL_H
17 #define OHOS_ABILITY_RUNTIME_DATA_ABILITY_IMPL_H
18 
19 #include "ability_impl.h"
20 
21 namespace OHOS {
22 namespace NativeRdb {
23 class DataAbilityPredicates;
24 class ValuesBucket;
25 }  // namespace NativeRdb
26 namespace AppExecFwk {
27 class DataAbilityImpl final : public AbilityImpl {
28 public:
29     /**
30      * @brief Constructor.
31      *
32      */
33     DataAbilityImpl() = default;
34 
35     /**
36      * @brief Destructor.
37      *
38      */
39     ~DataAbilityImpl() = default;
40 
41     /**
42      * @brief Handling the life cycle switching of PageAbility.
43      *
44      * @param want Indicates the structure containing information about the ability.
45      * @param targetState The life cycle state to switch to.
46      * @param sessionInfo  Indicates the sessionInfo.
47      *
48      */
49     void HandleAbilityTransaction(const Want &want, const AAFwk::LifeCycleStateInfo &targetState,
50         sptr<AAFwk::SessionInfo> sessionInfo = nullptr);
51 
52     /**
53      * @brief Obtains the MIME types of files supported.
54      *
55      * @param uri Indicates the path of the files to obtain.
56      * @param mimeTypeFilter Indicates the MIME types of the files to obtain. This parameter cannot be null.
57      *
58      * @return Returns the matched MIME types. If there is no match, null is returned.
59      */
60     std::vector<std::string> GetFileTypes(const Uri &uri, const std::string &mimeTypeFilter);
61 
62     /**
63      * @brief Opens a file in a specified remote path.
64      *
65      * @param uri Indicates the path of the file to open.
66      * @param mode Indicates the file open mode, which can be "r" for read-only access, "w" for write-only access
67      * (erasing whatever data is currently in the file), "wt" for write access that truncates any existing file,
68      * "wa" for write-only access to append to any existing data, "rw" for read and write access on any existing data,
69      *  or "rwt" for read and write access that truncates any existing file.
70      *
71      * @return Returns the file descriptor.
72      */
73     int OpenFile(const Uri &uri, const std::string &mode);
74 
75     /**
76      * @brief This is like openFile, open a file that need to be able to return sub-sections of files,often assets
77      * inside of their .hap.
78      *
79      * @param uri Indicates the path of the file to open.
80      * @param mode Indicates the file open mode, which can be "r" for read-only access, "w" for write-only access
81      * (erasing whatever data is currently in the file), "wt" for write access that truncates any existing file,
82      * "wa" for write-only access to append to any existing data, "rw" for read and write access on any existing
83      * data, or "rwt" for read and write access that truncates any existing file.
84      *
85      * @return Returns the RawFileDescriptor object containing file descriptor.
86      */
87     int OpenRawFile(const Uri &uri, const std::string &mode);
88 
89     /**
90      * @brief Inserts a single data record into the database.
91      *
92      * @param uri Indicates the path of the data to operate.
93      * @param value  Indicates the data record to insert. If this parameter is null, a blank row will be inserted.
94      *
95      * @return Returns the index of the inserted data record.
96      */
97     int Insert(const Uri &uri, const NativeRdb::ValuesBucket &value);
98 
99     /**
100      * @brief Updates data records in the database.
101      *
102      * @param uri Indicates the path of data to update.
103      * @param value Indicates the data to update. This parameter can be null.
104      * @param predicates Indicates filter criteria. You should define the processing logic when this parameter is null.
105      *
106      * @return Returns the number of data records updated.
107      */
108     int Update(
109         const Uri &uri, const NativeRdb::ValuesBucket &value, const NativeRdb::DataAbilityPredicates &predicates);
110 
111     /**
112      * @brief Deletes one or more data records from the database.
113      *
114      * @param uri Indicates the path of the data to operate.
115      * @param predicates Indicates filter criteria. You should define the processing logic when this parameter is null.
116      *
117      * @return Returns the number of data records deleted.
118      */
119     int Delete(const Uri &uri, const NativeRdb::DataAbilityPredicates &predicates);
120 
121     /**
122      * @brief Deletes one or more data records from the database.
123      *
124      * @param uri Indicates the path of data to query.
125      * @param columns Indicates the columns to query. If this parameter is null, all columns are queried.
126      * @param predicates Indicates filter criteria. You should define the processing logic when this parameter is null.
127      *
128      * @return Returns the query result.
129      */
130     std::shared_ptr<NativeRdb::AbsSharedResultSet> Query(
131         const Uri &uri, std::vector<std::string> &columns, const NativeRdb::DataAbilityPredicates &predicates);
132 
133     /**
134      * @brief Calls the method of the Data ability.
135      *
136      * @param uri Indicates the Data ability of the method to call.
137      * @param method Indicates the method to call.
138      * @param arg Indicates the parameter of the String type.
139      * @param pacMap Defines a PacMap object for storing a series of values.
140      *
141      * @return Returns the call result.
142      */
143     std::shared_ptr<AppExecFwk::PacMap> Call(
144         const Uri &uri, const std::string &method, const std::string &arg, const AppExecFwk::PacMap &pacMap);
145 
146     /**
147      * @brief Obtains the MIME type matching the data specified by the URI of the Data ability. This method should be
148      * implemented by a Data ability. Data abilities supports general data types, including text, HTML, and JPEG.
149      *
150      * @param uri Indicates the URI of the data.
151      *
152      * @return Returns the MIME type that matches the data specified by uri.
153      */
154     std::string GetType(const Uri &uri);
155 
156     /**
157      * @brief Reloads data in the database.
158      *
159      * @param uri Indicates the position where the data is to reload. This parameter is mandatory.
160      * @param extras Indicates the PacMap object containing the additional parameters to be passed in this call. This
161      * parameter can be null. If a custom Sequenceable object is put in the PacMap object and will be transferred across
162      * processes, you must call BasePacMap.setClassLoader(ClassLoader) to set a class loader for the custom object.
163      *
164      * @return Returns true if the data is successfully reloaded; returns false otherwise.
165      */
166     bool Reload(const Uri &uri, const PacMap &extras);
167 
168     /**
169      * @brief Inserts multiple data records into the database.
170      *
171      * @param uri Indicates the path of the data to operate.
172      * @param values Indicates the data records to insert.
173      *
174      * @return Returns the number of data records inserted.
175      */
176     int BatchInsert(const Uri &uri, const std::vector<NativeRdb::ValuesBucket> &values);
177 
178     /**
179      * @brief Converts the given uri that refer to the Data ability into a normalized URI. A normalized URI can be used
180      * across devices, persisted, backed up, and restored. It can refer to the same item in the Data ability even if the
181      * context has changed. If you implement URI normalization for a Data ability, you must also implement
182      * denormalizeUri(ohos.utils.net.Uri) to enable URI denormalization. After this feature enabled, URIs passed to any
183      * method that is called on Data ability must require normalization verification and denormalization. The default
184      * implementation of this method returns null, indicating that this Data ability does not support URI normalization.
185      *
186      * @param uri Indicates the Uri object to normalize.
187      *
188      * @return Returns the normalized Uri object if the Data ability supports URI normalization; returns null otherwise.
189      */
190     Uri NormalizeUri(const Uri &uri);
191 
192     /**
193      * @brief Converts the given normalized uri generated by normalizeUri(ohos.utils.net.Uri) into a denormalized one.
194      * The default implementation of this method returns the original URI passed to it.
195      *
196      * @param uri uri Indicates the Uri object to denormalize.
197      *
198      * @return Returns the denormalized Uri object if the denormalization is successful; returns the original Uri
199      * passed to this method if there is nothing to do; returns null if the data identified by the original Uri
200      * cannot be found in the current environment.
201      */
202     Uri DenormalizeUri(const Uri &uri);
203 
204     /**
205      * @brief Performs batch operations on the database.
206      *
207      * @param operations Indicates a list of database operations on the database.
208      * @return Returns the result of each operation, in array.
209      */
210     std::vector<std::shared_ptr<DataAbilityResult>> ExecuteBatch(
211         const std::vector<std::shared_ptr<DataAbilityOperation>> &operations);
212 
213 private:
214     bool CheckExecuteBatchPermission(
215         const std::vector<std::shared_ptr<DataAbilityOperation>> &operations) const;
216     bool CheckOpenFilePermission(const std::string &mode) const;
217     bool CheckReadAndWritePermission(const std::string &permissionType) const;
218     std::string GetPermissionInfo(const std::string &permissionType) const;
219 };
220 }  // namespace AppExecFwk
221 }  // namespace OHOS
222 #endif  // OHOS_ABILITY_RUNTIME_DATA_ABILITY_IMPL_H
223