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